This is a basic program that provides the necessary BLE service to allow communications with the UPAS

Dependencies:   BLE_API mbed nRF51822 CronoDot EEPROM NCP5623BMUTBG ADS1115 BME280 Calibration_one MCP40D17 SDFileSystem LSM303 SI1145 STC3100

Fork of BLE_Button by Bluetooth Low Energy

main.cpp

Committer:
jelord
Date:
2015-10-20
Revision:
10:66549fa08986
Parent:
9:0f6951db24f1
Child:
11:1058647c66e8

File content as of revision 10:66549fa08986:

//CODE BY JAKE LORD
//ALL RIGHTS RESERVED BY VOLCKENS GROUP, FORT COLLINS CO
#include "mbed.h"
#include "BLE.h"
#include "UPAS_Service.h"

BLE         ble;
DigitalOut  led1(LED1); //Use of leds is important for debugging

const static char     DEVICE_NAME[] = "UPAS"; //Will hold the actual name of the whichever UPAS is being connected to
static const uint16_t uuid16_list[] = {UPAS_Service::UPAS_SERVICE_UUID}; //Currently a custom 16-bit representation of 128-bit UUID

UPAS_Service *upasServicePtr;

void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)//Code called when mbed ble senses a disconnect
{
    ble.gap().startAdvertising();
}

void periodicCallback(void)
{
    led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */
}

void writeCharCallback(const GattWriteCallbackParams *params)
{
    led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */
    
    // check to see what characteristic was written, by handle
    if(params->handle == upasServicePtr->writeChar.getValueHandle()) {
        // toggle LED if only 1 byte is written
        if(params->len == 1) {
            led1 = params->data[0];
        }
        // update the readChar with the value of writeChar
        ble.updateCharacteristicValue(upasServicePtr->readChar.getValueHandle(),params->data,params->len);
    }
}

int main(void)
{
    led1 = 1;
    Ticker ticker;
    ticker.attach(periodicCallback, 1);
    
    uint8_t  readValues[57] = {0x31,0x30,0x2F,0x31,0x38,0x2F,0x31,0x35,0x20,0x31,0x32,0x3A,0x30,0x30,0x3A,0x30,0x30}; //dummy array for testing

    ble.init();
    ble.gap().onDisconnection(disconnectionCallback);
    ble.gattServer().onDataWritten(writeCharCallback); //add writeCharCallback (custom function) to whenever data is being written to device
  
    UPAS_Service upasService(ble, false,readValues); //Create a GattService that is defined in UPAS_Service.h
    upasServicePtr = &upasService; //Create a pointer to the service (Allows advertisement without specifically adding the service

    /* setup advertising 
    Following lines do the follow:
        1:Declare the device as Bluetooth Smart(Low-Energy)
        2.Advertise the UPAS service that will send and receive the 57-bits of settable values in the UPAS EEPROM
        3.Advertise the name that will be associated with the UPAS
        4.Allow the UPAS to advertise unrestricted (this might change) */
        
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
    ble.gap().setAdvertisingInterval(160); /* 160ms. */
    ble.gap().startAdvertising();
    
    //Loop keeps device in infinite loop waiting for events to occur
    while (true) {
        ble.waitForEvent();
    }
}