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

Revision:
17:077712e4e5e3
Parent:
16:e066ab7e8fb3
Child:
18:c911a8928d0b
diff -r e066ab7e8fb3 -r 077712e4e5e3 main.cpp
--- a/main.cpp	Tue Nov 03 20:33:27 2015 +0000
+++ b/main.cpp	Fri Dec 04 22:08:09 2015 +0000
@@ -7,6 +7,19 @@
 #include "CronoDot.h"
 #include "NCP5623BMUTBG.h"
 
+#include "SDFileSystem.h"
+#include "Adafruit_ADS1015.h"
+#include "MCP40D17.h"
+#include "BME280.h"
+#include "Calibration.h"
+
+//List of files not currently in this Code
+//#include "LSM303.h"
+//#include "SI1145.h"
+//#include "STC3100.h"
+//#include "US_Menu.h"
+
+
 
 DigitalOut          blower(p29, 0);
 DigitalOut          pbKill(p18, 1);
@@ -15,6 +28,13 @@
 CronoDot            RTC(p22, p20);  
 NCP5623BMUTBG       RGB_LED(p22, p20);   
 
+
+
+I2C                 i2c(p22, p20);
+Adafruit_ADS1115    ads(&i2c);
+Calibration         calibrations(1);     //Default serial/calibration if there are no values for the selected option
+
+
 BLE         ble;
 /*EEPROM ADDRESSING:
     0:Status bit-Unused
@@ -35,13 +55,20 @@
     55-56: Menu Options
     57+ Nothing*/
 
+Timeout         stop;   //This is the stop call back object
+Timeout         logg;   //This is the logging call back object
+
+uint16_t serial_num = 1;                // Default serial/calibration number
+int RunReady =0;
+
+
 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;
 
 
-//MARK: THIS IS ALL CODE NOT TAKEN FROM OTHER MENU>>>DONT ALTER!!!
+
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)//Code called when mbed ble senses a disconnect
 {
     ble.gap().startAdvertising();
@@ -49,23 +76,31 @@
 
 void periodicCallback(void)
 {
- //   led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */
     RTC.get_time();
     const uint8_t refreshPassValues[6] = {RTC.seconds, RTC.minutes,RTC.hour,RTC.date,RTC.month,RTC.year};
     ble.updateCharacteristicValue( upasServicePtr->rtcCharacteristic.getValueHandle(),refreshPassValues,6);
-    RGB_LED.set_led(3,0,2);
+    
+}
+void periodicPrint(void)
+{
+ //   led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */
+    //RGB_LED.set_led(1,1,5);
+    
 }
 
+/*
+This function is called the BLE GATT Server every time a characterisitic is written to
+All logic revolving around which bits to write to EEPROM, and what to see the RTC to us done here
+*/
 void writeCharacteristicCallback(const GattWriteCallbackParams *params)
 {
     uint8_t *writeData =  const_cast<uint8_t*>(params->data);
-    RGB_LED.set_led(2,3,1);
     // check to see what characteristic was written, by handle
     if(params->handle == upasServicePtr->rtcCharacteristic.getValueHandle()) {
 
         //ble.updateCharacteristicValue(upasServicePtr->readChar.getValueHandle(),params->data,params->len);
         
-       // E2PROM.write(0x00015, writeData+6, 12);
+        E2PROM.write(0x00015, writeData+6, 12);
         RTC.set_time(writeData[0],writeData[1],writeData[2],writeData[3],writeData[3],writeData[4],writeData[5]);
    
     }else if(params->handle == upasServicePtr->sampleTimeCharacteristic.getValueHandle()){
@@ -76,9 +111,14 @@
         E2PROM.write(0x00001,writeData,15);
     
     }else if(params->handle == upasServicePtr->runReadyCharacteristic.getValueHandle()){
-        /* Trigger twenty-four run-time mode*/
-        RGB_LED.set_led(2,3,1);
-        //RunReady = 2;
+        uint8_t runData = writeData[0];
+        
+        if(runData == 10){
+             RunReady = 10;
+             RGB_LED.set_led(1,2,3);
+        }else{ 
+             RunReady = 2;
+        }
     
     }else if(params->handle == upasServicePtr->runModeCharacteristic.getValueHandle()){
         /* Trigger demo mode*/
@@ -89,9 +129,8 @@
 
 int main(void)
 {
-   // led1 = 1;
     Ticker ticker;
-    ticker.attach(periodicCallback, 10);
+    ticker.attach(periodicPrint, 600); //currently unused. But do not want to comment out
     RGB_LED.set_led(1,1,1);
     RTC.get_time();
     uint8_t rtcPassValues[6] = {RTC.seconds, RTC.minutes,RTC.hour,RTC.date,RTC.month,RTC.year};
@@ -117,11 +156,44 @@
     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().setAdvertisingInterval(30); /* 160ms. */
     ble.gap().startAdvertising();
     
     //Loop keeps device in infinite loop waiting for events to occur
     while (1) {
-        //ble.waitForEvent();
+        ble.waitForEvent();
+        if(RunReady==2 && blower ==0){ //Code used to see if one-click run should begin
+            calibrations.initialize(serial_num);
+            RGB_LED.set_led(3,0,2);
+            blower=1;
+            RunReady=0;
+         }
+         if(RunReady==2 && blower ==1){ //Code used to see if one-click run is done.
+            blower=0;
+            RunReady=0;
+         }
+         
+        if(RunReady==10){ //Check to see if app is done with configurations
+           blower = 0;
+           ble.gap().stopAdvertising();
+           break;
+       }
     }
+    uint8_t startAndEndTime[12] = {0,}; 
+    E2PROM.read(0x00015, startAndEndTime, 12); //Grab start and end times from EEPROM
+    while(!RTC.compare(startAndEndTime[0], startAndEndTime[1], startAndEndTime[2], startAndEndTime[3], startAndEndTime[4], startAndEndTime[5])) {  // this while waits for the start time by looping until the start time
+            wait(0.5);
+            RTC.get_time(); 
+
+        }
+    calibrations.initialize(serial_num);
+    blower=1;
+    
+    while(!RTC.compare(startAndEndTime[6], startAndEndTime[7], startAndEndTime[8], startAndEndTime[9], startAndEndTime[10], startAndEndTime[11])) { //Waits for end time
+        wait(0.5);
+        RTC.get_time(); 
+
+    }
+    pbKill = 0; // this is were we shut everything down
+
 }