Skip to content

Commit 272e40c

Browse files
bors[bot]Henrik Alsérkalkyl
authored
Merge #57
57: Add BLE Beacon example r=therealprof a=kalkyl Co-authored-by: Henrik Alsér <[email protected]> Co-authored-by: Henrik Alsér <[email protected]>
2 parents 084c049 + 5345df8 commit 272e40c

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
- Rearrange LED display modules under the same root module and change their
1111
APIs to be more aligned with each other.
12+
- Add BLE Beacon demo.
1213

1314
## [0.10.1] - 2021-05-25
1415

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

0 commit comments

Comments
 (0)