Code supports writing to the SD card as well as working with the Volckens group smartphone apps for the mbed HRM1017

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

Fork of UPAS_BLE_and_USB by Volckens Group Sensors

Revision:
71:78edbceff4fc
Parent:
70:81f04e69e08a
Child:
72:0b36575ab00d
--- a/main.cpp	Sun Aug 16 17:10:37 2015 +0000
+++ b/main.cpp	Wed Aug 19 18:05:51 2015 +0000
@@ -10,12 +10,34 @@
 #include "CronoDot.h"
 #include "EEPROM.h"
 #include "Honduras_SerialMenu.h"
+#include "BLEDevice.h" 
+#include "BLE_Menu.h"
+
+#define BLE_UUID_TXRX_SERVICE            0x0000 /**< The UUID of the Nordic UART Service. */
+#define BLE_UUID_TX_CHARACTERISTIC       0x0002 /**< The UUID of the TX Characteristic. */
+#define BLE_UUIDS_RX_CHARACTERISTIC      0x0003 /**< The UUID of the RX Characteristic. */
 
 #define SERIAL_BAUD_RATE    9600
 #define SCL         20
 #define SDA         22
 //#define Crono       0xD0       //D0 for the chronoDot
 
+uint8_t txPayload[TXRX_BUF_LEN] = {0,};
+uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
+
+static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
+static const uint8_t uart_tx_uuid[]   = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
+static const uint8_t uart_rx_uuid[]   = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
+static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
+
+GattCharacteristic  txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
+GattCharacteristic  rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
+GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
+GattService         uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
+
+BLEDevice   ble;
+BLE_Menu    bleMenu(ble, txPayload, rxPayload, uart_base_uuid_rev, uart_base_uuid, uartService);
+
 I2C i2c(p22, p20);
 Adafruit_ADS1115 ads(&i2c);
 MCP40D17 DigPot(&i2c);
@@ -112,6 +134,17 @@
 SDFileSystem sd(SPIS_PSELMOSI, SPIS_PSELMISO, SPIS_PSELSCK, SPIS_PSELSS, "sd"); // I believe this matches Todd's pinout, let me know if this doesn't work. (p12, p13, p15, p14)
 
 
+void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
+{
+    bleMenu.disconnectionCallback();
+}
+
+void WrittenHandler(const GattCharacteristicWriteCBParams *Handler) // called any time the phone sends a message
+{
+    bleMenu.WrittenHandler(pc, txPayload, txCharacteristic,  Handler);
+    //returns things to the txPayload
+}
+
 void check_stop()   // this checks if it's time to stop and shutdown
 {
     //RTC.get_time(); //debug
@@ -217,9 +250,24 @@
 
 // Setup and Initialization
 //---------------------------------------------------------------------------------------------//
-
     
-
+    //**************//BLE initialization//**************//
+    //bleMenu.read_settings(E2PROM, device_name);
+    uint8_t BLE_name[] = {device_name[0], device_name[1], device_name[2], device_name[3], device_name[4], device_name[5], device_name[6], device_name[7], device_name[8]};
+    ble.init(); 
+   // setup advertising 
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
+    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,(const uint8_t *)BLE_name, sizeof(BLE_name) - 1); // ~8 char max!
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
+    // 100ms; in multiples of 0.625ms. 
+    ble.setAdvertisingInterval(160);
+    ble.addService(uartService);
+    ble.startAdvertising();     
+    ble.onDisconnection(disconnectionCallback); //what happens when disconected
+    ble.onDataWritten(WrittenHandler);          //what happens when the phone sends a message
+    //**************//BLE initialization//**************//
+    
     Menu.read_menu(E2PROM, RunReady, volflowSet, device_name, dutyUp, dutyDown);       //Read all data from the EEPROM here
     
     //Test for errors
@@ -229,18 +277,23 @@
         Menu.Start(pc, E2PROM, RTC, RunReady, volflowSet, device_name, dutyUp, dutyDown);  //Forces you to open the menu
         Menu.save_menu(E2PROM, RunReady, volflowSet, device_name, dutyUp, dutyDown);       //Save all data to the EEPROM
     }
-    
+    //RunReady = 0; //debug always open the menu
     if(RunReady == 0){
-            RGB_LED.set_led(0,1,1); // error code/color
-            pc.printf("Change Name\r\n");
+        RGB_LED.set_led(0,1,1); // error code/color
+        pc.printf("Change Name\r\n");
+        while(RunReady == 0){
+            RGB_LED.set_led(0,1,1);   // error code/color
             Menu.Start(pc, E2PROM, RTC, RunReady, volflowSet, device_name, dutyUp, dutyDown);  //Forces you to open the menu
-            RunReady = 1;
-            Menu.save_menu(E2PROM, RunReady, volflowSet, device_name, dutyUp, dutyDown);       //Save all data to the EEPROM
-            pbKill = 0;
-            }
-            
-      RunReady = 0;
-      Menu.save_menu(E2PROM, RunReady, volflowSet, device_name, dutyUp, dutyDown);       //Save all data to the EEPROM      
+            RGB_LED.set_led(0,10,10); // error code/color
+            bleMenu.open_menu(txPayload, rxCharacteristic, device_name, RTC, RunReady);
+        }
+        //RunReady = 1;
+        Menu.save_menu(E2PROM, RunReady, volflowSet, device_name, dutyUp, dutyDown);       //Save all data to the EEPROM
+        pbKill = 0;
+    }
+    
+    RunReady = 0;
+    Menu.save_menu(E2PROM, RunReady, volflowSet, device_name, dutyUp, dutyDown);       //Save all data to the EEPROM      
 
     stop.attach(&check_stop, 60);    // check if we should shut down every 5 seconds, starting 60s after the start.