This commit is contained in:
Jaroslaw Konik 2024-04-20 16:03:47 +02:00
parent 23538ef624
commit 5031c09995
3 changed files with 52 additions and 6 deletions

1
Cargo.lock generated
View file

@ -1101,6 +1101,7 @@ dependencies = [
"embassy-executor", "embassy-executor",
"embassy-net", "embassy-net",
"embassy-net-driver", "embassy-net-driver",
"embassy-sync",
"embassy-time", "embassy-time",
"embedded-hal-async", "embedded-hal-async",
"embedded-io-async", "embedded-io-async",

View file

@ -23,6 +23,7 @@ embedded-io-async = "0.6.1"
embassy-net-driver = "0.2.0" embassy-net-driver = "0.2.0"
const_format = "0.2.32" const_format = "0.2.32"
embedded-hal-async = "1.0.0" embedded-hal-async = "1.0.0"
embassy-sync = "0.5.0"
[profile.dev] [profile.dev]
# Rust debug is too slow. # Rust debug is too slow.

View file

@ -5,6 +5,7 @@
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_net::tcp::TcpSocket; use embassy_net::tcp::TcpSocket;
use embassy_net::{Config, Ipv4Address, Stack, StackResources}; use embassy_net::{Config, Ipv4Address, Stack, StackResources};
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, mutex::Mutex};
use esp_hal::{self, IO}; use esp_hal::{self, IO};
use const_format::formatcp; use const_format::formatcp;
@ -12,7 +13,7 @@ use embassy_time::{Duration, Timer};
use embedded_hal_async::digital::Wait; use embedded_hal_async::digital::Wait;
use esp_backtrace as _; use esp_backtrace as _;
use esp_hal::clock::ClockControl; use esp_hal::clock::ClockControl;
use esp_hal::gpio::{AnyPin, Input, PullUp}; use esp_hal::gpio::{AnyPin, Input, Output, PullUp, PushPull};
use esp_hal::Rng; use esp_hal::Rng;
use esp_hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup}; use esp_hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup};
use esp_println::println; use esp_println::println;
@ -86,9 +87,14 @@ async fn main(spawner: Spawner) {
} }
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let button = io.pins.gpio12.into_pull_up_input(); let ring_button = io.pins.gpio12.into_pull_up_input();
println!("Spawning button task"); let open_button = io.pins.gpio14.into_pull_up_input();
spawner.must_spawn(button_task(button.into(), stack)); let status_led = io.pins.gpio27.into_push_pull_output();
let opener = make_static!(Mutex::new(io.pins.gpio26.into_push_pull_output().into()));
spawner.must_spawn(ring_button_task(ring_button.into(), stack));
spawner.must_spawn(open_button_task(open_button.into(), opener));
spawner.must_spawn(status_led_task(status_led.into()));
} }
async fn ring(stack: &'static Stack<WifiDevice<'static, WifiStaDevice>>) -> Result<(), &str> { async fn ring(stack: &'static Stack<WifiDevice<'static, WifiStaDevice>>) -> Result<(), &str> {
@ -145,7 +151,7 @@ async fn ring(stack: &'static Stack<WifiDevice<'static, WifiStaDevice>>) -> Resu
} }
#[embassy_executor::task] #[embassy_executor::task]
async fn button_task( async fn ring_button_task(
mut button: AnyPin<Input<PullUp>>, mut button: AnyPin<Input<PullUp>>,
stack: &'static Stack<WifiDevice<'static, WifiStaDevice>>, stack: &'static Stack<WifiDevice<'static, WifiStaDevice>>,
) { ) {
@ -153,7 +159,7 @@ async fn button_task(
button.wait_for_falling_edge().await.unwrap(); button.wait_for_falling_edge().await.unwrap();
Timer::after_millis(10).await; Timer::after_millis(10).await;
if button.is_low().unwrap() { if button.is_low().unwrap() {
println!("Button pressed"); println!("Bell button pressed");
ring(stack) ring(stack)
.await .await
.unwrap_or_else(|e| println!("ring error: {}", e)); .unwrap_or_else(|e| println!("ring error: {}", e));
@ -161,6 +167,44 @@ async fn button_task(
} }
} }
#[embassy_executor::task]
async fn status_led_task(mut status_led: AnyPin<Output<PushPull>>) {
loop {
status_led.set_high();
Timer::after_millis(100).await;
status_led.set_low();
Timer::after_secs(1).await;
}
}
#[embassy_executor::task]
async fn open_button_task(
mut button: AnyPin<Input<PullUp>>,
opener: &'static Mutex<CriticalSectionRawMutex, AnyPin<Output<PushPull>>>,
) {
loop {
button.wait_for_falling_edge().await.unwrap();
Timer::after_millis(10).await;
if button.is_low().unwrap() {
println!("Open button pressed");
let spawner = Spawner::for_current_executor().await;
spawner
.spawn(open_task(opener))
.unwrap_or_else(|_| println!("Already opening"));
}
}
}
#[embassy_executor::task]
async fn open_task(opener: &'static Mutex<CriticalSectionRawMutex, AnyPin<Output<PushPull>>>) {
let mut opener = opener.lock().await;
opener.set_high();
Timer::after_secs(1).await;
opener.set_low();
}
#[embassy_executor::task] #[embassy_executor::task]
async fn connection(mut controller: WifiController<'static>) { async fn connection(mut controller: WifiController<'static>) {
println!("start connection task"); println!("start connection task");