From 0c17c0314f570f15efed549e0c50487a30400203 Mon Sep 17 00:00:00 2001 From: aOK Date: Tue, 11 Jun 2024 14:34:48 +0300 Subject: [PATCH] working button to trigger relay and blinking led code --- README.md | 1 + src/main.rs | 4 +-- src/run/blinky.rs | 32 ++++++++++++++--------- src/run/button.rs | 65 ++++++++++++++++++++++++++++++----------------- src/run/relay.rs | 6 ++--- 5 files changed, 67 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 32f66c2..d4418ae 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # Creating a new repository on the command line + touch README.md git init git checkout -b main diff --git a/src/main.rs b/src/main.rs index bef2608..35162d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, diff --git a/src/run/blinky.rs b/src/run/blinky.rs index d39b7c0..9990e8b 100644 --- a/src/run/blinky.rs +++ b/src/run/blinky.rs @@ -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; } } diff --git a/src/run/button.rs b/src/run/button.rs index 1a5da9a..2df681c 100644 --- a/src/run/button.rs +++ b/src/run/button.rs @@ -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; } } diff --git a/src/run/relay.rs b/src/run/relay.rs index 6ff8bf3..8efc81a 100644 --- a/src/run/relay.rs +++ b/src/run/relay.rs @@ -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();