Geo beacon for VF.

Dependencies:   MMA8452 aconno_bsp adc52832_common

main.cpp

Committer:
jurica238814
Date:
2017-07-19
Revision:
7:89c9abaa257e
Parent:
4:331dddea780e
Child:
12:6b072c2a061c

File content as of revision 7:89c9abaa257e:

/*
 *
 *  Made by Jurica Resetar @ aconno
 *  aconno.de
 *  All rights reserved.
 *
 */

#include "mbed.h"
#include "ble/BLE.h"
#include "GapAdvertisingData.h"
#include "acd52832_bsp.h"

#define SLEEP_TIME              (5.0)      // Sleep time in seconds
#define SHORT_SLEEP_TIME        (0.5)      // Shorter sleep time (s) 
#define SHORT_SLEEP_TIME_PERIOD (60)       // Time after a last scanned advertisment. In the period, sleep time is SHORT_SLEEP_TIME
#define AWAKE_TIME              (0.25)     // Was 0.15 -> Was not enough
#define BUZZER                  (p31)

/* Static constants for the BLE example */
#define MAX_BLE_PACKET_SIZE (31)
#define MSD_SIZE            (18)
#define MSD_ID              (0xFF)
#define BUZZ_TIME           (2.0)   // Buzz time in s


bool SLEEP = true;
uint8_t tempSleepTime = SLEEP_TIME;
int8_t txPower = 4;
uint8_t MSD[MSD_SIZE] = {0x59, 0x00, 0xE1, 0x61, 0x35, 0xBA, 0xC0, 0xEC, 0x47, 0x2A, 0x98, 0x00, 0xAF, 0x18, 0x43, 0xFF, 0x05, 0x00};
uint8_t my_mac_address[6] = {};

void turnBuzzOff(void);
void goToSleep();

Ticker WakeSleepT;
Ticker turnBuzzOffT;
Ticker sleepChanger;
PwmOut buzzer(BUZZER);
BLE &ble = BLE::Instance();

/* Restart Advertising on disconnection*/
void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
    BLE::Instance().gap().startAdvertising();
}


/**
 * This function is called when the ble initialization process has failed
 */
void onBleInitError(BLE &ble, ble_error_t error){
    /* Avoid compiler warnings */
    (void) ble;
    (void) error;
    /* Initialization error handling should go here */
}

/**
 * Callback triggered when the ble initialization process has finished
 */
void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){
    BLE&        ble   = params->ble;
    ble_error_t error = params->error;

    if (error != BLE_ERROR_NONE) {
        /* In case of error, forward the error handling to onBleInitError */
        onBleInitError(ble, error);
        return;
    }

    /* Ensure that it is the default instance of BLE */
    if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
        return;
    }

    ble.gap().onDisconnection(disconnectionCallback);

    /* Get my MAC address */
    BLEProtocol::AddressType_t temp_address_type;
    ble.gap().getAddress(&temp_address_type, my_mac_address);
    
    
    /* setup advertising */
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)MSD, MSD_SIZE);
    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);    
    ble.gap().setAdvertisingInterval(100);  // --> Has to be at least 100ms!
    ble.gap().startAdvertising();
}


uint8_t findMSDIndex(const Gap::AdvertisementCallbackParams_t *params){
    uint8_t i=0;
    uint8_t len;
    
    do{
        len = params->advertisingData[i];
        i++;
        if(params->advertisingData[i] == MSD_ID) return i;
        else i += (len-1);
    }while(i<MAX_BLE_PACKET_SIZE);
    
    return 0;
}

/**
 *  Function is called when BLE radio discovers any kind of advertisment 
 */
void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params){
    uint8_t i=0;
    uint8_t msdOffset;
    
    msdOffset = findMSDIndex(params);
    if(msdOffset == 0){
        // There's no MSD in BLE advertisement data
        return;
    }
    
    if ((params->advertisingData[msdOffset]) == MSD_ID){ 
        // 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]) == my_mac_address[5-i]){
                        continue;
                    }
                    else{
                        return;
                    }
                }
                turnBuzzOffT.detach();
                WakeSleepT.detach();
                buzzer.write(0.5F);
                turnBuzzOffT.attach(turnBuzzOff, BUZZ_TIME);
            }
        }
    }
}

/* Call this function few minutes (TBD) after a last scanned advertisment */
void changeSleepTime(){
    tempSleepTime = SLEEP_TIME;    
    sleepChanger.detach();
}

/**
 *  The function is called when ticker generates interrupt
 */
void turnBuzzOff(void){
    buzzer.write(0.0F);
    tempSleepTime = SHORT_SLEEP_TIME;
    turnBuzzOffT.detach();
    WakeSleepT.attach(goToSleep, AWAKE_TIME);
    sleepChanger.attach(changeSleepTime, SHORT_SLEEP_TIME_PERIOD);
}

void WakeMeUp(){
    WakeSleepT.detach();
    WakeSleepT.attach(goToSleep, AWAKE_TIME);
    
    ble.gap().startScan(advertisementCallback);
    ble.gap().startAdvertising();
    SLEEP = false;
}

void goToSleep(){
    WakeSleepT.detach();
    WakeSleepT.attach(WakeMeUp, tempSleepTime);
    
    ble.gap().stopAdvertising();
    ble.gap().stopScan();
    SLEEP = true;
}

int main(void){
    WakeSleepT.attach(goToSleep, AWAKE_TIME);
    ble.init(bleInitComplete);
    ble.gap().setTxPower(txPower);
    GapAdvertisingData postavke = GapAdvertisingData();
    
    ble.gap().setScanParams(100, 100);
    ble.gap().startScan(advertisementCallback);
    
    buzzer.period(0.0009F);
    buzzer.write(0.0F);
    
    __enable_irq();
    
    /* SpinWait for initialization to complete. This is necessary because the
     * BLE object is used in the main loop below. */
    while (ble.hasInitialized()  == false){
         /* spin loop */ 
    }
    
    while(true){
        if(SLEEP) __WFI();
        else ble.waitForEvent();
    }
}