Bluetooth World Eddystone

Dependencies:   microbit1

Fork of BTW1_Eddystone_Solution by Wendy Warne

Committer:
wwbluetooth
Date:
Mon Jul 17 21:41:46 2017 +0000
Revision:
5:c175ef4d3b80
Parent:
2:511941b33870
updates

Who changed what in which revision?

UserRevisionLine numberNew 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
wwbluetooth 2:511941b33870 5 char URL[] = "https://goo.gl/7diLTx";
wwbluetooth 2:511941b33870 6
wwbluetooth 2:511941b33870 7 // lvl : Pwr@ 1m : Pwr@ 0m
wwbluetooth 2:511941b33870 8 // 0 : -90 : -49
wwbluetooth 2:511941b33870 9 // 1 : -78 : -37
wwbluetooth 2:511941b33870 10 // 2 : -74 : -33
wwbluetooth 2:511941b33870 11 // 3 : -69 : -28
wwbluetooth 2:511941b33870 12 // 4 : -66 : -25
wwbluetooth 2:511941b33870 13 // 5 : -61 : -20
wwbluetooth 2:511941b33870 14 // 6 : -56 : -15
wwbluetooth 2:511941b33870 15 // 7 : -51 : -10
wwbluetooth 2:511941b33870 16
bluetooth_mdw 0:580a61fd96a1 17 const int8_t CALIBRATED_POWERS[] = {-49, -37, -33, -28, -25, -20, -15, -10};
bluetooth_mdw 0:580a61fd96a1 18
bluetooth_mdw 0:580a61fd96a1 19 uint8_t advertising = 0;
bluetooth_mdw 0:580a61fd96a1 20 uint8_t tx_power_level = 6;
bluetooth_mdw 0:580a61fd96a1 21
bluetooth_mdw 0:580a61fd96a1 22 void startAdvertising() {
bluetooth_mdw 0:580a61fd96a1 23 uBit.bleManager.advertiseEddystoneUrl(URL, CALIBRATED_POWERS[tx_power_level-1], false);
bluetooth_mdw 0:580a61fd96a1 24 uBit.bleManager.setTransmitPower(tx_power_level);
bluetooth_mdw 0:580a61fd96a1 25 uBit.display.scroll("ADV");
bluetooth_mdw 0:580a61fd96a1 26 advertising = 1;
bluetooth_mdw 0:580a61fd96a1 27 }
bluetooth_mdw 0:580a61fd96a1 28
bluetooth_mdw 0:580a61fd96a1 29 void stopAdvertising() {
bluetooth_mdw 0:580a61fd96a1 30 uBit.bleManager.stopAdvertising();
bluetooth_mdw 0:580a61fd96a1 31 uBit.display.scroll("OFF");
bluetooth_mdw 0:580a61fd96a1 32 advertising = 0;
bluetooth_mdw 0:580a61fd96a1 33 }
bluetooth_mdw 0:580a61fd96a1 34
bluetooth_mdw 0:580a61fd96a1 35 void onButtonA(MicroBitEvent)
bluetooth_mdw 0:580a61fd96a1 36 {
bluetooth_mdw 0:580a61fd96a1 37 if (advertising == 1) {
bluetooth_mdw 0:580a61fd96a1 38 return;
bluetooth_mdw 0:580a61fd96a1 39 }
bluetooth_mdw 0:580a61fd96a1 40 startAdvertising();
bluetooth_mdw 0:580a61fd96a1 41 }
bluetooth_mdw 0:580a61fd96a1 42
bluetooth_mdw 0:580a61fd96a1 43 void onButtonB(MicroBitEvent)
bluetooth_mdw 0:580a61fd96a1 44 {
bluetooth_mdw 0:580a61fd96a1 45 if (advertising == 0) {
bluetooth_mdw 0:580a61fd96a1 46 return;
bluetooth_mdw 0:580a61fd96a1 47 }
bluetooth_mdw 0:580a61fd96a1 48 stopAdvertising();
bluetooth_mdw 0:580a61fd96a1 49 }
bluetooth_mdw 0:580a61fd96a1 50
bluetooth_mdw 0:580a61fd96a1 51 int main()
bluetooth_mdw 0:580a61fd96a1 52 {
bluetooth_mdw 0:580a61fd96a1 53 // Initialise the micro:bit runtime.
bluetooth_mdw 0:580a61fd96a1 54 uBit.init();
bluetooth_mdw 0:580a61fd96a1 55
bluetooth_mdw 0:580a61fd96a1 56 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
bluetooth_mdw 0:580a61fd96a1 57 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
bluetooth_mdw 0:580a61fd96a1 58
bluetooth_mdw 0:580a61fd96a1 59 startAdvertising();
bluetooth_mdw 0:580a61fd96a1 60
bluetooth_mdw 0:580a61fd96a1 61 release_fiber();
bluetooth_mdw 0:580a61fd96a1 62 }