diff --git a/src/bin/temp_rh_measurement.rs b/src/bin/temp_rh_measurement.rs index 4cf083f..2e9ea80 100644 --- a/src/bin/temp_rh_measurement.rs +++ b/src/bin/temp_rh_measurement.rs @@ -20,6 +20,62 @@ use embassy_nrf::twim; use embassy_nrf::{bind_interrupts, peripherals}; use {defmt_rtt as _, panic_probe as _}; +// +// +// BLE code +// +// + +#[nrf_softdevice::gatt_service(uuid = "181a")] +struct EnvironmentalService { + #[characteristic(uuid = "2a6e", read, notify)] + temperature: u32, + #[characteristic(uuid = "2a6f", read, notify)] + humidity: u32, +} + +#[nrf_softdevice::gatt_server] +struct Server { + bas: EnvironmentalService, +} + +#[embassy_executor::task] +async fn softdevice_task(sd: &'static Softdevice) -> ! { + sd.run().await +} + +#[embassy_executor::task] +async fn ble_task(sd: &'static Softdevice, server: &'static Server) { + static ADV_DATA: LegacyAdvertisementPayload = LegacyAdvertisementBuilder::new() + .flags(&[Flag::GeneralDiscovery, Flag::LE_Only]) + .services_16(ServiceList::Complete, &[ServiceUuid16::BATTERY]) + .full_name("CommonSense") + .build(); + + static SCAN_DATA: LegacyAdvertisementPayload = LegacyAdvertisementBuilder::new() + .services_128( + ServiceList::Complete, + &[0x9e7312e0_2354_11eb_9f10_fbc30a62cf38_u128.to_le_bytes()], + ) + .build(); + + loop { + info!("Creating config"); + let config = peripheral::Config::default(); + info!("Creating adv object"); + let adv = peripheral::ConnectableAdvertisement::ScannableUndirected { + adv_data: &ADV_DATA, + scan_data: &SCAN_DATA, + }; + + let conn = unwrap!(peripheral::advertise_connectable(sd, adv, &config).await); + + let _e = gatt_server::run(&conn, server, |_| {}).await; + + Timer::after_secs(10).await; + } +} + // // // Sensing code @@ -78,72 +134,6 @@ async fn measurement_task(mut sensor: TempHumiditySensor, server: &'static Serve } } -// -// -// BLE code -// -// - -#[nrf_softdevice::gatt_service(uuid = "181a")] -struct EnvironmentalService { - #[characteristic(uuid = "2a6e", read, notify)] - temperature: u32, - #[characteristic(uuid = "2a6f", read, notify)] - humidity: u32, -} - -#[nrf_softdevice::gatt_server] -struct Server { - bas: EnvironmentalService, -} - -#[embassy_executor::task] -async fn softdevice_task(sd: &'static Softdevice) -> ! { - sd.run().await -} - -#[embassy_executor::task] -async fn ble_task(sd: &'static Softdevice, server: &'static Server) { - static ADV_DATA: LegacyAdvertisementPayload = LegacyAdvertisementBuilder::new() - .flags(&[Flag::GeneralDiscovery, Flag::LE_Only]) - .services_16(ServiceList::Complete, &[ServiceUuid16::BATTERY]) - .full_name("CommonSense") - .build(); - - static SCAN_DATA: LegacyAdvertisementPayload = LegacyAdvertisementBuilder::new() - .services_128( - ServiceList::Complete, - &[0x9e7312e0_2354_11eb_9f10_fbc30a62cf38_u128.to_le_bytes()], - ) - .build(); - - loop { - info!("Creating config"); - let config = peripheral::Config::default(); - info!("Creating adv object"); - let adv = peripheral::ConnectableAdvertisement::ScannableUndirected { - adv_data: &ADV_DATA, - scan_data: &SCAN_DATA, - }; - - let conn = unwrap!(peripheral::advertise_connectable(sd, adv, &config).await); - - let _e = gatt_server::run(&conn, server, |e| match e { - ServerEvent::Bas(e) => match e { - EnvironmentalServiceEvent::HumidityCccdWrite { notifications } => { - info!("humidity notifications: {}", notifications) - } - EnvironmentalServiceEvent::TemperatureCccdWrite { notifications } => { - info!("temperature notifications: {}", notifications) - } - }, - }) - .await; - - Timer::after_secs(10).await; - } -} - // // // Main code