Martin Woolley / eddystone_test

Dependencies:   microbit-eddystone

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "MicroBit.h"
00002 
00003 MicroBit uBit;
00004 
00005 char URL[] = "https://goo.gl/TlUTF7";
00006 const int8_t CALIBRATED_POWERS[] = {-49, -37, -33, -28, -25, -20, -15, -10};
00007 
00008 uint8_t advertising = 0;
00009 uint8_t tx_power_level = 6;
00010 
00011 void startAdvertising() {
00012     uBit.bleManager.advertiseEddystoneUrl(URL, CALIBRATED_POWERS[tx_power_level-1], false);
00013     uBit.bleManager.setTransmitPower(tx_power_level);
00014     uBit.display.scroll("ADV");
00015     advertising = 1;
00016 }
00017 
00018 void stopAdvertising() {
00019     uBit.bleManager.stopAdvertising();
00020     uBit.display.scroll("OFF");
00021     advertising = 0;
00022 }
00023 
00024 void onButtonA(MicroBitEvent)
00025 {
00026     if (advertising == 1) {
00027         return;
00028     }
00029     startAdvertising();
00030 }
00031 
00032 void onButtonB(MicroBitEvent)
00033 {
00034     if (advertising == 0) {
00035         return;
00036     }
00037     stopAdvertising();
00038 }
00039 
00040 int main()
00041 {
00042     // Initialise the micro:bit runtime.
00043     uBit.init();
00044 
00045     uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
00046     uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
00047     
00048     startAdvertising();
00049     
00050     release_fiber();
00051 }