implement voltage to ppm for example
[apps/native/st-things-co2-meter.git] / src / co2-sensor.c
index d269b9a..ce85c21 100644 (file)
@@ -1,27 +1,42 @@
-/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+/* ****************************************************************
  *
- * Licensed under the Flora License, Version 1.1 (the License);
+ * Copyright 2017 Samsung Electronics All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- * http://floralicense.org/license/
+ *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an AS IS BASIS,
+ * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
- */
+ *
+ ******************************************************************/
 
 #include <peripheral_io.h>
+#include <math.h>
 #include "adc-mcp3008.h"
 #include "log.h"
 
+#define CO2_SENSOR_VOLTAGE_GAIN (8.5)
+
 #define CO2_SENSOR_REF_VOLTAGE (5)
 #define CO2_SENSOR_VALUE_MAX (1024)
 
+/* CAUTION !!  These data are EXAMPLE Data, NOT REAL data */
+#define CO2_SENSOR_DEFAULT_VALUE_ZP (690) // 400ppm
+#define CO2_SENSOR_DEFAULT_VALUE_1000 (644)
+
+#define USE_EXAMPLE_CODE
+
+static const double log400 = 2.602;
+
 static bool initialized = false;
+static double co2_zp_volt = -1;
+static double slope_value = -1;
 
 void co2_sensor_close(void)
 {
@@ -39,6 +54,7 @@ int co2_sensor_read(int ch_num, unsigned int *out_value)
                retv_if(ret != 0, -1);
                initialized = true;
        }
+
        ret = adc_mcp3008_read(ch_num, &read_value);
        retv_if(ret != 0, -1);
 
@@ -47,24 +63,64 @@ int co2_sensor_read(int ch_num, unsigned int *out_value)
        return 0;
 }
 
-double co2_sensor_value_to_voltage(unsigned int value)
+static inline double __value_to_volt(unsigned int value)
+{
+       return (double)value
+               * (double)(CO2_SENSOR_REF_VOLTAGE / CO2_SENSOR_VALUE_MAX)
+               / CO2_SENSOR_VOLTAGE_GAIN;
+}
+
+static double __calc_slope(double zp_volt, double sec_volt, double x_axis_diff)
+{
+       return (zp_volt - sec_volt) / (x_axis_diff);
+}
+
+/* To use to calibrate ppm value*/
+int co2_sensor_set_calibration_values(unsigned int zero_point_v,
+               unsigned int second_point_ppm, unsigned int second_point_v)
 {
-       double v = 0;
+       double sec_volt = -1;
+       double x_axis_diff = -1;
+
+       retv_if(zero_point_v <= second_point_v, -1);
+       retv_if(second_point_ppm <= 400, -1);
 
-       v = (double)value * CO2_SENSOR_REF_VOLTAGE / CO2_SENSOR_VALUE_MAX;
+       co2_zp_volt = __value_to_volt(zero_point_v);
+       sec_volt = __value_to_volt(second_point_v);
+       x_axis_diff = log400 - log(second_point_ppm);
 
-       return v;
+       slope_value = __calc_slope(co2_zp_volt, sec_volt, x_axis_diff);
+
+       return 0;
 }
 
-/* Not implemented, please see c source code */
-unsigned int co2_sensor_voltage_to_ppm(double voltage)
+double co2_sensor_value_to_voltage(unsigned int value)
 {
-       int ppm = 0;
+       retv_if(value > 1023, -100000000.0); // out of value range
 
+       return __value_to_volt(value);
+}
+
+/* Not implemented !!! */
+unsigned int co2_sensor_voltage_to_ppm(double voltage)
+{
+       double ppm = 0;
        /* Make this function yourself */
        /* Please ref. 'Theory' section in https://sandboxelectronics.com/?p=147#Theory */
 
-       return ppm;
+       /* Example */
+#ifdef USE_EXAMPLE_CODE
+       if (slope_value < 0)
+               co2_sensor_set_calibration_values(CO2_SENSOR_DEFAULT_VALUE_ZP,
+                       CO2_SENSOR_DEFAULT_VALUE_1000, 1000);
+
+       if (voltage >= co2_zp_volt)
+               return 400;
+
+       ppm = pow(10, (voltage - co2_zp_volt)/slope_value + log400);
+#endif
+
+       return (unsigned int)ppm;
 }
 
 unsigned int co2_sensor_value_to_ppm(unsigned int value)