implement voltage to ppm for example
[apps/native/st-things-co2-meter.git] / src / co2-sensor.c
1 /* ****************************************************************
2  *
3  * Copyright 2017 Samsung Electronics All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  ******************************************************************/
18
19 #include <peripheral_io.h>
20 #include <math.h>
21 #include "adc-mcp3008.h"
22 #include "log.h"
23
24 #define CO2_SENSOR_VOLTAGE_GAIN (8.5)
25
26 #define CO2_SENSOR_REF_VOLTAGE (5)
27 #define CO2_SENSOR_VALUE_MAX (1024)
28
29 /* CAUTION !!  These data are EXAMPLE Data, NOT REAL data */
30 #define CO2_SENSOR_DEFAULT_VALUE_ZP (690) // 400ppm
31 #define CO2_SENSOR_DEFAULT_VALUE_1000 (644)
32
33 #define USE_EXAMPLE_CODE
34
35 static const double log400 = 2.602;
36
37 static bool initialized = false;
38 static double co2_zp_volt = -1;
39 static double slope_value = -1;
40
41 void co2_sensor_close(void)
42 {
43         adc_mcp3008_fini();
44         initialized = false;
45 }
46
47 int co2_sensor_read(int ch_num, unsigned int *out_value)
48 {
49         unsigned int read_value = 0;
50         int ret = 0;
51
52         if (!initialized) {
53                 ret = adc_mcp3008_init();
54                 retv_if(ret != 0, -1);
55                 initialized = true;
56         }
57
58         ret = adc_mcp3008_read(ch_num, &read_value);
59         retv_if(ret != 0, -1);
60
61         *out_value = read_value;
62
63         return 0;
64 }
65
66 static inline double __value_to_volt(unsigned int value)
67 {
68         return (double)value
69                 * (double)(CO2_SENSOR_REF_VOLTAGE / CO2_SENSOR_VALUE_MAX)
70                 / CO2_SENSOR_VOLTAGE_GAIN;
71 }
72
73 static double __calc_slope(double zp_volt, double sec_volt, double x_axis_diff)
74 {
75         return (zp_volt - sec_volt) / (x_axis_diff);
76 }
77
78 /* To use to calibrate ppm value*/
79 int co2_sensor_set_calibration_values(unsigned int zero_point_v,
80                 unsigned int second_point_ppm, unsigned int second_point_v)
81 {
82         double sec_volt = -1;
83         double x_axis_diff = -1;
84
85         retv_if(zero_point_v <= second_point_v, -1);
86         retv_if(second_point_ppm <= 400, -1);
87
88         co2_zp_volt = __value_to_volt(zero_point_v);
89         sec_volt = __value_to_volt(second_point_v);
90         x_axis_diff = log400 - log(second_point_ppm);
91
92         slope_value = __calc_slope(co2_zp_volt, sec_volt, x_axis_diff);
93
94         return 0;
95 }
96
97 double co2_sensor_value_to_voltage(unsigned int value)
98 {
99         retv_if(value > 1023, -100000000.0); // out of value range
100
101         return __value_to_volt(value);
102 }
103
104 /* Not implemented !!! */
105 unsigned int co2_sensor_voltage_to_ppm(double voltage)
106 {
107         double ppm = 0;
108         /* Make this function yourself */
109         /* Please ref. 'Theory' section in https://sandboxelectronics.com/?p=147#Theory */
110
111         /* Example */
112 #ifdef USE_EXAMPLE_CODE
113         if (slope_value < 0)
114                 co2_sensor_set_calibration_values(CO2_SENSOR_DEFAULT_VALUE_ZP,
115                         CO2_SENSOR_DEFAULT_VALUE_1000, 1000);
116
117         if (voltage >= co2_zp_volt)
118                 return 400;
119
120         ppm = pow(10, (voltage - co2_zp_volt)/slope_value + log400);
121 #endif
122
123         return (unsigned int)ppm;
124 }
125
126 unsigned int co2_sensor_value_to_ppm(unsigned int value)
127 {
128         /* You can use this function after implementing co2_sensor_voltage_to_ppm() function */
129         return co2_sensor_voltage_to_ppm(co2_sensor_value_to_voltage(value));
130 }
131