Team Walter / Mbed OS NonPingPong_PICO_LoRa_LP0

Dependencies:   SX1276GenericLib USBDevice

Fork of NonPingPong_PICO_LoRa_LP0 by Walter Luu

Files at this revision

API Documentation at this revision

Comitter:
walterluu
Date:
Tue Oct 13 00:45:46 2020 +0000
Parent:
4:216dd74ca8e4
Child:
6:51f492ca61a2
Commit message:
Firmware Master and Slave in functional stage

Changed in this revision

AO32Lib/AO32_lib.cpp Show annotated file Show diff for this revision Revisions of this file
AO32Lib/AO32_lib.h Show annotated file Show diff for this revision Revisions of this file
MAX44009/MAX44009_lib.cpp Show annotated file Show diff for this revision Revisions of this file
MAX44009/MAX44009_lib.h Show annotated file Show diff for this revision Revisions of this file
OT07Lib/OT07_lib.cpp Show annotated file Show diff for this revision Revisions of this file
OT07Lib/OT07_lib.h Show annotated file Show diff for this revision Revisions of this file
global_buffers.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/AO32Lib/AO32_lib.cpp	Mon Oct 12 21:56:39 2020 +0000
+++ b/AO32Lib/AO32_lib.cpp	Tue Oct 13 00:45:46 2020 +0000
@@ -119,7 +119,8 @@
     int error = AO32_read_register(i2c, I2C_add, AO32_HARV_H, data, 2);     // burst read AO32_HARV_H and AO32_HARV_L
     
     // Calculate harvesting counts from data
-    countHi = int(data[0] << 8);        // might cause trouble, * 256 instead?
+//    countHi = int(data[0] << 8);        // might cause trouble, * 256 instead?
+    countHi = int(data[0]) * 256;
     countLo = int(data[1]);
     counts = countHi + countLo;
     
@@ -129,35 +130,24 @@
     return resp;  
 }
 
-
-//******************************************************************************
-// get_luxvalue(char)       read lux value from AO32 device register
-//                 char     I2C address
-// returns                  LuxResponse luxValue = light intensity in lux 
-//                          status = register read result
-//******************************************************************************
+double calc_OCV(char *data) {
+    
+    // data is an array of size 2, only needs the first byte
+    
+    double voltage = (double)(data[0]) / 100;
+    
+    return voltage;
+    
+}
 
-//LuxResponse get_luxvalue(I2C *i2c, char I2C_add){
-//    char data[2];       // 2 bytes of raw Lux Register 
-//    double lux;
-////    int count;
-//    
-//    // Read lux value, 2 bytes 
-//    int error = AO32_read_lux_register(i2c, I2C_add, AO32_LUX_HI, data);     
-//    
-//    //calculate lux from data     
-////    count = (int)(data[0]*256 + data[1]);
-////    if (count >= 32768)count = count - 65536;     // 2s comp
-////    T = (double)count*0.005; 
-//    
-//    int exponent;
-//    int mantissa;
-//    exponent = data[0] >> 4;
-//    mantissa = (data[0] << 4) + data[1];
-//    lux = 0.045 * mantissa * pow(2, exponent);
-//    
-//    LuxResponse resp;
-//    resp.luxValue = lux;
-//    resp.status = error; // 1 for nack/error. 0 for ack/success
-//    return resp;  
-//}
\ No newline at end of file
+int calc_Harvest(char *data) {
+    
+    // data is an array of size 2
+    
+    int countHi = int(data[0]) * 256;
+    int countLo = int(data[1]);
+    int harvest_counts = countHi + countLo;
+    
+    return harvest_counts;
+    
+}
\ No newline at end of file
--- a/AO32Lib/AO32_lib.h	Mon Oct 12 21:56:39 2020 +0000
+++ b/AO32Lib/AO32_lib.h	Tue Oct 13 00:45:46 2020 +0000
@@ -99,4 +99,8 @@
 // returns                  HarvCnt harvCount = Harvester Count "LX Pulses" in decimal
 //                          status = Status of harvesting count read. 0 for success, 1 for error
 //******************************************************************************
-HarvCnt get_Harvest(I2C *i2c, char I2C_add);
\ No newline at end of file
+HarvCnt get_Harvest(I2C *i2c, char I2C_add);
+
+double calc_OCV(char *data);
+
+int calc_Harvest(char *data);
\ No newline at end of file
--- a/MAX44009/MAX44009_lib.cpp	Mon Oct 12 21:56:39 2020 +0000
+++ b/MAX44009/MAX44009_lib.cpp	Tue Oct 13 00:45:46 2020 +0000
@@ -137,3 +137,14 @@
     return resp;  
 }
 
+double calc_lux(char *data) {
+    
+    // data is an array of size 2
+    
+    int exponent = int(data[0] >> 4);
+    int mantissa = (int)((data[0] << 4) & 0xF0) + (int)(data[1]);
+    double lux = 0.045 * mantissa * pow((double) 2, exponent);
+    
+    return lux;
+    
+}
\ No newline at end of file
--- a/MAX44009/MAX44009_lib.h	Mon Oct 12 21:56:39 2020 +0000
+++ b/MAX44009/MAX44009_lib.h	Tue Oct 13 00:45:46 2020 +0000
@@ -77,4 +77,6 @@
 // returns                  LuxResponse luxValue = light intensity in lux 
 //                          status = register read result
 //******************************************************************************
-LuxResponse get_luxvalue(I2C *i2c, char I2C_add);
\ No newline at end of file
+LuxResponse get_luxvalue(I2C *i2c, char I2C_add);
+
+double calc_lux(char *data);
\ No newline at end of file
--- a/OT07Lib/OT07_lib.cpp	Mon Oct 12 21:56:39 2020 +0000
+++ b/OT07Lib/OT07_lib.cpp	Tue Oct 13 00:45:46 2020 +0000
@@ -140,6 +140,18 @@
     return resp;  
 }
 
+
+double calc_temperature(char *data) {
+    
+    // data is an array of size 2
+    
+    int count = (int)(data[0]*256 + data[1]);
+    if (count >= 32768)count = count - 65536;     // 2s comp
+    double Temp = (double)count*0.005; 
+    
+    return Temp;
+}
+
 // other functions
 
 //void write_settings_file(int interval, bool device_logged[MAX_DEVICES])
--- a/OT07Lib/OT07_lib.h	Mon Oct 12 21:56:39 2020 +0000
+++ b/OT07Lib/OT07_lib.h	Tue Oct 13 00:45:46 2020 +0000
@@ -79,4 +79,6 @@
 // returns                  TempResponse tempC = temperature in oC 
 //                          status = register read result
 //******************************************************************************
-TempResponse get_temperature(I2C *i2c, char I2C_add);
\ No newline at end of file
+TempResponse get_temperature(I2C *i2c, char I2C_add);
+
+double calc_temperature(char *data);
\ No newline at end of file
--- a/global_buffers.h	Mon Oct 12 21:56:39 2020 +0000
+++ b/global_buffers.h	Tue Oct 13 00:45:46 2020 +0000
@@ -28,12 +28,12 @@
 /***************************************************************************
  * MASTER Device
  **************************************************************************/\
-//#define MASTER 1
+#define MASTER 1
 
 /***************************************************************************
  * SLAVE Device
  **************************************************************************/\
-#define SLAVE 1
+//#define SLAVE 1
 
 
 /***************************************************************************
--- a/main.cpp	Mon Oct 12 21:56:39 2020 +0000
+++ b/main.cpp	Tue Oct 13 00:45:46 2020 +0000
@@ -120,10 +120,10 @@
      **************************************************************************/
     #if   MASTER == 1 // Master Device
 //        uint8_t curr_MAX77650_from_slave[size_of_MAX77650];
-        uint8_t curr_raw_temp_from_slave[size_of_MAX30208];
+//        uint8_t curr_raw_temp_from_slave[size_of_MAX30208];
+        char curr_raw_temp_from_slave[size_of_MAX30208];   // to match data type
 //        uint8_t prev_MAX77650_from_slave[size_of_MAX77650];
-        uint8_t prev_raw_temp_from_slave[size_of_MAX30208];
-//        bool chrg_status = false;; //True = ON False = OFF    
+//        uint8_t prev_raw_temp_from_slave[size_of_MAX30208];  
     #elif SLAVE == 1 // Slave Device
 //        uint8_t curr_MAX77650_to_master[size_of_MAX77650];
 //        bool chrg_status = false; //True = ON False = OFF
@@ -135,8 +135,9 @@
      * MAX44009 Data Buffers
      **************************************************************************/
     #if   MASTER == 1 // Master Device
-        uint8_t curr_raw_light_from_slave[size_of_MAX44009];
-        uint8_t prev_raw_light_from_slave[size_of_MAX44009];
+//        uint8_t curr_raw_light_from_slave[size_of_MAX44009];
+        char curr_raw_light_from_slave[size_of_MAX44009];    // to match data type
+//        uint8_t prev_raw_light_from_slave[size_of_MAX44009];
     //    static int16_t conv_frame_data_from_slave[64];
     #elif SLAVE == 1 // Slave Device
         uint8_t curr_raw_light_to_master[size_of_MAX44009];
@@ -148,8 +149,9 @@
      * MAX20361 Data Buffers
      **************************************************************************/
     #if   MASTER == 1 // Master Device
-        uint8_t curr_raw_AO32_from_slave[size_of_MAX20361];
-        uint8_t prev_raw_AO32_from_slave[size_of_MAX20361];
+//        uint8_t curr_raw_AO32_from_slave[size_of_MAX20361];
+        char curr_raw_AO32_from_slave[size_of_MAX20361];            // to match data type
+//        uint8_t prev_raw_AO32_from_slave[size_of_MAX20361];
     //    static int16_t conv_frame_data_from_slave[64];
     #elif SLAVE == 1 // Slave Device
         uint8_t curr_raw_AO32_to_master[size_of_MAX20361];
@@ -237,7 +239,7 @@
     
     //************* MAX44009 Variables  ****************
     char rawluxdata[2];          
-    char MAX44009_i2c_add = 0x96;      // 0b1001 011x
+    char MAX44009_i2c_add = 0x94;      // 0b1001 010x
     
     //************* AO32 Variables  ****************
     char rawOCVdata[2];                 // only needs 1
@@ -268,73 +270,81 @@
         convert_temperature(&i2cBus0, OT07_i2c_add);  //send OW convert selected device
         wait_ms(CONVERT_T_DELAY);  //wait 20 ms for convert temperature to complete
         int temp_error = OT07_read_register(&i2cBus0, OT07_i2c_add, OT07_FIFO_DATA, rawtempdata, 2); 
-        pc.printf("OT07 add[%02X] data[%02X] data[%02X]\r\n", OT07_FIFO_DATA, rawtempdata[0], rawtempdata[1]);  
+//        pc.printf("OT07 add[%02X] data[%02X] data[%02X]\r\n", OT07_FIFO_DATA, rawtempdata[0], rawtempdata[1]);  
             
         //calculate temperature from data     
         int count = (int)(rawtempdata[0]*256 + rawtempdata[1]);
         if (count >= 32768)count = count - 65536;     // 2s comp
         double Temp = (double)count*0.005; 
         pc.printf("OT07  temperature[%.3f]  status[%d]\r\n", Temp, temp_error);
+        double tempFinal = calc_temperature(rawtempdata);
+        pc.printf("OT07  Final temperature[%.3f] \r\n", tempFinal);
+        pc.printf("\r\n");
+        
         
         //fill raw temp data into the array
         curr_raw_temp_to_master[0] = rawtempdata[0];
         curr_raw_temp_to_master[1] = rawtempdata[1];      
-            
-       // for (int i = 0; i < 3; i++) {
-//            convert_temperature(&i2cBus0, i2c_add);  //send OW convert selected device
-//            wait_ms(CONVERT_T_DELAY);  //wait 20 ms for convert temperature to complete 
-//            T = get_temperature(&i2cBus0, i2c_add);
-//            pc.printf("OT07  temperature[%.3f]  status[%d]\r\n", T.tempC, T.status);
-//        }
         
         /***************************************************************************
         * Light Intensity Sensor Data Measurement
         **************************************************************************/
         // obtain register hex values
-//        int lux_error = MAX44009_read_lux_register(&i2cBus0, MAX44009_i2c_add, MAX44009_LUX_HI, rawluxdata);
-        int lux_error1 = MAX44009_read_register(&i2cBus0, MAX44009_i2c_add, MAX44009_LUX_HI, &rawluxdata[0]);
-        int lux_error2 = MAX44009_read_register(&i2cBus0, MAX44009_i2c_add, MAX44009_LUX_LO, &rawluxdata[1]);
-        pc.printf("MAX44009 hi_add[%02X] hi_data[%02X] lo_add[%02X] lo_data[%02X]\r\n", MAX44009_LUX_HI, rawluxdata[0], MAX44009_LUX_LO, rawluxdata[1]);
+        int lux_error = MAX44009_read_lux_register(&i2cBus0, MAX44009_i2c_add, MAX44009_LUX_HI, rawluxdata);
+//        int lux_error1 = MAX44009_read_register(&i2cBus0, MAX44009_i2c_add, MAX44009_LUX_HI, &rawluxdata[0]);
+//        int lux_error2 = MAX44009_read_register(&i2cBus0, MAX44009_i2c_add, MAX44009_LUX_LO, &rawluxdata[1]);
+//        pc.printf("MAX44009 hi_add[%02X] hi_data[%02X] lo_add[%02X] lo_data[%02X]\r\n", MAX44009_LUX_HI, rawluxdata[0], MAX44009_LUX_LO, rawluxdata[1]);
         
         //calculate temperature from data  
         int exponent = int(rawluxdata[0] >> 4);
-        int mantissa = int(rawluxdata[0] << 4) + int(rawluxdata[1]);
+        int mantissa = (int)((rawluxdata[0] << 4) & 0xF0) + (int)(rawluxdata[1]);
         double lux = 0.045 * mantissa * pow((double) 2, exponent);
-        pc.printf("MAX44009  exponent[%d]  mantissa[%d]\r\n", exponent, mantissa);
-        pc.printf("MAX44009  lux[%f]  status[%d] status[%d]\r\n", lux, lux_error1, lux_error2);
+//        pc.printf("MAX44009  exponent[%d]  mantissa[%d]\r\n", exponent, mantissa);
+//        pc.printf("MAX44009  lux[%.2f]  status[%d] status[%d]\r\n", lux, lux_error1, lux_error2);
+        pc.printf("MAX44009  lux[%.2f]  status[%d]\r\n", lux, lux_error);
+        double luxFinal = calc_lux(rawluxdata);
+        pc.printf("MAX44009  Final Lux[%.2f] \r\n", luxFinal);
+        pc.printf("\r\n");
         
         //fill raw lux data into the array
-//        curr_raw_light_to_master[0] = rawluxdata[0];
-//        curr_raw_light_to_master[1] = rawluxdata[1];
+        curr_raw_light_to_master[0] = rawluxdata[0];
+        curr_raw_light_to_master[1] = rawluxdata[1];
         
         
         /***************************************************************************
         * Solar Harvester Data Measurement
         **************************************************************************/
         // obtain register hex values
-        char AO32ID[2];
-        int id_error = AO32_read_register(&i2cBus0, AO32_i2c_add, AO32_DEVICE_ID, AO32ID);  // testing
-        pc.printf("AO32 add[%02X] data[%02X]\r\n", AO32_DEVICE_ID, AO32ID[0]);              // should be 0x00: 0x11
+//        char AO32ID[2];
+//        int id_error = AO32_read_register(&i2cBus0, AO32_i2c_add, AO32_DEVICE_ID, AO32ID);  // testing
+//        pc.printf("AO32 add[%02X] data[%02X]\r\n", AO32_DEVICE_ID, AO32ID[0]);              // should be 0x00: 0x11
         
         int ocv_error = AO32_read_register(&i2cBus0, AO32_i2c_add, AO32_VOC, rawOCVdata);
-        pc.printf("AO32 add[%02X] data[%02X]\r\n", AO32_VOC, rawOCVdata[0]);
+//        pc.printf("AO32 add[%02X] data[%02X]\r\n", AO32_VOC, rawOCVdata[0]);
         int cnt_error = AO32_read_register(&i2cBus0, AO32_i2c_add, AO32_HARV_H, rawCntdata, 2); // burst read 2 bytes
-        pc.printf("AO32 hi_add[%02X] hi_data[%02X] lo_add[%02X] lo_data[%02X]\r\n", AO32_HARV_H, rawCntdata[0], AO32_HARV_L, rawCntdata[1]);
+//        pc.printf("AO32 hi_add[%02X] hi_data[%02X] lo_add[%02X] lo_data[%02X]\r\n", AO32_HARV_H, rawCntdata[0], AO32_HARV_L, rawCntdata[1]);
         
         //calculate open circuit voltage from data
-        double voltage = int(rawOCVdata[0]) / 100;
+        double voltage = (double)(rawOCVdata[0]) / 100;
         pc.printf("AO32  OCV[%.2f]  status[%d]\r\n", voltage, ocv_error);  
+        double OCVFinal = calc_OCV(rawOCVdata);
+        pc.printf("AO32  Final OCV[%.2f]  \r\n", OCVFinal);
+        pc.printf("\r\n");
         
         //calculate harvesting counts from data
-        int countHi = int(rawCntdata[0] << 8); // might cause trouble, * 256 instead?
+        int countHi = int(rawCntdata[0]) * 256;
         int countLo = int(rawCntdata[1]);
         int harvest_counts = countHi + countLo;
         pc.printf("AO32  HarvesterCnt[%d]  status[%d]\r\n", harvest_counts, cnt_error); 
+        int countFinal = calc_Harvest(rawCntdata);
+        pc.printf("AO32 Final HarvesterCnt[%d]\r\n",  countFinal);
+        pc.printf("\r\n");
+        
         
         //fill raw AO32 data into the array
-//        curr_raw_AO32_to_master[0] = rawOCVdata[0];         // Fill OCV hex first
-//        curr_raw_AO32_to_master[1] = rawCntdata[0];         // Fill Harvesting count high byte
-//        curr_raw_AO32_to_master[2] = rawCntdata[1];         // Fill Harvesting count low byte
+        curr_raw_AO32_to_master[0] = rawOCVdata[0];         // Fill OCV hex first
+        curr_raw_AO32_to_master[1] = rawCntdata[0];         // Fill Harvesting count high byte
+        curr_raw_AO32_to_master[2] = rawCntdata[1];         // Fill Harvesting count low byte
         
         /***************************************************************************
         * Fill Payload Buffer With Data From Main Program Buffers for next LoRa Transmition
@@ -380,10 +390,10 @@
         * Slave Device: Print out Master Data
         **************************************************************************/
         //        memcpy(ID_of_master,              &BufferRx[rx_idx_signature], size_signature);
-        pc.printf("Print ID_of_master\r\n");
-        for(int i = 0; i < sizeof(ID_of_master); i++){
-            pc.printf("%d \r\n", ID_of_master[i]);
-        }
+//        pc.printf("Print ID_of_master\r\n");
+//        for(int i = 0; i < sizeof(ID_of_master); i++){
+//            pc.printf("%d \r\n", ID_of_master[i]);
+//        }
             
         //        memcpy(curr_dum_from_master,      &BufferRx[rx_idx_dum],       size_of_dum);
 //        pc.printf("Print Dum From Master\r\n");
@@ -411,7 +421,7 @@
 //    SX1276GateWayReceive(333);
     SX1276PingPong();
     int sendTime = TimeOnAirSend();
-    pc.printf("Tx Time on Air: %d \r\n", sendTime);
+//    pc.printf("Tx Time on Air: %d \r\n", sendTime);
     
     /***************************************************************************
      * Fill Main Program Buffers With Data From Received Payload Buffer
@@ -433,30 +443,44 @@
 //    }
     
     //        memcpy(curr_raw_temp_from_slave, &BufferRx[rx_idx_MAX30208],  size_of_MAX30208);
-    pc.printf("Print MAX30208 data\r\n");
-    for(int i = 0; i < sizeof(curr_raw_temp_from_slave); i++){
-        pc.printf("%d \r\n", curr_raw_temp_from_slave[i]); 
-    }
-    
+//    pc.printf("Print MAX30208 data\r\n");
+//    for(int i = 0; i < sizeof(curr_raw_temp_from_slave); i++){
+//        pc.printf("[%02X]\r\n", curr_raw_temp_from_slave[i]); 
+//    } 
     
-    int count = curr_raw_temp_from_slave[0] * 256 +  curr_raw_temp_from_slave[1];
-    if (count >= 32768)count = count - 65536;     // 2s comp
-    double tempResult = count * 0.005; 
-//    pc.printf("OT07  temperature[%.3f]  status[%d]\r\n", Temp, error);
-    pc.printf("MSG: [%.3f] [] [] [] []\r\n", tempResult);
+//    int count = curr_raw_temp_from_slave[0] * 256 +  curr_raw_temp_from_slave[1];
+//    if (count >= 32768)count = count - 65536;     // 2s comp
+//    double tempResult = count * 0.005; 
+    
+    double tempResult = calc_temperature(curr_raw_temp_from_slave);
+//    pc.printf("MSG: [%.3f] [] [] [] []\r\n", tempResult);
     
             
     //        memcpy(curr_raw_light_from_slave, &BufferRx[rx_idx_MAX44009], size_of_MAX44009);
-    // pc.printf("Print MAX44009 data\r\n");
+//     pc.printf("Print MAX44009 data\r\n");
 //    for(int i = 0; i < sizeof(curr_raw_light_from_slave); i++){
-//        pc.printf("%d \r\n", curr_raw_light_from_slave[i]);
+//        pc.printf("[%02X] \r\n", curr_raw_light_from_slave[i]);
 //    }
+    
+    double luxResult = calc_lux(curr_raw_light_from_slave);
+//    pc.printf("MSG: [%.3f] [%.2f] [] [] []\r\n", tempResult, luxResult);
             
     //        memcpy(curr_raw_AO32_from_slave,  &BufferRx[rx_idx_MAX20361],  size_of_MAX20361);
-    //pc.printf("Print MAX20361 data\r\n");
+//    pc.printf("Print MAX20361 data\r\n");
 //    for(int i = 0; i < sizeof(curr_raw_AO32_from_slave); i++){
-//        pc.printf("%d \r\n", curr_raw_AO32_from_slave[i]);
+//        pc.printf("[%02X] \r\n", curr_raw_AO32_from_slave[i]);
 //    }
+    
+    char OCVrawHex[2];
+    OCVrawHex[0] = curr_raw_AO32_from_slave[0];
+    OCVrawHex[1] = curr_raw_AO32_from_slave[1];
+    char CntrawHex[2];
+    CntrawHex[0] = curr_raw_AO32_from_slave[2];
+    CntrawHex[1] = curr_raw_AO32_from_slave[3];
+    
+    double OCVResult = calc_OCV(OCVrawHex);
+    int CntResult = calc_Harvest(CntrawHex);
+    pc.printf("MSG: [%.3f] [%.2f] [%.2f] [%d] []\r\n", tempResult, luxResult, OCVResult, CntResult);
             
     //        memcpy(curr_raw_other_from_slave,  &BufferRx[rx_idx_other],  size_of_other);
     //pc.printf("Print Other data\r\n");
@@ -465,7 +489,7 @@
 //    }
     
     // print loop counter
-    pc.printf("Loop Counter Master: %d \r\n", loopCnt);
+//    pc.printf("Loop Counter Master: %d \r\n", loopCnt);
     loopCnt = loopCnt + 1;
     
 //    wait(3);