update
Fork of BluetoothWorldBeaconsClass by
main.cpp@7:a6a3137843c6, 2017-09-26 (annotated)
- Committer:
- PEZAT
- Date:
- Tue Sep 26 07:34:31 2017 +0000
- Revision:
- 7:a6a3137843c6
- Parent:
- 2:e0b345a505f7
Update Code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bluetooth_mdw | 0:580a61fd96a1 | 1 | #include "MicroBit.h" |
bluetooth_mdw | 0:580a61fd96a1 | 2 | |
bluetooth_mdw | 0:580a61fd96a1 | 3 | MicroBit uBit; |
bluetooth_mdw | 0:580a61fd96a1 | 4 | |
PEZAT | 7:a6a3137843c6 | 5 | //************************************************************************ |
PEZAT | 7:a6a3137843c6 | 6 | char URL[] = "https://Bluetooth.com"; |
bluetooth_mdw | 0:580a61fd96a1 | 7 | |
PEZAT | 7:a6a3137843c6 | 8 | // lvl : Pwr@ 1m : Pwr@ 0m |
PEZAT | 7:a6a3137843c6 | 9 | // 0 : -90 : -49 |
PEZAT | 7:a6a3137843c6 | 10 | // 1 : -78 : -37 |
PEZAT | 7:a6a3137843c6 | 11 | // 2 : -74 : -33 |
PEZAT | 7:a6a3137843c6 | 12 | // 3 : -69 : -28 |
PEZAT | 7:a6a3137843c6 | 13 | // 4 : -66 : -25 |
PEZAT | 7:a6a3137843c6 | 14 | // 5 : -61 : -20 |
PEZAT | 7:a6a3137843c6 | 15 | // 6 : -56 : -15 |
PEZAT | 7:a6a3137843c6 | 16 | // 7 : -51 : -10 |
PEZAT | 7:a6a3137843c6 | 17 | |
PEZAT | 7:a6a3137843c6 | 18 | const int8_t CALIBRATED_POWERS[] = {-49, -37, -33, -28, -25, -20, -15, -10}; |
PEZAT | 7:a6a3137843c6 | 19 | |
PEZAT | 7:a6a3137843c6 | 20 | uint8_t advertising = 0; |
PEZAT | 7:a6a3137843c6 | 21 | uint8_t tx_power_level = 6; |
PEZAT | 7:a6a3137843c6 | 22 | |
PEZAT | 7:a6a3137843c6 | 23 | void startAdvertising() { |
PEZAT | 7:a6a3137843c6 | 24 | uBit.bleManager.advertiseEddystoneUrl(URL, CALIBRATED_POWERS[tx_power_level-1], false); |
PEZAT | 7:a6a3137843c6 | 25 | uBit.bleManager.setTransmitPower(tx_power_level); |
PEZAT | 7:a6a3137843c6 | 26 | uBit.display.scroll("ADV"); |
PEZAT | 7:a6a3137843c6 | 27 | advertising = 1; |
PEZAT | 7:a6a3137843c6 | 28 | } |
PEZAT | 7:a6a3137843c6 | 29 | |
PEZAT | 7:a6a3137843c6 | 30 | void stopAdvertising() { |
PEZAT | 7:a6a3137843c6 | 31 | uBit.bleManager.stopAdvertising(); |
PEZAT | 7:a6a3137843c6 | 32 | uBit.display.scroll("OFF"); |
PEZAT | 7:a6a3137843c6 | 33 | advertising = 0; |
PEZAT | 7:a6a3137843c6 | 34 | } |
PEZAT | 7:a6a3137843c6 | 35 | |
PEZAT | 7:a6a3137843c6 | 36 | void onButtonA(MicroBitEvent) |
PEZAT | 7:a6a3137843c6 | 37 | { |
PEZAT | 7:a6a3137843c6 | 38 | if (advertising == 1) { |
PEZAT | 7:a6a3137843c6 | 39 | return; |
PEZAT | 7:a6a3137843c6 | 40 | } |
PEZAT | 7:a6a3137843c6 | 41 | startAdvertising(); |
PEZAT | 7:a6a3137843c6 | 42 | } |
PEZAT | 7:a6a3137843c6 | 43 | |
PEZAT | 7:a6a3137843c6 | 44 | void onButtonB(MicroBitEvent) |
PEZAT | 7:a6a3137843c6 | 45 | { |
PEZAT | 7:a6a3137843c6 | 46 | if (advertising == 0) { |
PEZAT | 7:a6a3137843c6 | 47 | return; |
PEZAT | 7:a6a3137843c6 | 48 | } |
PEZAT | 7:a6a3137843c6 | 49 | stopAdvertising(); |
PEZAT | 7:a6a3137843c6 | 50 | } |
PEZAT | 7:a6a3137843c6 | 51 | //******************************************************************************** |
bluetooth_mdw | 0:580a61fd96a1 | 52 | |
bluetooth_mdw | 0:580a61fd96a1 | 53 | |
bluetooth_mdw | 0:580a61fd96a1 | 54 | |
bluetooth_mdw | 0:580a61fd96a1 | 55 | int main() |
bluetooth_mdw | 0:580a61fd96a1 | 56 | { |
PEZAT | 7:a6a3137843c6 | 57 | //***************************************************************************************** |
PEZAT | 7:a6a3137843c6 | 58 | // Initialize the micro:bit runtime. |
PEZAT | 7:a6a3137843c6 | 59 | uBit.init(); |
PEZAT | 7:a6a3137843c6 | 60 | |
PEZAT | 7:a6a3137843c6 | 61 | uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA); |
PEZAT | 7:a6a3137843c6 | 62 | uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB); |
PEZAT | 7:a6a3137843c6 | 63 | |
PEZAT | 7:a6a3137843c6 | 64 | uBit.bleManager.advertise(); //First Advertisment, adv name only |
PEZAT | 7:a6a3137843c6 | 65 | |
PEZAT | 7:a6a3137843c6 | 66 | release_fiber(); |
PEZAT | 7:a6a3137843c6 | 67 | //****************************************************************************************** |
wwbluetooth | 2:e0b345a505f7 | 68 | |
bluetooth_mdw | 0:580a61fd96a1 | 69 | } |