#![no_std]
#![no_main]

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

fn classify(raw: u16) -> &'static str {
    match raw {
        0..=1365 => "low",
        1366..=2730 => "medium",
        _ => "high",
    }
}

#[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 mut adc = Adc::new(dp.ADC1, &mut rcc);
    let mut analog_pin = gpioa.pa1.into_analog(&mut gpioa.crl);

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

    loop {
        let raw: u16 = adc.read(&mut analog_pin).unwrap();
        info!("adc raw={} level={}", raw, classify(raw));
        block!(timer.wait()).unwrap();
    }
}
