aidanReceiver

Dependencies:   mbed BLE_API nRF51822

receiver_main.cpp

Committer:
jzabins2
Date:
2019-04-17
Revision:
2:89b8aef35194
Parent:
1:3ad5b46f9abc
Child:
3:ca23d318cb12

File content as of revision 2:89b8aef35194:

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

#define LED_RED     p21
#define LED_GREEN   p22
#define LED_BLUE    p23
#define BUTTON_PIN  p17
#define BATTERY_PIN p1

DigitalOut led1(LED_RED);
DigitalOut blueLed(LED_BLUE);

const static char DEVICE_NAME[] = "Joe-Aidan";

struct Data {
  uint32_t seqNum;
};

// void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)

void scanCallback(const Gap::AdvertisementCallbackParams_t *params)
{
    if (*(params->Gap::AdvertisementCallbackParams_t::peerAddr) == (std::uint8_t)0xF34F887FED4E) {
        char * buf = (char*) params->advertisingData;
        
        if (buf[0] == GapAdvertisingData::COMPLETE_LOCAL_NAME) {
            blueLed = 0;
        }
        
        /*
        for (int i=0; i < params->advertisingDataLen; i++) {
            if ( i >= params->advertisingDataLen-2 && buf[i] == 'n' ) {
                blueLed = 0;
            }
        }
        */
        
        led1 = 0;
    }
    
//    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));

    return;
}


void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
{
    BLE &ble          = params->ble;
    ble_error_t error = params->error;

    if (error != BLE_ERROR_NONE) {
        return;
    }
    
    led1 = 1;
    blueLed = 1;

    
    /* Set up scanning prodedure */
    ble.gap().setScanParams(GapScanningParams::SCAN_INTERVAL_MAX, GapScanningParams::SCAN_WINDOW_MAX, 0, false);
    ble.gap().startScan(scanCallback);
}


int main(void)
{
    BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
    ble.init(bleInitComplete);
    
    /* 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 (1) {
        ble.waitForEvent(); // low power wait for event
    }
}