This repository is for Bluetooth Asia 2018 developer training, beacon session.
Fork of BluetoothAsiaBeaconClass by
beacon_src.txt@21:6512562f058d, 2018-03-14 (annotated)
- Committer:
- krenbluetoothsig
- Date:
- Wed Mar 14 09:19:49 2018 +0000
- Revision:
- 21:6512562f058d
- Child:
- 22:4f919611ff42
1. NEW: add beacon_src.txt which includes source code.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
krenbluetoothsig | 21:6512562f058d | 1 | /* |
krenbluetoothsig | 21:6512562f058d | 2 | |
krenbluetoothsig | 21:6512562f058d | 3 | #include "MicroBit.h" |
krenbluetoothsig | 21:6512562f058d | 4 | |
krenbluetoothsig | 21:6512562f058d | 5 | MicroBit uBit; |
krenbluetoothsig | 21:6512562f058d | 6 | |
krenbluetoothsig | 21:6512562f058d | 7 | char URL[] = "http://www.bluetooth.com"; |
krenbluetoothsig | 21:6512562f058d | 8 | |
krenbluetoothsig | 21:6512562f058d | 9 | // lvl : Pwr@ 1m : Pwr@ 0m |
krenbluetoothsig | 21:6512562f058d | 10 | // 0 : -90 : -49 |
krenbluetoothsig | 21:6512562f058d | 11 | // 1 : -78 : -37 |
krenbluetoothsig | 21:6512562f058d | 12 | // 2 : -74 : -33 |
krenbluetoothsig | 21:6512562f058d | 13 | // 3 : -69 : -28 |
krenbluetoothsig | 21:6512562f058d | 14 | // 4 : -66 : -25 |
krenbluetoothsig | 21:6512562f058d | 15 | // 5 : -61 : -20 |
krenbluetoothsig | 21:6512562f058d | 16 | // 6 : -56 : -15 |
krenbluetoothsig | 21:6512562f058d | 17 | // 7 : -51 : -10 |
krenbluetoothsig | 21:6512562f058d | 18 | |
krenbluetoothsig | 21:6512562f058d | 19 | const int8_t CALIBRATED_POWERS[] = {-49, -37, -33, -28, -25, -20, -15, -10}; |
krenbluetoothsig | 21:6512562f058d | 20 | |
krenbluetoothsig | 21:6512562f058d | 21 | uint8_t advertising = 0; |
krenbluetoothsig | 21:6512562f058d | 22 | uint8_t tx_power_level = 6; |
krenbluetoothsig | 21:6512562f058d | 23 | |
krenbluetoothsig | 21:6512562f058d | 24 | void startAdvertising() { |
krenbluetoothsig | 21:6512562f058d | 25 | uBit.bleManager.advertiseEddystoneUrl(URL, CALIBRATED_POWERS[tx_power_level-1], false); |
krenbluetoothsig | 21:6512562f058d | 26 | uBit.bleManager.setTransmitPower(tx_power_level); |
krenbluetoothsig | 21:6512562f058d | 27 | uBit.display.scroll("ADV"); |
krenbluetoothsig | 21:6512562f058d | 28 | advertising = 1; |
krenbluetoothsig | 21:6512562f058d | 29 | } |
krenbluetoothsig | 21:6512562f058d | 30 | |
krenbluetoothsig | 21:6512562f058d | 31 | void stopAdvertising() { |
krenbluetoothsig | 21:6512562f058d | 32 | uBit.bleManager.stopAdvertising(); |
krenbluetoothsig | 21:6512562f058d | 33 | uBit.display.scroll("OFF"); |
krenbluetoothsig | 21:6512562f058d | 34 | advertising = 0; |
krenbluetoothsig | 21:6512562f058d | 35 | } |
krenbluetoothsig | 21:6512562f058d | 36 | |
krenbluetoothsig | 21:6512562f058d | 37 | void onButtonA(MicroBitEvent) |
krenbluetoothsig | 21:6512562f058d | 38 | { |
krenbluetoothsig | 21:6512562f058d | 39 | if (advertising == 1) { |
krenbluetoothsig | 21:6512562f058d | 40 | return; |
krenbluetoothsig | 21:6512562f058d | 41 | } |
krenbluetoothsig | 21:6512562f058d | 42 | startAdvertising(); |
krenbluetoothsig | 21:6512562f058d | 43 | } |
krenbluetoothsig | 21:6512562f058d | 44 | |
krenbluetoothsig | 21:6512562f058d | 45 | void onButtonB(MicroBitEvent) |
krenbluetoothsig | 21:6512562f058d | 46 | { |
krenbluetoothsig | 21:6512562f058d | 47 | if (advertising == 0) { |
krenbluetoothsig | 21:6512562f058d | 48 | return; |
krenbluetoothsig | 21:6512562f058d | 49 | } |
krenbluetoothsig | 21:6512562f058d | 50 | stopAdvertising(); |
krenbluetoothsig | 21:6512562f058d | 51 | } |
krenbluetoothsig | 21:6512562f058d | 52 | |
krenbluetoothsig | 21:6512562f058d | 53 | int main() |
krenbluetoothsig | 21:6512562f058d | 54 | { |
krenbluetoothsig | 21:6512562f058d | 55 | // Initialise the micro:bit runtime. |
krenbluetoothsig | 21:6512562f058d | 56 | uBit.init(); |
krenbluetoothsig | 21:6512562f058d | 57 | |
krenbluetoothsig | 21:6512562f058d | 58 | uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA); |
krenbluetoothsig | 21:6512562f058d | 59 | uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB); |
krenbluetoothsig | 21:6512562f058d | 60 | |
krenbluetoothsig | 21:6512562f058d | 61 | startAdvertising(); |
krenbluetoothsig | 21:6512562f058d | 62 | |
krenbluetoothsig | 21:6512562f058d | 63 | release_fiber(); |
krenbluetoothsig | 21:6512562f058d | 64 | } |
krenbluetoothsig | 21:6512562f058d | 65 | |
krenbluetoothsig | 21:6512562f058d | 66 | |
krenbluetoothsig | 21:6512562f058d | 67 | |
krenbluetoothsig | 21:6512562f058d | 68 | */ |