Skip to content

Commit 7337b89

Browse files
author
Henrik Alsér
committed
Add BLE example
1 parent 084c049 commit 7337b89

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

examples/ble-beacon/Cargo.toml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[package]
2+
name = "ble-beacon"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
[dependencies]
7+
embedded-hal = "0.2.4"
8+
cortex-m-rt = "0.6.12"
9+
panic-halt = "0.2.0"
10+
defmt-rtt = "0.2.0"
11+
defmt = "0.2.0"
12+
rubble = "0.0.4"
13+
rubble-nrf5x = { version = "0.0.4", default-features = false, features = ["51"] }
14+
15+
[dependencies.microbit]
16+
path = "../../microbit"
17+
optional = true
18+
19+
[dependencies.microbit-v2]
20+
path = "../../microbit-v2"
21+
optional = true
22+
23+
[features]
24+
v1 = ["microbit"]
25+
v2 = ["microbit-v2"]
26+
27+
default = [
28+
"defmt-default",
29+
]
30+
31+
# do NOT modify these features
32+
defmt-default = []
33+
defmt-trace = []
34+
defmt-debug = []
35+
defmt-info = []
36+
defmt-warn = []
37+
defmt-error = []

examples/ble-beacon/src/main.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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

Comments
 (0)