|
| 1 | +#![no_main] |
| 2 | +#![no_std] |
| 3 | + |
| 4 | +use panic_halt as _; |
| 5 | + |
| 6 | +use cortex_m_rt::entry; |
| 7 | +use embedded_hal::blocking::delay::DelayMs; |
| 8 | +use microbit::hal::{clocks::Clocks, timer::Timer}; |
| 9 | +use rubble::beacon::Beacon; |
| 10 | +use rubble::link::{ad_structure::AdStructure, CompanyId, MIN_PDU_BUF}; |
| 11 | +use rubble_nrf5x::radio::{BleRadio, PacketBuffer}; |
| 12 | +use rubble_nrf5x::utils::get_device_address; |
| 13 | + |
| 14 | +#[entry] |
| 15 | +fn main() -> ! { |
| 16 | + static mut BLE_TX_BUF: PacketBuffer = [0; MIN_PDU_BUF]; |
| 17 | + static mut BLE_RX_BUF: PacketBuffer = [0; MIN_PDU_BUF]; |
| 18 | + if let Some(p) = microbit::Peripherals::take() { |
| 19 | + // On reset, the internal high frequency clock is already used, but we |
| 20 | + // also need to switch to the external HF oscillator. This is needed |
| 21 | + // for Bluetooth to work. |
| 22 | + let _clocks = Clocks::new(p.CLOCK).enable_ext_hfosc(); |
| 23 | + |
| 24 | + // Determine device address |
| 25 | + let device_address = get_device_address(); |
| 26 | + |
| 27 | + // Rubble currently requires an RX buffer even though the radio is only used as a TX-only beacon. |
| 28 | + let mut radio = BleRadio::new(p.RADIO, &p.FICR, BLE_TX_BUF, BLE_RX_BUF); |
| 29 | + |
| 30 | + let mut timer = Timer::new(p.TIMER0); |
| 31 | + |
| 32 | + loop { |
| 33 | + defmt::info!("Local name"); |
| 34 | + let beacon = Beacon::new( |
| 35 | + device_address, |
| 36 | + &[AdStructure::CompleteLocalName("Rusty microbit")], |
| 37 | + ) |
| 38 | + .unwrap(); |
| 39 | + beacon.broadcast(&mut radio); |
| 40 | + timer.delay_ms(1_000_u16); |
| 41 | + |
| 42 | + let data = "Hello world"; |
| 43 | + defmt::info!("{}", data); |
| 44 | + let beacon = Beacon::new( |
| 45 | + device_address, |
| 46 | + &[AdStructure::ManufacturerSpecificData { |
| 47 | + company_identifier: CompanyId::from_raw(0xffff), |
| 48 | + payload: data.as_bytes(), |
| 49 | + }], |
| 50 | + ) |
| 51 | + .unwrap(); |
| 52 | + beacon.broadcast(&mut radio); |
| 53 | + timer.delay_ms(1_000_u16); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + loop { |
| 58 | + continue; |
| 59 | + } |
| 60 | +} |
0 commit comments