#![no_std]
#![no_main]
use core::ops::Sub;
use core::panic::PanicInfo;
use esp_hal::{clock::CpuClock, delay::Delay, main, peripherals::Interrupt};
use esp_println::println;
use embedded_hal_compat::ReverseCompat;
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
println!("Panic: {}", info);
loop {}
}
use button_driver::{Button, ButtonConfig, InstantProvider};
use esp_hal::gpio::{Input, Pull};
use esp_hal::timer::timg::TimerGroup;
use core::time::Duration;
use esp_hal::timer::Timer;
use fugit::Instant;
use esp_hal::gpio::Pin;
#[main]
fn main() -> ! {
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
let peripherals = esp_hal::init(config);
//let mut button_driver: ButtonDriver = ButtonDriver::new(peripherals.GPIO0.into(), peripherals.GPIO1.into());
//let mut display_driver: DisplayDriver = DisplayDriver::new(peripherals.GPIO4.into(), peripherals.GPIO6.into(), peripherals.I2C0);
//display_driver.output("Hello world!");
//let mut button: Button::<_, EspInstant> = Button::new(Input::new(peripherals.GPIO8, Pull::Down), ButtonConfig::default());
let mut delay = Delay::new();
let button_input = Input::new(peripherals.GPIO1, Pull::Down);
let mut delay = Delay::new();
loop {
println!("{:?}", button_input.is_high());
//print_when_pressed(&mut button, &mut delay);
/*
button.tick();
if button.is_clicked() {
println!("Click");
} else if button.is_double_clicked() {
println!("Double click");
} else if button.is_triple_clicked() {
println!("Triple click");
} else if let Some(dur) = button.current_holding_time() {
println!("Held for {:?}", dur);
} else if let Some(dur) = button.held_time() {
println!("Total holding time {:?}", dur);
}
button.reset();
*/
/*
button_driver.tick();
if button_driver.get_states().0 {
println!("First pressed!");
}
if button_driver.get_states().1 {
println!("Second pressed!");
}
*/
}
}
fn print_when_pressed(button: &mut Input<'_>, delay: &mut Delay) {
let mut was_pressed = false;
loop {
let is_pressed = button.is_high();
if is_pressed && !was_pressed {
println!("Button pressed!");
}
was_pressed = is_pressed;
delay.delay_millis(100);
}
}
#[derive(Clone, Debug, PartialEq, PartialOrd)]
struct EspInstant {
counter: Duration,
}
impl Sub<EspInstant> for EspInstant {
type Output = Duration;
fn sub(self, rhs: EspInstant) -> Self::Output {
self.counter - rhs.counter
}
}
impl InstantProvider<Duration> for EspInstant {
fn now() -> Self {
//println!("Now");
EspInstant {
counter: Duration::from_nanos((esp_hal::time::now() - Instant::<u64, 1, 1000000>::from_ticks(0)).to_nanos()),
}
}
}