blob: afdb8bbaba270d0dff3050153c6d2fe04079d0b3 [file] [log] [blame]
Philipp Vollmer41b91352018-02-05 09:55:54 +01001#![no_std]
Woyten2b94dd52018-07-09 21:06:53 +02002
torfmasterc84685a2019-02-08 19:23:22 +01003use libtock::ble_parser;
torfmaster3d605242019-12-16 22:36:39 +01004use libtock::result::TockResult;
torfmasterc84685a2019-02-08 19:23:22 +01005use libtock::simple_ble;
Woyten30fc95b2019-01-10 21:09:46 +01006use serde::Deserialize;
Philipp Vollmer41b91352018-02-05 09:55:54 +01007
Johnathan Van Why074fa7d2020-10-14 17:06:50 -07008libtock_core::stack_size! {0x800}
9
Philipp Vollmer349417d2018-02-24 17:29:19 +010010#[derive(Deserialize)]
11struct LedCommand {
12 pub nr: u8,
13 pub st: bool,
14}
Philipp Vollmer41b91352018-02-05 09:55:54 +010015
torfmasterb06f6372019-12-17 09:20:48 +010016#[libtock::main]
torfmaster3d605242019-12-16 22:36:39 +010017async fn main() -> TockResult<()> {
Woytenb1226202020-01-13 22:47:48 +010018 let mut drivers = libtock::retrieve_drivers()?;
torfmaster98de36a2020-02-23 18:55:12 +010019 let leds_driver = drivers.leds.init_driver()?;
torfmaster595d9c62019-12-27 18:15:31 +010020
torfmasterdc326a42020-02-26 23:17:59 +010021 let mut ble_scanning_driver_factory = drivers.ble_scanning;
torfmaster76c81942020-02-23 17:31:59 +010022 let mut ble_scanning_driver = ble_scanning_driver_factory.create_driver();
23 let mut ble_scanning_driver_sharing = ble_scanning_driver.share_memory()?;
24 let ble_scanning_driver_scanning = ble_scanning_driver_sharing.start()?;
torfmaster0f902f42020-01-04 13:07:01 +010025
torfmaster76c81942020-02-23 17:31:59 +010026 loop {
27 let value = ble_scanning_driver_scanning.stream_values().await;
28 ble_parser::find(&value, simple_ble::gap_data::SERVICE_DATA as u8)
Woyten2b94dd52018-07-09 21:06:53 +020029 .and_then(|service_data| ble_parser::extract_for_service([91, 79], service_data))
30 .and_then(|payload| corepack::from_bytes::<LedCommand>(&payload).ok())
torfmaster98de36a2020-02-23 18:55:12 +010031 .and_then(|msg| leds_driver.get(msg.nr as usize).ok())
torfmaster76c81942020-02-23 17:31:59 +010032 .and_then(|led| led.on().ok());
33 }
Philipp Vollmer41b91352018-02-05 09:55:54 +010034}