joey shelton / Mbed OS OT07_I2C_GUI_Firmware

Dependencies:   SDFileSystem max32630fthr USBDevice

Fork of FTHR_SD_Demo by Maxim Integrated

Revision:
24:454dc350bb17
Parent:
23:aa2407a3b30d
Child:
25:08b220797bd9
--- a/main.cpp	Fri Mar 22 19:57:15 2019 +0000
+++ b/main.cpp	Wed Mar 27 19:43:41 2019 +0000
@@ -39,6 +39,11 @@
     char I2C_address;           // I2C addess, based on GPIO0 and GPIO1 at power up
 }; 
 
+struct TempResponse {
+    double tempC;     // Temperature in °C 
+    int status;       // Status of Temperature read. 0 for success, 1 for error
+};
+
 const char* settings_file = "/sd/settings.txt";
 const char* log_file = "/sd/MAX30208Log.csv";
 
@@ -300,22 +305,27 @@
 //******************************************************************************
 // get_temperature(char)    read temperature from OT07 device FIFO register
 //                 char     I2C address
-// returns                  double temperature in oC
+// returns                  TempResponse tempC = temperature in oC 
+//                          status = register read result
 //******************************************************************************
 
-double get_temperature(char I2C_add){
+TempResponse get_temperature(char I2C_add){
     char data[2];
     double T;
     int count;
- 
-    OT07_read_register(I2C_add,OT07_FIFO_DATA,data,2);     // Read temperature from FIFO, 2 bytes 
-    //if(DEBUG)db.printf("get_temperature -- FIFO[%02X %02X]\r\n",data[0],data[1]);
+    
+    // Read temperature from FIFO, 2 bytes 
+    int error = OT07_read_register(I2C_add,OT07_FIFO_DATA,data,2);     
+    
     //calculate temperture from data     
     count = (int)(data[0]*256 + data[1]);
     if (count >= 32768)count = count - 65536;     // 2s comp
     T = (double)count*0.005; 
-
-    return T;  
+    
+    TempResponse resp;
+    resp.tempC = T;
+    resp.status = error; // 1 for nack/error. 0 for ack/success
+    return resp;  
 }
 
 
@@ -561,7 +571,7 @@
     char device_id[ID_LENGTH];                   //8 byte rom id of current slected device 
     char rom_id_list[MAX_DEVICES][ID_LENGTH];    //List of rom id for each device on OW bus 
     
-    double T[MAX_DEVICES];  
+    TempResponse T[MAX_DEVICES];  
     
     bool device_logged[MAX_DEVICES];
     //initialize device_logged to all false;
@@ -941,9 +951,9 @@
                                 if(n == 2){//get temperatures from selected device                                                            
                                         
                                     convert_temperature(OT07[device].I2C_address);  //send OW convert selected device
-                                    wait(0.02);  //wait 20 ms for convert temperature to complete                         
+                                    wait(0.02);  //wait 20 ms for convert temperature to complete 
                                     T[device] = get_temperature(OT07[device].I2C_address);
-                                    pc.printf("device[%02X]  temperature[%.3f]\r\n",device,T[device]);
+                                    pc.printf("device[%02X]  temperature[%.3f]  status[%d]\r\n",device,T[device].tempC, T[device].status);
                                 }
                                 
                                 if(n == 3){ // "t 1 3"  get temperature for devices 1 thru 3
@@ -958,7 +968,7 @@
                                     }
                                     
                                     for(j=device;j<=arg1;j++){                    
-                                        pc.printf("device[%02X]  temperature[%.3f]\r\n",j,T[j]);
+                                        pc.printf("device[%02X]  temperature[%.3f]  status[%d]\r\n",j,T[j].tempC, T[j].status);
                                     }  
                                 }
                                 
@@ -1003,16 +1013,6 @@
             if(time_count >= time_to_sample){ //take next sample
                 time_to_sample += log_interval;   // calculate time for next sample
                 
-                ///////////////////////temp conversion and assignment to T[]//////////////////////////////////////
-                // start conversion    
-                /*convert_temperature();
-                wait(0.02);               //wait 20ms for convert to complete
-                for(j=0;j<device_count;j++){ 
-                    for(k=0;k<8;k++){                   
-                        device_id[k] = rom_id_list[j][k];   // get device_id from rom_id_list
-                    }
-                    T[j] = get_temperature(device_id);            
-                }*/
                 for(j=0;j<device_count;j++){            
                     convert_temperature(OT07[j].I2C_address);
                     wait(0.02);  //wait 20ms for convert temperature to complete 
@@ -1030,7 +1030,7 @@
                     {
                         if(device_logged[j])
                         {
-                            fprintf(fp,",%.3f",T[j]);
+                            fprintf(fp,",%.3f",T[j].tempC);
                         }
                         
                     }