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