Geo beacon for VF.

Dependencies:   MMA8452 aconno_bsp adc52832_common

Committer:
jurica238814
Date:
Tue Jan 09 09:34:13 2018 +0000
Revision:
34:9856c51ec646
Parent:
33:449fcc43b287
Child:
35:7917a7f951c7
Buzzer freq. changed to 4kHz.

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