Skip to content

Commit

Permalink
await error & memo pullup-resistance
Browse files Browse the repository at this point in the history
  • Loading branch information
YoshitakaNaraoka committed Feb 24, 2025
1 parent a730a94 commit 479e042
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 33 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 5 additions & 32 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 479e042

Please sign in to comment.