Geo beacon for VF.

Dependencies:   MMA8452 aconno_bsp adc52832_common

Committer:
jurica238814
Date:
Thu Jul 20 13:44:49 2017 +0000
Revision:
9:2ab2be19add9
Parent:
8:570eb66d50b5
Child:
10:fd91664032d8
acc disabled. Buzzing does not work.

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 6:d14e3df498f4 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 6:d14e3df498f4 13 #include "mma8452.h"
jurica238814 1:5f34885f5cff 14
jurica238814 8:570eb66d50b5 15 #define DEBUG 1
jurica238814 8:570eb66d50b5 16
jurica238814 9:2ab2be19add9 17 #define SLEEP_TIME (2.0) // Sleep time in seconds WAS 0.85
jurica238814 9:2ab2be19add9 18 #define AWAKE_TIME (2.0) // Was 0.15
jurica238814 1:5f34885f5cff 19 #define BUZZER (p31)
jurica238814 0:f8c1e0b2d473 20
jurica238814 1:5f34885f5cff 21 /* Static constants for the BLE example */
jurica238814 3:2a4ac5b87046 22 #define MAX_BLE_PACKET_SIZE (31)
jurica238814 3:2a4ac5b87046 23 #define MSD_SIZE (18)
jurica238814 3:2a4ac5b87046 24 #define MSD_ID (0xFF)
jurica238814 3:2a4ac5b87046 25 #define BUZZ_TIME (1.0) // Buzz time in s
jurica238814 1:5f34885f5cff 26
jurica238814 6:d14e3df498f4 27 /* Static constants for the accelerometer */
jurica238814 6:d14e3df498f4 28 #define WHO_AM_I 0x0D // Type 'read' : This should return the device id of 0x2A
jurica238814 6:d14e3df498f4 29 #define OUT_Z_MSB 0x05 // Type 'read' : z axis - 8 most significatn bit of a 12 bit sample
jurica238814 6:d14e3df498f4 30 #define I2C_DATA (p29)
jurica238814 6:d14e3df498f4 31 #define I2C_CLK (p2)
jurica238814 6:d14e3df498f4 32 #define INT2_PIN (p4)
jurica238814 0:f8c1e0b2d473 33
jurica238814 8:570eb66d50b5 34 uint8_t sleepFlag = 1;
jurica238814 0:f8c1e0b2d473 35 int8_t txPower = 4;
jurica238814 1:5f34885f5cff 36 uint8_t MSD[MSD_SIZE] = {0x59, 0x00, 0xE1, 0x61, 0x35, 0xBA, 0xC0, 0xEC, 0x47, 0x2A, 0x98, 0x00, 0xAF, 0x18, 0x43, 0xFF, 0x05, 0x00};
jurica238814 6:d14e3df498f4 37 uint8_t my_mac_address[6] = {};
jurica238814 6:d14e3df498f4 38 uint8_t buzzer_flag = 0;
jurica238814 0:f8c1e0b2d473 39
jurica238814 2:5504b714c9ae 40 void turnBuzzOff(void);
jurica238814 2:5504b714c9ae 41 void goToSleep();
jurica238814 0:f8c1e0b2d473 42
jurica238814 2:5504b714c9ae 43 Ticker WakeSleepT;
jurica238814 2:5504b714c9ae 44 Ticker turnBuzzOffT;
jurica238814 1:5f34885f5cff 45 PwmOut buzzer(BUZZER);
jurica238814 6:d14e3df498f4 46 PwmOut gyro_power(p7);
jurica238814 6:d14e3df498f4 47 PwmOut i2c_power(p5); // I2C Pull-ups power pin
jurica238814 6:d14e3df498f4 48 InterruptIn gyro_pulse(INT2_PIN);
jurica238814 6:d14e3df498f4 49 Acc_MMA8452 acc(I2C_DATA, I2C_CLK, MMA8452_ADDRESS);
jurica238814 1:5f34885f5cff 50 BLE &ble = BLE::Instance();
jurica238814 2:5504b714c9ae 51
jurica238814 8:570eb66d50b5 52 #if DEBUG
jurica238814 8:570eb66d50b5 53 DigitalOut int_led(p22);
jurica238814 9:2ab2be19add9 54 DigitalOut awake(p24);
jurica238814 8:570eb66d50b5 55 #endif
jurica238814 8:570eb66d50b5 56
jurica238814 0:f8c1e0b2d473 57 /* Restart Advertising on disconnection*/
jurica238814 0:f8c1e0b2d473 58 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
jurica238814 0:f8c1e0b2d473 59 BLE::Instance().gap().startAdvertising();
jurica238814 0:f8c1e0b2d473 60 }
jurica238814 0:f8c1e0b2d473 61
jurica238814 3:2a4ac5b87046 62 /**
jurica238814 3:2a4ac5b87046 63 * The function is called when ticker generates interrupt
jurica238814 3:2a4ac5b87046 64 */
jurica238814 2:5504b714c9ae 65 void turnBuzzOff(void){
jurica238814 2:5504b714c9ae 66 buzzer.write(0.0F);
jurica238814 2:5504b714c9ae 67 turnBuzzOffT.detach();
jurica238814 2:5504b714c9ae 68 WakeSleepT.attach(goToSleep, AWAKE_TIME);
jurica238814 1:5f34885f5cff 69 }
jurica238814 0:f8c1e0b2d473 70
jurica238814 0:f8c1e0b2d473 71 /**
jurica238814 0:f8c1e0b2d473 72 * This function is called when the ble initialization process has failed
jurica238814 0:f8c1e0b2d473 73 */
jurica238814 0:f8c1e0b2d473 74 void onBleInitError(BLE &ble, ble_error_t error){
jurica238814 0:f8c1e0b2d473 75 /* Avoid compiler warnings */
jurica238814 0:f8c1e0b2d473 76 (void) ble;
jurica238814 0:f8c1e0b2d473 77 (void) error;
jurica238814 0:f8c1e0b2d473 78 /* Initialization error handling should go here */
jurica238814 0:f8c1e0b2d473 79 }
jurica238814 0:f8c1e0b2d473 80
jurica238814 0:f8c1e0b2d473 81 /**
jurica238814 0:f8c1e0b2d473 82 * Callback triggered when the ble initialization process has finished
jurica238814 0:f8c1e0b2d473 83 */
jurica238814 0:f8c1e0b2d473 84 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){
jurica238814 0:f8c1e0b2d473 85 BLE& ble = params->ble;
jurica238814 0:f8c1e0b2d473 86 ble_error_t error = params->error;
jurica238814 0:f8c1e0b2d473 87
jurica238814 0:f8c1e0b2d473 88 if (error != BLE_ERROR_NONE) {
jurica238814 0:f8c1e0b2d473 89 /* In case of error, forward the error handling to onBleInitError */
jurica238814 0:f8c1e0b2d473 90 onBleInitError(ble, error);
jurica238814 0:f8c1e0b2d473 91 return;
jurica238814 0:f8c1e0b2d473 92 }
jurica238814 0:f8c1e0b2d473 93
jurica238814 0:f8c1e0b2d473 94 /* Ensure that it is the default instance of BLE */
jurica238814 0:f8c1e0b2d473 95 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
jurica238814 0:f8c1e0b2d473 96 return;
jurica238814 0:f8c1e0b2d473 97 }
jurica238814 0:f8c1e0b2d473 98
jurica238814 0:f8c1e0b2d473 99 ble.gap().onDisconnection(disconnectionCallback);
jurica238814 0:f8c1e0b2d473 100
jurica238814 1:5f34885f5cff 101 /* Get my MAC address */
jurica238814 1:5f34885f5cff 102 BLEProtocol::AddressType_t temp_address_type;
jurica238814 1:5f34885f5cff 103 ble.gap().getAddress(&temp_address_type, my_mac_address);
jurica238814 1:5f34885f5cff 104
jurica238814 1:5f34885f5cff 105
jurica238814 0:f8c1e0b2d473 106 /* setup advertising */
jurica238814 0:f8c1e0b2d473 107 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
jurica238814 1:5f34885f5cff 108 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)MSD, MSD_SIZE);
jurica238814 0:f8c1e0b2d473 109 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
jurica238814 2:5504b714c9ae 110 ble.gap().setAdvertisingInterval(100); // --> Has to be at least 100ms!
jurica238814 2:5504b714c9ae 111 ble.gap().startAdvertising();
jurica238814 0:f8c1e0b2d473 112 }
jurica238814 0:f8c1e0b2d473 113
jurica238814 3:2a4ac5b87046 114
jurica238814 3:2a4ac5b87046 115 uint8_t findMSDIndex(const Gap::AdvertisementCallbackParams_t *params){
jurica238814 1:5f34885f5cff 116 uint8_t i=0;
jurica238814 3:2a4ac5b87046 117 uint8_t len;
jurica238814 3:2a4ac5b87046 118
jurica238814 3:2a4ac5b87046 119 do{
jurica238814 3:2a4ac5b87046 120 len = params->advertisingData[i];
jurica238814 3:2a4ac5b87046 121 i++;
jurica238814 3:2a4ac5b87046 122 if(params->advertisingData[i] == MSD_ID) return i;
jurica238814 3:2a4ac5b87046 123 else i += (len-1);
jurica238814 3:2a4ac5b87046 124 }while(i<MAX_BLE_PACKET_SIZE);
jurica238814 3:2a4ac5b87046 125
jurica238814 3:2a4ac5b87046 126 return 0;
jurica238814 3:2a4ac5b87046 127 }
jurica238814 3:2a4ac5b87046 128
jurica238814 3:2a4ac5b87046 129 /**
jurica238814 3:2a4ac5b87046 130 * Function is called when BLE radio discovers any kind of advertisment
jurica238814 3:2a4ac5b87046 131 */
jurica238814 3:2a4ac5b87046 132 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params){
jurica238814 3:2a4ac5b87046 133 uint8_t i=0;
jurica238814 3:2a4ac5b87046 134 uint8_t msdOffset;
jurica238814 3:2a4ac5b87046 135
jurica238814 3:2a4ac5b87046 136 msdOffset = findMSDIndex(params);
jurica238814 3:2a4ac5b87046 137 if(msdOffset == 0){
jurica238814 3:2a4ac5b87046 138 // There's no MSD in BLE advertisement data
jurica238814 3:2a4ac5b87046 139 return;
jurica238814 3:2a4ac5b87046 140 }
jurica238814 3:2a4ac5b87046 141
jurica238814 3:2a4ac5b87046 142 if ((params->advertisingData[msdOffset]) == MSD_ID){
jurica238814 1:5f34885f5cff 143 // Follows Manufacturer Specific Data
jurica238814 3:2a4ac5b87046 144 if ((params->advertisingData[msdOffset+1]) == 0x59){
jurica238814 3:2a4ac5b87046 145 if ((params->advertisingData[msdOffset+2]) == 0x00){
jurica238814 1:5f34885f5cff 146 for(i=0; i<6; i++){
jurica238814 3:2a4ac5b87046 147 if((params->advertisingData[msdOffset+i+3]) == my_mac_address[5-i]){
jurica238814 1:5f34885f5cff 148 continue;
jurica238814 1:5f34885f5cff 149 }
jurica238814 1:5f34885f5cff 150 else{
jurica238814 9:2ab2be19add9 151 //return;
jurica238814 1:5f34885f5cff 152 }
jurica238814 1:5f34885f5cff 153 }
jurica238814 2:5504b714c9ae 154 turnBuzzOffT.detach();
jurica238814 2:5504b714c9ae 155 WakeSleepT.detach();
jurica238814 2:5504b714c9ae 156 buzzer.write(0.5F);
jurica238814 2:5504b714c9ae 157 turnBuzzOffT.attach(turnBuzzOff, BUZZ_TIME);
jurica238814 1:5f34885f5cff 158 }
jurica238814 1:5f34885f5cff 159 }
jurica238814 2:5504b714c9ae 160 }
jurica238814 2:5504b714c9ae 161 }
jurica238814 2:5504b714c9ae 162
jurica238814 2:5504b714c9ae 163 void WakeMeUp(){
jurica238814 2:5504b714c9ae 164 WakeSleepT.detach();
jurica238814 2:5504b714c9ae 165 WakeSleepT.attach(goToSleep, AWAKE_TIME);
jurica238814 2:5504b714c9ae 166
jurica238814 2:5504b714c9ae 167 ble.gap().startScan(advertisementCallback);
jurica238814 2:5504b714c9ae 168 ble.gap().startAdvertising();
jurica238814 8:570eb66d50b5 169 sleepFlag = 0;
jurica238814 2:5504b714c9ae 170 }
jurica238814 2:5504b714c9ae 171
jurica238814 2:5504b714c9ae 172 void goToSleep(){
jurica238814 2:5504b714c9ae 173 WakeSleepT.detach();
jurica238814 2:5504b714c9ae 174 WakeSleepT.attach(WakeMeUp, SLEEP_TIME);
jurica238814 2:5504b714c9ae 175
jurica238814 2:5504b714c9ae 176 ble.gap().stopAdvertising();
jurica238814 2:5504b714c9ae 177 ble.gap().stopScan();
jurica238814 8:570eb66d50b5 178 sleepFlag = 1;
jurica238814 1:5f34885f5cff 179 }
jurica238814 1:5f34885f5cff 180
jurica238814 6:d14e3df498f4 181 void buzz(void){
jurica238814 6:d14e3df498f4 182 buzzer.write(0.5f);
jurica238814 6:d14e3df498f4 183 wait_ms(100);
jurica238814 6:d14e3df498f4 184 buzzer.write(0.0f);
jurica238814 9:2ab2be19add9 185 sleepFlag = 0;
jurica238814 6:d14e3df498f4 186 }
jurica238814 6:d14e3df498f4 187
jurica238814 6:d14e3df498f4 188 void pulse_handler(void){
jurica238814 6:d14e3df498f4 189 #if DEBUG
jurica238814 6:d14e3df498f4 190 int_led = !int_led;
jurica238814 6:d14e3df498f4 191 #endif
jurica238814 6:d14e3df498f4 192 i2c_power.write(1.0F);
jurica238814 6:d14e3df498f4 193 buzzer_flag = 1;
jurica238814 8:570eb66d50b5 194 // Be awake some time
jurica238814 9:2ab2be19add9 195 //WakeSleepT.detach();
jurica238814 9:2ab2be19add9 196 //WakeSleepT.attach(goToSleep, AWAKE_TIME);
jurica238814 6:d14e3df498f4 197 }
jurica238814 6:d14e3df498f4 198
jurica238814 0:f8c1e0b2d473 199 int main(void){
jurica238814 2:5504b714c9ae 200 WakeSleepT.attach(goToSleep, AWAKE_TIME);
jurica238814 0:f8c1e0b2d473 201 ble.init(bleInitComplete);
jurica238814 0:f8c1e0b2d473 202 ble.gap().setTxPower(txPower);
jurica238814 0:f8c1e0b2d473 203 GapAdvertisingData postavke = GapAdvertisingData();
jurica238814 0:f8c1e0b2d473 204
jurica238814 2:5504b714c9ae 205 ble.gap().setScanParams(100, 100);
jurica238814 1:5f34885f5cff 206 ble.gap().startScan(advertisementCallback);
jurica238814 1:5f34885f5cff 207
jurica238814 1:5f34885f5cff 208 buzzer.period(0.001F);
jurica238814 1:5f34885f5cff 209 buzzer.write(0.0F);
jurica238814 6:d14e3df498f4 210 gyro_power.period(0.01F);
jurica238814 6:d14e3df498f4 211 gyro_power.write(1.0F);
jurica238814 6:d14e3df498f4 212 i2c_power.period(0.01F);
jurica238814 6:d14e3df498f4 213 i2c_power.write(1.0F);
jurica238814 6:d14e3df498f4 214 wait_ms(1000);
jurica238814 6:d14e3df498f4 215
jurica238814 6:d14e3df498f4 216 /* Pulse interrupt detection */
jurica238814 6:d14e3df498f4 217 acc.set_register((char)CTRL_REG_4, (char) 0x04); // INT_EN_FF_MT Freefall/motion interrupt enabled
jurica238814 6:d14e3df498f4 218 wait_ms(1);
jurica238814 6:d14e3df498f4 219 acc.set_register((char)FF_MT_CFG, (char) 0b01011000); //ELE, Motion Flag ON, YEFE, X Event Flag Enable
jurica238814 6:d14e3df498f4 220 wait_ms(1);
jurica238814 6:d14e3df498f4 221 acc.set_register((char)CTRL_REG_5, (char) 0x00); // INT_EN_FF_MT interrupt is router t0 INT2
jurica238814 6:d14e3df498f4 222 wait_ms(1);
jurica238814 6:d14e3df498f4 223 acc.set_register((char)FF_COUNT, (char) 0x08); // Set Counter degister value (10ms)
jurica238814 6:d14e3df498f4 224 wait_ms(1);
jurica238814 6:d14e3df498f4 225 acc.set_register((char)FF_MT_THS, (char) 0x90); // Set TH value for motion detection on 1 G (1/0.063) and DBCNTM = 1 (Increments or clears counter)
jurica238814 6:d14e3df498f4 226 wait_ms(1);
jurica238814 6:d14e3df498f4 227
jurica238814 6:d14e3df498f4 228 /* Setup for the interrupt handler */
jurica238814 9:2ab2be19add9 229 //gyro_pulse.rise(&pulse_handler); // -------------------------------------
jurica238814 6:d14e3df498f4 230 acc.set_register((char)CTRL_REG_1, (char) 0x01); // Flow data rate and Active mode
jurica238814 6:d14e3df498f4 231 wait(1);
jurica238814 0:f8c1e0b2d473 232
jurica238814 2:5504b714c9ae 233 __enable_irq();
jurica238814 2:5504b714c9ae 234
jurica238814 0:f8c1e0b2d473 235 /* SpinWait for initialization to complete. This is necessary because the
jurica238814 0:f8c1e0b2d473 236 * BLE object is used in the main loop below. */
jurica238814 3:2a4ac5b87046 237 while (ble.hasInitialized() == false){
jurica238814 3:2a4ac5b87046 238 /* spin loop */
jurica238814 3:2a4ac5b87046 239 }
jurica238814 1:5f34885f5cff 240
jurica238814 9:2ab2be19add9 241 while(1){
jurica238814 9:2ab2be19add9 242 ble.waitForEvent();
jurica238814 9:2ab2be19add9 243 awake != awake;
jurica238814 9:2ab2be19add9 244 wait_ms(100);
jurica238814 9:2ab2be19add9 245 }
jurica238814 9:2ab2be19add9 246
jurica238814 1:5f34885f5cff 247 while(true){
jurica238814 9:2ab2be19add9 248 if(sleepFlag){
jurica238814 9:2ab2be19add9 249 if(!awake) awake = 1;
jurica238814 9:2ab2be19add9 250 __WFI();
jurica238814 9:2ab2be19add9 251 }
jurica238814 9:2ab2be19add9 252 else{
jurica238814 9:2ab2be19add9 253 if(awake) awake = 0;
jurica238814 9:2ab2be19add9 254 ble.waitForEvent();
jurica238814 9:2ab2be19add9 255 }
jurica238814 0:f8c1e0b2d473 256 }
jurica238814 0:f8c1e0b2d473 257 }