#![no_std]

use libtock::ble_parser;
use libtock::result::TockResult;
use libtock::simple_ble;
use serde::Deserialize;

libtock_core::stack_size! {0x800}

#[derive(Deserialize)]
struct LedCommand {
    pub nr: u8,
}

#[libtock::main]
async fn main() -> TockResult<()> {
    let mut drivers = libtock::retrieve_drivers()?;
    let leds_driver = drivers.leds.init_driver()?;

    let mut ble_scanning_driver_factory = drivers.ble_scanning;
    let mut ble_scanning_driver = ble_scanning_driver_factory.create_driver();
    let mut ble_scanning_driver_sharing = ble_scanning_driver.share_memory()?;
    let ble_scanning_driver_scanning = ble_scanning_driver_sharing.start()?;

    loop {
        let value = ble_scanning_driver_scanning.stream_values().await;
        ble_parser::find(&value, simple_ble::gap_data::SERVICE_DATA as u8)
            .and_then(|service_data| ble_parser::extract_for_service([91, 79], service_data))
            .and_then(|payload| corepack::from_bytes::<LedCommand>(&payload).ok())
            .and_then(|msg| leds_driver.get(msg.nr as usize).ok())
            .and_then(|led| led.on().ok());
    }
}
