Geo beacon for VF.

Dependencies:   MMA8452 aconno_bsp adc52832_common

Committer:
jurica238814
Date:
Wed Jul 26 20:11:45 2017 +0000
Revision:
13:d51127eed926
Parent:
12:6b072c2a061c
Child:
14:d506c0679c0b
Stable version with longer delays. Works fine! (without acc!).

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 13:d51127eed926 18 #define SLEEP_TIME (2.0) /* Sleep time in seconds WAS 0.85 */
jurica238814 13:d51127eed926 19 #define ADV_TIMER_TIME (1.1) /* Advertising time (in s) */
jurica238814 13:d51127eed926 20 #define SCAN_TIMER_TIME (1.5) /* Scanning time (in s) */
jurica238814 13:d51127eed926 21 #define FREE_TIME (1.01) /* Time between end of a scanning and sleep mode */
jurica238814 13:d51127eed926 22 #define AWAKE_TIME (ADV_TIMER_TIME+SCAN_TIMER_TIME+FREE_TIME) /* Was 0.15 */
jurica238814 13:d51127eed926 23 #define SHORT_SLEEP_TIME (2.5) /* Shorter sleep time (s) */
jurica238814 13:d51127eed926 24 #define SHORT_SLEEP_TIME_PERIOD (60) /* 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 13:d51127eed926 32 #define BUZZ_TIME (2.0) /* Buzz time in s */
jurica238814 13:d51127eed926 33 #define ADV_INTERVAL (100) /* Advertising interval (in ms) */
jurica238814 13:d51127eed926 34 #define SCAN_INTERVAL (SCAN_TIMER_TIME) /* Scan interval (in ms) */
jurica238814 13:d51127eed926 35 #define SCAN_WINDOW (SCAN_TIMER_TIME)
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 7:89c9abaa257e 47 uint8_t tempSleepTime = SLEEP_TIME;
jurica238814 0:f8c1e0b2d473 48 int8_t txPower = 4;
jurica238814 1:5f34885f5cff 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 6:d14e3df498f4 50 uint8_t my_mac_address[6] = {};
jurica238814 6:d14e3df498f4 51 uint8_t buzzer_flag = 0;
jurica238814 0:f8c1e0b2d473 52
jurica238814 10:fd91664032d8 53 enum _radioState{
jurica238814 10:fd91664032d8 54 OFF,
jurica238814 10:fd91664032d8 55 ADVERTISING,
jurica238814 10:fd91664032d8 56 SCANNING
jurica238814 10:fd91664032d8 57 };
jurica238814 10:fd91664032d8 58 enum _radioState radioState = OFF;
jurica238814 0:f8c1e0b2d473 59
jurica238814 2:5504b714c9ae 60 void turnBuzzOff(void);
jurica238814 2:5504b714c9ae 61 void goToSleep();
jurica238814 10:fd91664032d8 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 6:d14e3df498f4 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 1:5f34885f5cff 124 ble.gap().getAddress(&temp_address_type, my_mac_address);
jurica238814 1:5f34885f5cff 125
jurica238814 1:5f34885f5cff 126
jurica238814 0:f8c1e0b2d473 127 /* setup advertising */
jurica238814 10:fd91664032d8 128
jurica238814 0:f8c1e0b2d473 129 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
jurica238814 1:5f34885f5cff 130 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)MSD, MSD_SIZE);
jurica238814 0:f8c1e0b2d473 131 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
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 3:2a4ac5b87046 151 /**
jurica238814 3:2a4ac5b87046 152 * Function is called when BLE radio discovers any kind of advertisment
jurica238814 3:2a4ac5b87046 153 */
jurica238814 3:2a4ac5b87046 154 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params){
jurica238814 3:2a4ac5b87046 155 uint8_t i=0;
jurica238814 3:2a4ac5b87046 156 uint8_t msdOffset;
jurica238814 3:2a4ac5b87046 157
jurica238814 3:2a4ac5b87046 158 msdOffset = findMSDIndex(params);
jurica238814 3:2a4ac5b87046 159 if(msdOffset == 0){
jurica238814 3:2a4ac5b87046 160 // There's no MSD in BLE advertisement data
jurica238814 3:2a4ac5b87046 161 return;
jurica238814 3:2a4ac5b87046 162 }
jurica238814 3:2a4ac5b87046 163
jurica238814 3:2a4ac5b87046 164 if ((params->advertisingData[msdOffset]) == MSD_ID){
jurica238814 1:5f34885f5cff 165 // Follows Manufacturer Specific Data
jurica238814 3:2a4ac5b87046 166 if ((params->advertisingData[msdOffset+1]) == 0x59){
jurica238814 3:2a4ac5b87046 167 if ((params->advertisingData[msdOffset+2]) == 0x00){
jurica238814 1:5f34885f5cff 168 for(i=0; i<6; i++){
jurica238814 3:2a4ac5b87046 169 if((params->advertisingData[msdOffset+i+3]) == my_mac_address[5-i]){
jurica238814 1:5f34885f5cff 170 continue;
jurica238814 1:5f34885f5cff 171 }
jurica238814 1:5f34885f5cff 172 else{
jurica238814 1:5f34885f5cff 173 return;
jurica238814 1:5f34885f5cff 174 }
jurica238814 1:5f34885f5cff 175 }
jurica238814 13:d51127eed926 176 /*
jurica238814 13:d51127eed926 177 ble.gap().stopScan();
jurica238814 2:5504b714c9ae 178 turnBuzzOffT.detach();
jurica238814 2:5504b714c9ae 179 WakeSleepT.detach();
jurica238814 2:5504b714c9ae 180 buzzer.write(0.5F);
jurica238814 2:5504b714c9ae 181 turnBuzzOffT.attach(turnBuzzOff, BUZZ_TIME);
jurica238814 13:d51127eed926 182 */
jurica238814 13:d51127eed926 183 buzzer.write(0.5F);
jurica238814 13:d51127eed926 184 WakeSleepT.detach();
jurica238814 13:d51127eed926 185 turnBuzzOffT.attach(turnBuzzOff, BUZZ_TIME);
jurica238814 13:d51127eed926 186 //wait_ms(100);
jurica238814 13:d51127eed926 187 //buzzer.write(0.0F);
jurica238814 1:5f34885f5cff 188 }
jurica238814 1:5f34885f5cff 189 }
jurica238814 2:5504b714c9ae 190 }
jurica238814 2:5504b714c9ae 191 }
jurica238814 2:5504b714c9ae 192
jurica238814 12:6b072c2a061c 193
jurica238814 7:89c9abaa257e 194 /* Call this function few minutes (TBD) after a last scanned advertisment */
jurica238814 7:89c9abaa257e 195 void changeSleepTime(){
jurica238814 7:89c9abaa257e 196 tempSleepTime = SLEEP_TIME;
jurica238814 7:89c9abaa257e 197 sleepChanger.detach();
jurica238814 7:89c9abaa257e 198 }
jurica238814 7:89c9abaa257e 199
jurica238814 12:6b072c2a061c 200
jurica238814 7:89c9abaa257e 201 /**
jurica238814 7:89c9abaa257e 202 * The function is called when ticker generates interrupt
jurica238814 7:89c9abaa257e 203 */
jurica238814 7:89c9abaa257e 204 void turnBuzzOff(void){
jurica238814 7:89c9abaa257e 205 buzzer.write(0.0F);
jurica238814 7:89c9abaa257e 206 tempSleepTime = SHORT_SLEEP_TIME;
jurica238814 7:89c9abaa257e 207 turnBuzzOffT.detach();
jurica238814 13:d51127eed926 208 //ble.gap().startScan(advertisementCallback);
jurica238814 13:d51127eed926 209 //WakeSleepT.attach(WakeMeUp, AWAKE_TIME);
jurica238814 13:d51127eed926 210 WakeSleepT.detach();
jurica238814 13:d51127eed926 211 radioState = SCANNING;
jurica238814 13:d51127eed926 212 //WakeMeUp();
jurica238814 13:d51127eed926 213
jurica238814 7:89c9abaa257e 214 sleepChanger.attach(changeSleepTime, SHORT_SLEEP_TIME_PERIOD);
jurica238814 13:d51127eed926 215
jurica238814 13:d51127eed926 216 radioState = OFF;
jurica238814 13:d51127eed926 217 sleepFlag = 1;
jurica238814 13:d51127eed926 218 ble.gap().stopAdvertising(); // Just in case
jurica238814 13:d51127eed926 219 ble.gap().stopScan();
jurica238814 13:d51127eed926 220 WakeSleepT.detach();
jurica238814 13:d51127eed926 221 //WakeSleepT.attach(goToSleep, FREE_TIME);
jurica238814 13:d51127eed926 222 WakeSleepT.attach(WakeMeUp, tempSleepTime);
jurica238814 13:d51127eed926 223 #if DEBUG
jurica238814 13:d51127eed926 224 advLED = 1;
jurica238814 13:d51127eed926 225 scanLED = 1;
jurica238814 13:d51127eed926 226 #endif
jurica238814 7:89c9abaa257e 227 }
jurica238814 7:89c9abaa257e 228
jurica238814 10:fd91664032d8 229 void startAdvertising(){
jurica238814 10:fd91664032d8 230 ble.gap().startAdvertising();
jurica238814 10:fd91664032d8 231 #if DEBUG
jurica238814 10:fd91664032d8 232 advLED = 0;
jurica238814 10:fd91664032d8 233 scanLED = 1;
jurica238814 10:fd91664032d8 234 #endif
jurica238814 10:fd91664032d8 235 WakeSleepT.detach();
jurica238814 10:fd91664032d8 236 WakeSleepT.attach(WakeMeUp, ADV_TIMER_TIME); // Call the wakeMeUp function
jurica238814 10:fd91664032d8 237 }
jurica238814 10:fd91664032d8 238
jurica238814 10:fd91664032d8 239 void startScanning(){
jurica238814 10:fd91664032d8 240 ble.gap().stopAdvertising();
jurica238814 10:fd91664032d8 241 ble.gap().setScanParams(SCAN_INTERVAL, SCAN_WINDOW);
jurica238814 10:fd91664032d8 242 ble.gap().setScanTimeout(SCAN_TIMER_TIME);
jurica238814 2:5504b714c9ae 243 ble.gap().startScan(advertisementCallback);
jurica238814 10:fd91664032d8 244 #if DEBUG
jurica238814 10:fd91664032d8 245 advLED = 1;
jurica238814 10:fd91664032d8 246 scanLED = 0;
jurica238814 10:fd91664032d8 247 #endif
jurica238814 2:5504b714c9ae 248 WakeSleepT.detach();
jurica238814 10:fd91664032d8 249 WakeSleepT.attach(WakeMeUp, SCAN_TIMER_TIME);
jurica238814 10:fd91664032d8 250 }
jurica238814 10:fd91664032d8 251
jurica238814 10:fd91664032d8 252 void WakeMeUp(){
jurica238814 8:570eb66d50b5 253 sleepFlag = 0;
jurica238814 10:fd91664032d8 254 switch(radioState){
jurica238814 10:fd91664032d8 255 case OFF:{
jurica238814 10:fd91664032d8 256 startAdvertising();
jurica238814 13:d51127eed926 257 radioState = ADVERTISING;
jurica238814 10:fd91664032d8 258 break;
jurica238814 10:fd91664032d8 259 }
jurica238814 10:fd91664032d8 260 case ADVERTISING:{
jurica238814 10:fd91664032d8 261 startScanning();
jurica238814 13:d51127eed926 262 radioState = SCANNING;
jurica238814 10:fd91664032d8 263 break;
jurica238814 10:fd91664032d8 264 }
jurica238814 10:fd91664032d8 265 case SCANNING:{
jurica238814 10:fd91664032d8 266 radioState = OFF;
jurica238814 13:d51127eed926 267 sleepFlag = 1;
jurica238814 10:fd91664032d8 268 ble.gap().stopAdvertising(); // Just in case
jurica238814 10:fd91664032d8 269 ble.gap().stopScan();
jurica238814 10:fd91664032d8 270 WakeSleepT.detach();
jurica238814 10:fd91664032d8 271 WakeSleepT.attach(goToSleep, FREE_TIME);
jurica238814 10:fd91664032d8 272 #if DEBUG
jurica238814 10:fd91664032d8 273 advLED = 1;
jurica238814 10:fd91664032d8 274 scanLED = 1;
jurica238814 10:fd91664032d8 275 #endif
jurica238814 10:fd91664032d8 276 break;
jurica238814 10:fd91664032d8 277 }
jurica238814 10:fd91664032d8 278 default: return;
jurica238814 10:fd91664032d8 279 }
jurica238814 2:5504b714c9ae 280 }
jurica238814 2:5504b714c9ae 281
jurica238814 2:5504b714c9ae 282 void goToSleep(){
jurica238814 2:5504b714c9ae 283 WakeSleepT.detach();
jurica238814 7:89c9abaa257e 284 WakeSleepT.attach(WakeMeUp, tempSleepTime);
jurica238814 2:5504b714c9ae 285 ble.gap().stopAdvertising();
jurica238814 2:5504b714c9ae 286 ble.gap().stopScan();
jurica238814 8:570eb66d50b5 287 sleepFlag = 1;
jurica238814 1:5f34885f5cff 288 }
jurica238814 1:5f34885f5cff 289
jurica238814 6:d14e3df498f4 290 void buzz(void){
jurica238814 6:d14e3df498f4 291 buzzer.write(0.5f);
jurica238814 6:d14e3df498f4 292 wait_ms(100);
jurica238814 6:d14e3df498f4 293 buzzer.write(0.0f);
jurica238814 9:2ab2be19add9 294 sleepFlag = 0;
jurica238814 1:5f34885f5cff 295 }
jurica238814 1:5f34885f5cff 296
jurica238814 6:d14e3df498f4 297 void pulse_handler(void){
jurica238814 10:fd91664032d8 298 #if DEBUG_ACC
jurica238814 6:d14e3df498f4 299 int_led = !int_led;
jurica238814 6:d14e3df498f4 300 #endif
jurica238814 6:d14e3df498f4 301 i2c_power.write(1.0F);
jurica238814 6:d14e3df498f4 302 buzzer_flag = 1;
jurica238814 8:570eb66d50b5 303 // Be awake some time
jurica238814 9:2ab2be19add9 304 //WakeSleepT.detach();
jurica238814 9:2ab2be19add9 305 //WakeSleepT.attach(goToSleep, AWAKE_TIME);
jurica238814 6:d14e3df498f4 306 }
jurica238814 6:d14e3df498f4 307
jurica238814 10:fd91664032d8 308 int main(void){
jurica238814 10:fd91664032d8 309 #if DEBUG
jurica238814 10:fd91664032d8 310 awake = 1;
jurica238814 10:fd91664032d8 311 advLED = 1;
jurica238814 10:fd91664032d8 312 scanLED = 1;
jurica238814 10:fd91664032d8 313 #endif
jurica238814 2:5504b714c9ae 314 WakeSleepT.attach(goToSleep, AWAKE_TIME);
jurica238814 0:f8c1e0b2d473 315 ble.init(bleInitComplete);
jurica238814 0:f8c1e0b2d473 316 ble.gap().setTxPower(txPower);
jurica238814 0:f8c1e0b2d473 317 GapAdvertisingData postavke = GapAdvertisingData();
jurica238814 0:f8c1e0b2d473 318
jurica238814 7:89c9abaa257e 319 buzzer.period(0.0009F);
jurica238814 1:5f34885f5cff 320 buzzer.write(0.0F);
jurica238814 6:d14e3df498f4 321 gyro_power.period(0.01F);
jurica238814 6:d14e3df498f4 322 gyro_power.write(1.0F);
jurica238814 6:d14e3df498f4 323 i2c_power.period(0.01F);
jurica238814 6:d14e3df498f4 324 i2c_power.write(1.0F);
jurica238814 6:d14e3df498f4 325 wait_ms(1000);
jurica238814 6:d14e3df498f4 326
jurica238814 6:d14e3df498f4 327 /* Pulse interrupt detection */
jurica238814 6:d14e3df498f4 328 acc.set_register((char)CTRL_REG_4, (char) 0x04); // INT_EN_FF_MT Freefall/motion interrupt enabled
jurica238814 6:d14e3df498f4 329 wait_ms(1);
jurica238814 6:d14e3df498f4 330 acc.set_register((char)FF_MT_CFG, (char) 0b01011000); //ELE, Motion Flag ON, YEFE, X Event Flag Enable
jurica238814 6:d14e3df498f4 331 wait_ms(1);
jurica238814 6:d14e3df498f4 332 acc.set_register((char)CTRL_REG_5, (char) 0x00); // INT_EN_FF_MT interrupt is router t0 INT2
jurica238814 6:d14e3df498f4 333 wait_ms(1);
jurica238814 6:d14e3df498f4 334 acc.set_register((char)FF_COUNT, (char) 0x08); // Set Counter degister value (10ms)
jurica238814 6:d14e3df498f4 335 wait_ms(1);
jurica238814 6:d14e3df498f4 336 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 337 wait_ms(1);
jurica238814 6:d14e3df498f4 338
jurica238814 6:d14e3df498f4 339 /* Setup for the interrupt handler */
jurica238814 13:d51127eed926 340 gyro_pulse.rise(&pulse_handler); // -------------------------------------
jurica238814 6:d14e3df498f4 341 acc.set_register((char)CTRL_REG_1, (char) 0x01); // Flow data rate and Active mode
jurica238814 6:d14e3df498f4 342 wait(1);
jurica238814 0:f8c1e0b2d473 343
jurica238814 2:5504b714c9ae 344 __enable_irq();
jurica238814 2:5504b714c9ae 345
jurica238814 0:f8c1e0b2d473 346 /* SpinWait for initialization to complete. This is necessary because the
jurica238814 0:f8c1e0b2d473 347 * BLE object is used in the main loop below. */
jurica238814 3:2a4ac5b87046 348 while (ble.hasInitialized() == false){
jurica238814 3:2a4ac5b87046 349 /* spin loop */
jurica238814 3:2a4ac5b87046 350 }
jurica238814 9:2ab2be19add9 351
jurica238814 1:5f34885f5cff 352 while(true){
jurica238814 9:2ab2be19add9 353 if(sleepFlag){
jurica238814 9:2ab2be19add9 354 if(!awake) awake = 1;
jurica238814 9:2ab2be19add9 355 __WFI();
jurica238814 9:2ab2be19add9 356 }
jurica238814 9:2ab2be19add9 357 else{
jurica238814 9:2ab2be19add9 358 if(awake) awake = 0;
jurica238814 9:2ab2be19add9 359 ble.waitForEvent();
jurica238814 9:2ab2be19add9 360 }
jurica238814 0:f8c1e0b2d473 361 }
jurica238814 0:f8c1e0b2d473 362 }