Geo beacon for VF.

Dependencies:   MMA8452 aconno_bsp adc52832_common

Committer:
dbartolovic
Date:
Thu Jul 05 09:10:54 2018 +0000
Branch:
Lizzy_hardware
Revision:
41:7d8ce2d8d167
Parent:
38:12f6e20e66a2
Child:
42:bffc939efdf3
Added define for acnfreight

Who changed what in which revision?

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