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
|
# Creating a new repository on the command line
|
||||||
|
|
||||||
touch README.md
|
touch README.md
|
||||||
git init
|
git init
|
||||||
git checkout -b main
|
git checkout -b main
|
||||||
|
|
|
@ -48,8 +48,8 @@ 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_pin = Input::new(io.pins.gpio12, Pull::Down);
|
let button_pin = Input::new(io.pins.gpio12, Pull::Down);
|
||||||
let led_pin = Output::new(io.pins.gpio13, Level::Low);
|
let led_pin = Output::new(io.pins.gpio2, Level::Low);
|
||||||
let relay_pin = Output::new(io.pins.gpio2, Level::Low);
|
let relay_pin = Output::new(io.pins.gpio16, Level::Low);
|
||||||
|
|
||||||
let channel: &'static mut PubSubChannel<
|
let channel: &'static mut PubSubChannel<
|
||||||
NoopRawMutex,
|
NoopRawMutex,
|
||||||
|
|
|
@ -1,29 +1,37 @@
|
||||||
extern crate embassy_executor;
|
extern crate embassy_executor;
|
||||||
|
|
||||||
use crate::confg::Sens;
|
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 embassy_time::{Duration, Timer};
|
||||||
use hal::gpio::{Gpio13, Output};
|
use hal::gpio::{Gpio2, Output};
|
||||||
|
|
||||||
use super::shared::{Command, QUEUE_SIZE};
|
use super::shared::{Command, QUEUE_SIZE};
|
||||||
|
|
||||||
#[embassy_executor::task]
|
#[embassy_executor::task]
|
||||||
pub async fn blink_green(
|
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>,
|
mut subscriber: Subscriber<'static, NoopRawMutex, Command, QUEUE_SIZE, QUEUE_SIZE, QUEUE_SIZE>,
|
||||||
) {
|
) {
|
||||||
|
let mut led_state = false;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
log::info!("{}", Sens("Loop..."));
|
match subscriber.try_next_message() {
|
||||||
let command = subscriber.next_message_pure().await;
|
Some(WaitResult::Message(command)) => {
|
||||||
// if command.is_on {
|
led_state = command.led_state;
|
||||||
if command.led_state {
|
}
|
||||||
pin.set_high();
|
Some(WaitResult::Lagged(_)) | None => {
|
||||||
esp_println::println!("Set LED High: {}", command.led_state);
|
// No new message, continue with the current state
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if led_state {
|
||||||
|
pin.toggle();
|
||||||
|
Timer::after(Duration::from_millis(200)).await;
|
||||||
} else {
|
} else {
|
||||||
pin.set_low();
|
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;
|
extern crate embassy_executor;
|
||||||
|
|
||||||
|
use embassy_time::{Duration, Timer};
|
||||||
// use esp_println::println;
|
// use esp_println::println;
|
||||||
use hal::gpio::{Gpio12, Input};
|
use hal::gpio::{Gpio12, Input};
|
||||||
|
|
||||||
|
@ -12,21 +13,27 @@ pub async fn button_task(
|
||||||
button_pin: Input<'static, Gpio12>,
|
button_pin: Input<'static, Gpio12>,
|
||||||
publisher: Publisher<'static, NoopRawMutex, Command, QUEUE_SIZE, QUEUE_SIZE, QUEUE_SIZE>,
|
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 previous_button_state = false;
|
||||||
let mut led_state = false;
|
let mut led_state = false;
|
||||||
let mut relay_state = false;
|
let mut relay_state = false;
|
||||||
|
let mut press_count = 0;
|
||||||
|
let mut last_press_time = embassy_time::Instant::now();
|
||||||
loop {
|
loop {
|
||||||
// println!("Button State: {}", command_current_state.is_on);
|
|
||||||
let current_button_state = button_pin.is_high();
|
let current_button_state = button_pin.is_high();
|
||||||
|
|
||||||
if current_button_state && !previous_button_state {
|
if current_button_state && !previous_button_state {
|
||||||
// Button was pressed
|
// Button was pressed
|
||||||
|
let now = embassy_time::Instant::now();
|
||||||
|
let elapsed = now.duration_since(last_press_time);
|
||||||
|
last_press_time = now;
|
||||||
|
|
||||||
|
if elapsed < Duration::from_millis(500) {
|
||||||
|
press_count += 1;
|
||||||
|
} else {
|
||||||
|
press_count = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if press_count == 1 {
|
||||||
led_state = !led_state;
|
led_state = !led_state;
|
||||||
relay_state = !relay_state;
|
relay_state = !relay_state;
|
||||||
let payload = Command {
|
let payload = Command {
|
||||||
|
@ -34,19 +41,29 @@ pub async fn button_task(
|
||||||
led_state,
|
led_state,
|
||||||
relay_state,
|
relay_state,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Send the payload through the channel
|
|
||||||
publisher.publish(payload).await;
|
publisher.publish(payload).await;
|
||||||
|
|
||||||
esp_println::println!(
|
esp_println::println!(
|
||||||
"Button pressed, LED State: {}, Relay State: {}",
|
"Single press: LED State: {}, Relay State: {}",
|
||||||
led_state,
|
led_state,
|
||||||
relay_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
|
if current_button_state {
|
||||||
embassy_time::Timer::after(embassy_time::Duration::from_millis(50)).await;
|
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 crate::confg::Sens;
|
||||||
use embassy_sync::{blocking_mutex::raw::NoopRawMutex, pubsub::Subscriber};
|
use embassy_sync::{blocking_mutex::raw::NoopRawMutex, pubsub::Subscriber};
|
||||||
use esp_println::println;
|
use esp_println::println;
|
||||||
use hal::gpio::{Gpio2, Output};
|
use hal::gpio::{Gpio16, Output};
|
||||||
|
|
||||||
use super::shared::{Command, QUEUE_SIZE};
|
use super::shared::{Command, QUEUE_SIZE};
|
||||||
|
|
||||||
#[embassy_executor::task]
|
#[embassy_executor::task]
|
||||||
pub async fn relay_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>,
|
mut subscriber: Subscriber<'static, NoopRawMutex, Command, QUEUE_SIZE, QUEUE_SIZE, QUEUE_SIZE>,
|
||||||
) {
|
) {
|
||||||
loop {
|
loop {
|
||||||
|
@ -17,7 +17,7 @@ pub async fn relay_task(
|
||||||
let command = subscriber.next_message_pure().await;
|
let command = subscriber.next_message_pure().await;
|
||||||
// if command.is_on {
|
// if command.is_on {
|
||||||
if command.relay_state {
|
if command.relay_state {
|
||||||
relay.set_high();
|
relay.toggle();
|
||||||
println!("Set Relay High: {}", command.is_on);
|
println!("Set Relay High: {}", command.is_on);
|
||||||
} else {
|
} else {
|
||||||
relay.set_low();
|
relay.set_low();
|
||||||
|
|
Loading…
Reference in a new issue