Mbed library for SPS30 i2c communication (Configured to be interfaced with TresidderOS) Inherits members and methods from SensorDriver class

Dependents:   SPS30_TEST

Files at this revision

API Documentation at this revision

Comitter:
ziqiyap
Date:
Thu Mar 28 01:14:03 2019 +0000
Parent:
9:a5fe43e183e2
Commit message:
added test cases; added timeout counter for data not ready

Changed in this revision

sps30.cpp Show annotated file Show diff for this revision Revisions of this file
sps30.h Show annotated file Show diff for this revision Revisions of this file
--- a/sps30.cpp	Wed Mar 27 06:04:36 2019 +0000
+++ b/sps30.cpp	Thu Mar 28 01:14:03 2019 +0000
@@ -415,10 +415,12 @@
         int val_data;
         if (i < 4) 
         {
+//            sensor_float_data[i] = 99999.00;        // test use case 3c; expected output: EmTrace for sensor data out-of-range with data value
             val_data = ValidateData(sensor_float_data[i], MASS_MIN, MASS_MAX);
         }
         else if (i < 9) 
         {
+//            sensor_float_data[i] = 99999.00;        // test use case 3c; expected output: EmTrace for sensor data out-of-range with data value
             val_data = ValidateData(sensor_float_data[i], NUM_MIN, NUM_MAX);
         }
 
@@ -442,16 +444,20 @@
     if (dat == SPSNOACKERROR) 
     {
         SendSensorEmTrace(SENSOR_DCN);
+        sps_status = 0;
     }
     
     dat = Sps30::StartMeasurement();
     if (dat == SPSNOACKERROR) 
     {
         SendSensorEmTrace(SENSOR_DCN);
+        sps_status = 0;
     }
     
     SendSensorEmTrace(SENSOR_CN);
     sensor_serial = ConvertSerialNumber(sn);
+    sps_status = 1;
+    t_count = 0;
 
     return;
 }
@@ -465,13 +471,21 @@
     uint8_t dat = Sps30::GetReadyStatus();
     if (dat == SPSNOACKERROR)
     {
-        SendSensorEmTrace(SENSOR_DCN);
+        if (sps_status == 1) SendSensorEmTrace(SENSOR_DCN);
+        sps_status = 0;
         return SENSOR_DCN;
     }
     
+    if (sps_status == 0) 
+    {
+        Sps30::InitSensor();   // If was disconnected, reinitialise
+        Sps30::GetReadyStatus();
+    }
+//    sps_ready = 0;               // test use case 3b; expeced output: EMTrace for sensor data not ready
     if (sps_ready == SPSISREADY)
     {
         uint8_t crcc = Sps30::ReadMeasurement();
+//        crcc = SPSCRCERROR;      // test use case 3b; expeced output: EMTrace for sensor data not ready
         if (crcc == SPSNOERROR)
         {
             Sps30::GetDataArray();
@@ -480,6 +494,7 @@
         else if (crcc == SPSNOACKERROR)
         {
             SendSensorEmTrace(SENSOR_DCN);
+            sps_status = 0;
             return SENSOR_DCN;
         }
         else
@@ -489,8 +504,15 @@
         }
     }
     else
-    {
-        SendSensorEmTrace(SENSOR_DATAERR);
+    {        
+        if (t_count == TIMEOUT_COUNT)     // timeout counter for data not ready
+        {
+            SendSensorEmTrace(SENSOR_DATAERR);
+            Sps30::SoftReset();
+            t_count = 0;
+        }
+        t_count++;
+
         return SENSOR_DATAERR;
     }
 }
--- a/sps30.h	Wed Mar 27 06:04:36 2019 +0000
+++ b/sps30.h	Thu Mar 28 01:14:03 2019 +0000
@@ -37,6 +37,7 @@
 #define NUM_MIN             0.00f
 
 #define I2C_FREQUENCY_STD   100000              // SPS30 uses 100MHz for I2C communication
+#define TIMEOUT_COUNT       3
 
 /** Create SPS30 controller class
  * @brief inherits SensorDriver members and methods essential for Sensor Thread
@@ -94,8 +95,10 @@
     };
     
     char i2cbuff[60];
-        
-    uint8_t sn[33];         /**< ASCII Serial Number */
+    
+    bool sps_status;    /**< status bit */
+    int t_count;        /**< timeout counter */
+    uint8_t sn[33];     /**< ASCII Serial Number */
     
     std::vector<float> sensor_float_data;       /** Vector of sensor float data **/
     std::vector<std::string> sensor_data_name;  /** Private list of sensor data types **/