From 479e042b1b5122e30d4b9fcc6cfb277b419a46ae Mon Sep 17 00:00:00 2001 From: Placeless Date: Mon, 24 Feb 2025 13:39:18 +0900 Subject: [PATCH] await error & memo pullup-resistance --- Cargo.toml | 2 ++ README.md | 2 +- src/main.rs | 37 +++++-------------------------------- 3 files changed, 8 insertions(+), 33 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 408db63..6b6407b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,6 +34,8 @@ egui = "0.31.0" display-interface-spi = { version = "0.5.0" } chrono = "0.4" button-driver = "0.2.2" +async-button = "0.2.0" +tokio = "1.43.0" [build-dependencies] embuild = "0.33" diff --git a/README.md b/README.md index 526f47b..cc614f1 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ TTGO T-Watch 2020 v3 firmware written in Rustlang ### I/Os - Interrupt RTC: IO37 - - PMU/AXP202(Button) + - PMU/AXP202(Button-pull up resistance) - interrupt IO35 - I2C_SDA IO21 - I2C_SCL IO22 diff --git a/src/main.rs b/src/main.rs index 7ef86da..27cd10d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,46 +4,19 @@ use esp_idf_sys::EspError; use log::info; use std::thread; use std::time::Duration; +use async_button::*; fn main() -> Result<(), EspError> { esp_idf_svc::log::EspLogger::initialize_default(); let peripherals = Peripherals::take().unwrap(); let pin = PinDriver::input(peripherals.pins.gpio35)?; - let mut last_state = pin.is_high(); - let debounce_delay = Duration::from_millis(50); - - // Timer トレイトを直接インスタンス化するのではなく、周辺機器から取得したタイマーインスタンスを使用する - let timer = peripherals.timer00; - let mut timer_driver = TimerDriver::new(timer, &Config::default())?; - timer_driver.enable(true)?; // true を渡してタイマーを有効にする - - let mut last_click_time = timer_driver.counter()?; - let double_click_interval = Duration::from_millis(300); - let mut click_count = 0; + let mut button = Button::new(pin, ButtonConfig::default()); loop { - let current_state = pin.is_low(); - if last_state != current_state { - thread::sleep(debounce_delay); - if pin.is_low() == current_state { - last_state = current_state; - if current_state { - let now = timer_driver.counter()?; - let elapsed = now - last_click_time; - if elapsed < double_click_interval.as_micros() as u64 { - click_count += 1; - info!("double click"); - } else { - click_count = 1; - info!("click"); - } - last_click_time = now; - } else { - info!("button released"); - } - } + match button.update().await { + ButtonEvent::ShortPress { count } => {info!("HelloButton!")}, + ButtonEvent::LongPress => {info!("It hurts!")}, } - thread::sleep(Duration::from_millis(10)); } } \ No newline at end of file