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