This project contains two type of beacons: Eddystone and iBeacon.

Dependencies:   microbit-iBeacon

Fork of BTW_ComboBeacon_Solution by KEN OGAMI

Committer:
krenbluetoothsig
Date:
Wed Aug 01 02:48:13 2018 +0000
Revision:
17:6715daf92c78
Parent:
16:2a71c1b5fd71
Child:
18:eeea3e01140c
1. CHG: add some "TODO" which will be used on hands-on.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
krenbluetoothsig 17:6715daf92c78 1 #include "MicroBit.h"
krenbluetoothsig 17:6715daf92c78 2
krenbluetoothsig 17:6715daf92c78 3 /******************************************************
krenbluetoothsig 17:6715daf92c78 4 * Macros
krenbluetoothsig 17:6715daf92c78 5 ******************************************************/
krenbluetoothsig 17:6715daf92c78 6 // TODO: add ADV on and off flag
krenbluetoothsig 17:6715daf92c78 7 #define ADV_ON 1
krenbluetoothsig 17:6715daf92c78 8 #define ADV_OFF 0
krenbluetoothsig 17:6715daf92c78 9
krenbluetoothsig 17:6715daf92c78 10 /******************************************************
krenbluetoothsig 17:6715daf92c78 11 * Constants
krenbluetoothsig 17:6715daf92c78 12 ******************************************************/
krenbluetoothsig 17:6715daf92c78 13 // TODO: constants, used for iBeacon UUID(MSB first), Major, Minor and txpower setting
krenbluetoothsig 17:6715daf92c78 14 const uint8_t iBeaconProximityUUIDraw[] = {
krenbluetoothsig 17:6715daf92c78 15 0x01, 0xD9, 0xC8, 0x00, 0x45, 0xF3, 0x40, 0x24, 0x93, 0xB0, 0xB0, 0xCD, 0x4A, 0x50, 0x2A, 0xF7
krenbluetoothsig 17:6715daf92c78 16 }; // 01D9C800-45F3-4024-93B0-B0CD4A502AF7 use this UUID including dashes in mobile application
krenbluetoothsig 17:6715daf92c78 17 const UUID iBeaconProximityUUID(iBeaconProximityUUIDraw);
krenbluetoothsig 17:6715daf92c78 18 const int16_t iBeaconMajor = 1234;
krenbluetoothsig 17:6715daf92c78 19 const int16_t iBeaconMinor = 5678;
krenbluetoothsig 17:6715daf92c78 20
krenbluetoothsig 17:6715daf92c78 21 // lvl : Pwr@ 1m : Pwr@ 0m
krenbluetoothsig 17:6715daf92c78 22 // 0 : -90 : -49
krenbluetoothsig 17:6715daf92c78 23 // 1 : -78 : -37
krenbluetoothsig 17:6715daf92c78 24 // 2 : -74 : -33
krenbluetoothsig 17:6715daf92c78 25 // 3 : -69 : -28
krenbluetoothsig 17:6715daf92c78 26 // 4 : -66 : -25
krenbluetoothsig 17:6715daf92c78 27 // 5 : -61 : -20
krenbluetoothsig 17:6715daf92c78 28 // 6 : -56 : -15
krenbluetoothsig 17:6715daf92c78 29 // 7 : -51 : -10
krenbluetoothsig 17:6715daf92c78 30
krenbluetoothsig 17:6715daf92c78 31 const int8_t CALIBRATED_POWERS[] = {-49, -37, -33, -28, -25, -20, -15, -10};
krenbluetoothsig 17:6715daf92c78 32
krenbluetoothsig 17:6715daf92c78 33 /******************************************************
krenbluetoothsig 17:6715daf92c78 34 * Variables
krenbluetoothsig 17:6715daf92c78 35 ******************************************************/
krenbluetoothsig 17:6715daf92c78 36 //microbit object
krenbluetoothsig 17:6715daf92c78 37 MicroBit uBit;
krenbluetoothsig 17:6715daf92c78 38 //Initial USB-to-Serial, baud rate is 9600-8-N-1
krenbluetoothsig 17:6715daf92c78 39 Serial pcCom(USBTX, USBRX);
krenbluetoothsig 17:6715daf92c78 40
krenbluetoothsig 17:6715daf92c78 41 // TODO: global data and functions
krenbluetoothsig 17:6715daf92c78 42 char eddystoneURL[] = "https://www.bluetooth.com";
krenbluetoothsig 17:6715daf92c78 43 uint8_t advertising = 0;
krenbluetoothsig 17:6715daf92c78 44 uint8_t tx_power_level = 6;
krenbluetoothsig 17:6715daf92c78 45 BLE_BEACON_TYPE beacon_type = BEACON_TYPE_EDDYSTONE; // BEACON_TYPE_EDDYSTONE, BEACON_TYPE_IBEACON, BEACON_TYPE_NONE
krenbluetoothsig 17:6715daf92c78 46
krenbluetoothsig 17:6715daf92c78 47
krenbluetoothsig 17:6715daf92c78 48
krenbluetoothsig 17:6715daf92c78 49 /******************************************************
krenbluetoothsig 17:6715daf92c78 50 * Functions
krenbluetoothsig 17:6715daf92c78 51 ******************************************************/
krenbluetoothsig 17:6715daf92c78 52 // TODO: functions, startAdvertising and stopAdvertising
krenbluetoothsig 17:6715daf92c78 53 void startAdvertising() {
krenbluetoothsig 17:6715daf92c78 54 switch(beacon_type)
krenbluetoothsig 17:6715daf92c78 55 {
krenbluetoothsig 17:6715daf92c78 56 case BEACON_TYPE_EDDYSTONE:
krenbluetoothsig 17:6715daf92c78 57 uBit.bleManager.advertiseEddystoneUrl(eddystoneURL, CALIBRATED_POWERS[tx_power_level - 1], false);
krenbluetoothsig 17:6715daf92c78 58 uBit.display.print("E");
krenbluetoothsig 17:6715daf92c78 59 pcCom.printf("eddystone\r\n");
krenbluetoothsig 17:6715daf92c78 60 break;
krenbluetoothsig 17:6715daf92c78 61 //
krenbluetoothsig 17:6715daf92c78 62 case BEACON_TYPE_IBEACON:
krenbluetoothsig 17:6715daf92c78 63 uBit.bleManager.advertiseIBeacon(iBeaconProximityUUID, iBeaconMajor, iBeaconMinor, CALIBRATED_POWERS[0]);
krenbluetoothsig 17:6715daf92c78 64 uBit.display.print("i");
krenbluetoothsig 17:6715daf92c78 65 pcCom.printf("iBeacon\r\n");
krenbluetoothsig 17:6715daf92c78 66 break;
krenbluetoothsig 17:6715daf92c78 67 //
krenbluetoothsig 17:6715daf92c78 68 case BEACON_TYPE_NONE:
krenbluetoothsig 17:6715daf92c78 69 default:
krenbluetoothsig 17:6715daf92c78 70 pcCom.printf("unknow\r\n");
krenbluetoothsig 17:6715daf92c78 71 break;
krenbluetoothsig 17:6715daf92c78 72 }
krenbluetoothsig 17:6715daf92c78 73 uBit.bleManager.setTransmitPower(tx_power_level);
krenbluetoothsig 17:6715daf92c78 74 advertising = 1;
krenbluetoothsig 17:6715daf92c78 75 }
krenbluetoothsig 17:6715daf92c78 76
krenbluetoothsig 17:6715daf92c78 77 void stopAdvertising() {
krenbluetoothsig 17:6715daf92c78 78 pcCom.printf("ADV OFF\r\n");
krenbluetoothsig 17:6715daf92c78 79 uBit.bleManager.stopAdvertising();
krenbluetoothsig 17:6715daf92c78 80 advertising = 0;
krenbluetoothsig 17:6715daf92c78 81 }
krenbluetoothsig 17:6715daf92c78 82
krenbluetoothsig 17:6715daf92c78 83 // TODO: functions, used for ButtonA and ButtonB interrupt callbacks
krenbluetoothsig 17:6715daf92c78 84 void onButtonA(MicroBitEvent)
krenbluetoothsig 17:6715daf92c78 85 {
krenbluetoothsig 17:6715daf92c78 86 // toggle advertising
krenbluetoothsig 17:6715daf92c78 87 switch(advertising)
krenbluetoothsig 17:6715daf92c78 88 {
krenbluetoothsig 17:6715daf92c78 89 case 0:
krenbluetoothsig 17:6715daf92c78 90 pcCom.printf("ADV ON ");
krenbluetoothsig 17:6715daf92c78 91 uBit.display.scroll("ON");
krenbluetoothsig 17:6715daf92c78 92 startAdvertising();
krenbluetoothsig 17:6715daf92c78 93 break;
krenbluetoothsig 17:6715daf92c78 94 case 1:
krenbluetoothsig 17:6715daf92c78 95 pcCom.printf("ADV OFF");
krenbluetoothsig 17:6715daf92c78 96 stopAdvertising();
krenbluetoothsig 17:6715daf92c78 97 uBit.display.scroll("OFF");
krenbluetoothsig 17:6715daf92c78 98 break;
krenbluetoothsig 17:6715daf92c78 99 default:
krenbluetoothsig 17:6715daf92c78 100 break;
krenbluetoothsig 17:6715daf92c78 101 }
krenbluetoothsig 17:6715daf92c78 102 }
krenbluetoothsig 17:6715daf92c78 103
krenbluetoothsig 17:6715daf92c78 104 void onButtonB(MicroBitEvent)
krenbluetoothsig 17:6715daf92c78 105 {
krenbluetoothsig 17:6715daf92c78 106 // ButtonB is used to toggle beacon types, switch beacons types
krenbluetoothsig 17:6715daf92c78 107 // between iBeacon and Eddystone.
krenbluetoothsig 17:6715daf92c78 108 stopAdvertising();
krenbluetoothsig 17:6715daf92c78 109 switch(beacon_type)
krenbluetoothsig 17:6715daf92c78 110 {
krenbluetoothsig 17:6715daf92c78 111 case BEACON_TYPE_EDDYSTONE:
krenbluetoothsig 17:6715daf92c78 112 beacon_type = BEACON_TYPE_IBEACON;
krenbluetoothsig 17:6715daf92c78 113 break;
krenbluetoothsig 17:6715daf92c78 114 case BEACON_TYPE_IBEACON:
krenbluetoothsig 17:6715daf92c78 115 beacon_type = BEACON_TYPE_EDDYSTONE;
krenbluetoothsig 17:6715daf92c78 116 break;
krenbluetoothsig 17:6715daf92c78 117 case BEACON_TYPE_NONE:
krenbluetoothsig 17:6715daf92c78 118 default:
krenbluetoothsig 17:6715daf92c78 119 beacon_type = BEACON_TYPE_EDDYSTONE;
krenbluetoothsig 17:6715daf92c78 120 break;
krenbluetoothsig 17:6715daf92c78 121 }
krenbluetoothsig 17:6715daf92c78 122 startAdvertising();
krenbluetoothsig 17:6715daf92c78 123 }
krenbluetoothsig 17:6715daf92c78 124
krenbluetoothsig 17:6715daf92c78 125
krenbluetoothsig 17:6715daf92c78 126
krenbluetoothsig 17:6715daf92c78 127 int main()
krenbluetoothsig 17:6715daf92c78 128 {
krenbluetoothsig 17:6715daf92c78 129 // Serial output after a reset
krenbluetoothsig 17:6715daf92c78 130 pcCom.printf("mBit Beacon.\n\r");
krenbluetoothsig 17:6715daf92c78 131
krenbluetoothsig 17:6715daf92c78 132 // Initialize the micro:bit runtime.
krenbluetoothsig 17:6715daf92c78 133 uBit.init();
krenbluetoothsig 17:6715daf92c78 134
krenbluetoothsig 17:6715daf92c78 135 // TODO: Register the interrupt and callback for ButtonA and ButtonB
krenbluetoothsig 17:6715daf92c78 136 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
krenbluetoothsig 17:6715daf92c78 137 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
krenbluetoothsig 17:6715daf92c78 138
krenbluetoothsig 17:6715daf92c78 139 //TODO: First Advertisment, adv name only
krenbluetoothsig 17:6715daf92c78 140 uBit.bleManager.advertise();
krenbluetoothsig 17:6715daf92c78 141
krenbluetoothsig 17:6715daf92c78 142 release_fiber();
krenbluetoothsig 17:6715daf92c78 143 }