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