#![no_main]

use cortex_m::asm::nop;
use cortex_m_rt::entry;
use nrf52833_hal as hal;
use panic_halt as _;

use hal::{
    pac::Peripherals,
    gpio::p0::Parts as P0Parts,
    saadc::{SaadcConfig, Saadc},
 };
use rtt_target::{self, rprintln, rtt_init_print};

#[entry]
fn main() -> ! {
    let board = Peripherals::take().unwrap();
    let gpios = P0Parts::new(board.P0);
    
    // initialize saadc interface
    let saadc_config = SaadcConfig::default();
    let mut saadc = Saadc::new(board.SAADC, saadc_config);
    let mut saadc_pin = gpios.p0_02.into_floating_input(); // the pin your analog device is connected to

    rtt_init_print!();

    loop {
        for _ in 0..200_000 {
            nop();
        }
    
        // blocking read from saadc for `saadc_config.time` microseconds
        let saadc_result: Result<i16, ()> = saadc.read_channel(&mut saadc_pin);

        match saadc_result {
            Ok(value) => {
                let temp: i32 = (500 * value as i32) / 1024;
                rprintln!("{:?}", temp / 100);
            },
            Err(_) => {}

        };
    }

}