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-23
Revision:
11:1058647c66e8
Parent:
10:66549fa08986
Child:
12:27273e6a50b3

File content as of revision 11:1058647c66e8:

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

BLE         ble;
//DigitalOut  led1(LED1); //Use of leds is important for debugging
DigitalOut          blower(p29, 0);
DigitalOut          pbKill(p18, 1);
DigitalOut          GPS_EN(p4,0); 
EEPROM              E2PROM(p22, p20);
CronoDot            RTC(p22, p20);  
NCP5623BMUTBG       RGB_LED(p22, p20);          
/*EEPROM ADDRESSING:
    0:Status bit-Unused
    1-15:Device Name
    16-19:Flow Rate
    20: Data Log Interval
    21-26: Start Time: ssmmHHddMMyy
    27-32: Stop Time: ssmmHHddMMyy
    33: Duty Up
    34: Duty Down
    35-38: Home Latitude
    39-42: Home Longitude
    43-46: Work Latitude
    47-50: Work Longitude
    51: Runready: Currently useless, should be 0
    52-53: Device Calibration
    54: Consider RunReady
    55-56: Menu Options
    57+ Nothing*/
//    .write(uint32_t addr, uint8_t dt[], uint16_t length);
//        addr -- is where the data in dt[] will be stored, max of 0x3FFFF, 0x00000 to 0x1FFFF on EEPROM 1 and 0x20000 to 0x3FFFF on EEPROM 2 
//        dt[] -- is unit8_t array that contains the data to be stored. This can only be uint8_t types, single bytes
//        length -- is the number of bytes to save t the EEPROM, starting at dt[0]
//        
//        returns:
//            Ture(1) -- when if has finished saving the data 
//            False(0) -- when length is to long to fit on the remainder of the page, when this happens it does not save any data in order to avoid over writing past data
//
//    .read(uint32_t addr, uint8_t *rt_data, uint16_t length);
//        addr -- where the data is requested from, max of 0x3FFFF, 0x00000 to 0x1FFFF on EEPROM 1 and 0x20000 to 0x3FFFF on EEPROM 2 
//        rt_data -- a local variable where the data from the EEPROM should be stored, again in uint8_t types
//        length -- how much to read from the EERPOM, this may be limited to <256 bytes?

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 readCharCallin(const GattReadCallbackParams *eventDataP)
{
     RTC.get_time();
}
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);
        uint8_t *writeData =  const_cast<uint8_t*>(params->data);
      
        E2PROM.write(0x00015, writeData+6, 12);
        RTC.set_time(writeData[0],writeData[1],writeData[2],writeData[3],writeData[3],writeData[4],writeData[5]);
    }
}

int main(void)
{
   // led1 = 1;
    Ticker ticker;
    ticker.attach(periodicCallback, 1);
    RGB_LED.set_led(1,1,1);
    RTC.get_time();
    uint8_t newReadValues[20] = {RTC.seconds, RTC.minutes,RTC.hour,RTC.date,RTC.month,RTC.year};
    E2PROM.read(0x00015, newReadValues+6, 12);

    ble.init();
    ble.gap().onDisconnection(disconnectionCallback);
    ble.gattServer().onDataWritten(writeCharCallback); //add writeCharCallback (custom function) to whenever data is being written to device
    ble.gattServer().onDataRead(readCharCallin);
    UPAS_Service upasService(ble, false,newReadValues); //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();
    }
}