Geo beacon for VF.

Dependencies:   MMA8452 aconno_bsp adc52832_common

Committer:
dbartolovic
Date:
Tue Sep 25 10:39:46 2018 +0000
Branch:
Lizzy_hardware
Revision:
42:bffc939efdf3
Parent:
41:7d8ce2d8d167
Added new advertisement format for freight

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
dbartolovic 42:bffc939efdf3 79 #if ACN_FREIGHT
dbartolovic 42:bffc939efdf3 80
dbartolovic 42:bffc939efdf3 81 /* Aconno ID universal to all projects */
dbartolovic 42:bffc939efdf3 82 #define ACONNO_ID (0x69)
dbartolovic 42:bffc939efdf3 83 /* Internal unique product identification. When doing new projects, just
dbartolovic 42:bffc939efdf3 84 * increment old project ID by one. */
dbartolovic 42:bffc939efdf3 85 #define PRODUCT_ID (0x03)
dbartolovic 42:bffc939efdf3 86 /* Version of the firmware. Start from zero and go upwards. */
dbartolovic 42:bffc939efdf3 87 #define VERSION (0x02)
dbartolovic 42:bffc939efdf3 88
dbartolovic 42:bffc939efdf3 89 /* Manufacturer that will sell the product. 0x0059 for general use case. */
dbartolovic 42:bffc939efdf3 90 #define MANUFACTURER_ID (0x0059)
dbartolovic 42:bffc939efdf3 91
dbartolovic 42:bffc939efdf3 92 struct __attribute__((packed, aligned(1))) AdvertisingFormat
dbartolovic 42:bffc939efdf3 93 {
dbartolovic 42:bffc939efdf3 94 uint16_t manufacturerID;
dbartolovic 42:bffc939efdf3 95 uint8_t aconnoID;
dbartolovic 42:bffc939efdf3 96 uint8_t productID;
dbartolovic 42:bffc939efdf3 97 uint8_t version;
dbartolovic 42:bffc939efdf3 98 };
dbartolovic 42:bffc939efdf3 99
dbartolovic 42:bffc939efdf3 100 #endif
dbartolovic 42:bffc939efdf3 101
dbartolovic 42:bffc939efdf3 102
jurica238814 27:2c67f07590fd 103 uint8_t sleepFlag = true;
jurica238814 34:9856c51ec646 104 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 105 uint8_t startBuzz[2] = {0xBA, 0xBE};
jurica238814 25:8ac3ff431ab1 106 uint8_t stopBuzz[2] = {0xDE, 0xAD};
jurica238814 23:729717272b31 107 uint8_t myMacAddress[6] = {};
jurica238814 6:d14e3df498f4 108 uint8_t buzzer_flag = 0;
jurica238814 0:f8c1e0b2d473 109
jurica238814 14:d506c0679c0b 110 enum RadioState{
jurica238814 10:fd91664032d8 111 OFF,
jurica238814 10:fd91664032d8 112 ADVERTISING,
Dautor 26:148aa2e2460c 113 SCANNING,
Dautor 26:148aa2e2460c 114 };
jurica238814 34:9856c51ec646 115
jurica238814 14:d506c0679c0b 116 enum RadioState radioState = OFF;
jurica238814 0:f8c1e0b2d473 117
jurica238814 14:d506c0679c0b 118 void GoToSleep();
jurica238814 14:d506c0679c0b 119 void StartAdvertising();
jurica238814 10:fd91664032d8 120 void startScanning();
jurica238814 10:fd91664032d8 121 void WakeMeUp();
jurica238814 0:f8c1e0b2d473 122
jurica238814 2:5504b714c9ae 123 Ticker WakeSleepT;
jurica238814 7:89c9abaa257e 124 Ticker sleepChanger;
jurica238814 35:7917a7f951c7 125 Ticker toggleBuzzer;
dbartolovic 41:7d8ce2d8d167 126 #if ACN_FREIGHT
dbartolovic 41:7d8ce2d8d167 127 Ticker toggleLed;
dbartolovic 41:7d8ce2d8d167 128 #endif
jurica238814 27:2c67f07590fd 129 NRF52_PWM buzzer(NRF_PWM2);
jurica238814 27:2c67f07590fd 130
jurica238814 19:abf14a5ada93 131 #if USE_ACC
dbartolovic 37:4b38af411b64 132 DigitalOut accPower(ACC_POWER);
dbartolovic 37:4b38af411b64 133 DigitalOut i2cPower(I2C_POWER);
jurica238814 19:abf14a5ada93 134 InterruptIn accPulse(INT2_PIN);
jurica238814 21:10c3b8176be0 135 Acc_MMA8452 acc(I2C_DATA, I2C_CLK, MMA8452_ADDRESS);
jurica238814 27:2c67f07590fd 136
jurica238814 19:abf14a5ada93 137 #endif
jurica238814 1:5f34885f5cff 138 BLE &ble = BLE::Instance();
jurica238814 16:a338d2417fd5 139 ACKService<4> *ackServicePtr;
jurica238814 10:fd91664032d8 140
jurica238814 22:8d106fd5fa84 141 #if DEBUG || DEBUG_MAC || DEBUG_CONNECTION
dbartolovic 37:4b38af411b64 142 NRF52_DigitalOut advLED(RED_LED); // Red
dbartolovic 37:4b38af411b64 143 NRF52_DigitalOut scanLED(BLUE_LED); // Blue
dbartolovic 37:4b38af411b64 144 NRF52_DigitalOut connectedLED(GREEN_LED); // Green
jurica238814 10:fd91664032d8 145 #endif
jurica238814 18:e844d3e6ab88 146
jurica238814 10:fd91664032d8 147 #if DEBUG_ACC
dbartolovic 37:4b38af411b64 148 NRF52_DigitalOut int_led(RED_LED);
dbartolovic 37:4b38af411b64 149 NRF52_DigitalOut act_led(RED_LED);
jurica238814 8:570eb66d50b5 150 #endif
jurica238814 8:570eb66d50b5 151
dbartolovic 41:7d8ce2d8d167 152 #if ACN_FREIGHT
dbartolovic 41:7d8ce2d8d167 153 NRF52_DigitalOut signal_led(RED_LED);
dbartolovic 41:7d8ce2d8d167 154 #endif
dbartolovic 41:7d8ce2d8d167 155
dbartolovic 41:7d8ce2d8d167 156 #if ACN_FREIGHT
dbartolovic 41:7d8ce2d8d167 157 void ledToggle(){
dbartolovic 41:7d8ce2d8d167 158 signal_led.toggle();
dbartolovic 41:7d8ce2d8d167 159 }
dbartolovic 41:7d8ce2d8d167 160 #endif
dbartolovic 41:7d8ce2d8d167 161
jurica238814 35:7917a7f951c7 162 void buzzerToggle(){
jurica238814 35:7917a7f951c7 163 static uint8_t initState = 1;
jurica238814 35:7917a7f951c7 164 if(initState){
jurica238814 35:7917a7f951c7 165 // initial state is off
jurica238814 35:7917a7f951c7 166 buzzer.enable(BUZZER_FREQUENCY_Hz);
jurica238814 35:7917a7f951c7 167 buzzer.enableChannel(0, BUZZER);
jurica238814 35:7917a7f951c7 168 buzzer.setDuty(0,0.5f);
jurica238814 35:7917a7f951c7 169 initState = 0;
jurica238814 36:36d44b58980a 170 toggleBuzzer.detach();
jurica238814 36:36d44b58980a 171 toggleBuzzer.attach(buzzerToggle, BUZZER_OFF_TIME_S);
jurica238814 35:7917a7f951c7 172 }
jurica238814 35:7917a7f951c7 173 else{
jurica238814 35:7917a7f951c7 174 buzzer.enable(0);
jurica238814 35:7917a7f951c7 175 buzzer.setDuty(0, 0);
jurica238814 35:7917a7f951c7 176 buzzer.disable();
jurica238814 35:7917a7f951c7 177 initState = 1;
jurica238814 36:36d44b58980a 178 toggleBuzzer.detach();
jurica238814 36:36d44b58980a 179 toggleBuzzer.attach(buzzerToggle, BUZZER_ON_TIME_S);
jurica238814 35:7917a7f951c7 180 }
jurica238814 35:7917a7f951c7 181 }
Dautor 26:148aa2e2460c 182
Dautor 26:148aa2e2460c 183 void buzzerStart(){
jurica238814 27:2c67f07590fd 184 buzzer.enable(BUZZER_FREQUENCY_Hz);
Dautor 26:148aa2e2460c 185 buzzer.enableChannel(0, BUZZER);
jurica238814 27:2c67f07590fd 186 buzzer.setDuty(0,0.5f);
Dautor 26:148aa2e2460c 187 }
Dautor 26:148aa2e2460c 188 void buzzerStop(){
jurica238814 27:2c67f07590fd 189 buzzer.enable(0);
jurica238814 27:2c67f07590fd 190 buzzer.setDuty(0, 0);
Dautor 26:148aa2e2460c 191 buzzer.disable();
jurica238814 22:8d106fd5fa84 192 }
jurica238814 16:a338d2417fd5 193
jurica238814 21:10c3b8176be0 194 void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params){
jurica238814 17:51a5456a46cd 195 #if DEBUG_CONNECTION
jurica238814 17:51a5456a46cd 196 scanLED = !scanLED; // Blue
jurica238814 17:51a5456a46cd 197 wait_ms(100);
jurica238814 17:51a5456a46cd 198 scanLED = !scanLED; // Blue
jurica238814 17:51a5456a46cd 199 wait_ms(100);
jurica238814 17:51a5456a46cd 200 scanLED = !scanLED; // Blue
jurica238814 17:51a5456a46cd 201 wait_ms(100);
jurica238814 17:51a5456a46cd 202 scanLED = !scanLED; // Blue
jurica238814 17:51a5456a46cd 203 wait_ms(100);
jurica238814 17:51a5456a46cd 204 scanLED = !scanLED; // Blue
jurica238814 17:51a5456a46cd 205 wait_ms(100);
jurica238814 17:51a5456a46cd 206 scanLED = !scanLED; // Blue
jurica238814 17:51a5456a46cd 207 wait_ms(100);
jurica238814 17:51a5456a46cd 208 scanLED = 1; // Blue
jurica238814 17:51a5456a46cd 209 #endif
jurica238814 17:51a5456a46cd 210 WakeSleepT.detach();
jurica238814 17:51a5456a46cd 211 sleepFlag = false;
jurica238814 16:a338d2417fd5 212 }
jurica238814 16:a338d2417fd5 213
jurica238814 16:a338d2417fd5 214
jurica238814 0:f8c1e0b2d473 215 /* Restart Advertising on disconnection*/
jurica238814 0:f8c1e0b2d473 216 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
Dautor 26:148aa2e2460c 217 buzzerStop();
jurica238814 17:51a5456a46cd 218 #if DEBUG_CONNECTION
jurica238814 17:51a5456a46cd 219 advLED = !advLED; // RED
jurica238814 17:51a5456a46cd 220 wait_ms(100);
jurica238814 17:51a5456a46cd 221 advLED = !advLED;
jurica238814 17:51a5456a46cd 222 wait_ms(100);
jurica238814 17:51a5456a46cd 223 advLED = !advLED;
jurica238814 17:51a5456a46cd 224 wait_ms(100);
jurica238814 17:51a5456a46cd 225 advLED = !advLED;
jurica238814 17:51a5456a46cd 226 wait_ms(100);
jurica238814 17:51a5456a46cd 227 advLED = 1;
jurica238814 17:51a5456a46cd 228 wait_ms(100);
jurica238814 17:51a5456a46cd 229 advLED = 1;
jurica238814 17:51a5456a46cd 230 #endif
jurica238814 17:51a5456a46cd 231 WakeSleepT.attach(WakeMeUp, FREE_TIME_S);
jurica238814 17:51a5456a46cd 232 sleepFlag = true;
jurica238814 18:e844d3e6ab88 233
jurica238814 0:f8c1e0b2d473 234 }
jurica238814 0:f8c1e0b2d473 235
jurica238814 16:a338d2417fd5 236 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
jurica238814 25:8ac3ff431ab1 237 if(params->handle == ackServicePtr->getACKCharacteristicHandle()){
jurica238814 16:a338d2417fd5 238 // Something is written into AckCharacteristic
jurica238814 25:8ac3ff431ab1 239 if(params->data[0] == startBuzz[0]){
jurica238814 25:8ac3ff431ab1 240 if(params->data[1] == startBuzz[1]){
jurica238814 17:51a5456a46cd 241 #if DEBUG_CONNECTION
jurica238814 17:51a5456a46cd 242 connectedLED = !connectedLED; // BLUE
jurica238814 17:51a5456a46cd 243 wait_ms(100);
jurica238814 17:51a5456a46cd 244 connectedLED = !connectedLED;
jurica238814 17:51a5456a46cd 245 wait_ms(100);
jurica238814 17:51a5456a46cd 246 connectedLED = !connectedLED;
jurica238814 17:51a5456a46cd 247 wait_ms(100);
jurica238814 17:51a5456a46cd 248 connectedLED = !connectedLED;
jurica238814 17:51a5456a46cd 249 wait_ms(100);
jurica238814 17:51a5456a46cd 250 connectedLED = !connectedLED;
jurica238814 17:51a5456a46cd 251 wait_ms(100);
jurica238814 17:51a5456a46cd 252 connectedLED = 1;
jurica238814 17:51a5456a46cd 253 wait_ms(100);
jurica238814 17:51a5456a46cd 254 #endif
jurica238814 35:7917a7f951c7 255 //buzzerStart();
jurica238814 36:36d44b58980a 256 toggleBuzzer.attach(buzzerToggle, BUZZER_ON_TIME_S);
dbartolovic 41:7d8ce2d8d167 257 #if ACN_FREIGHT
dbartolovic 41:7d8ce2d8d167 258 toggleLed.attach(ledToggle, LED_BLINK_HALF_T);
dbartolovic 41:7d8ce2d8d167 259 #endif
jurica238814 16:a338d2417fd5 260 return;
jurica238814 16:a338d2417fd5 261 }
jurica238814 25:8ac3ff431ab1 262 }
Dautor 26:148aa2e2460c 263 else if(params->data[0] == stopBuzz[0]){
Dautor 26:148aa2e2460c 264 if(params->data[1] == stopBuzz[1]){
jurica238814 35:7917a7f951c7 265 toggleBuzzer.detach();
jurica238814 27:2c67f07590fd 266 buzzerStop();
dbartolovic 41:7d8ce2d8d167 267 #if ACN_FREIGHT
dbartolovic 41:7d8ce2d8d167 268 toggleLed.detach();
dbartolovic 41:7d8ce2d8d167 269 signal_led = 1;
dbartolovic 41:7d8ce2d8d167 270 #endif
Dautor 26:148aa2e2460c 271 WakeSleepT.detach();
Dautor 26:148aa2e2460c 272 WakeSleepT.attach(WakeMeUp, FREE_TIME_S);
Dautor 26:148aa2e2460c 273 ble.disconnect(Gap::LOCAL_HOST_TERMINATED_CONNECTION);
jurica238814 25:8ac3ff431ab1 274 }
jurica238814 25:8ac3ff431ab1 275 }
jurica238814 16:a338d2417fd5 276 }
jurica238814 16:a338d2417fd5 277 else{
jurica238814 25:8ac3ff431ab1 278 // Execute this for wrong data written into characteristic
jurica238814 25:8ac3ff431ab1 279 return;
jurica238814 16:a338d2417fd5 280 }
jurica238814 16:a338d2417fd5 281 }
jurica238814 0:f8c1e0b2d473 282
jurica238814 0:f8c1e0b2d473 283 /**
jurica238814 0:f8c1e0b2d473 284 * This function is called when the ble initialization process has failed
jurica238814 0:f8c1e0b2d473 285 */
jurica238814 0:f8c1e0b2d473 286 void onBleInitError(BLE &ble, ble_error_t error){
jurica238814 0:f8c1e0b2d473 287 /* Avoid compiler warnings */
jurica238814 0:f8c1e0b2d473 288 (void) ble;
jurica238814 0:f8c1e0b2d473 289 (void) error;
jurica238814 0:f8c1e0b2d473 290 /* Initialization error handling should go here */
jurica238814 0:f8c1e0b2d473 291 }
jurica238814 0:f8c1e0b2d473 292
jurica238814 0:f8c1e0b2d473 293 /**
jurica238814 0:f8c1e0b2d473 294 * Callback triggered when the ble initialization process has finished
jurica238814 0:f8c1e0b2d473 295 */
jurica238814 0:f8c1e0b2d473 296 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){
jurica238814 0:f8c1e0b2d473 297 BLE& ble = params->ble;
jurica238814 0:f8c1e0b2d473 298 ble_error_t error = params->error;
jurica238814 0:f8c1e0b2d473 299
jurica238814 0:f8c1e0b2d473 300 if (error != BLE_ERROR_NONE) {
jurica238814 0:f8c1e0b2d473 301 /* In case of error, forward the error handling to onBleInitError */
jurica238814 0:f8c1e0b2d473 302 onBleInitError(ble, error);
jurica238814 0:f8c1e0b2d473 303 return;
jurica238814 0:f8c1e0b2d473 304 }
jurica238814 0:f8c1e0b2d473 305
jurica238814 0:f8c1e0b2d473 306 /* Ensure that it is the default instance of BLE */
jurica238814 0:f8c1e0b2d473 307 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
jurica238814 0:f8c1e0b2d473 308 return;
jurica238814 0:f8c1e0b2d473 309 }
jurica238814 16:a338d2417fd5 310
jurica238814 16:a338d2417fd5 311 uint8_t init_values[4] = {0,0,0,0};
jurica238814 1:5f34885f5cff 312 /* Get my MAC address */
jurica238814 1:5f34885f5cff 313 BLEProtocol::AddressType_t temp_address_type;
jurica238814 14:d506c0679c0b 314 ble.gap().getAddress(&temp_address_type, myMacAddress);
jurica238814 16:a338d2417fd5 315 ackServicePtr = new ACKService<4>(ble, init_values);
jurica238814 16:a338d2417fd5 316 ackServicePtr->updateMacAddress(myMacAddress); // Update MAC address
jurica238814 21:10c3b8176be0 317
jurica238814 16:a338d2417fd5 318 ble.gap().onDisconnection(disconnectionCallback);
jurica238814 34:9856c51ec646 319 ble.gap().onConnection(onConnectionCallback);
jurica238814 17:51a5456a46cd 320 ble.gattServer().onDataWritten(onDataWrittenCallback);
jurica238814 1:5f34885f5cff 321
dbartolovic 42:bffc939efdf3 322 #if ACN_FREIGHT
dbartolovic 42:bffc939efdf3 323 AdvertisingFormat msd;
dbartolovic 42:bffc939efdf3 324
dbartolovic 42:bffc939efdf3 325 msd.manufacturerID = MANUFACTURER_ID;
dbartolovic 42:bffc939efdf3 326 msd.aconnoID = ACONNO_ID;
dbartolovic 42:bffc939efdf3 327 msd.productID = PRODUCT_ID;
dbartolovic 42:bffc939efdf3 328 msd.version = VERSION;
dbartolovic 42:bffc939efdf3 329
dbartolovic 42:bffc939efdf3 330 ble.gap().accumulateAdvertisingPayload(
dbartolovic 42:bffc939efdf3 331 GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA,
dbartolovic 42:bffc939efdf3 332 (uint8_t*)&msd, sizeof(msd));
dbartolovic 42:bffc939efdf3 333 #endif
dbartolovic 42:bffc939efdf3 334
dbartolovic 42:bffc939efdf3 335 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS, (uint8_t*)UUID, sizeof(UUID));
jurica238814 10:fd91664032d8 336 ble.gap().setAdvertisingInterval(ADV_INTERVAL); // --> Has to be at least 100ms!
jurica238814 0:f8c1e0b2d473 337 }
jurica238814 0:f8c1e0b2d473 338
jurica238814 2:5504b714c9ae 339
jurica238814 12:6b072c2a061c 340
jurica238814 34:9856c51ec646 341 void startAdvertising(){
jurica238814 34:9856c51ec646 342 ble.gap().startAdvertising();
jurica238814 10:fd91664032d8 343 #if DEBUG
jurica238814 34:9856c51ec646 344 advLED = 0;
jurica238814 34:9856c51ec646 345 scanLED = 1;
jurica238814 10:fd91664032d8 346 #endif
jurica238814 2:5504b714c9ae 347 WakeSleepT.detach();
jurica238814 34:9856c51ec646 348 WakeSleepT.attach(WakeMeUp, ADV_TIMER_TIME_S); // Call the wakeMeUp function
jurica238814 10:fd91664032d8 349 }
jurica238814 10:fd91664032d8 350
jurica238814 10:fd91664032d8 351 void WakeMeUp(){
jurica238814 34:9856c51ec646 352 sleepFlag = false;
jurica238814 10:fd91664032d8 353 switch(radioState){
jurica238814 10:fd91664032d8 354 case OFF:{
jurica238814 34:9856c51ec646 355 radioState = ADVERTISING;
jurica238814 10:fd91664032d8 356 startAdvertising();
jurica238814 10:fd91664032d8 357 break;
jurica238814 10:fd91664032d8 358 }
jurica238814 10:fd91664032d8 359 case ADVERTISING:{
jurica238814 10:fd91664032d8 360 radioState = OFF;
jurica238814 10:fd91664032d8 361 WakeSleepT.detach();
jurica238814 34:9856c51ec646 362 WakeSleepT.attach(GoToSleep, FREE_TIME_S);
jurica238814 10:fd91664032d8 363 break;
jurica238814 10:fd91664032d8 364 }
jurica238814 10:fd91664032d8 365 default: return;
jurica238814 10:fd91664032d8 366 }
jurica238814 2:5504b714c9ae 367 }
jurica238814 2:5504b714c9ae 368
jurica238814 14:d506c0679c0b 369 void GoToSleep(){
jurica238814 2:5504b714c9ae 370 WakeSleepT.detach();
jurica238814 34:9856c51ec646 371 WakeSleepT.attach(WakeMeUp, SLEEP_TIME_S);
jurica238814 2:5504b714c9ae 372 ble.gap().stopAdvertising();
jurica238814 34:9856c51ec646 373 sleepFlag = true;
jurica238814 16:a338d2417fd5 374 #if DEBUG
jurica238814 16:a338d2417fd5 375 advLED = 1;
jurica238814 16:a338d2417fd5 376 scanLED = 1;
jurica238814 16:a338d2417fd5 377 #endif
jurica238814 1:5f34885f5cff 378 }
jurica238814 1:5f34885f5cff 379
jurica238814 19:abf14a5ada93 380 #if USE_ACC
jurica238814 27:2c67f07590fd 381 void pulse_handler(){
jurica238814 27:2c67f07590fd 382 #if DEBUG_WAKEUP_BUZZER
Dautor 26:148aa2e2460c 383 buzzerStart();
jurica238814 27:2c67f07590fd 384 wait_ms(50);
Dautor 26:148aa2e2460c 385 buzzerStop();
jurica238814 27:2c67f07590fd 386 #endif
jurica238814 27:2c67f07590fd 387 #if DEBUG_ACC
jurica238814 27:2c67f07590fd 388 int_led = !int_led;
jurica238814 27:2c67f07590fd 389 #endif
jurica238814 19:abf14a5ada93 390 }
jurica238814 19:abf14a5ada93 391 #endif
jurica238814 6:d14e3df498f4 392
Dautor 26:148aa2e2460c 393 int main(){
jurica238814 18:e844d3e6ab88 394 #if DEBUG || DEBUG_MAC
jurica238814 10:fd91664032d8 395 advLED = 1;
jurica238814 10:fd91664032d8 396 scanLED = 1;
jurica238814 16:a338d2417fd5 397 connectedLED = 1;
jurica238814 10:fd91664032d8 398 #endif
jurica238814 14:d506c0679c0b 399
jurica238814 19:abf14a5ada93 400 #if USE_ACC
jurica238814 19:abf14a5ada93 401 accPower = 1;
jurica238814 19:abf14a5ada93 402 i2cPower = 1;
jurica238814 27:2c67f07590fd 403 #endif
dbartolovic 41:7d8ce2d8d167 404
dbartolovic 41:7d8ce2d8d167 405 #if ACN_FREIGHT
dbartolovic 41:7d8ce2d8d167 406 signal_led = 1;
dbartolovic 41:7d8ce2d8d167 407 #endif
jurica238814 16:a338d2417fd5 408
jurica238814 16:a338d2417fd5 409 ble.init(bleInitComplete);
jurica238814 19:abf14a5ada93 410 /* SpinWait for initialization to complete. This is necessary because the BLE object is used in the main loop below. */
Dautor 26:148aa2e2460c 411 while (ble.hasInitialized() == false){ /* spin loop */ }
jurica238814 31:caef580f5943 412 __enable_irq();
jurica238814 31:caef580f5943 413
jurica238814 27:2c67f07590fd 414 buzzerStart();
jurica238814 27:2c67f07590fd 415 wait_ms(500);
jurica238814 27:2c67f07590fd 416 buzzerStop();
jurica238814 34:9856c51ec646 417
jurica238814 31:caef580f5943 418 WakeSleepT.attach(GoToSleep, AWAKE_TIME_S);
jurica238814 27:2c67f07590fd 419
jurica238814 27:2c67f07590fd 420 while(true){
jurica238814 27:2c67f07590fd 421 ble.waitForEvent();
jurica238814 0:f8c1e0b2d473 422 }
jurica238814 0:f8c1e0b2d473 423 }