Geo beacon for VF.
Dependencies: MMA8452 aconno_bsp adc52832_common
Diff: main.cpp
- Revision:
- 16:a338d2417fd5
- Parent:
- 15:934a04c958f5
- Child:
- 17:51a5456a46cd
--- a/main.cpp Fri Jul 28 12:28:05 2017 +0000
+++ b/main.cpp Thu Aug 10 11:02:02 2017 +0000
@@ -11,12 +11,15 @@
#include "GapAdvertisingData.h"
#include "acd52832_bsp.h"
#include "mma8452.h"
+#include "AckService.h"
+#include "nrf52_uart.h"
#define DEBUG (1)
#define DEBUG_ACC (0)
+#define PRINT (1)
-#define SLEEP_TIME_S (2.0) /* Sleep time in seconds WAS 0.85 */
-#define ADV_TIMER_TIME_S (1.0) /* Advertising time (in s) */
+#define SLEEP_TIME_S (1.0) /* Sleep time in seconds WAS 0.85 */
+#define ADV_TIMER_TIME_S (4.0) /* Advertising time (in s) */
#define SCAN_TIMER_TIME_S (2.0) /* Scanning time (in s) */
#define FREE_TIME_S (0.1) /* Time between end of a scanning and sleep mode */
#define AWAKE_TIME_S (ADV_TIMER_TIME_S+SCAN_TIMER_TIME_S+FREE_TIME_S) /* Was 0.15 */
@@ -24,7 +27,6 @@
#define SHORT_SLEEP_TIME_PERIOD_S (5) /* Time after a last scanned advertisment. In the period, sleep time is SHORT_SLEEP_TIME */
#define MAC_SIZE_B (6)
-
/* Static constants for the BLE example */
#define MAX_BLE_PACKET_SIZE (31)
#define MSD_SIZE (18)
@@ -35,7 +37,6 @@
#define SCAN_INTERVAL (SCAN_TIMER_TIME_S) /* Scan interval (in ms) */
#define SCAN_WINDOW (SCAN_TIMER_TIME_S)
-
/* Static constants for the accelerometer */
#define WHO_AM_I 0x0D /* Type 'read' : This should return the device id of 0x2A */
#define OUT_Z_MSB 0x05 /* Type 'read' : z axis - 8 most significatn bit of a 12 bit sample */
@@ -44,6 +45,20 @@
#define INT2_PIN (p4)
#define BUZZER (p31)
+#if PRINT
+ /* Defines for debugging over uart */
+ #define TX (p25)
+ #define RX (p26)
+ NRF52_UART uart(TX,RX, Baud9600);
+ char printBuffer[30] = {};
+#endif
+
+
+
+bool shushShush = false;
+const static uint16_t ACK_SERVICE_UUID = 0xCBE0;
+const static uint16_t ACK_CHARA_UUID = 0xCBE1;
+
uint8_t sleepFlag = 0;
uint8_t tempSleepTime = SLEEP_TIME_S;
int8_t txPower = 4;
@@ -73,22 +88,83 @@
InterruptIn gyro_pulse(INT2_PIN);
Acc_MMA8452 acc(I2C_DATA, I2C_CLK, MMA8452_ADDRESS);
BLE &ble = BLE::Instance();
-
+ACKService<4> *ackServicePtr;
#if DEBUG
- DigitalOut advLED(p22);
- DigitalOut scanLED(p23);
- DigitalOut awake(p24);
+ DigitalOut advLED(p22); // Red
+ DigitalOut scanLED(p23); // Blue
+ DigitalOut connectedLED(p24); // Green
#endif
#if DEBUG_ACC
DigitalOut int_led(p22);
#endif
+
+void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params){
+ scanLED = !scanLED; // Blue
+ wait_ms(100);
+ scanLED = !scanLED; // Blue
+ wait_ms(100);
+ scanLED = !scanLED; // Blue
+ wait_ms(100);
+ scanLED = !scanLED; // Blue
+ wait_ms(100);
+ scanLED = !scanLED; // Blue
+ wait_ms(100);
+ scanLED = !scanLED; // Blue
+ wait_ms(100);
+ scanLED = 1; // Blue
+
+ //WakeSleepT.detach();
+}
+
+
/* Restart Advertising on disconnection*/
void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
- //BLE::Instance().gap().startAdvertising();
+ shushShush = true;
+ advLED = !advLED; // RED
+ wait_ms(100);
+ advLED = !advLED;
+ wait_ms(100);
+ advLED = !advLED;
+ wait_ms(100);
+ advLED = !advLED;
+ wait_ms(100);
+ advLED = !advLED;
+ wait_ms(100);
+ advLED = 1;
}
+void onDataWrittenCallback(const GattWriteCallbackParams *params) {
+ if(params->handle == ACK_CHARA_UUID || 1){
+ // Something is written into AckCharacteristic
+ if(params->data[0] == 0xBA)
+ if(params->data[1] == 0xBE){
+ connectedLED = !connectedLED; // BLUE
+ wait_ms(100);
+ connectedLED = !connectedLED;
+ wait_ms(100);
+ connectedLED = !connectedLED;
+ wait_ms(100);
+ connectedLED = !connectedLED;
+ wait_ms(100);
+ connectedLED = !connectedLED;
+ wait_ms(100);
+ connectedLED = 1;
+ wait_ms(100);
+ /*
+ This function should make advertiser stop
+ */
+ ble.disconnect(Gap::LOCAL_HOST_TERMINATED_CONNECTION);
+ return;
+ }
+ }
+ else{
+ // Execute this for wrong data written into characteristic
+ wait_ms(500);
+
+ }
+}
/**
* This function is called when the ble initialization process has failed
@@ -117,21 +193,26 @@
if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
return;
}
-
- ble.gap().onDisconnection(disconnectionCallback);
-
+
+ uint8_t init_values[4] = {0,0,0,0};
/* Get my MAC address */
BLEProtocol::AddressType_t temp_address_type;
ble.gap().getAddress(&temp_address_type, myMacAddress);
+ ackServicePtr = new ACKService<4>(ble, init_values);
+ ackServicePtr->updateMacAddress(myMacAddress); // Update MAC address
+ ble.gap().onDisconnection(disconnectionCallback);
+
+ //ble.gap().onConnection(onConnectionCallback); // -->> Uncomment these two lines for shush-shush
+ //ble.gattServer().onDataWritten(onDataWrittenCallback);
/* setup advertising */
-
//ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
//ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)msd, MSD_SIZE);
ble.gap().setAdvertisingInterval(ADV_INTERVAL); // --> Has to be at least 100ms!
- //ble.gap().startAdvertising();
+ //ble.gap().startAdvertising();
+
}
@@ -157,14 +238,32 @@
uint8_t CheckMac(const Gap::AdvertisementCallbackParams_t *params, uint8_t *myMacAddress, uint8_t msdOffset){
int i=0;
+ /* Get my MAC address */
+ BLEProtocol::AddressType_t temp_address_type;
+ ble.gap().getAddress(&temp_address_type, myMacAddress);
+
if(msdOffset == 0){
+ for(i=0; i<10; i++){
+ scanLED = !scanLED; // BLUE
+ wait_ms(100);
+ }
return 0; // There's no MSD in BLE advertisement data
}
for(i=0; i<6; i++){
- if((params->advertisingData[msdOffset+i+3]) != myMacAddress[5-i]){
+ //if((params->advertisingData[0+i+4]) != myMacAddress[i]){
+ //if(params->advertisingData[4] != myMacAddress[4]){
+ if(params->advertisingData[msdOffset + 3 + i] != myMacAddress[5-i]){ // myMacAddress[0] == 0x91
+ for(i=0; i<10; i++){
+ connectedLED = !connectedLED; // Green
+ wait_ms(100);
+ }
return 0;
}
}
+ for(i=0; i<10; i++){
+ advLED = !advLED; // RED
+ wait_ms(100);
+ }
return 1;
}
@@ -175,7 +274,7 @@
uint8_t msdOffset, i;
//msdOffset = findMSDIndex(params);
- msdOffset = 4;
+ msdOffset = 1; // 1 or 4
if(msdOffset == 0){
return; // There's no MSD in BLE advertisement data
}
@@ -183,11 +282,14 @@
// Follows Manufacturer Specific Data
if ((params->advertisingData[msdOffset+1]) == 0x59){
if ((params->advertisingData[msdOffset+2]) == 0x00){
+ /*
for(i=0; i<6; i++){
if((params->advertisingData[msdOffset+i+3]) != myMacAddress[5-i]){
return;
}
- //if(CheckMac(params, myMacAddress, msdOffset) || 1){
+ }
+ */
+ if(CheckMac(params, myMacAddress, msdOffset)){
ble.gap().stopScan();
buzzer.write(0.5F);
WakeSleepT.detach();
@@ -220,13 +322,21 @@
}
void startAdvertising(){
- ble.gap().startAdvertising();
- #if DEBUG
- advLED = 0;
- scanLED = 1;
- #endif
- WakeSleepT.detach();
- WakeSleepT.attach(WakeMeUp, ADV_TIMER_TIME_S); // Call the wakeMeUp function
+ if(shushShush){
+ // Do not advertise! Go to sleep
+ WakeSleepT.detach();
+ ble.gap().stopAdvertising();
+ WakeMeUp();
+ }
+ else{
+ ble.gap().startAdvertising();
+ #if DEBUG
+ advLED = 0;
+ scanLED = 1;
+ #endif
+ WakeSleepT.detach();
+ WakeSleepT.attach(WakeMeUp, ADV_TIMER_TIME_S); // Call the wakeMeUp function
+ }
}
void startScanning(){
@@ -258,11 +368,8 @@
case SCANNING:{
radioState = OFF;
WakeSleepT.detach();
- WakeSleepT.attach(GoToSleep, FREE_TIME_S);
- #if DEBUG
- advLED = 1;
- scanLED = 1;
- #endif
+ //WakeSleepT.attach(GoToSleep, FREE_TIME_S);
+ GoToSleep();
break;
}
default: return;
@@ -275,6 +382,10 @@
ble.gap().stopAdvertising();
ble.gap().stopScan();
sleepFlag = 1;
+ #if DEBUG
+ advLED = 1;
+ scanLED = 1;
+ #endif
}
void buzz(void){
@@ -297,22 +408,34 @@
int main(void){
#if DEBUG
- awake = 1;
advLED = 1;
scanLED = 1;
+ connectedLED = 1;
#endif
- WakeSleepT.attach(GoToSleep, AWAKE_TIME_S);
- ble.init(bleInitComplete);
- ble.gap().setTxPower(txPower);
- GapAdvertisingData postavke = GapAdvertisingData();
-
buzzer.period(0.0009F);
buzzer.write(0.0F);
gyro_power.period(0.01F);
gyro_power.write(1.0F);
i2c_power.period(0.01F);
i2c_power.write(1.0F);
+
+ int i;
+ for(i=0; i<10; i++){
+ #if PRINT
+ printBuffer[0] = 'B';
+ printBuffer[1] = 'o';
+ printBuffer[2] = 'k';
+ uart.send(printBuffer, 3);
+ #endif
+ wait_ms(100);
+ }
+
+ WakeSleepT.attach(GoToSleep, AWAKE_TIME_S);
+ ble.init(bleInitComplete);
+ ble.gap().setTxPower(txPower);
+ GapAdvertisingData postavke = GapAdvertisingData();
+
wait_ms(1000);
/* Pulse interrupt detection */
@@ -328,8 +451,8 @@
wait_ms(1);
/* Setup for the interrupt handler */
- gyro_pulse.rise(&pulse_handler); // -------------------------------------
- acc.set_register((char)CTRL_REG_1, (char) 0x01); // Flow data rate and Active mode
+ //gyro_pulse.rise(&pulse_handler); // -------------------------------------
+ //acc.set_register((char)CTRL_REG_1, (char) 0x01); // Flow data rate and Active mode
wait(1);
__enable_irq();
@@ -342,11 +465,11 @@
while(true){
if(sleepFlag){
- if(!awake) awake = 1;
+ //if(!awake) awake = 1;
__WFI();
}
else{
- if(awake) awake = 0;
+ //if(awake) awake = 0;
ble.waitForEvent();
}
}

