Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add README to bluetooth-services; Move display scroll so services are created first #27

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions source/examples/bluetooth-services/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Bluetooth Services

The Bluetooth services project aims to provide a sample program that creates all of the services in micro:bit profile.

However, with the current Bluetooth stack the micro:bit will throw an `020` [out of memory error](https://microbit.org/guide/hardware/error-codes/) if all services are created simultaneously.

To avoid this both the DFU and Event Service are disabled in the project's `config.json`. If you wish to test these out, the easiest way to free enough memory is to disable the Magnetometer Service.

---

Enable DFU and Event Service in `config.json`
```
{
"microbit-dal": {
"bluetooth": {
"enabled": 1,
"pairing_mode": 1,
"private_addressing": 0,
"open": 0,
"whitelist": 1,
"advertising_timeout": 0,
"tx_power": 0,
"dfu_service": 1,
"event_service": 1,
"device_info_service": 1,
"security_level": "SECURITY_MODE_ENCRYPTION_NO_MITM"
},
"gatt_table_size": "0x700"
}
}
```

Disable the Magnetometer Service by commenting it out:
```
new MicroBitAccelerometerService(*uBit.ble, uBit.accelerometer);
new MicroBitButtonService(*uBit.ble);
new MicroBitIOPinService(*uBit.ble, uBit.io);
new MicroBitLEDService(*uBit.ble, uBit.display);
// new MicroBitMagnetometerService(*uBit.ble, uBit.compass);
new MicroBitTemperatureService(*uBit.ble, uBit.thermometer);
```
39 changes: 26 additions & 13 deletions source/examples/bluetooth-services/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,27 @@ DEALINGS IN THE SOFTWARE.

MicroBit uBit;

// we use events abd the 'connected' variable to keep track of the status of the Bluetooth connection
void onConnected(MicroBitEvent)
{
uBit.display.print("C");
}
#define NONE 0
#define CONNECTED 'C'
#define DISCONNECTED 'D'

uint8_t lastEvent = NONE;

void onDisconnected(MicroBitEvent)
// An event handler for BLE connection events
// We keep track of the last event to show the correct character after the initial info has been displayed
void onBLEEvent(MicroBitEvent evt)
{
uBit.display.print("D");
// Determine if the event originates from a connect or disconnect
switch(evt.value) {
case MICROBIT_BLE_EVT_CONNECTED:
lastEvent = CONNECTED;
break;
case MICROBIT_BLE_EVT_DISCONNECTED:
lastEvent = DISCONNECTED;
break;
}

uBit.display.printChar(lastEvent);
}

int main()
Expand Down Expand Up @@ -84,12 +96,7 @@ int main()
// TX Power Level
// 0-7 taken from tx_power in config.json


// Services/Pairing Config/Power Level
uBit.display.scroll("BLE ABDILMT/P/0");

uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_CONNECTED, onConnected);
uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_DISCONNECTED, onDisconnected);
uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_EVT_ANY, onBLEEvent);

new MicroBitAccelerometerService(*uBit.ble, uBit.accelerometer);
new MicroBitButtonService(*uBit.ble);
Expand All @@ -98,6 +105,12 @@ int main()
new MicroBitMagnetometerService(*uBit.ble, uBit.compass);
new MicroBitTemperatureService(*uBit.ble, uBit.thermometer);

// Services/Pairing Config/Power Level
uBit.display.scroll("BLE ABDILMT/P/0");

if(lastEvent)
uBit.display.printChar(lastEvent);

// If main exits, there may still be other fibers running or registered event handlers etc.
// Simply release this fiber, which will mean we enter the scheduler. Worse case, we then
// sit in the idle task forever, in a power efficient sleep.
Expand Down