-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d5ef4e1
commit 3ad39c1
Showing
1 changed file
with
35 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,45 @@ | ||
#[warn(unused_imports)] | ||
use std::time::Instant; | ||
|
||
use button_driver::{Button, ButtonConfig}; | ||
use esp_idf_hal::{gpio::PinDriver, prelude::Peripherals}; | ||
use esp_idf_sys::EspError; | ||
use esp_idf_hal::gpio::InputPin; | ||
use esp_idf_hal::gpio::Input; | ||
|
||
/* | ||
impl Button for Instant { | ||
fn new(&self) { | ||
} | ||
} | ||
*/ | ||
|
||
struct MyPinWrapper<'a, T: InputPin> { | ||
pin: Button<'a, T, Input>, | ||
} | ||
|
||
//PinWrapperトレイトをMyPinWrapperに実装する。 | ||
impl<'a, T: > PinWrapper for MyPinWrapper<'a, T> { | ||
//PinWrapperトレイトのメソッドを実装する。 | ||
fn is_high(&mut self) -> bool { | ||
self.pin.is_high().unwrap_or(false) | ||
} | ||
//他のPinWrapperトレイトのメソッドを実装する。 | ||
} | ||
use log::info; | ||
|
||
fn main() -> Result<(), EspError> { | ||
esp_idf_svc::log::EspLogger::initialize_default(); | ||
|
||
let peripherals = Peripherals::take().unwrap(); | ||
let pin = PinDriver::input(peripherals.pins.gpio35)?; | ||
let my_pin = MyPinWrapper { pin }; | ||
let mut button: button_driver::Button<MyPinWrapper<'_, Gpio>, I> = Button::new(my_pin, ButtonConfig::default()); | ||
|
||
loop { | ||
button.tick(); | ||
|
||
if button.is_clicked() { | ||
println!("Clicked!"); | ||
} | ||
|
||
button.reset(); | ||
|
||
let pin = PinDriver::input(peripherals.pins.gpio9)?; | ||
|
||
let mut button = Button::<_, Instant>::new(pin, ButtonConfig::default()); | ||
|
||
loop { | ||
button.tick(); | ||
|
||
if let Some(dur) = button.held_time() { | ||
info!("Total holding time {:?}", dur); | ||
|
||
if button.is_clicked() { | ||
info!("Clicked + held"); | ||
} else if button.is_double_clicked() { | ||
info!("Double clicked + held"); | ||
} else if button.holds() == 2 && button.clicks() > 0 { | ||
info!("Held twice with {} clicks", button.clicks()); | ||
} else if button.holds() == 2 { | ||
info!("Held twice"); | ||
} | ||
} else { | ||
if button.is_clicked() { | ||
info!("Click"); | ||
} else if button.is_double_clicked() { | ||
info!("Double click"); | ||
} else if button.is_triple_clicked() { | ||
info!("Triple click"); | ||
} else if let Some(dur) = button.current_holding_time() { | ||
info!("Held for {:?}", dur); | ||
} | ||
} | ||
|
||
button.reset(); | ||
} | ||
} |