Geo beacon for VF.

Dependencies:   MMA8452 aconno_bsp adc52832_common

Committer:
jurica238814
Date:
Thu Jul 27 15:04:50 2017 +0000
Revision:
14:d506c0679c0b
Parent:
13:d51127eed926
Child:
15:934a04c958f5
Debug version. Beacons ping each other (the send and scan for the same msg).

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 4:331dddea780e 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 10:fd91664032d8 15 #define DEBUG (1)
jurica238814 10:fd91664032d8 16 #define DEBUG_ACC (0)
jurica238814 8:570eb66d50b5 17
jurica238814 14:d506c0679c0b 18 #define SLEEP_TIME_S (2.0) /* Sleep time in seconds WAS 0.85 */
jurica238814 14:d506c0679c0b 19 #define ADV_TIMER_TIME_S (1.0) /* Advertising time (in s) */
jurica238814 14:d506c0679c0b 20 #define SCAN_TIMER_TIME_S (2.0) /* Scanning time (in s) */
jurica238814 14:d506c0679c0b 21 #define FREE_TIME_S (0.1) /* Time between end of a scanning and sleep mode */
jurica238814 14:d506c0679c0b 22 #define AWAKE_TIME_S (ADV_TIMER_TIME_S+SCAN_TIMER_TIME_S+FREE_TIME_S) /* Was 0.15 */
jurica238814 14:d506c0679c0b 23 #define SHORT_SLEEP_TIME_S (0.5) /* Shorter sleep time (s) */
jurica238814 14:d506c0679c0b 24 #define SHORT_SLEEP_TIME_PERIOD_S (5) /* Time after a last scanned advertisment. In the period, sleep time is SHORT_SLEEP_TIME */
jurica238814 12:6b072c2a061c 25
jurica238814 0:f8c1e0b2d473 26
jurica238814 1:5f34885f5cff 27 /* Static constants for the BLE example */
jurica238814 3:2a4ac5b87046 28 #define MAX_BLE_PACKET_SIZE (31)
jurica238814 3:2a4ac5b87046 29 #define MSD_SIZE (18)
jurica238814 3:2a4ac5b87046 30 #define MSD_ID (0xFF)
jurica238814 12:6b072c2a061c 31
jurica238814 14:d506c0679c0b 32 #define BUZZ_TIME_S (0.25) /* Buzz time in s */
jurica238814 13:d51127eed926 33 #define ADV_INTERVAL (100) /* Advertising interval (in ms) */
jurica238814 14:d506c0679c0b 34 #define SCAN_INTERVAL (SCAN_TIMER_TIME_S) /* Scan interval (in ms) */
jurica238814 14:d506c0679c0b 35 #define SCAN_WINDOW (SCAN_TIMER_TIME_S)
jurica238814 1:5f34885f5cff 36
jurica238814 0:f8c1e0b2d473 37
jurica238814 6:d14e3df498f4 38 /* Static constants for the accelerometer */
jurica238814 13:d51127eed926 39 #define WHO_AM_I 0x0D /* Type 'read' : This should return the device id of 0x2A */
jurica238814 13:d51127eed926 40 #define OUT_Z_MSB 0x05 /* Type 'read' : z axis - 8 most significatn bit of a 12 bit sample */
jurica238814 6:d14e3df498f4 41 #define I2C_DATA (p29)
jurica238814 6:d14e3df498f4 42 #define I2C_CLK (p2)
jurica238814 6:d14e3df498f4 43 #define INT2_PIN (p4)
jurica238814 10:fd91664032d8 44 #define BUZZER (p31)
jurica238814 0:f8c1e0b2d473 45
jurica238814 10:fd91664032d8 46 uint8_t sleepFlag = 0;
jurica238814 14:d506c0679c0b 47 uint8_t tempSleepTime = SLEEP_TIME_S;
jurica238814 0:f8c1e0b2d473 48 int8_t txPower = 4;
jurica238814 14:d506c0679c0b 49 uint8_t msd[MSD_SIZE] = {0x59, 0x00, 0xE1, 0x61, 0x35, 0xBA, 0xC0, 0xEC, 0x47, 0x2A, 0x98, 0x00, 0xAF, 0x18, 0x43, 0xFF, 0x05, 0x00};
jurica238814 14:d506c0679c0b 50 uint8_t myMacAddress[6] = {};
jurica238814 6:d14e3df498f4 51 uint8_t buzzer_flag = 0;
jurica238814 0:f8c1e0b2d473 52
jurica238814 14:d506c0679c0b 53 enum RadioState{
jurica238814 10:fd91664032d8 54 OFF,
jurica238814 10:fd91664032d8 55 ADVERTISING,
jurica238814 10:fd91664032d8 56 SCANNING
jurica238814 10:fd91664032d8 57 };
jurica238814 14:d506c0679c0b 58 enum RadioState radioState = OFF;
jurica238814 0:f8c1e0b2d473 59
jurica238814 14:d506c0679c0b 60 void TurnBuzzOff(void);
jurica238814 14:d506c0679c0b 61 void GoToSleep();
jurica238814 14:d506c0679c0b 62 void StartAdvertising();
jurica238814 10:fd91664032d8 63 void startScanning();
jurica238814 10:fd91664032d8 64 void WakeMeUp();
jurica238814 0:f8c1e0b2d473 65
jurica238814 2:5504b714c9ae 66 Ticker WakeSleepT;
jurica238814 2:5504b714c9ae 67 Ticker turnBuzzOffT;
jurica238814 7:89c9abaa257e 68 Ticker sleepChanger;
jurica238814 1:5f34885f5cff 69 PwmOut buzzer(BUZZER);
jurica238814 6:d14e3df498f4 70 PwmOut gyro_power(p7);
jurica238814 14:d506c0679c0b 71 PwmOut i2c_power(p5); /* I2C Pull-ups power pin */
jurica238814 6:d14e3df498f4 72 InterruptIn gyro_pulse(INT2_PIN);
jurica238814 6:d14e3df498f4 73 Acc_MMA8452 acc(I2C_DATA, I2C_CLK, MMA8452_ADDRESS);
jurica238814 1:5f34885f5cff 74 BLE &ble = BLE::Instance();
jurica238814 2:5504b714c9ae 75
jurica238814 10:fd91664032d8 76
jurica238814 8:570eb66d50b5 77 #if DEBUG
jurica238814 10:fd91664032d8 78 DigitalOut advLED(p22);
jurica238814 10:fd91664032d8 79 DigitalOut scanLED(p23);
jurica238814 10:fd91664032d8 80 DigitalOut awake(p24);
jurica238814 10:fd91664032d8 81 #endif
jurica238814 10:fd91664032d8 82 #if DEBUG_ACC
jurica238814 8:570eb66d50b5 83 DigitalOut int_led(p22);
jurica238814 8:570eb66d50b5 84 #endif
jurica238814 8:570eb66d50b5 85
jurica238814 0:f8c1e0b2d473 86 /* Restart Advertising on disconnection*/
jurica238814 0:f8c1e0b2d473 87 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
jurica238814 10:fd91664032d8 88 //BLE::Instance().gap().startAdvertising();
jurica238814 0:f8c1e0b2d473 89 }
jurica238814 0:f8c1e0b2d473 90
jurica238814 0:f8c1e0b2d473 91
jurica238814 0:f8c1e0b2d473 92 /**
jurica238814 0:f8c1e0b2d473 93 * This function is called when the ble initialization process has failed
jurica238814 0:f8c1e0b2d473 94 */
jurica238814 0:f8c1e0b2d473 95 void onBleInitError(BLE &ble, ble_error_t error){
jurica238814 0:f8c1e0b2d473 96 /* Avoid compiler warnings */
jurica238814 0:f8c1e0b2d473 97 (void) ble;
jurica238814 0:f8c1e0b2d473 98 (void) error;
jurica238814 0:f8c1e0b2d473 99 /* Initialization error handling should go here */
jurica238814 0:f8c1e0b2d473 100 }
jurica238814 0:f8c1e0b2d473 101
jurica238814 0:f8c1e0b2d473 102 /**
jurica238814 0:f8c1e0b2d473 103 * Callback triggered when the ble initialization process has finished
jurica238814 0:f8c1e0b2d473 104 */
jurica238814 0:f8c1e0b2d473 105 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){
jurica238814 0:f8c1e0b2d473 106 BLE& ble = params->ble;
jurica238814 0:f8c1e0b2d473 107 ble_error_t error = params->error;
jurica238814 0:f8c1e0b2d473 108
jurica238814 0:f8c1e0b2d473 109 if (error != BLE_ERROR_NONE) {
jurica238814 0:f8c1e0b2d473 110 /* In case of error, forward the error handling to onBleInitError */
jurica238814 0:f8c1e0b2d473 111 onBleInitError(ble, error);
jurica238814 0:f8c1e0b2d473 112 return;
jurica238814 0:f8c1e0b2d473 113 }
jurica238814 0:f8c1e0b2d473 114
jurica238814 0:f8c1e0b2d473 115 /* Ensure that it is the default instance of BLE */
jurica238814 0:f8c1e0b2d473 116 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
jurica238814 0:f8c1e0b2d473 117 return;
jurica238814 0:f8c1e0b2d473 118 }
jurica238814 0:f8c1e0b2d473 119
jurica238814 0:f8c1e0b2d473 120 ble.gap().onDisconnection(disconnectionCallback);
jurica238814 0:f8c1e0b2d473 121
jurica238814 1:5f34885f5cff 122 /* Get my MAC address */
jurica238814 1:5f34885f5cff 123 BLEProtocol::AddressType_t temp_address_type;
jurica238814 14:d506c0679c0b 124 ble.gap().getAddress(&temp_address_type, myMacAddress);
jurica238814 1:5f34885f5cff 125
jurica238814 1:5f34885f5cff 126
jurica238814 0:f8c1e0b2d473 127 /* setup advertising */
jurica238814 10:fd91664032d8 128
jurica238814 14:d506c0679c0b 129 //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
jurica238814 14:d506c0679c0b 130 //ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
jurica238814 14:d506c0679c0b 131 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)msd, MSD_SIZE);
jurica238814 10:fd91664032d8 132 ble.gap().setAdvertisingInterval(ADV_INTERVAL); // --> Has to be at least 100ms!
jurica238814 10:fd91664032d8 133 //ble.gap().startAdvertising();
jurica238814 0:f8c1e0b2d473 134 }
jurica238814 0:f8c1e0b2d473 135
jurica238814 3:2a4ac5b87046 136
jurica238814 3:2a4ac5b87046 137 uint8_t findMSDIndex(const Gap::AdvertisementCallbackParams_t *params){
jurica238814 1:5f34885f5cff 138 uint8_t i=0;
jurica238814 3:2a4ac5b87046 139 uint8_t len;
jurica238814 3:2a4ac5b87046 140
jurica238814 3:2a4ac5b87046 141 do{
jurica238814 3:2a4ac5b87046 142 len = params->advertisingData[i];
jurica238814 3:2a4ac5b87046 143 i++;
jurica238814 3:2a4ac5b87046 144 if(params->advertisingData[i] == MSD_ID) return i;
jurica238814 3:2a4ac5b87046 145 else i += (len-1);
jurica238814 3:2a4ac5b87046 146 }while(i<MAX_BLE_PACKET_SIZE);
jurica238814 3:2a4ac5b87046 147
jurica238814 3:2a4ac5b87046 148 return 0;
jurica238814 3:2a4ac5b87046 149 }
jurica238814 3:2a4ac5b87046 150
jurica238814 14:d506c0679c0b 151 uint8_t CheckMac(const Gap::AdvertisementCallbackParams_t *params, uint8_t *myMacAddress, uint8_t msdOffset){
jurica238814 14:d506c0679c0b 152 int i=0;
jurica238814 14:d506c0679c0b 153
jurica238814 14:d506c0679c0b 154 if(msdOffset == 0){
jurica238814 14:d506c0679c0b 155 return 0; // There's no MSD in BLE advertisement data
jurica238814 14:d506c0679c0b 156 }
jurica238814 14:d506c0679c0b 157 for(i=0; i<6; i++){
jurica238814 14:d506c0679c0b 158 if(!(params->advertisingData[msdOffset+i+3]) == *(myMacAddress+5-i)){
jurica238814 14:d506c0679c0b 159 return 0;
jurica238814 14:d506c0679c0b 160 }
jurica238814 14:d506c0679c0b 161 }
jurica238814 14:d506c0679c0b 162 return 1;
jurica238814 14:d506c0679c0b 163 }
jurica238814 14:d506c0679c0b 164
jurica238814 3:2a4ac5b87046 165 /**
jurica238814 3:2a4ac5b87046 166 * Function is called when BLE radio discovers any kind of advertisment
jurica238814 3:2a4ac5b87046 167 */
jurica238814 3:2a4ac5b87046 168 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params){
jurica238814 3:2a4ac5b87046 169 uint8_t msdOffset;
jurica238814 3:2a4ac5b87046 170
jurica238814 3:2a4ac5b87046 171 msdOffset = findMSDIndex(params);
jurica238814 3:2a4ac5b87046 172 if(msdOffset == 0){
jurica238814 14:d506c0679c0b 173 return; // There's no MSD in BLE advertisement data
jurica238814 3:2a4ac5b87046 174 }
jurica238814 14:d506c0679c0b 175 if ((params->advertisingData[msdOffset]) == MSD_ID){
jurica238814 1:5f34885f5cff 176 // Follows Manufacturer Specific Data
jurica238814 3:2a4ac5b87046 177 if ((params->advertisingData[msdOffset+1]) == 0x59){
jurica238814 3:2a4ac5b87046 178 if ((params->advertisingData[msdOffset+2]) == 0x00){
jurica238814 14:d506c0679c0b 179 if(CheckMac(params, myMacAddress, msdOffset)){
jurica238814 14:d506c0679c0b 180 ble.gap().stopScan();
jurica238814 14:d506c0679c0b 181 buzzer.write(0.5F);
jurica238814 14:d506c0679c0b 182 WakeSleepT.detach();
jurica238814 14:d506c0679c0b 183 turnBuzzOffT.detach();
jurica238814 14:d506c0679c0b 184 turnBuzzOffT.attach(TurnBuzzOff, BUZZ_TIME_S);
jurica238814 1:5f34885f5cff 185 }
jurica238814 1:5f34885f5cff 186 }
jurica238814 1:5f34885f5cff 187 }
jurica238814 2:5504b714c9ae 188 }
jurica238814 2:5504b714c9ae 189 }
jurica238814 2:5504b714c9ae 190
jurica238814 12:6b072c2a061c 191
jurica238814 7:89c9abaa257e 192 /* Call this function few minutes (TBD) after a last scanned advertisment */
jurica238814 7:89c9abaa257e 193 void changeSleepTime(){
jurica238814 14:d506c0679c0b 194 tempSleepTime = SLEEP_TIME_S;
jurica238814 7:89c9abaa257e 195 sleepChanger.detach();
jurica238814 7:89c9abaa257e 196 }
jurica238814 7:89c9abaa257e 197
jurica238814 12:6b072c2a061c 198
jurica238814 7:89c9abaa257e 199 /**
jurica238814 7:89c9abaa257e 200 * The function is called when ticker generates interrupt
jurica238814 7:89c9abaa257e 201 */
jurica238814 14:d506c0679c0b 202 void TurnBuzzOff(void){
jurica238814 7:89c9abaa257e 203 buzzer.write(0.0F);
jurica238814 14:d506c0679c0b 204 tempSleepTime = SHORT_SLEEP_TIME_S;
jurica238814 7:89c9abaa257e 205 turnBuzzOffT.detach();
jurica238814 13:d51127eed926 206 WakeSleepT.detach();
jurica238814 14:d506c0679c0b 207 sleepChanger.attach(changeSleepTime, SHORT_SLEEP_TIME_PERIOD_S);
jurica238814 14:d506c0679c0b 208 WakeSleepT.attach(WakeMeUp, FREE_TIME_S);
jurica238814 7:89c9abaa257e 209 }
jurica238814 7:89c9abaa257e 210
jurica238814 10:fd91664032d8 211 void startAdvertising(){
jurica238814 10:fd91664032d8 212 ble.gap().startAdvertising();
jurica238814 10:fd91664032d8 213 #if DEBUG
jurica238814 10:fd91664032d8 214 advLED = 0;
jurica238814 10:fd91664032d8 215 scanLED = 1;
jurica238814 10:fd91664032d8 216 #endif
jurica238814 10:fd91664032d8 217 WakeSleepT.detach();
jurica238814 14:d506c0679c0b 218 WakeSleepT.attach(WakeMeUp, ADV_TIMER_TIME_S); // Call the wakeMeUp function
jurica238814 10:fd91664032d8 219 }
jurica238814 10:fd91664032d8 220
jurica238814 10:fd91664032d8 221 void startScanning(){
jurica238814 10:fd91664032d8 222 ble.gap().stopAdvertising();
jurica238814 10:fd91664032d8 223 ble.gap().setScanParams(SCAN_INTERVAL, SCAN_WINDOW);
jurica238814 14:d506c0679c0b 224 ble.gap().setScanTimeout(SCAN_TIMER_TIME_S);
jurica238814 2:5504b714c9ae 225 ble.gap().startScan(advertisementCallback);
jurica238814 10:fd91664032d8 226 #if DEBUG
jurica238814 10:fd91664032d8 227 advLED = 1;
jurica238814 10:fd91664032d8 228 scanLED = 0;
jurica238814 10:fd91664032d8 229 #endif
jurica238814 2:5504b714c9ae 230 WakeSleepT.detach();
jurica238814 14:d506c0679c0b 231 WakeSleepT.attach(WakeMeUp, SCAN_TIMER_TIME_S);
jurica238814 10:fd91664032d8 232 }
jurica238814 10:fd91664032d8 233
jurica238814 10:fd91664032d8 234 void WakeMeUp(){
jurica238814 8:570eb66d50b5 235 sleepFlag = 0;
jurica238814 10:fd91664032d8 236 switch(radioState){
jurica238814 10:fd91664032d8 237 case OFF:{
jurica238814 14:d506c0679c0b 238 radioState = ADVERTISING;
jurica238814 10:fd91664032d8 239 startAdvertising();
jurica238814 10:fd91664032d8 240 break;
jurica238814 10:fd91664032d8 241 }
jurica238814 10:fd91664032d8 242 case ADVERTISING:{
jurica238814 14:d506c0679c0b 243 radioState = SCANNING;
jurica238814 10:fd91664032d8 244 startScanning();
jurica238814 10:fd91664032d8 245 break;
jurica238814 10:fd91664032d8 246 }
jurica238814 10:fd91664032d8 247 case SCANNING:{
jurica238814 10:fd91664032d8 248 radioState = OFF;
jurica238814 10:fd91664032d8 249 WakeSleepT.detach();
jurica238814 14:d506c0679c0b 250 WakeSleepT.attach(GoToSleep, FREE_TIME_S);
jurica238814 10:fd91664032d8 251 #if DEBUG
jurica238814 10:fd91664032d8 252 advLED = 1;
jurica238814 10:fd91664032d8 253 scanLED = 1;
jurica238814 10:fd91664032d8 254 #endif
jurica238814 10:fd91664032d8 255 break;
jurica238814 10:fd91664032d8 256 }
jurica238814 10:fd91664032d8 257 default: return;
jurica238814 10:fd91664032d8 258 }
jurica238814 2:5504b714c9ae 259 }
jurica238814 2:5504b714c9ae 260
jurica238814 14:d506c0679c0b 261 void GoToSleep(){
jurica238814 2:5504b714c9ae 262 WakeSleepT.detach();
jurica238814 7:89c9abaa257e 263 WakeSleepT.attach(WakeMeUp, tempSleepTime);
jurica238814 2:5504b714c9ae 264 ble.gap().stopAdvertising();
jurica238814 2:5504b714c9ae 265 ble.gap().stopScan();
jurica238814 8:570eb66d50b5 266 sleepFlag = 1;
jurica238814 1:5f34885f5cff 267 }
jurica238814 1:5f34885f5cff 268
jurica238814 6:d14e3df498f4 269 void buzz(void){
jurica238814 6:d14e3df498f4 270 buzzer.write(0.5f);
jurica238814 6:d14e3df498f4 271 wait_ms(100);
jurica238814 6:d14e3df498f4 272 buzzer.write(0.0f);
jurica238814 9:2ab2be19add9 273 sleepFlag = 0;
jurica238814 1:5f34885f5cff 274 }
jurica238814 1:5f34885f5cff 275
jurica238814 6:d14e3df498f4 276 void pulse_handler(void){
jurica238814 10:fd91664032d8 277 #if DEBUG_ACC
jurica238814 6:d14e3df498f4 278 int_led = !int_led;
jurica238814 6:d14e3df498f4 279 #endif
jurica238814 6:d14e3df498f4 280 i2c_power.write(1.0F);
jurica238814 6:d14e3df498f4 281 buzzer_flag = 1;
jurica238814 8:570eb66d50b5 282 // Be awake some time
jurica238814 9:2ab2be19add9 283 //WakeSleepT.detach();
jurica238814 9:2ab2be19add9 284 //WakeSleepT.attach(goToSleep, AWAKE_TIME);
jurica238814 6:d14e3df498f4 285 }
jurica238814 6:d14e3df498f4 286
jurica238814 10:fd91664032d8 287 int main(void){
jurica238814 10:fd91664032d8 288 #if DEBUG
jurica238814 10:fd91664032d8 289 awake = 1;
jurica238814 10:fd91664032d8 290 advLED = 1;
jurica238814 10:fd91664032d8 291 scanLED = 1;
jurica238814 10:fd91664032d8 292 #endif
jurica238814 14:d506c0679c0b 293
jurica238814 14:d506c0679c0b 294 WakeSleepT.attach(GoToSleep, AWAKE_TIME_S);
jurica238814 0:f8c1e0b2d473 295 ble.init(bleInitComplete);
jurica238814 0:f8c1e0b2d473 296 ble.gap().setTxPower(txPower);
jurica238814 0:f8c1e0b2d473 297 GapAdvertisingData postavke = GapAdvertisingData();
jurica238814 0:f8c1e0b2d473 298
jurica238814 7:89c9abaa257e 299 buzzer.period(0.0009F);
jurica238814 1:5f34885f5cff 300 buzzer.write(0.0F);
jurica238814 6:d14e3df498f4 301 gyro_power.period(0.01F);
jurica238814 6:d14e3df498f4 302 gyro_power.write(1.0F);
jurica238814 6:d14e3df498f4 303 i2c_power.period(0.01F);
jurica238814 6:d14e3df498f4 304 i2c_power.write(1.0F);
jurica238814 6:d14e3df498f4 305 wait_ms(1000);
jurica238814 6:d14e3df498f4 306
jurica238814 6:d14e3df498f4 307 /* Pulse interrupt detection */
jurica238814 6:d14e3df498f4 308 acc.set_register((char)CTRL_REG_4, (char) 0x04); // INT_EN_FF_MT Freefall/motion interrupt enabled
jurica238814 6:d14e3df498f4 309 wait_ms(1);
jurica238814 6:d14e3df498f4 310 acc.set_register((char)FF_MT_CFG, (char) 0b01011000); //ELE, Motion Flag ON, YEFE, X Event Flag Enable
jurica238814 6:d14e3df498f4 311 wait_ms(1);
jurica238814 6:d14e3df498f4 312 acc.set_register((char)CTRL_REG_5, (char) 0x00); // INT_EN_FF_MT interrupt is router t0 INT2
jurica238814 6:d14e3df498f4 313 wait_ms(1);
jurica238814 6:d14e3df498f4 314 acc.set_register((char)FF_COUNT, (char) 0x08); // Set Counter degister value (10ms)
jurica238814 6:d14e3df498f4 315 wait_ms(1);
jurica238814 6:d14e3df498f4 316 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 317 wait_ms(1);
jurica238814 6:d14e3df498f4 318
jurica238814 6:d14e3df498f4 319 /* Setup for the interrupt handler */
jurica238814 13:d51127eed926 320 gyro_pulse.rise(&pulse_handler); // -------------------------------------
jurica238814 6:d14e3df498f4 321 acc.set_register((char)CTRL_REG_1, (char) 0x01); // Flow data rate and Active mode
jurica238814 6:d14e3df498f4 322 wait(1);
jurica238814 0:f8c1e0b2d473 323
jurica238814 2:5504b714c9ae 324 __enable_irq();
jurica238814 2:5504b714c9ae 325
jurica238814 0:f8c1e0b2d473 326 /* SpinWait for initialization to complete. This is necessary because the
jurica238814 0:f8c1e0b2d473 327 * BLE object is used in the main loop below. */
jurica238814 3:2a4ac5b87046 328 while (ble.hasInitialized() == false){
jurica238814 3:2a4ac5b87046 329 /* spin loop */
jurica238814 3:2a4ac5b87046 330 }
jurica238814 9:2ab2be19add9 331
jurica238814 1:5f34885f5cff 332 while(true){
jurica238814 9:2ab2be19add9 333 if(sleepFlag){
jurica238814 9:2ab2be19add9 334 if(!awake) awake = 1;
jurica238814 9:2ab2be19add9 335 __WFI();
jurica238814 9:2ab2be19add9 336 }
jurica238814 9:2ab2be19add9 337 else{
jurica238814 9:2ab2be19add9 338 if(awake) awake = 0;
jurica238814 9:2ab2be19add9 339 ble.waitForEvent();
jurica238814 9:2ab2be19add9 340 }
jurica238814 0:f8c1e0b2d473 341 }
jurica238814 0:f8c1e0b2d473 342 }