#![no_std]
#![no_main]

use esp_backtrace as _;
use esp_println::println;
use hal::{clock::ClockControl, 
gpio::IO, 
peripherals::Peripherals, 
prelude::*, 
Delay, 
adc::*,
};

#[entry]
fn main() -> ! {
    let peripherals = Peripherals::take();
    let system = peripherals.SYSTEM.split();
    let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

    let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);

    //implementation of analog 1

    let analog = peripherals.APB_SARADC.split();

    let mut adc1_config = AdcConfig::new();

    let mut pin = adc1_config.enable_pin(io.pins.gpio2.into_analog(), Attenuation::Attenuation11dB);

    let mut adc1 = ADC::<ADC1>::adc(analog.adc1, adc1_config).unwrap();

    let mut delay = Delay::new(&clocks);
    

    // Initialize the Delay peripheral, and use it to toggle the LED state in a
    // loop.
    let mut delay = Delay::new(&clocks);
    println!("Hello world!\r");
    loop {
        //////////////////////////////////////////////////////////
        delay.delay_ms(1000u32);
        //check of analog input
        let pin_value: u16 = nb::block!(adc1.read(&mut pin)).unwrap();
        println!("PIN2 ADC reading = {} \r", pin_value);
    }
}