#![deny(unsafe_code)]
#![allow(clippy::empty_loop)]
#![no_main]
#![no_std]

// Halt on panic
use panic_halt as _; // panic handler

use cortex_m_rt::entry;
use stm32f4xx_hal::{self as hal};

use crate::hal::{pac, prelude::*};

mod leds;
use crate::leds::{Leds, ToggleLed};
mod state_machine;
use crate::state_machine::StateMachine;

#[entry]
fn main() -> ! {
    if let (Some(dp), Some(cp)) = (
        pac::Peripherals::take(),
        cortex_m::peripheral::Peripherals::take(),
    ) {
        //GPIOD ophalen
        let gpiod = dp.GPIOD.split();
        //led pinnen uphalen
        let mut green_pin = &mut gpiod.pd12.into_push_pull_output();
        let mut red_pin = &mut gpiod.pd14.into_push_pull_output();
        let mut blue_pin = &mut gpiod.pd15.into_push_pull_output();
        let mut green = ToggleLed::new(&mut green_pin).expect("faild to set green led");
        let mut red = ToggleLed::new(&mut red_pin).expect("faild to set red led");
        let mut blue = ToggleLed::new(&mut blue_pin).expect("faild to set blue led");
        let mut leds = Leds::new(&mut red, &mut green, &mut blue);
        //leds struct en state machine maken
        let mut state_machine = StateMachine::new(&mut leds);

        //Klok instellen
        let rcc = dp.RCC.constrain();
        let clocks = rcc.cfgr.sysclk(48.MHz()).freeze();

        // Create a delay abstraction based on SysTick
        let mut delay = cp.SYST.delay(&clocks);
        loop {
            let _ = state_machine.second_passed();
            delay.delay_ms(1000_u32);
        }
    }

    loop {}
}