Geo beacon for VF.

Dependencies:   MMA8452 aconno_bsp adc52832_common

Committer:
jurica238814
Date:
Mon Jul 17 11:35:44 2017 +0000
Revision:
3:2a4ac5b87046
Parent:
2:5504b714c9ae
Child:
4:331dddea780e
Child:
5:0f42dcae4cdf
Parser added.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jurica238814 1:5f34885f5cff 1 /*
jurica238814 0:f8c1e0b2d473 2 *
jurica238814 1:5f34885f5cff 3 * Made by Jurica Resetar @ aconno
jurica238814 1:5f34885f5cff 4 * aconno.de
jurica238814 1:5f34885f5cff 5 * All rights reserved
jurica238814 0:f8c1e0b2d473 6 *
jurica238814 0:f8c1e0b2d473 7 */
jurica238814 0:f8c1e0b2d473 8
jurica238814 0:f8c1e0b2d473 9 #include "mbed.h"
jurica238814 0:f8c1e0b2d473 10 #include "ble/BLE.h"
jurica238814 1:5f34885f5cff 11 #include "GapAdvertisingData.h"
jurica238814 0:f8c1e0b2d473 12 #include "acd52832_bsp.h"
jurica238814 1:5f34885f5cff 13
jurica238814 3:2a4ac5b87046 14 #define SLEEP_TIME (0.85) // Sleep time in seconds WAS 0.85
jurica238814 3:2a4ac5b87046 15 #define AWAKE_TIME (0.15) // Was 0.15
jurica238814 1:5f34885f5cff 16 #define BUZZER (p31)
jurica238814 0:f8c1e0b2d473 17
jurica238814 1:5f34885f5cff 18 /* Static constants for the BLE example */
jurica238814 3:2a4ac5b87046 19 #define MAX_BLE_PACKET_SIZE (31)
jurica238814 3:2a4ac5b87046 20 #define MSD_SIZE (18)
jurica238814 3:2a4ac5b87046 21 #define MSD_ID (0xFF)
jurica238814 3:2a4ac5b87046 22 #define BUZZ_TIME (1.0) // Buzz time in s
jurica238814 1:5f34885f5cff 23
jurica238814 0:f8c1e0b2d473 24
jurica238814 0:f8c1e0b2d473 25 bool SLEEP = true;
jurica238814 0:f8c1e0b2d473 26 int8_t txPower = 4;
jurica238814 1:5f34885f5cff 27 uint8_t MSD[MSD_SIZE] = {0x59, 0x00, 0xE1, 0x61, 0x35, 0xBA, 0xC0, 0xEC, 0x47, 0x2A, 0x98, 0x00, 0xAF, 0x18, 0x43, 0xFF, 0x05, 0x00};
jurica238814 1:5f34885f5cff 28 uint8_t my_mac_address[6] = {};
jurica238814 0:f8c1e0b2d473 29
jurica238814 2:5504b714c9ae 30 void turnBuzzOff(void);
jurica238814 2:5504b714c9ae 31 void goToSleep();
jurica238814 0:f8c1e0b2d473 32
jurica238814 2:5504b714c9ae 33 Ticker WakeSleepT;
jurica238814 2:5504b714c9ae 34 Ticker turnBuzzOffT;
jurica238814 1:5f34885f5cff 35 PwmOut buzzer(BUZZER);
jurica238814 1:5f34885f5cff 36 BLE &ble = BLE::Instance();
jurica238814 2:5504b714c9ae 37
jurica238814 0:f8c1e0b2d473 38 /* Restart Advertising on disconnection*/
jurica238814 0:f8c1e0b2d473 39 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
jurica238814 0:f8c1e0b2d473 40 BLE::Instance().gap().startAdvertising();
jurica238814 0:f8c1e0b2d473 41 }
jurica238814 0:f8c1e0b2d473 42
jurica238814 3:2a4ac5b87046 43 /**
jurica238814 3:2a4ac5b87046 44 * The function is called when ticker generates interrupt
jurica238814 3:2a4ac5b87046 45 */
jurica238814 2:5504b714c9ae 46 void turnBuzzOff(void){
jurica238814 2:5504b714c9ae 47 buzzer.write(0.0F);
jurica238814 2:5504b714c9ae 48 turnBuzzOffT.detach();
jurica238814 2:5504b714c9ae 49 WakeSleepT.attach(goToSleep, AWAKE_TIME);
jurica238814 1:5f34885f5cff 50 }
jurica238814 0:f8c1e0b2d473 51
jurica238814 0:f8c1e0b2d473 52 /**
jurica238814 0:f8c1e0b2d473 53 * This function is called when the ble initialization process has failed
jurica238814 0:f8c1e0b2d473 54 */
jurica238814 0:f8c1e0b2d473 55 void onBleInitError(BLE &ble, ble_error_t error){
jurica238814 0:f8c1e0b2d473 56 /* Avoid compiler warnings */
jurica238814 0:f8c1e0b2d473 57 (void) ble;
jurica238814 0:f8c1e0b2d473 58 (void) error;
jurica238814 0:f8c1e0b2d473 59 /* Initialization error handling should go here */
jurica238814 0:f8c1e0b2d473 60 }
jurica238814 0:f8c1e0b2d473 61
jurica238814 0:f8c1e0b2d473 62 /**
jurica238814 0:f8c1e0b2d473 63 * Callback triggered when the ble initialization process has finished
jurica238814 0:f8c1e0b2d473 64 */
jurica238814 0:f8c1e0b2d473 65 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){
jurica238814 0:f8c1e0b2d473 66 BLE& ble = params->ble;
jurica238814 0:f8c1e0b2d473 67 ble_error_t error = params->error;
jurica238814 0:f8c1e0b2d473 68
jurica238814 0:f8c1e0b2d473 69 if (error != BLE_ERROR_NONE) {
jurica238814 0:f8c1e0b2d473 70 /* In case of error, forward the error handling to onBleInitError */
jurica238814 0:f8c1e0b2d473 71 onBleInitError(ble, error);
jurica238814 0:f8c1e0b2d473 72 return;
jurica238814 0:f8c1e0b2d473 73 }
jurica238814 0:f8c1e0b2d473 74
jurica238814 0:f8c1e0b2d473 75 /* Ensure that it is the default instance of BLE */
jurica238814 0:f8c1e0b2d473 76 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
jurica238814 0:f8c1e0b2d473 77 return;
jurica238814 0:f8c1e0b2d473 78 }
jurica238814 0:f8c1e0b2d473 79
jurica238814 0:f8c1e0b2d473 80 ble.gap().onDisconnection(disconnectionCallback);
jurica238814 0:f8c1e0b2d473 81
jurica238814 1:5f34885f5cff 82 /* Get my MAC address */
jurica238814 1:5f34885f5cff 83 BLEProtocol::AddressType_t temp_address_type;
jurica238814 1:5f34885f5cff 84 ble.gap().getAddress(&temp_address_type, my_mac_address);
jurica238814 1:5f34885f5cff 85
jurica238814 1:5f34885f5cff 86
jurica238814 0:f8c1e0b2d473 87 /* setup advertising */
jurica238814 0:f8c1e0b2d473 88 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
jurica238814 1:5f34885f5cff 89 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)MSD, MSD_SIZE);
jurica238814 0:f8c1e0b2d473 90 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
jurica238814 2:5504b714c9ae 91 ble.gap().setAdvertisingInterval(100); // --> Has to be at least 100ms!
jurica238814 2:5504b714c9ae 92 ble.gap().startAdvertising();
jurica238814 0:f8c1e0b2d473 93 }
jurica238814 0:f8c1e0b2d473 94
jurica238814 3:2a4ac5b87046 95
jurica238814 3:2a4ac5b87046 96 uint8_t findMSDIndex(const Gap::AdvertisementCallbackParams_t *params){
jurica238814 1:5f34885f5cff 97 uint8_t i=0;
jurica238814 3:2a4ac5b87046 98 uint8_t len;
jurica238814 3:2a4ac5b87046 99
jurica238814 3:2a4ac5b87046 100 do{
jurica238814 3:2a4ac5b87046 101 len = params->advertisingData[i];
jurica238814 3:2a4ac5b87046 102 i++;
jurica238814 3:2a4ac5b87046 103 if(params->advertisingData[i] == MSD_ID) return i;
jurica238814 3:2a4ac5b87046 104 else i += (len-1);
jurica238814 3:2a4ac5b87046 105 }while(i<MAX_BLE_PACKET_SIZE);
jurica238814 3:2a4ac5b87046 106
jurica238814 3:2a4ac5b87046 107 return 0;
jurica238814 3:2a4ac5b87046 108 }
jurica238814 3:2a4ac5b87046 109
jurica238814 3:2a4ac5b87046 110 /**
jurica238814 3:2a4ac5b87046 111 * Function is called when BLE radio discovers any kind of advertisment
jurica238814 3:2a4ac5b87046 112 */
jurica238814 3:2a4ac5b87046 113 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params){
jurica238814 3:2a4ac5b87046 114 uint8_t i=0;
jurica238814 3:2a4ac5b87046 115 uint8_t msdOffset;
jurica238814 3:2a4ac5b87046 116
jurica238814 3:2a4ac5b87046 117 msdOffset = findMSDIndex(params);
jurica238814 3:2a4ac5b87046 118 if(msdOffset == 0){
jurica238814 3:2a4ac5b87046 119 // There's no MSD in BLE advertisement data
jurica238814 3:2a4ac5b87046 120 return;
jurica238814 3:2a4ac5b87046 121 }
jurica238814 3:2a4ac5b87046 122
jurica238814 3:2a4ac5b87046 123 if ((params->advertisingData[msdOffset]) == MSD_ID){
jurica238814 1:5f34885f5cff 124 // Follows Manufacturer Specific Data
jurica238814 3:2a4ac5b87046 125 if ((params->advertisingData[msdOffset+1]) == 0x59){
jurica238814 3:2a4ac5b87046 126 if ((params->advertisingData[msdOffset+2]) == 0x00){
jurica238814 1:5f34885f5cff 127 for(i=0; i<6; i++){
jurica238814 3:2a4ac5b87046 128 if((params->advertisingData[msdOffset+i+3]) == my_mac_address[5-i]){
jurica238814 1:5f34885f5cff 129 continue;
jurica238814 1:5f34885f5cff 130 }
jurica238814 1:5f34885f5cff 131 else{
jurica238814 1:5f34885f5cff 132 return;
jurica238814 1:5f34885f5cff 133 }
jurica238814 1:5f34885f5cff 134 }
jurica238814 2:5504b714c9ae 135 turnBuzzOffT.detach();
jurica238814 2:5504b714c9ae 136 WakeSleepT.detach();
jurica238814 2:5504b714c9ae 137 buzzer.write(0.5F);
jurica238814 2:5504b714c9ae 138 turnBuzzOffT.attach(turnBuzzOff, BUZZ_TIME);
jurica238814 1:5f34885f5cff 139 }
jurica238814 1:5f34885f5cff 140 }
jurica238814 2:5504b714c9ae 141 }
jurica238814 2:5504b714c9ae 142 }
jurica238814 2:5504b714c9ae 143
jurica238814 2:5504b714c9ae 144 void WakeMeUp(){
jurica238814 2:5504b714c9ae 145 WakeSleepT.detach();
jurica238814 2:5504b714c9ae 146 WakeSleepT.attach(goToSleep, AWAKE_TIME);
jurica238814 2:5504b714c9ae 147
jurica238814 2:5504b714c9ae 148 ble.gap().startScan(advertisementCallback);
jurica238814 2:5504b714c9ae 149 ble.gap().startAdvertising();
jurica238814 2:5504b714c9ae 150 SLEEP = false;
jurica238814 2:5504b714c9ae 151 }
jurica238814 2:5504b714c9ae 152
jurica238814 2:5504b714c9ae 153 void goToSleep(){
jurica238814 2:5504b714c9ae 154 WakeSleepT.detach();
jurica238814 2:5504b714c9ae 155 WakeSleepT.attach(WakeMeUp, SLEEP_TIME);
jurica238814 2:5504b714c9ae 156
jurica238814 2:5504b714c9ae 157 ble.gap().stopAdvertising();
jurica238814 2:5504b714c9ae 158 ble.gap().stopScan();
jurica238814 2:5504b714c9ae 159 SLEEP = true;
jurica238814 1:5f34885f5cff 160 }
jurica238814 1:5f34885f5cff 161
jurica238814 0:f8c1e0b2d473 162 int main(void){
jurica238814 2:5504b714c9ae 163 WakeSleepT.attach(goToSleep, AWAKE_TIME);
jurica238814 0:f8c1e0b2d473 164 ble.init(bleInitComplete);
jurica238814 0:f8c1e0b2d473 165 ble.gap().setTxPower(txPower);
jurica238814 0:f8c1e0b2d473 166 GapAdvertisingData postavke = GapAdvertisingData();
jurica238814 0:f8c1e0b2d473 167
jurica238814 2:5504b714c9ae 168 ble.gap().setScanParams(100, 100);
jurica238814 1:5f34885f5cff 169 ble.gap().startScan(advertisementCallback);
jurica238814 1:5f34885f5cff 170
jurica238814 1:5f34885f5cff 171 buzzer.period(0.001F);
jurica238814 1:5f34885f5cff 172 buzzer.write(0.0F);
jurica238814 0:f8c1e0b2d473 173
jurica238814 2:5504b714c9ae 174 __enable_irq();
jurica238814 2:5504b714c9ae 175
jurica238814 0:f8c1e0b2d473 176 /* SpinWait for initialization to complete. This is necessary because the
jurica238814 0:f8c1e0b2d473 177 * BLE object is used in the main loop below. */
jurica238814 3:2a4ac5b87046 178 while (ble.hasInitialized() == false){
jurica238814 3:2a4ac5b87046 179 /* spin loop */
jurica238814 3:2a4ac5b87046 180 }
jurica238814 1:5f34885f5cff 181
jurica238814 1:5f34885f5cff 182 while(true){
jurica238814 3:2a4ac5b87046 183 if(SLEEP) __WFI();
jurica238814 3:2a4ac5b87046 184 else ble.waitForEvent();
jurica238814 0:f8c1e0b2d473 185 }
jurica238814 0:f8c1e0b2d473 186 }