#![no_std]
#![no_main]

use cortex_m_rt::entry;
use defmt::info;
use nb::block;
use stm32f1xx_hal::{pac, prelude::*, timer::Timer};
use {defmt_rtt as _, panic_probe as _};

#[entry]
fn main() -> ! {
    let cp = cortex_m::Peripherals::take().unwrap();
    let dp = pac::Peripherals::take().unwrap();

    let mut rcc = dp.RCC.constrain();
    let mut gpioa = dp.GPIOA.split(&mut rcc);
    let button = gpioa.pa0.into_pull_up_input(&mut gpioa.crl);

    let mut timer = Timer::syst(cp.SYST, &rcc.clocks).counter_hz();
    timer.start(40.Hz()).unwrap();

    let mut last_pressed = false;

    loop {
        let pressed = button.is_low();
        if pressed != last_pressed {
            if pressed {
                info!("button: pressed");
            } else {
                info!("button: released");
            }
            last_pressed = pressed;
        }

        block!(timer.wait()).unwrap();
    }
}
