Skip to content

Commit e1aa371

Browse files
committed
Merge branch 'release/v10.1.0'
2 parents 28ddcab + 8fcf1f3 commit e1aa371

File tree

14 files changed

+222
-167
lines changed

14 files changed

+222
-167
lines changed

.github/workflows/examples.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
- "examples/arduino-bluefruit-bleuart"
1515
- "examples/arduino-nina-b1-generic-example"
1616
- "examples/arduino-serial-plotter"
17-
- "examples/mbed-rtos-ble-thermometer"
17+
- "examples/mbed-rtos-ble-battery"
1818
- "examples/mbed-rtos-blink-baremetal"
1919
- "examples/mbed-rtos-nfc"
2020
- "examples/zephyr-ble-beacon"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
mbed-os/connectivity/cellular/*
2+
mbed-os/connectivity/libraries/*
3+
mbed-os/connectivity/drivers/*
4+
mbed-os/connectivity/lorawan/*
5+
mbed-os/connectivity/lwipstack/*
6+
mbed-os/connectivity/mbedtls/*
7+
mbed-os/connectivity/nanostack/*
8+
mbed-os/connectivity/netsocket/*
9+
mbed-os/features/*
10+
mbed-os/storage/*

examples/mbed-rtos-ble-thermometer/README.md renamed to examples/mbed-rtos-ble-battery/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ How to build PlatformIO based project
88

99
```shell
1010
# Change directory to example
11-
$ cd platform-nordicnrf52/examples/mbed-legacy-examples/mbed-rtos-ble-thermometer
11+
$ cd platform-nordicnrf52/examples/mbed-legacy-examples/mbed-rtos-ble-battery
1212

1313
# Build project
1414
$ pio run

examples/mbed-rtos-ble-thermometer/mbed_app.json renamed to examples/mbed-rtos-ble-battery/mbed_app.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
{
22
"target_overrides": {
3+
"*": {
4+
"platform.stdio-baud-rate": 115200,
5+
"mbed-trace.enable": false,
6+
"mbed-trace.max-level": "TRACE_LEVEL_DEBUG",
7+
"cordio.trace-hci-packets": false,
8+
"cordio.trace-cordio-wsf-traces": false,
9+
"ble.trace-human-readable-enums": false
10+
},
311
"K64F": {
412
"target.components_add": ["BlueNRG_MS"],
513
"target.features_add": ["BLE"],
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2019 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <events/mbed_events.h>
18+
#include "ble/BLE.h"
19+
#include "ble/Gap.h"
20+
#include "pretty_printer.h"
21+
#include "mbed-trace/mbed_trace.h"
22+
23+
const static char DEVICE_NAME[] = "BATTERY";
24+
25+
using namespace std::literals::chrono_literals;
26+
27+
static events::EventQueue event_queue(/* event count */ 16 * EVENTS_EVENT_SIZE);
28+
29+
class BatteryDemo : ble::Gap::EventHandler {
30+
public:
31+
BatteryDemo(BLE &ble, events::EventQueue &event_queue) :
32+
_ble(ble),
33+
_event_queue(event_queue),
34+
_battery_level(50),
35+
_adv_data_builder(_adv_buffer)
36+
{
37+
}
38+
39+
void start()
40+
{
41+
/* mbed will call on_init_complete when when ble is ready */
42+
_ble.init(this, &BatteryDemo::on_init_complete);
43+
44+
/* this will never return */
45+
_event_queue.dispatch_forever();
46+
}
47+
48+
private:
49+
/** Callback triggered when the ble initialization process has finished */
50+
void on_init_complete(BLE::InitializationCompleteCallbackContext *params)
51+
{
52+
if (params->error != BLE_ERROR_NONE) {
53+
print_error(params->error, "Ble initialization failed.");
54+
return;
55+
}
56+
57+
print_mac_address();
58+
59+
start_advertising();
60+
}
61+
62+
void start_advertising()
63+
{
64+
/* create advertising parameters and payload */
65+
66+
ble::AdvertisingParameters adv_parameters(
67+
/* you cannot connect to this device, you can only read its advertising data,
68+
* scannable means that the device has extra advertising data that the peer can receive if it
69+
* "scans" it which means it is using active scanning (it sends a scan request) */
70+
ble::advertising_type_t::SCANNABLE_UNDIRECTED,
71+
ble::adv_interval_t(ble::millisecond_t(1000))
72+
);
73+
74+
/* when advertising you can optionally add extra data that is only sent
75+
* if the central requests it by doing active scanning (sending scan requests),
76+
* in this example we set this payload first because we want to later reuse
77+
* the same _adv_data_builder builder for payload updates */
78+
79+
const uint8_t _vendor_specific_data[4] = { 0xAD, 0xDE, 0xBE, 0xEF };
80+
_adv_data_builder.setManufacturerSpecificData(_vendor_specific_data);
81+
82+
_ble.gap().setAdvertisingScanResponse(
83+
ble::LEGACY_ADVERTISING_HANDLE,
84+
_adv_data_builder.getAdvertisingData()
85+
);
86+
87+
/* now we set the advertising payload that gets sent during advertising without any scan requests */
88+
89+
_adv_data_builder.clear();
90+
_adv_data_builder.setFlags();
91+
_adv_data_builder.setName(DEVICE_NAME);
92+
93+
/* we add the battery level as part of the payload so it's visible to any device that scans,
94+
* this part of the payload will be updated periodically without affecting the rest of the payload */
95+
_adv_data_builder.setServiceData(GattService::UUID_BATTERY_SERVICE, {&_battery_level, 1});
96+
97+
/* setup advertising */
98+
99+
ble_error_t error = _ble.gap().setAdvertisingParameters(
100+
ble::LEGACY_ADVERTISING_HANDLE,
101+
adv_parameters
102+
);
103+
104+
if (error) {
105+
print_error(error, "_ble.gap().setAdvertisingParameters() failed");
106+
return;
107+
}
108+
109+
error = _ble.gap().setAdvertisingPayload(
110+
ble::LEGACY_ADVERTISING_HANDLE,
111+
_adv_data_builder.getAdvertisingData()
112+
);
113+
114+
if (error) {
115+
print_error(error, "_ble.gap().setAdvertisingPayload() failed");
116+
return;
117+
}
118+
119+
/* start advertising */
120+
121+
error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
122+
123+
if (error) {
124+
print_error(error, "_ble.gap().startAdvertising() failed");
125+
return;
126+
}
127+
128+
/* we simulate battery discharging by updating it every second */
129+
_event_queue.call_every(
130+
1000ms,
131+
[this]() {
132+
update_battery_level();
133+
}
134+
);
135+
}
136+
137+
void update_battery_level()
138+
{
139+
if (_battery_level-- == 10) {
140+
_battery_level = 100;
141+
}
142+
143+
/* update the payload with the new value of the bettery level, the rest of the payload remains the same */
144+
ble_error_t error = _adv_data_builder.setServiceData(GattService::UUID_BATTERY_SERVICE, make_Span(&_battery_level, 1));
145+
146+
if (error) {
147+
print_error(error, "_adv_data_builder.setServiceData() failed");
148+
return;
149+
}
150+
151+
/* set the new payload, we don't need to stop advertising */
152+
error = _ble.gap().setAdvertisingPayload(
153+
ble::LEGACY_ADVERTISING_HANDLE,
154+
_adv_data_builder.getAdvertisingData()
155+
);
156+
157+
if (error) {
158+
print_error(error, "_ble.gap().setAdvertisingPayload() failed");
159+
return;
160+
}
161+
}
162+
163+
private:
164+
BLE &_ble;
165+
events::EventQueue &_event_queue;
166+
167+
uint8_t _battery_level;
168+
169+
uint8_t _adv_buffer[ble::LEGACY_ADVERTISING_MAX_SIZE];
170+
ble::AdvertisingDataBuilder _adv_data_builder;
171+
};
172+
173+
/* Schedule processing of events from the BLE middleware in the event queue. */
174+
void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context)
175+
{
176+
event_queue.call(Callback<void()>(&context->ble, &BLE::processEvents));
177+
}
178+
179+
int main()
180+
{
181+
mbed_trace_init();
182+
183+
BLE &ble = BLE::Instance();
184+
ble.onEventsToProcess(schedule_ble_events);
185+
186+
BatteryDemo demo(ble, event_queue);
187+
demo.start();
188+
189+
return 0;
190+
}

examples/mbed-rtos-ble-thermometer/src/main.cpp

Lines changed: 0 additions & 163 deletions
This file was deleted.

0 commit comments

Comments
 (0)