HTM Demo of BLE with Microbit
main.cpp@0:d44120452d31, 2019-08-06 (annotated)
- Committer:
- dw02136
- Date:
- Tue Aug 06 15:16:48 2019 +0000
- Revision:
- 0:d44120452d31
- Child:
- 1:7623f55dd990
First Issue
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dw02136 | 0:d44120452d31 | 1 | /* |
dw02136 | 0:d44120452d31 | 2 | The MIT License (MIT) |
dw02136 | 0:d44120452d31 | 3 | |
dw02136 | 0:d44120452d31 | 4 | Copyright (c) 2016 British Broadcasting Corporation. |
dw02136 | 0:d44120452d31 | 5 | This software is provided by Lancaster University by arrangement with the BBC. |
dw02136 | 0:d44120452d31 | 6 | |
dw02136 | 0:d44120452d31 | 7 | Permission is hereby granted, free of charge, to any person obtaining a |
dw02136 | 0:d44120452d31 | 8 | copy of this software and associated documentation files (the "Software"), |
dw02136 | 0:d44120452d31 | 9 | to deal in the Software without restriction, including without limitation |
dw02136 | 0:d44120452d31 | 10 | the rights to use, copy, modify, merge, publish, distribute, sublicense, |
dw02136 | 0:d44120452d31 | 11 | and/or sell copies of the Software, and to permit persons to whom the |
dw02136 | 0:d44120452d31 | 12 | Software is furnished to do so, subject to the following conditions: |
dw02136 | 0:d44120452d31 | 13 | |
dw02136 | 0:d44120452d31 | 14 | The above copyright notice and this permission notice shall be included in |
dw02136 | 0:d44120452d31 | 15 | all copies or substantial portions of the Software. |
dw02136 | 0:d44120452d31 | 16 | |
dw02136 | 0:d44120452d31 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
dw02136 | 0:d44120452d31 | 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
dw02136 | 0:d44120452d31 | 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
dw02136 | 0:d44120452d31 | 20 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
dw02136 | 0:d44120452d31 | 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
dw02136 | 0:d44120452d31 | 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
dw02136 | 0:d44120452d31 | 23 | DEALINGS IN THE SOFTWARE. |
dw02136 | 0:d44120452d31 | 24 | */ |
dw02136 | 0:d44120452d31 | 25 | |
dw02136 | 0:d44120452d31 | 26 | #include "MicroBit.h" |
dw02136 | 0:d44120452d31 | 27 | |
dw02136 | 0:d44120452d31 | 28 | const static char DEVICE_NAME[] = "MY_MICROBIT"; |
dw02136 | 0:d44120452d31 | 29 | static volatile bool triggerSensorPolling = false; |
dw02136 | 0:d44120452d31 | 30 | |
dw02136 | 0:d44120452d31 | 31 | MicroBit uBit; |
dw02136 | 0:d44120452d31 | 32 | Ticker ticker; |
dw02136 | 0:d44120452d31 | 33 | Serial pc(USBTX, USBRX); |
dw02136 | 0:d44120452d31 | 34 | |
dw02136 | 0:d44120452d31 | 35 | static Gap::ConnectionParams_t connectionParams; |
dw02136 | 0:d44120452d31 | 36 | |
dw02136 | 0:d44120452d31 | 37 | /* Battery Level Service */ |
dw02136 | 0:d44120452d31 | 38 | uint8_t batt = 100; /* Battery level */ |
dw02136 | 0:d44120452d31 | 39 | GattCharacteristic battLevel ( GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, |
dw02136 | 0:d44120452d31 | 40 | (uint8_t *)batt, 1, 1, |
dw02136 | 0:d44120452d31 | 41 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
dw02136 | 0:d44120452d31 | 42 | GattCharacteristic *battChars[] = {&battLevel, }; |
dw02136 | 0:d44120452d31 | 43 | GattService battService(GattService::UUID_BATTERY_SERVICE, battChars, |
dw02136 | 0:d44120452d31 | 44 | sizeof(battChars) / sizeof(GattCharacteristic *)); |
dw02136 | 0:d44120452d31 | 45 | uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE}; |
dw02136 | 0:d44120452d31 | 46 | |
dw02136 | 0:d44120452d31 | 47 | void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params) |
dw02136 | 0:d44120452d31 | 48 | { |
dw02136 | 0:d44120452d31 | 49 | pc.printf("connected. Got handle %u\r\n", params->handle); |
dw02136 | 0:d44120452d31 | 50 | |
dw02136 | 0:d44120452d31 | 51 | connectionParams.slaveLatency = 1; |
dw02136 | 0:d44120452d31 | 52 | if (uBit.ble->gap().updateConnectionParams(params->handle, &connectionParams) != BLE_ERROR_NONE) { |
dw02136 | 0:d44120452d31 | 53 | pc.printf("failed to update connection paramter\r\n"); |
dw02136 | 0:d44120452d31 | 54 | } |
dw02136 | 0:d44120452d31 | 55 | } |
dw02136 | 0:d44120452d31 | 56 | |
dw02136 | 0:d44120452d31 | 57 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
dw02136 | 0:d44120452d31 | 58 | { |
dw02136 | 0:d44120452d31 | 59 | pc.printf("Disconnected handle %u, reason %u\r\n", params->handle, params->reason); |
dw02136 | 0:d44120452d31 | 60 | pc.printf("Restarting the advertising process\r\n"); |
dw02136 | 0:d44120452d31 | 61 | |
dw02136 | 0:d44120452d31 | 62 | uBit.ble->gap().startAdvertising(); |
dw02136 | 0:d44120452d31 | 63 | } |
dw02136 | 0:d44120452d31 | 64 | |
dw02136 | 0:d44120452d31 | 65 | void periodicCallback(void) |
dw02136 | 0:d44120452d31 | 66 | { |
dw02136 | 0:d44120452d31 | 67 | triggerSensorPolling = true; |
dw02136 | 0:d44120452d31 | 68 | } |
dw02136 | 0:d44120452d31 | 69 | |
dw02136 | 0:d44120452d31 | 70 | int main() |
dw02136 | 0:d44120452d31 | 71 | { |
dw02136 | 0:d44120452d31 | 72 | // Initialise the micro:bit runtime. |
dw02136 | 0:d44120452d31 | 73 | pc.printf("Initialising MicroBit\r\n"); |
dw02136 | 0:d44120452d31 | 74 | uBit.ble = new BLEDevice(); |
dw02136 | 0:d44120452d31 | 75 | uBit.init(); |
dw02136 | 0:d44120452d31 | 76 | uBit.ble->init(); |
dw02136 | 0:d44120452d31 | 77 | pc.printf("Init done\r\n"); |
dw02136 | 0:d44120452d31 | 78 | |
dw02136 | 0:d44120452d31 | 79 | uBit.ble->gap().onConnection(onConnectionCallback); |
dw02136 | 0:d44120452d31 | 80 | uBit.ble->gap().onDisconnection(disconnectionCallback); |
dw02136 | 0:d44120452d31 | 81 | |
dw02136 | 0:d44120452d31 | 82 | /* setup advertising */ |
dw02136 | 0:d44120452d31 | 83 | uBit.ble->gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
dw02136 | 0:d44120452d31 | 84 | uBit.ble->gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)uuid16_list, sizeof(uuid16_list)); |
dw02136 | 0:d44120452d31 | 85 | uBit.ble->gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_REMOTE_CONTROL); |
dw02136 | 0:d44120452d31 | 86 | uBit.ble->accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
dw02136 | 0:d44120452d31 | 87 | uBit.ble->gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
dw02136 | 0:d44120452d31 | 88 | uBit.ble->setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
dw02136 | 0:d44120452d31 | 89 | uBit.ble->startAdvertising(); |
dw02136 | 0:d44120452d31 | 90 | pc.printf("Start Advertising\r\n"); |
dw02136 | 0:d44120452d31 | 91 | |
dw02136 | 0:d44120452d31 | 92 | uBit.ble->gattServer().addService(battService); |
dw02136 | 0:d44120452d31 | 93 | |
dw02136 | 0:d44120452d31 | 94 | ticker.attach(periodicCallback, 1); |
dw02136 | 0:d44120452d31 | 95 | |
dw02136 | 0:d44120452d31 | 96 | // Insert your code here! |
dw02136 | 0:d44120452d31 | 97 | while(1) |
dw02136 | 0:d44120452d31 | 98 | { |
dw02136 | 0:d44120452d31 | 99 | //uBit.display.scroll("ADV"); |
dw02136 | 0:d44120452d31 | 100 | if (triggerSensorPolling) { |
dw02136 | 0:d44120452d31 | 101 | triggerSensorPolling = false; |
dw02136 | 0:d44120452d31 | 102 | |
dw02136 | 0:d44120452d31 | 103 | // Decrement the battery level. |
dw02136 | 0:d44120452d31 | 104 | batt <=50 ? batt=100 : batt--; |
dw02136 | 0:d44120452d31 | 105 | pc.printf("Batt is %u\r\n", batt); |
dw02136 | 0:d44120452d31 | 106 | |
dw02136 | 0:d44120452d31 | 107 | uBit.ble->gattServer().write(battLevel.getValueAttribute().getHandle(), (uint8_t *)&batt, sizeof(batt)); //Mod |
dw02136 | 0:d44120452d31 | 108 | } else { |
dw02136 | 0:d44120452d31 | 109 | uBit.ble->waitForEvent(); |
dw02136 | 0:d44120452d31 | 110 | } |
dw02136 | 0:d44120452d31 | 111 | } |
dw02136 | 0:d44120452d31 | 112 | |
dw02136 | 0:d44120452d31 | 113 | // If main exits, there may still be other fibers running or registered event handlers etc. |
dw02136 | 0:d44120452d31 | 114 | // Simply release this fiber, which will mean we enter the scheduler. Worse case, we then |
dw02136 | 0:d44120452d31 | 115 | // sit in the idle task forever, in a power efficient sleep. |
dw02136 | 0:d44120452d31 | 116 | //release_fiber(); |
dw02136 | 0:d44120452d31 | 117 | } |
dw02136 | 0:d44120452d31 | 118 |