Simon Hawe / D7_MLX_AND_BAT

Dependencies:   X_NUCLEO_IKS01A1 MLX90614 d7a_1x wizzi-utils

Fork of D7A_1x_demo_sensors_OS5 by WizziLab

Revision:
8:01f0225408cf
Parent:
7:8de29807f970
Child:
9:25180d6a4c3a
Child:
11:7938e138cb7a
--- a/main.cpp	Thu Feb 02 17:30:47 2017 +0000
+++ b/main.cpp	Fri Feb 03 14:36:58 2017 +0000
@@ -38,10 +38,12 @@
 #include "DebouncedInterrupt.h"
 #include "files.h"
 
-// -----------------------------------------------
-// Hardware configuration
-// -----------------------------------------------
+
 #if defined(TARGET_STM32L152RE)
+
+    // -----------------------------------------------
+    // Hardware configuration for sh2001 / sh2030
+    // -----------------------------------------------
     #define D7A_PIN_TX              (D10)
     #define D7A_PIN_RX              (D2)
     #define D7A_PIN_RTS             (D13)
@@ -51,7 +53,36 @@
     #define DEBUG_BUTTON            (USER_BUTTON)
     #define SENSOR_I2C_SDA          (D14)
     #define SENSOR_I2C_SCL          (D15)
+ 
+    // -----------------------------------------------
+    // Select the sensors
+    // -1 : disable, 0 : simu, 1 : hardware   
+    // -----------------------------------------------
+    #ifdef TARGET_HAS_IKS01A1    
+        // use all sensors from IKS01A1 shield for Nucleo-64
+        #define _MAG_EN_            (1)
+        #define _ACC_EN_            (1)
+        #define _GYR_EN_            (1)
+        #define _PRE_EN_            (1)
+        #define _HUM_EN_            (1)
+        #define _TEM1_EN_           (1)
+        #define _TEM2_EN_           (1)
+    #else
+        // simulate sensors
+        #define _MAG_EN_            (0)
+        #define _ACC_EN_            (0)
+        #define _GYR_EN_            (0)
+        #define _PRE_EN_            (0)
+        #define _HUM_EN_            (0)
+        #define _TEM1_EN_           (0)
+        #define _TEM2_EN_           (0)
+    #endif // TARGET_HAS_IKS01A1
+
 #elif defined(TARGET_STM32L432KC)
+
+    // -----------------------------------------------
+    // Hardware configuration for sh2050
+    // -----------------------------------------------
     #define D7A_PIN_TX              (D5)
     #define D7A_PIN_RX              (D4)
     #define D7A_PIN_RTS             (D11)
@@ -61,6 +92,27 @@
     #define DEBUG_BUTTON            (D9)
     #define SENSOR_I2C_SDA          (D0)
     #define SENSOR_I2C_SCL          (D1)
+    #define SENSOR_LIGHT_EN         (A1)
+    #define SENSOR_LIGHT_MEAS       (A4)
+    
+    // -----------------------------------------------
+    // Select the sensors available on the sh2050
+    // -1 : disable, 0 : simu, 1 : hardware   
+    // -----------------------------------------------
+    #define _MAG_EN_                (0)
+    #define _ACC_EN_                (0)
+    #define _PRE_EN_                (1)
+    #define _HUM_EN_                (1)
+    #define _TEM1_EN_               (1)
+    #define _TEM2_EN_               (1)
+    #define _LIGHT_EN_              (1)
+    
+    // -----------------------------------------------
+    // Hardware I/O
+    // -----------------------------------------------
+    AnalogIn g_light_meas(SENSOR_LIGHT_MEAS);
+    DigitalOut g_light_en(SENSOR_LIGHT_EN);
+    
 #else
     #error "Please choose or add the right platform."
 #endif
@@ -68,17 +120,32 @@
 // Enable DBG led blinking (ms)
 #define DGB_LED_BLINK_PERIOD        (1000)
 
-// Define Sensor Mode
-#define _SENSORS_SIMU_              (1)
+// Defaults
+#ifndef _MAG_EN_
+    #define _MAG_EN_                (-1)
+#endif // _MAG_EN_
+#ifndef _ACC_EN_
+    #define _ACC_EN_                (-1)
+#endif // _ACC_EN_
+#ifndef _GYR_EN_    
+    #define _GYR_EN_                (-1)
+#endif // _GYR_EN_
+#ifndef _PRE_EN_
+    #define _PRE_EN_                (-1)
+#endif // _PRE_EN_
+#ifndef _HUM_EN_    
+    #define _HUM_EN_                (-1)
+#endif // _HUM_EN_
+#ifndef _TEM1_EN_
+    #define _TEM1_EN_               (-1)
+#endif // _TEM1_EN_
+#ifndef _TEM2_EN_
+    #define _TEM2_EN_               (-1)
+#endif // _TEM2_EN_
+#ifndef _LIGHT_EN_
+    #define _LIGHT_EN_              (-1)
+#endif // _LIGHT_EN_
 
-// Select the sensors
-#define _MAG_EN_                    (1)
-#define _ACC_EN_                    (1)
-#define _GYR_EN_                    (1)
-#define _PRE_EN_                    (1)
-#define _HUM_EN_                    (1)
-#define _TEM1_EN_                   (1)
-#define _TEM2_EN_                   (1)
 
 Semaphore button_user(0);
 Semaphore thread_ready(0);
@@ -276,93 +343,129 @@
 {
     return (int32_t)(v*100);
 }
-        
+
 bool mag_get_value(int32_t* buf)
 {
-#if (_SENSORS_SIMU_ == 1)
+#if (_MAG_EN_ == 0)
     return simul_sensor_value(buf, 3, -1900, 1900);
-#else
+#elif (_MAG_EN_ == 1)
     return CALL_METH(magnetometer, Get_M_Axes, buf, 0)? true : false;
+#else 
+    return false;
 #endif
 }
+SENSOR_THREAD_CTX(mag,  MAG_VALUE_FILE_ID,  MAG_CFG_FILE_ID,  mag_get_value,  3);
+
 
 bool acc_get_value(int32_t* buf)
 {
-#if (_SENSORS_SIMU_ == 1)
+#if (_ACC_EN_ == 0)
     return simul_sensor_value(buf, 3, -1900, 1900);
-#else
+#elif (_ACC_EN_ == 1)
     return CALL_METH(accelerometer, Get_X_Axes, buf, 0)? true : false;
+#else 
+    return false;
 #endif
 }
+SENSOR_THREAD_CTX(acc,  ACC_VALUE_FILE_ID,  ACC_CFG_FILE_ID,  acc_get_value,  3);
+
 
 bool gyr_get_value(int32_t* buf)
 {
-#if (_SENSORS_SIMU_ == 1)
+#if (_GYR_EN_ == 0)
     return simul_sensor_value(buf, 3, -40000, 40000);
-#else
+#elif (_GYR_EN_ == 1)
     return CALL_METH(gyroscope, Get_G_Axes, buf, 0)? true : false;
+#else 
+    return false;
 #endif
 }
+SENSOR_THREAD_CTX(gyr,  GYR_VALUE_FILE_ID,  GYR_CFG_FILE_ID,  gyr_get_value,  3);
+
 
 bool pre_get_value(int32_t* buf)
 {
-#if (_SENSORS_SIMU_ == 1)
+#if (_PRE_EN_ == 0)
     return simul_sensor_value(buf, 1, 96000, 104000);
-#else
+#elif (_PRE_EN_ == 1)
     bool err;
     float tmp;
     err = CALL_METH(pressure_sensor, GetPressure, &tmp, 0.0f)? true : false;
     buf[0] = float2_to_int(tmp);
     return err;
+#else 
+    return false;
 #endif
 }
+SENSOR_THREAD_CTX(pre,  PRE_VALUE_FILE_ID,  PRE_CFG_FILE_ID,  pre_get_value,  1);
+
 
 bool hum_get_value(int32_t* buf)
 {
-#if (_SENSORS_SIMU_ == 1)
+#if (_HUM_EN_ == 0)
     return simul_sensor_value(buf, 1, 1000, 9000);
-#else
+#elif (_HUM_EN_ == 1)
     bool err;
     float tmp;
     err = CALL_METH(humidity_sensor, GetHumidity, &tmp, 0.0f)? true : false;
     buf[0] = float2_to_int(tmp);
     return err;
+#else
+    return false;
 #endif
 }
+SENSOR_THREAD_CTX(hum,  HUM_VALUE_FILE_ID,  HUM_CFG_FILE_ID,  hum_get_value,  1);
 
 bool tem1_get_value(int32_t* buf)
 {
-#if (_SENSORS_SIMU_ == 1)
+#if (_TEM1_EN_ == 0)
     return simul_sensor_value(buf, 1, 1100, 3900);
-#else
+#elif (_TEM1_EN_ == 1)
     bool err;
     float tmp;
     err = CALL_METH(temp_sensor1, GetTemperature, &tmp, 0.0f)? true : false;
-    buf[0] = float2_to_int(tmp);
+    buf[0] = float2_to_int(tmp);    
     return err;
 #endif
 }
+SENSOR_THREAD_CTX(tem1, TEM1_VALUE_FILE_ID, TEM1_CFG_FILE_ID, tem1_get_value, 1);
+
 
 bool tem2_get_value(int32_t* buf)
 {
-#if (_SENSORS_SIMU_ == 1)
+#if (_TEM2_EN_ == 0)
     return simul_sensor_value(buf, 1, 5100, 10100);
-#else
+#elif (_TEM2_EN_ == 1)
     bool err;
     float tmp;
     err = CALL_METH(temp_sensor2, GetFahrenheit, &tmp, 0.0f)? true : false;
     buf[0] = float2_to_int(tmp);
     return err;
+#else
+    return false;
 #endif
 }
+SENSOR_THREAD_CTX(tem2, TEM2_VALUE_FILE_ID, TEM2_CFG_FILE_ID, tem2_get_value, 1);
 
-SENSOR_THREAD_CTX(mag,  MAG_VALUE_FILE_ID,  MAG_CFG_FILE_ID,  mag_get_value,  3);
-SENSOR_THREAD_CTX(acc,  ACC_VALUE_FILE_ID,  ACC_CFG_FILE_ID,  acc_get_value,  3);
-SENSOR_THREAD_CTX(gyr,  GYR_VALUE_FILE_ID,  GYR_CFG_FILE_ID,  gyr_get_value,  3);
-SENSOR_THREAD_CTX(pre,  PRE_VALUE_FILE_ID,  PRE_CFG_FILE_ID,  pre_get_value,  1);
-SENSOR_THREAD_CTX(hum,  HUM_VALUE_FILE_ID,  HUM_CFG_FILE_ID,  hum_get_value,  1);
-SENSOR_THREAD_CTX(tem1, TEM1_VALUE_FILE_ID, TEM1_CFG_FILE_ID, tem1_get_value, 1);
-SENSOR_THREAD_CTX(tem2, TEM2_VALUE_FILE_ID, TEM2_CFG_FILE_ID, tem2_get_value, 1);
+
+bool light_get_value(int32_t* buf)
+{
+#if (_LIGHT_EN_ == 0)
+    return simul_sensor_value(buf, 1, 0, 100);
+#elif (_LIGHT_EN_ == 1)
+    float tmp;
+    g_light_en = 0;
+    Thread::wait(10);
+    tmp = g_light_meas;
+    g_light_en = 1;
+    buf[0] = float2_to_int(tmp);
+    return false;
+#else
+    return false;
+#endif
+}
+SENSOR_THREAD_CTX(light, LIGHT_VALUE_FILE_ID, LIGHT_CFG_FILE_ID, light_get_value, 1);
+
 
 void sensor_thread()
 {
@@ -470,6 +573,9 @@
             case TEM2_CFG_FILE_ID:
                 fs_read_file(fid, 0, sizeof(sensor_config_t), (uint8_t*)&(tem2_thread_ctx.cfg));
                 break;
+            case LIGHT_CFG_FILE_ID:
+                fs_read_file(fid, 0, sizeof(sensor_config_t), (uint8_t*)&(light_thread_ctx.cfg));
+                break;                
             default:
                 break;
         }
@@ -551,10 +657,18 @@
     }
 }
 
-
 /*** Main function ------------------------------------------------------------- ***/
 int main()
 {
+    // I/O defaults
+#if defined(TARGET_STM32L152RE)
+     // xxx
+#elif defined(TARGET_STM32L432KC)
+    g_light_en = 1;
+#else
+    #error "Unsupported target"
+#endif
+
     // Start & initialize
     DBG_OPEN(DEBUG_LED);
     PRINT("\r\n--- Starting new run ---\r\n");
@@ -571,8 +685,6 @@
     d7a_msg_t** resp = d7a_write(65, 0, sizeof(d7a_revision_t), (uint8_t*)&f_dev_rev);
     d7a_free_msg(resp);
 
-#if (_SENSORS_SIMU_ == 1)
-    PRINT("(Simulated sensors)\r\n");
     uint32_t divider;
     
     // Retreive the simulation parameter from local file
@@ -584,32 +696,31 @@
     // Declare the simulation parameter file
     d7a_declare(SIMUL_FILE_ID, PERMANENT, RW_RW, sizeof(sensor_config_t), sizeof(sensor_config_t));
 
-#else
-        // Open I2C and initialise the sensors
+#if (_HUM_EN_ > 0 || _TEM1_EN_ > 0 || _MAG_EN_ > 0 || _ACC_EN_  > 0 || _GYR_EN_ > 0 || _PRE_EN_ > 0 || _TEM2_EN_ > 0)
+    // Open I2C and initialise the sensors
     DevI2C ext_i2c(SENSOR_I2C_SDA, SENSOR_I2C_SCL);
-    
-#if (_HUM_EN_ || _TEM1_EN_)
+#endif
+
+#if (_HUM_EN_ > 0 || _TEM1_EN_ > 0)
     humidity_sensor = new HTS221(ext_i2c);
     ASSERT(Init_HTS221(humidity_sensor), "Failed to init HTS221\r\n");
     temp_sensor1 = humidity_sensor;
 #endif // _TEM_EN_
-#if _MAG_EN_
+#if (_MAG_EN_ > 0)
     magnetometer = new LIS3MDL(ext_i2c);
     ASSERT(Init_LIS3MDL(magnetometer), "Failed to init LIS3MDL\r\n");
 #endif // _MAG_EN_
-#if (_ACC_EN_ || _GYR_EN_)
+#if (_ACC_EN_ > 0 || _GYR_EN_ > 0)
     accelerometer = new LSM6DS0(ext_i2c);
     ASSERT(Init_LSM6DS0(accelerometer), "Failed to init LSM6DS0\r\n");
     gyroscope = accelerometer;
 #endif // _ACC_EN_ || _GYR_EN_
-#if (_PRE_EN_ || _TEM2_EN_)
+#if (_PRE_EN_ > 0 || _TEM2_EN_ > 0)
     pressure_sensor = new LPS25H(ext_i2c);
     ASSERT(Init_LPS25H(pressure_sensor), "Failed to init LPS25H\r\n");
     temp_sensor2 = pressure_sensor;
 #endif // _PRE_EN_
 
-#endif // _SENSORS_SIMU_
-
     osStatus status;
 
     // Start sensors threads
@@ -619,27 +730,30 @@
                             ASSERT(status == osOK, "Failed to start ##_name## thread (err: %d)\r\n", status);\
                             thread_ready.wait();
                             
-#if _MAG_EN_
+#if (_MAG_EN_ >= 0)
     THREAD_START(mag);
 #endif // _MAG_EN_
-#if _ACC_EN_
+#if (_ACC_EN_ >= 0)
     THREAD_START(acc);
 #endif // _ACC_EN_
-#if _GYR_EN_
+#if (_GYR_EN_ >= 0)
     THREAD_START(gyr);
 #endif // _GYR_EN_
-#if _PRE_EN_
+#if (_PRE_EN_ >= 0)
     THREAD_START(pre);
 #endif // _PRE_EN_
-#if _HUM_EN_
+#if (_HUM_EN_ >= 0)
     THREAD_START(hum);
 #endif // _HUM_EN_
-#if _TEM1_EN_
+#if (_TEM1_EN_ >= 0)
     THREAD_START(tem1);
 #endif // _TEM1_EN_
-#if _TEM2_EN_
+#if (_TEM2_EN_ >= 0)
     THREAD_START(tem2);
 #endif // _TEM2_EN_
+#if (_LIGHT_EN_ >= 0)
+    THREAD_START(light);
+#endif // _LIGHT_EN_
 
     // File modified thread
     Thread fm_th(osPriorityNormal, 512, NULL);