working button to trigger relay and blinking led code
This commit is contained in:
parent
0e62f65a78
commit
0c17c0314f
5 changed files with 67 additions and 41 deletions
|
@ -1,4 +1,5 @@
|
|||
# Creating a new repository on the command line
|
||||
|
||||
touch README.md
|
||||
git init
|
||||
git checkout -b main
|
||||
|
|
|
@ -48,8 +48,8 @@ async fn main(spawner: Spawner) {
|
|||
|
||||
let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
|
||||
let button_pin = Input::new(io.pins.gpio12, Pull::Down);
|
||||
let led_pin = Output::new(io.pins.gpio13, Level::Low);
|
||||
let relay_pin = Output::new(io.pins.gpio2, Level::Low);
|
||||
let led_pin = Output::new(io.pins.gpio2, Level::Low);
|
||||
let relay_pin = Output::new(io.pins.gpio16, Level::Low);
|
||||
|
||||
let channel: &'static mut PubSubChannel<
|
||||
NoopRawMutex,
|
||||
|
|
|
@ -1,29 +1,37 @@
|
|||
extern crate embassy_executor;
|
||||
|
||||
use crate::confg::Sens;
|
||||
use embassy_sync::{blocking_mutex::raw::NoopRawMutex, pubsub::Subscriber};
|
||||
use embassy_sync::{
|
||||
blocking_mutex::raw::NoopRawMutex,
|
||||
pubsub::{Subscriber, WaitResult},
|
||||
};
|
||||
use embassy_time::{Duration, Timer};
|
||||
use hal::gpio::{Gpio13, Output};
|
||||
use hal::gpio::{Gpio2, Output};
|
||||
|
||||
use super::shared::{Command, QUEUE_SIZE};
|
||||
|
||||
#[embassy_executor::task]
|
||||
pub async fn blink_green(
|
||||
mut pin: Output<'static, Gpio13>,
|
||||
mut pin: Output<'static, Gpio2>,
|
||||
mut subscriber: Subscriber<'static, NoopRawMutex, Command, QUEUE_SIZE, QUEUE_SIZE, QUEUE_SIZE>,
|
||||
) {
|
||||
let mut led_state = false;
|
||||
|
||||
loop {
|
||||
log::info!("{}", Sens("Loop..."));
|
||||
let command = subscriber.next_message_pure().await;
|
||||
// if command.is_on {
|
||||
if command.led_state {
|
||||
pin.set_high();
|
||||
esp_println::println!("Set LED High: {}", command.led_state);
|
||||
match subscriber.try_next_message() {
|
||||
Some(WaitResult::Message(command)) => {
|
||||
led_state = command.led_state;
|
||||
}
|
||||
Some(WaitResult::Lagged(_)) | None => {
|
||||
// No new message, continue with the current state
|
||||
}
|
||||
}
|
||||
if led_state {
|
||||
pin.toggle();
|
||||
Timer::after(Duration::from_millis(200)).await;
|
||||
} else {
|
||||
pin.set_low();
|
||||
esp_println::println!("Set LED Low");
|
||||
Timer::after(Duration::from_millis(50)).await;
|
||||
}
|
||||
// }
|
||||
Timer::after(Duration::from_millis(330)).await;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
extern crate embassy_executor;
|
||||
|
||||
use embassy_time::{Duration, Timer};
|
||||
// use esp_println::println;
|
||||
use hal::gpio::{Gpio12, Input};
|
||||
|
||||
|
@ -12,41 +13,57 @@ pub async fn button_task(
|
|||
button_pin: Input<'static, Gpio12>,
|
||||
publisher: Publisher<'static, NoopRawMutex, Command, QUEUE_SIZE, QUEUE_SIZE, QUEUE_SIZE>,
|
||||
) {
|
||||
let _command_current_state = Command {
|
||||
is_on: false,
|
||||
led_state: false,
|
||||
relay_state: false,
|
||||
};
|
||||
|
||||
let mut previous_button_state = false;
|
||||
let mut led_state = false;
|
||||
let mut relay_state = false;
|
||||
let mut press_count = 0;
|
||||
let mut last_press_time = embassy_time::Instant::now();
|
||||
loop {
|
||||
// println!("Button State: {}", command_current_state.is_on);
|
||||
let current_button_state = button_pin.is_high();
|
||||
|
||||
if current_button_state && !previous_button_state {
|
||||
// Button was pressed
|
||||
led_state = !led_state;
|
||||
relay_state = !relay_state;
|
||||
let payload = Command {
|
||||
is_on: true,
|
||||
led_state,
|
||||
relay_state,
|
||||
};
|
||||
let now = embassy_time::Instant::now();
|
||||
let elapsed = now.duration_since(last_press_time);
|
||||
last_press_time = now;
|
||||
|
||||
// Send the payload through the channel
|
||||
publisher.publish(payload).await;
|
||||
if elapsed < Duration::from_millis(500) {
|
||||
press_count += 1;
|
||||
} else {
|
||||
press_count = 1;
|
||||
}
|
||||
|
||||
esp_println::println!(
|
||||
"Button pressed, LED State: {}, Relay State: {}",
|
||||
led_state,
|
||||
relay_state
|
||||
);
|
||||
if press_count == 1 {
|
||||
led_state = !led_state;
|
||||
relay_state = !relay_state;
|
||||
let payload = Command {
|
||||
is_on: true,
|
||||
led_state,
|
||||
relay_state,
|
||||
};
|
||||
publisher.publish(payload).await;
|
||||
esp_println::println!(
|
||||
"Single press: LED State: {}, Relay State: {}",
|
||||
led_state,
|
||||
relay_state
|
||||
);
|
||||
} else if press_count == 2 {
|
||||
esp_println::println!("Double press: Printing from thermal printer...");
|
||||
// Insert thermal printer logic here
|
||||
}
|
||||
}
|
||||
previous_button_state = current_button_state;
|
||||
|
||||
// Debouncing delay
|
||||
embassy_time::Timer::after(embassy_time::Duration::from_millis(50)).await;
|
||||
if current_button_state {
|
||||
let now = embassy_time::Instant::now();
|
||||
let elapsed = now.duration_since(last_press_time);
|
||||
if elapsed >= Duration::from_secs(2) {
|
||||
esp_println::println!("Long press: Rebooting device...");
|
||||
// Insert reboot logic here
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
previous_button_state = current_button_state;
|
||||
Timer::after(Duration::from_millis(50)).await;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,13 +3,13 @@ extern crate embassy_executor;
|
|||
use crate::confg::Sens;
|
||||
use embassy_sync::{blocking_mutex::raw::NoopRawMutex, pubsub::Subscriber};
|
||||
use esp_println::println;
|
||||
use hal::gpio::{Gpio2, Output};
|
||||
use hal::gpio::{Gpio16, Output};
|
||||
|
||||
use super::shared::{Command, QUEUE_SIZE};
|
||||
|
||||
#[embassy_executor::task]
|
||||
pub async fn relay_task(
|
||||
mut relay: Output<'static, Gpio2>,
|
||||
mut relay: Output<'static, Gpio16>,
|
||||
mut subscriber: Subscriber<'static, NoopRawMutex, Command, QUEUE_SIZE, QUEUE_SIZE, QUEUE_SIZE>,
|
||||
) {
|
||||
loop {
|
||||
|
@ -17,7 +17,7 @@ pub async fn relay_task(
|
|||
let command = subscriber.next_message_pure().await;
|
||||
// if command.is_on {
|
||||
if command.relay_state {
|
||||
relay.set_high();
|
||||
relay.toggle();
|
||||
println!("Set Relay High: {}", command.is_on);
|
||||
} else {
|
||||
relay.set_low();
|
||||
|
|
Loading…
Reference in a new issue