Geo beacon for VF.
Dependencies: MMA8452 aconno_bsp adc52832_common
main.cpp@35:7917a7f951c7, 2018-01-18 (annotated)
- Committer:
- jurica238814
- Date:
- Thu Jan 18 09:57:45 2018 +0000
- Revision:
- 35:7917a7f951c7
- Parent:
- 34:9856c51ec646
- Child:
- 36:36d44b58980a
Buzzer sequence added.
Who changed what in which revision?
User | Revision | Line number | New 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 | 16:a338d2417fd5 | 14 | #include "AckService.h" |
jurica238814 | 16:a338d2417fd5 | 15 | #include "nrf52_uart.h" |
jurica238814 | 22:8d106fd5fa84 | 16 | #include "nrf52_digital.h" |
Dautor | 26:148aa2e2460c | 17 | #include "acn_nrf52_pwm.h" |
jurica238814 | 1:5f34885f5cff | 18 | |
jurica238814 | 34:9856c51ec646 | 19 | #define DEBUG (0) |
jurica238814 | 27:2c67f07590fd | 20 | #define DEBUG_ACC (0) |
Dautor | 26:148aa2e2460c | 21 | #define DEBUG_PRINT_UART (0) |
jurica238814 | 17:51a5456a46cd | 22 | #define DEBUG_MAC (0) |
jurica238814 | 34:9856c51ec646 | 23 | #define DEBUG_CONNECTION (0) |
Dautor | 26:148aa2e2460c | 24 | #define DEBUG_WAKEUP_BUZZER (0) |
Dautor | 26:148aa2e2460c | 25 | |
jurica238814 | 27:2c67f07590fd | 26 | #define USE_ACC (0) |
jurica238814 | 8:570eb66d50b5 | 27 | |
jurica238814 | 34:9856c51ec646 | 28 | #define SLEEP_TIME_S (4.00) /* Sleep time (in s) */ |
jurica238814 | 27:2c67f07590fd | 29 | #define ADV_TIMER_TIME_S (1.00) /* Advertising time (in s) */ |
Dautor | 26:148aa2e2460c | 30 | #define FREE_TIME_S (0.1) /* Time between end of a scanning and sleep mode */ |
jurica238814 | 34:9856c51ec646 | 31 | #define AWAKE_TIME_S (ADV_TIMER_TIME_S+FREE_TIME_S) /* Was 0.15 */ |
Dautor | 26:148aa2e2460c | 32 | #define SHORT_SLEEP_TIME_S (0.5) /* Shorter sleep time (s) */ |
Dautor | 26:148aa2e2460c | 33 | #define SHORT_SLEEP_TIME_PERIOD_S (10) /* Time after a last scanned advertisment. In the period, sleep time is SHORT_SLEEP_TIME */ |
jurica238814 | 34:9856c51ec646 | 34 | #define BUZZER_FREQUENCY_Hz (4000) |
jurica238814 | 12:6b072c2a061c | 35 | |
Dautor | 26:148aa2e2460c | 36 | #define BUZZ_TIME_S (1) /* Buzz time in s */ |
jurica238814 | 25:8ac3ff431ab1 | 37 | #define ADV_INTERVAL (100) /* Advertising interval (in ms) */ |
jurica238814 | 34:9856c51ec646 | 38 | #define UUID_SIZE_B (16) |
jurica238814 | 1:5f34885f5cff | 39 | |
jurica238814 | 6:d14e3df498f4 | 40 | /* Static constants for the accelerometer */ |
jurica238814 | 13:d51127eed926 | 41 | #define WHO_AM_I 0x0D /* Type 'read' : This should return the device id of 0x2A */ |
jurica238814 | 13:d51127eed926 | 42 | #define OUT_Z_MSB 0x05 /* Type 'read' : z axis - 8 most significatn bit of a 12 bit sample */ |
jurica238814 | 6:d14e3df498f4 | 43 | #define I2C_DATA (p29) |
jurica238814 | 6:d14e3df498f4 | 44 | #define I2C_CLK (p2) |
jurica238814 | 6:d14e3df498f4 | 45 | #define INT2_PIN (p4) |
jurica238814 | 10:fd91664032d8 | 46 | #define BUZZER (p31) |
jurica238814 | 31:caef580f5943 | 47 | |
Dautor | 26:148aa2e2460c | 48 | #if DEBUG_PRINT_UART |
jurica238814 | 27:2c67f07590fd | 49 | #include "nrf52_uart.h" |
jurica238814 | 27:2c67f07590fd | 50 | NRF52_UART uart(p25, p26, Baud9600); |
jurica238814 | 27:2c67f07590fd | 51 | char buffer[255]; |
jurica238814 | 27:2c67f07590fd | 52 | #define SEND(...) {uint8_t len = sprintf(buffer, __VA_ARGS__); uart.send(buffer, len);} |
Dautor | 26:148aa2e2460c | 53 | #else |
jurica238814 | 27:2c67f07590fd | 54 | #define SEND(...) |
jurica238814 | 16:a338d2417fd5 | 55 | #endif |
jurica238814 | 16:a338d2417fd5 | 56 | |
jurica238814 | 16:a338d2417fd5 | 57 | |
jurica238814 | 27:2c67f07590fd | 58 | uint8_t sleepFlag = true; |
jurica238814 | 34:9856c51ec646 | 59 | uint8_t UUID[UUID_SIZE_B] = {0xE1, 0x61, 0x35, 0xBA, 0xC0, 0xEC, 0x47, 0x2A, 0x98, 0x00, 0xAF, 0x18, 0x43, 0xFF, 0x05, 0x00}; |
jurica238814 | 25:8ac3ff431ab1 | 60 | uint8_t startBuzz[2] = {0xBA, 0xBE}; |
jurica238814 | 25:8ac3ff431ab1 | 61 | uint8_t stopBuzz[2] = {0xDE, 0xAD}; |
jurica238814 | 23:729717272b31 | 62 | uint8_t myMacAddress[6] = {}; |
jurica238814 | 6:d14e3df498f4 | 63 | uint8_t buzzer_flag = 0; |
jurica238814 | 0:f8c1e0b2d473 | 64 | |
jurica238814 | 14:d506c0679c0b | 65 | enum RadioState{ |
jurica238814 | 10:fd91664032d8 | 66 | OFF, |
jurica238814 | 10:fd91664032d8 | 67 | ADVERTISING, |
Dautor | 26:148aa2e2460c | 68 | SCANNING, |
Dautor | 26:148aa2e2460c | 69 | }; |
jurica238814 | 34:9856c51ec646 | 70 | |
jurica238814 | 14:d506c0679c0b | 71 | enum RadioState radioState = OFF; |
jurica238814 | 0:f8c1e0b2d473 | 72 | |
jurica238814 | 14:d506c0679c0b | 73 | void GoToSleep(); |
jurica238814 | 14:d506c0679c0b | 74 | void StartAdvertising(); |
jurica238814 | 10:fd91664032d8 | 75 | void startScanning(); |
jurica238814 | 10:fd91664032d8 | 76 | void WakeMeUp(); |
jurica238814 | 0:f8c1e0b2d473 | 77 | |
jurica238814 | 2:5504b714c9ae | 78 | Ticker WakeSleepT; |
jurica238814 | 7:89c9abaa257e | 79 | Ticker sleepChanger; |
jurica238814 | 35:7917a7f951c7 | 80 | Ticker toggleBuzzer; |
jurica238814 | 27:2c67f07590fd | 81 | NRF52_PWM buzzer(NRF_PWM2); |
jurica238814 | 27:2c67f07590fd | 82 | |
jurica238814 | 19:abf14a5ada93 | 83 | #if USE_ACC |
jurica238814 | 19:abf14a5ada93 | 84 | DigitalOut accPower(p7); |
jurica238814 | 19:abf14a5ada93 | 85 | DigitalOut i2cPower(p5); |
jurica238814 | 19:abf14a5ada93 | 86 | InterruptIn accPulse(INT2_PIN); |
jurica238814 | 21:10c3b8176be0 | 87 | Acc_MMA8452 acc(I2C_DATA, I2C_CLK, MMA8452_ADDRESS); |
jurica238814 | 27:2c67f07590fd | 88 | |
jurica238814 | 19:abf14a5ada93 | 89 | #endif |
jurica238814 | 1:5f34885f5cff | 90 | BLE &ble = BLE::Instance(); |
jurica238814 | 16:a338d2417fd5 | 91 | ACKService<4> *ackServicePtr; |
jurica238814 | 10:fd91664032d8 | 92 | |
jurica238814 | 22:8d106fd5fa84 | 93 | #if DEBUG || DEBUG_MAC || DEBUG_CONNECTION |
Dautor | 26:148aa2e2460c | 94 | NRF52_DigitalOut advLED(p22); // Red |
Dautor | 26:148aa2e2460c | 95 | NRF52_DigitalOut scanLED(p23); // Blue |
Dautor | 26:148aa2e2460c | 96 | NRF52_DigitalOut connectedLED(p24); // Green |
jurica238814 | 10:fd91664032d8 | 97 | #endif |
jurica238814 | 18:e844d3e6ab88 | 98 | |
jurica238814 | 10:fd91664032d8 | 99 | #if DEBUG_ACC |
Dautor | 26:148aa2e2460c | 100 | NRF52_DigitalOut int_led(p22); |
Dautor | 26:148aa2e2460c | 101 | NRF52_DigitalOut act_led(p22); |
jurica238814 | 8:570eb66d50b5 | 102 | #endif |
jurica238814 | 8:570eb66d50b5 | 103 | |
jurica238814 | 35:7917a7f951c7 | 104 | void buzzerToggle(){ |
jurica238814 | 35:7917a7f951c7 | 105 | static uint8_t initState = 1; |
jurica238814 | 35:7917a7f951c7 | 106 | if(initState){ |
jurica238814 | 35:7917a7f951c7 | 107 | // initial state is off |
jurica238814 | 35:7917a7f951c7 | 108 | buzzer.enable(BUZZER_FREQUENCY_Hz); |
jurica238814 | 35:7917a7f951c7 | 109 | buzzer.enableChannel(0, BUZZER); |
jurica238814 | 35:7917a7f951c7 | 110 | buzzer.setDuty(0,0.5f); |
jurica238814 | 35:7917a7f951c7 | 111 | initState = 0; |
jurica238814 | 35:7917a7f951c7 | 112 | } |
jurica238814 | 35:7917a7f951c7 | 113 | else{ |
jurica238814 | 35:7917a7f951c7 | 114 | buzzer.enable(0); |
jurica238814 | 35:7917a7f951c7 | 115 | buzzer.setDuty(0, 0); |
jurica238814 | 35:7917a7f951c7 | 116 | buzzer.disable(); |
jurica238814 | 35:7917a7f951c7 | 117 | initState = 1; |
jurica238814 | 35:7917a7f951c7 | 118 | } |
jurica238814 | 35:7917a7f951c7 | 119 | } |
Dautor | 26:148aa2e2460c | 120 | |
Dautor | 26:148aa2e2460c | 121 | void buzzerStart(){ |
jurica238814 | 27:2c67f07590fd | 122 | buzzer.enable(BUZZER_FREQUENCY_Hz); |
Dautor | 26:148aa2e2460c | 123 | buzzer.enableChannel(0, BUZZER); |
jurica238814 | 27:2c67f07590fd | 124 | buzzer.setDuty(0,0.5f); |
Dautor | 26:148aa2e2460c | 125 | } |
Dautor | 26:148aa2e2460c | 126 | void buzzerStop(){ |
jurica238814 | 27:2c67f07590fd | 127 | buzzer.enable(0); |
jurica238814 | 27:2c67f07590fd | 128 | buzzer.setDuty(0, 0); |
Dautor | 26:148aa2e2460c | 129 | buzzer.disable(); |
jurica238814 | 22:8d106fd5fa84 | 130 | } |
jurica238814 | 16:a338d2417fd5 | 131 | |
jurica238814 | 21:10c3b8176be0 | 132 | void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params){ |
jurica238814 | 17:51a5456a46cd | 133 | #if DEBUG_CONNECTION |
jurica238814 | 17:51a5456a46cd | 134 | scanLED = !scanLED; // Blue |
jurica238814 | 17:51a5456a46cd | 135 | wait_ms(100); |
jurica238814 | 17:51a5456a46cd | 136 | scanLED = !scanLED; // Blue |
jurica238814 | 17:51a5456a46cd | 137 | wait_ms(100); |
jurica238814 | 17:51a5456a46cd | 138 | scanLED = !scanLED; // Blue |
jurica238814 | 17:51a5456a46cd | 139 | wait_ms(100); |
jurica238814 | 17:51a5456a46cd | 140 | scanLED = !scanLED; // Blue |
jurica238814 | 17:51a5456a46cd | 141 | wait_ms(100); |
jurica238814 | 17:51a5456a46cd | 142 | scanLED = !scanLED; // Blue |
jurica238814 | 17:51a5456a46cd | 143 | wait_ms(100); |
jurica238814 | 17:51a5456a46cd | 144 | scanLED = !scanLED; // Blue |
jurica238814 | 17:51a5456a46cd | 145 | wait_ms(100); |
jurica238814 | 17:51a5456a46cd | 146 | scanLED = 1; // Blue |
jurica238814 | 17:51a5456a46cd | 147 | #endif |
jurica238814 | 17:51a5456a46cd | 148 | WakeSleepT.detach(); |
jurica238814 | 17:51a5456a46cd | 149 | sleepFlag = false; |
jurica238814 | 16:a338d2417fd5 | 150 | } |
jurica238814 | 16:a338d2417fd5 | 151 | |
jurica238814 | 16:a338d2417fd5 | 152 | |
jurica238814 | 0:f8c1e0b2d473 | 153 | /* Restart Advertising on disconnection*/ |
jurica238814 | 0:f8c1e0b2d473 | 154 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){ |
Dautor | 26:148aa2e2460c | 155 | buzzerStop(); |
jurica238814 | 17:51a5456a46cd | 156 | #if DEBUG_CONNECTION |
jurica238814 | 17:51a5456a46cd | 157 | advLED = !advLED; // RED |
jurica238814 | 17:51a5456a46cd | 158 | wait_ms(100); |
jurica238814 | 17:51a5456a46cd | 159 | advLED = !advLED; |
jurica238814 | 17:51a5456a46cd | 160 | wait_ms(100); |
jurica238814 | 17:51a5456a46cd | 161 | advLED = !advLED; |
jurica238814 | 17:51a5456a46cd | 162 | wait_ms(100); |
jurica238814 | 17:51a5456a46cd | 163 | advLED = !advLED; |
jurica238814 | 17:51a5456a46cd | 164 | wait_ms(100); |
jurica238814 | 17:51a5456a46cd | 165 | advLED = 1; |
jurica238814 | 17:51a5456a46cd | 166 | wait_ms(100); |
jurica238814 | 17:51a5456a46cd | 167 | advLED = 1; |
jurica238814 | 17:51a5456a46cd | 168 | #endif |
jurica238814 | 17:51a5456a46cd | 169 | WakeSleepT.attach(WakeMeUp, FREE_TIME_S); |
jurica238814 | 17:51a5456a46cd | 170 | sleepFlag = true; |
jurica238814 | 18:e844d3e6ab88 | 171 | |
jurica238814 | 0:f8c1e0b2d473 | 172 | } |
jurica238814 | 0:f8c1e0b2d473 | 173 | |
jurica238814 | 16:a338d2417fd5 | 174 | void onDataWrittenCallback(const GattWriteCallbackParams *params) { |
jurica238814 | 25:8ac3ff431ab1 | 175 | if(params->handle == ackServicePtr->getACKCharacteristicHandle()){ |
jurica238814 | 16:a338d2417fd5 | 176 | // Something is written into AckCharacteristic |
jurica238814 | 25:8ac3ff431ab1 | 177 | if(params->data[0] == startBuzz[0]){ |
jurica238814 | 25:8ac3ff431ab1 | 178 | if(params->data[1] == startBuzz[1]){ |
jurica238814 | 17:51a5456a46cd | 179 | #if DEBUG_CONNECTION |
jurica238814 | 17:51a5456a46cd | 180 | connectedLED = !connectedLED; // BLUE |
jurica238814 | 17:51a5456a46cd | 181 | wait_ms(100); |
jurica238814 | 17:51a5456a46cd | 182 | connectedLED = !connectedLED; |
jurica238814 | 17:51a5456a46cd | 183 | wait_ms(100); |
jurica238814 | 17:51a5456a46cd | 184 | connectedLED = !connectedLED; |
jurica238814 | 17:51a5456a46cd | 185 | wait_ms(100); |
jurica238814 | 17:51a5456a46cd | 186 | connectedLED = !connectedLED; |
jurica238814 | 17:51a5456a46cd | 187 | wait_ms(100); |
jurica238814 | 17:51a5456a46cd | 188 | connectedLED = !connectedLED; |
jurica238814 | 17:51a5456a46cd | 189 | wait_ms(100); |
jurica238814 | 17:51a5456a46cd | 190 | connectedLED = 1; |
jurica238814 | 17:51a5456a46cd | 191 | wait_ms(100); |
jurica238814 | 17:51a5456a46cd | 192 | #endif |
jurica238814 | 35:7917a7f951c7 | 193 | //buzzerStart(); |
jurica238814 | 35:7917a7f951c7 | 194 | toggleBuzzer.attach(buzzerToggle, 0.250); |
jurica238814 | 16:a338d2417fd5 | 195 | return; |
jurica238814 | 16:a338d2417fd5 | 196 | } |
jurica238814 | 25:8ac3ff431ab1 | 197 | } |
Dautor | 26:148aa2e2460c | 198 | else if(params->data[0] == stopBuzz[0]){ |
Dautor | 26:148aa2e2460c | 199 | if(params->data[1] == stopBuzz[1]){ |
jurica238814 | 35:7917a7f951c7 | 200 | toggleBuzzer.detach(); |
jurica238814 | 27:2c67f07590fd | 201 | buzzerStop(); |
Dautor | 26:148aa2e2460c | 202 | WakeSleepT.detach(); |
Dautor | 26:148aa2e2460c | 203 | WakeSleepT.attach(WakeMeUp, FREE_TIME_S); |
Dautor | 26:148aa2e2460c | 204 | ble.disconnect(Gap::LOCAL_HOST_TERMINATED_CONNECTION); |
jurica238814 | 25:8ac3ff431ab1 | 205 | } |
jurica238814 | 25:8ac3ff431ab1 | 206 | } |
jurica238814 | 16:a338d2417fd5 | 207 | } |
jurica238814 | 16:a338d2417fd5 | 208 | else{ |
jurica238814 | 25:8ac3ff431ab1 | 209 | // Execute this for wrong data written into characteristic |
jurica238814 | 25:8ac3ff431ab1 | 210 | return; |
jurica238814 | 16:a338d2417fd5 | 211 | } |
jurica238814 | 16:a338d2417fd5 | 212 | } |
jurica238814 | 0:f8c1e0b2d473 | 213 | |
jurica238814 | 0:f8c1e0b2d473 | 214 | /** |
jurica238814 | 0:f8c1e0b2d473 | 215 | * This function is called when the ble initialization process has failed |
jurica238814 | 0:f8c1e0b2d473 | 216 | */ |
jurica238814 | 0:f8c1e0b2d473 | 217 | void onBleInitError(BLE &ble, ble_error_t error){ |
jurica238814 | 0:f8c1e0b2d473 | 218 | /* Avoid compiler warnings */ |
jurica238814 | 0:f8c1e0b2d473 | 219 | (void) ble; |
jurica238814 | 0:f8c1e0b2d473 | 220 | (void) error; |
jurica238814 | 0:f8c1e0b2d473 | 221 | /* Initialization error handling should go here */ |
jurica238814 | 0:f8c1e0b2d473 | 222 | } |
jurica238814 | 0:f8c1e0b2d473 | 223 | |
jurica238814 | 0:f8c1e0b2d473 | 224 | /** |
jurica238814 | 0:f8c1e0b2d473 | 225 | * Callback triggered when the ble initialization process has finished |
jurica238814 | 0:f8c1e0b2d473 | 226 | */ |
jurica238814 | 0:f8c1e0b2d473 | 227 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){ |
jurica238814 | 0:f8c1e0b2d473 | 228 | BLE& ble = params->ble; |
jurica238814 | 0:f8c1e0b2d473 | 229 | ble_error_t error = params->error; |
jurica238814 | 0:f8c1e0b2d473 | 230 | |
jurica238814 | 0:f8c1e0b2d473 | 231 | if (error != BLE_ERROR_NONE) { |
jurica238814 | 0:f8c1e0b2d473 | 232 | /* In case of error, forward the error handling to onBleInitError */ |
jurica238814 | 0:f8c1e0b2d473 | 233 | onBleInitError(ble, error); |
jurica238814 | 0:f8c1e0b2d473 | 234 | return; |
jurica238814 | 0:f8c1e0b2d473 | 235 | } |
jurica238814 | 0:f8c1e0b2d473 | 236 | |
jurica238814 | 0:f8c1e0b2d473 | 237 | /* Ensure that it is the default instance of BLE */ |
jurica238814 | 0:f8c1e0b2d473 | 238 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
jurica238814 | 0:f8c1e0b2d473 | 239 | return; |
jurica238814 | 0:f8c1e0b2d473 | 240 | } |
jurica238814 | 16:a338d2417fd5 | 241 | |
jurica238814 | 16:a338d2417fd5 | 242 | uint8_t init_values[4] = {0,0,0,0}; |
jurica238814 | 1:5f34885f5cff | 243 | /* Get my MAC address */ |
jurica238814 | 1:5f34885f5cff | 244 | BLEProtocol::AddressType_t temp_address_type; |
jurica238814 | 14:d506c0679c0b | 245 | ble.gap().getAddress(&temp_address_type, myMacAddress); |
jurica238814 | 16:a338d2417fd5 | 246 | ackServicePtr = new ACKService<4>(ble, init_values); |
jurica238814 | 16:a338d2417fd5 | 247 | ackServicePtr->updateMacAddress(myMacAddress); // Update MAC address |
jurica238814 | 21:10c3b8176be0 | 248 | |
jurica238814 | 16:a338d2417fd5 | 249 | ble.gap().onDisconnection(disconnectionCallback); |
jurica238814 | 34:9856c51ec646 | 250 | ble.gap().onConnection(onConnectionCallback); |
jurica238814 | 17:51a5456a46cd | 251 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
jurica238814 | 1:5f34885f5cff | 252 | |
jurica238814 | 34:9856c51ec646 | 253 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS , (uint8_t*)UUID, sizeof(UUID)); |
jurica238814 | 10:fd91664032d8 | 254 | ble.gap().setAdvertisingInterval(ADV_INTERVAL); // --> Has to be at least 100ms! |
jurica238814 | 0:f8c1e0b2d473 | 255 | } |
jurica238814 | 0:f8c1e0b2d473 | 256 | |
jurica238814 | 2:5504b714c9ae | 257 | |
jurica238814 | 12:6b072c2a061c | 258 | |
jurica238814 | 34:9856c51ec646 | 259 | void startAdvertising(){ |
jurica238814 | 34:9856c51ec646 | 260 | ble.gap().startAdvertising(); |
jurica238814 | 10:fd91664032d8 | 261 | #if DEBUG |
jurica238814 | 34:9856c51ec646 | 262 | advLED = 0; |
jurica238814 | 34:9856c51ec646 | 263 | scanLED = 1; |
jurica238814 | 10:fd91664032d8 | 264 | #endif |
jurica238814 | 2:5504b714c9ae | 265 | WakeSleepT.detach(); |
jurica238814 | 34:9856c51ec646 | 266 | WakeSleepT.attach(WakeMeUp, ADV_TIMER_TIME_S); // Call the wakeMeUp function |
jurica238814 | 10:fd91664032d8 | 267 | } |
jurica238814 | 10:fd91664032d8 | 268 | |
jurica238814 | 10:fd91664032d8 | 269 | void WakeMeUp(){ |
jurica238814 | 34:9856c51ec646 | 270 | sleepFlag = false; |
jurica238814 | 10:fd91664032d8 | 271 | switch(radioState){ |
jurica238814 | 10:fd91664032d8 | 272 | case OFF:{ |
jurica238814 | 34:9856c51ec646 | 273 | radioState = ADVERTISING; |
jurica238814 | 10:fd91664032d8 | 274 | startAdvertising(); |
jurica238814 | 10:fd91664032d8 | 275 | break; |
jurica238814 | 10:fd91664032d8 | 276 | } |
jurica238814 | 10:fd91664032d8 | 277 | case ADVERTISING:{ |
jurica238814 | 10:fd91664032d8 | 278 | radioState = OFF; |
jurica238814 | 10:fd91664032d8 | 279 | WakeSleepT.detach(); |
jurica238814 | 34:9856c51ec646 | 280 | WakeSleepT.attach(GoToSleep, FREE_TIME_S); |
jurica238814 | 10:fd91664032d8 | 281 | break; |
jurica238814 | 10:fd91664032d8 | 282 | } |
jurica238814 | 10:fd91664032d8 | 283 | default: return; |
jurica238814 | 10:fd91664032d8 | 284 | } |
jurica238814 | 2:5504b714c9ae | 285 | } |
jurica238814 | 2:5504b714c9ae | 286 | |
jurica238814 | 14:d506c0679c0b | 287 | void GoToSleep(){ |
jurica238814 | 2:5504b714c9ae | 288 | WakeSleepT.detach(); |
jurica238814 | 34:9856c51ec646 | 289 | WakeSleepT.attach(WakeMeUp, SLEEP_TIME_S); |
jurica238814 | 2:5504b714c9ae | 290 | ble.gap().stopAdvertising(); |
jurica238814 | 34:9856c51ec646 | 291 | sleepFlag = true; |
jurica238814 | 16:a338d2417fd5 | 292 | #if DEBUG |
jurica238814 | 16:a338d2417fd5 | 293 | advLED = 1; |
jurica238814 | 16:a338d2417fd5 | 294 | scanLED = 1; |
jurica238814 | 16:a338d2417fd5 | 295 | #endif |
jurica238814 | 1:5f34885f5cff | 296 | } |
jurica238814 | 1:5f34885f5cff | 297 | |
jurica238814 | 19:abf14a5ada93 | 298 | #if USE_ACC |
jurica238814 | 27:2c67f07590fd | 299 | void pulse_handler(){ |
jurica238814 | 27:2c67f07590fd | 300 | #if DEBUG_WAKEUP_BUZZER |
Dautor | 26:148aa2e2460c | 301 | buzzerStart(); |
jurica238814 | 27:2c67f07590fd | 302 | wait_ms(50); |
Dautor | 26:148aa2e2460c | 303 | buzzerStop(); |
jurica238814 | 27:2c67f07590fd | 304 | #endif |
jurica238814 | 27:2c67f07590fd | 305 | #if DEBUG_ACC |
jurica238814 | 27:2c67f07590fd | 306 | int_led = !int_led; |
jurica238814 | 27:2c67f07590fd | 307 | #endif |
jurica238814 | 19:abf14a5ada93 | 308 | } |
jurica238814 | 19:abf14a5ada93 | 309 | #endif |
jurica238814 | 6:d14e3df498f4 | 310 | |
Dautor | 26:148aa2e2460c | 311 | int main(){ |
jurica238814 | 18:e844d3e6ab88 | 312 | #if DEBUG || DEBUG_MAC |
jurica238814 | 10:fd91664032d8 | 313 | advLED = 1; |
jurica238814 | 10:fd91664032d8 | 314 | scanLED = 1; |
jurica238814 | 16:a338d2417fd5 | 315 | connectedLED = 1; |
jurica238814 | 10:fd91664032d8 | 316 | #endif |
jurica238814 | 14:d506c0679c0b | 317 | |
jurica238814 | 19:abf14a5ada93 | 318 | #if USE_ACC |
jurica238814 | 19:abf14a5ada93 | 319 | accPower = 1; |
jurica238814 | 19:abf14a5ada93 | 320 | i2cPower = 1; |
jurica238814 | 27:2c67f07590fd | 321 | #endif |
jurica238814 | 16:a338d2417fd5 | 322 | |
jurica238814 | 16:a338d2417fd5 | 323 | ble.init(bleInitComplete); |
jurica238814 | 19:abf14a5ada93 | 324 | /* SpinWait for initialization to complete. This is necessary because the BLE object is used in the main loop below. */ |
Dautor | 26:148aa2e2460c | 325 | while (ble.hasInitialized() == false){ /* spin loop */ } |
jurica238814 | 31:caef580f5943 | 326 | __enable_irq(); |
jurica238814 | 31:caef580f5943 | 327 | |
jurica238814 | 27:2c67f07590fd | 328 | buzzerStart(); |
jurica238814 | 27:2c67f07590fd | 329 | wait_ms(500); |
jurica238814 | 27:2c67f07590fd | 330 | buzzerStop(); |
jurica238814 | 34:9856c51ec646 | 331 | |
jurica238814 | 31:caef580f5943 | 332 | WakeSleepT.attach(GoToSleep, AWAKE_TIME_S); |
jurica238814 | 27:2c67f07590fd | 333 | |
jurica238814 | 27:2c67f07590fd | 334 | while(true){ |
jurica238814 | 27:2c67f07590fd | 335 | ble.waitForEvent(); |
jurica238814 | 0:f8c1e0b2d473 | 336 | } |
jurica238814 | 0:f8c1e0b2d473 | 337 | } |