5c58667bfbc481fd77f212759999cb1822cc6db2
[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 //#define USE_EXAMPLE_CODE
30
31 #ifdef USE_EXAMPLE_CODE
32 /* CAUTION !!  These data are EXAMPLE Data, NOT REAL data */
33 #define CO2_SENSOR_DEFAULT_VALUE_ZP (690) // 400ppm
34 #define CO2_SENSOR_DEFAULT_VALUE_1000 (600)
35 #endif /* USE_EXAMPLE_CODE */
36
37 static const double log400 = 2.602;
38
39 static bool initialized = false;
40 static double co2_zp_volt = -1;
41 static double slope_value = 10000; // A real slope value should be a negative value.
42
43 void co2_sensor_close(void)
44 {
45         adc_mcp3008_fini();
46         initialized = false;
47 }
48
49 int co2_sensor_read(int ch_num, unsigned int *out_value)
50 {
51         unsigned int read_value = 0;
52         int ret = 0;
53
54         if (!initialized) {
55                 ret = adc_mcp3008_init();
56                 retv_if(ret != 0, -1);
57                 initialized = true;
58         }
59
60         ret = adc_mcp3008_read(ch_num, &read_value);
61         retv_if(ret != 0, -1);
62
63         *out_value = read_value;
64
65         return 0;
66 }
67
68 static inline double __value_to_volt(unsigned int value)
69 {
70         return (double)value * CO2_SENSOR_REF_VOLTAGE / CO2_SENSOR_VALUE_MAX / 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         retvm_if(zero_point_v <= second_point_v, -1, "%u - %u", zero_point_v, second_point_v);
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 - log10(second_point_ppm);
91         slope_value = __calc_slope(co2_zp_volt, sec_volt, x_axis_diff);
92
93         return 0;
94 }
95
96 double co2_sensor_value_to_voltage(unsigned int value)
97 {
98         retv_if(value > 1023, -100000000.0); // out of value range
99
100         return __value_to_volt(value);
101 }
102
103 /* Not implemented !!! */
104 unsigned int co2_sensor_voltage_to_ppm(double voltage)
105 {
106         double ppm = 0;
107         /* Make this function yourself */
108         /* Please ref. 'Theory' section in https://sandboxelectronics.com/?p=147#Theory */
109
110         /* Example */
111 #ifdef USE_EXAMPLE_CODE
112         if (slope_value > 0)
113                 co2_sensor_set_calibration_values(CO2_SENSOR_DEFAULT_VALUE_ZP,
114                                 1000, CO2_SENSOR_DEFAULT_VALUE_1000);
115
116         if (voltage >= co2_zp_volt)
117                 return 400;
118
119         ppm = pow(10, (voltage - co2_zp_volt)/slope_value + log400);
120 //      if (ppm > 10000)
121 //              ppm = 10000;
122 #endif /* USE_EXAMPLE_CODE */
123
124         return (unsigned int)ppm;
125 }
126
127 unsigned int co2_sensor_value_to_ppm(unsigned int value)
128 {
129         /* You can use this function after implementing co2_sensor_voltage_to_ppm() function */
130         return co2_sensor_voltage_to_ppm(co2_sensor_value_to_voltage(value));
131 }
132