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:
118:9cf0a67fb581
Parent:
117:074ebef2dc12
Child:
120:c7818e89e3da
--- a/main.cpp	Thu Feb 18 04:28:56 2016 +0000
+++ b/main.cpp	Fri Feb 19 19:30:39 2016 +0000
@@ -168,110 +168,7 @@
 
 }
 
-/*
-void check_stop()   // this checks if it's time to stop and shutdown
-{
-    
-    if(RTC.compare(startAndEndTime[6], startAndEndTime[7], startAndEndTime[8], startAndEndTime[9], startAndEndTime[10], startAndEndTime[11])) {
-        pbKill = 0; // this is were we shut everything down
-        pc.printf("If you're reading this something has gone very wrong.");
-    }
-    stop.detach();
-    stop.attach(&check_stop, 9);
-    
-}
 
-//int r =0;
-//int g =1;
-//int b=1;
-void log_data()
-{
-    logg.detach();
-    logg.attach(&log_data, logInerval);     // reading and logging data must take significintly less than 0.5s. This can be increased.
-   // if(r==0) r=1;
-    //else r=0;
-    //RGB_LED.set_led(r,g,b);
-    //wait(0.5);
-    
-    RTC.get_time();
-    
-    omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
-    omronVolt = (omronReading*4.096)/(32768*2);
-
-    if(omronVolt<=calibrations.omronVMin) {
-        massflow = calibrations.omronMFMin;
-    } else if(omronVolt>=calibrations.omronVMax) {
-        massflow = calibrations.omronMFMax;
-    } else {
-        massflow = calibrations.MF4*pow(omronVolt,(float)4)+calibrations.MF3*pow(omronVolt,(float)3)+calibrations.MF2*pow(omronVolt,(float)2)+calibrations.MF1*omronVolt+calibrations.MF0;
-    }
-
-    atmoRho = ((press-((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)))*100)/(287.0531*(temp+273.15))+((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)*100)/(461.4964*(temp+273.15));
-    volflow = massflow/atmoRho;
-    sampledVol = sampledVol + ((((float)logInerval)/60.0)*volflow);
-    deltaVflow = volflow-volflowSet;
-    massflowSet = volflowSet*atmoRho;
-    deltaMflow = massflow-massflowSet;
-    if(abs(deltaMflow)>.025) {
-        digital_pot_change = (int)(gainFlow*deltaMflow);
-
-
-        if(abs(digital_pot_change)>=50) {
-            digital_pot_set = (int)(digital_pot_set+(int)((10.0*deltaMflow)));
-            RGB_LED.set_led(1,0,0);
-        }   else {
-            digital_pot_set = (digital_pot_set+ digital_pot_change);
-            RGB_LED.set_led(1,1,0);
-        }
-        
-                if(digital_pot_set>=digitalpotMax) {
-                    digital_pot_set = digitalpotMax;
-                    RGB_LED.set_led(1,0,0);
-                } else if(digital_pot_set<=digitalpotMin) {
-                    digital_pot_set = digitalpotMin;
-                    RGB_LED.set_led(1,0,0);
-                }
-
-
-        DigPot.writeRegister(digital_pot_set);
-
-    } else {
-        RGB_LED.set_led(0,1,0);
-    }
-    
-    movementsensor.getACCEL();
-    movementsensor.getCOMPASS();
-    compass = movementsensor.getCOMPASS_HEADING();
-    accel_x = movementsensor.AccelData.x;
-    accel_y = movementsensor.AccelData.y;
-    accel_z = movementsensor.AccelData.z;
-    accel_comp = pow(accel_x,(float)2)+pow(accel_y,(float)2)+pow(accel_z,(float)2)-1.0;
-    mag_x = movementsensor.MagData.x;
-    mag_y = movementsensor.MagData.y;
-    mag_z = movementsensor.MagData.z;
-    
-    vInReading = ads.readADC_SingleEnded(1, 0xD583); // read channel 0
-    vBlowerReading = ads.readADC_SingleEnded(2, 0xE783); // read channel 0
-    omronDiff = ads.readADC_Differential(0x8583); // differential channel 2-3
-    press = bmesensor.getPressure();
-    temp = bmesensor.getTemperature()-5.0;
-    rh = bmesensor.getHumidity();
-    uv =  lightsensor.getUV();
-    vis = lightsensor.getVIS();
-    ir = lightsensor.getIR();
-   
-    
-    FILE *fp = fopen(filename, "a");
-    fprintf(fp, "%02d,%02d,%02d,%02d,%02d,%02d,",RTC.year, RTC.month,RTC.date,RTC.hour,RTC.minutes,RTC.seconds);
-    fprintf(fp, "%1.3f,%1.3f,%2.2f,%4.2f,%2.1f,%1.3f,", omronVolt,massflow,temp,press,rh,atmoRho);
-    fprintf(fp, "%1.3f,%5.1f,%1.1f,%1.1f,%1.1f,%1.1f,", volflow, sampledVol, accel_x, accel_y, accel_z, accel_comp);
-    fprintf(fp, "%.1f,%.1f,%.1f,%.3f,%.3f,%.3f,%.1f,", angle_x,angle_y,angle_z,mag_x, mag_y, mag_z,compass);
-    fprintf(fp, "%d,%d,%d,%d,%d,%d," ,uv,omronReading, vInReading, vBlowerReading, omronDiff,gasG.getAmps());
-    fprintf(fp, "%d,%d,%d,%1.3f,%1.3f\r\n", gasG.getVolts(), gasG.getCharge(),digital_pot_set, deltaMflow, deltaVflow);
-    fclose(fp);
-    
-}
-*/
 
 /*EEPROM ADDRESSING:
     0:Status bit-Unused
@@ -342,48 +239,50 @@
     serialNumberAndType[4] = serialNumDigits[2]+48;
     serialNumberAndType[5] = serialNumDigits[3]+48;
     
+    uint8_t bleControlByte[1] = {0,};
+    E2PROM.read(0x00072,bleControlByte,1);
+    if(bleControlByte[0] == 0x01){
+        bleControlByte[0] = 0x02; 
+        ble.init();
+        ble.onDisconnection(disconnectionCallback);
+        ble.onDataWritten(WrittenHandler); //add writeCharCallback (custom function) to whenever data is being written to device
+        UPAS_Service upasService(ble, false,rtcPassValues,sampleTimePassValues,subjectLabelOriginal,dataLogOriginal,flowRateOriginal); //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
     
-    //E2PROM.read(0x00033,presetRunModeCheck,1); //commented out mem issue
-    ble.init();
-    ble.onDisconnection(disconnectionCallback);
-    ble.onDataWritten(WrittenHandler); //add writeCharCallback (custom function) to whenever data is being written to device
-    UPAS_Service upasService(ble, false,rtcPassValues,sampleTimePassValues,subjectLabelOriginal,dataLogOriginal,flowRateOriginal); //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 113+ 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) */
+        /* 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 113+ 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.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
+        ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
+        ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)serialNumberAndType, sizeof(serialNumberAndType));
+        ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+        ble.setAdvertisingInterval(160); /* 160ms. */
+        ble.startAdvertising();
+    
+        //**************//BLE initialization//**************//
+       
+       //Logic Loop waiting for responses from the BLE iPhone App.
+       //Will not break loop without response from the app
         
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)serialNumberAndType, sizeof(serialNumberAndType));
-    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-    ble.setAdvertisingInterval(160); /* 160ms. */
-    ble.startAdvertising();
-
-    //**************//BLE initialization//**************//
-   
-   //Logic Loop waiting for responses from the BLE iPhone App.
-   //Will not break loop without response from the app
-    
-    //outer loop commented out to prevent mem issues
-    //if(presetRunModeCheck[0] != 0x0C){
-       
+        
+        //Loop waits for response from app. When set to start, UPAS is turned off, but BLE is disabled once UPAS is turned on   
         while (1) {
             ble.waitForEvent();
              
             if(RunReady==10){ //Check to see if app is done with configurations         
                ble.stopAdvertising();
-               break;
+               E2PROM.write(0x00072,bleControlByte,1);
+               pbKill = 0;
            }
            
-            if(RunReady==12){ //If 24 hour mode has been set, then shut down the UPAS for automatic start later.       
-               pbKill = 0;
-           }
         }
+    }
+    bleControlByte[0] = 0x01;
+    E2PROM.write(0x00072,bleControlByte,1);
     
     E2PROM.read(0x00015, startAndEndTime, 12); //Grab start and end times from EEPROM
     RGB_LED.set_led(1,0,1);