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