7b6cad5a791bba2a797254600de2ba548131f30b
[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 = 10000;
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 * CO2_SENSOR_REF_VOLTAGE / CO2_SENSOR_VALUE_MAX / CO2_SENSOR_VOLTAGE_GAIN;
69 }
70
71 static double __calc_slope(double zp_volt, double sec_volt, double x_axis_diff)
72 {
73         return (zp_volt - sec_volt) / (x_axis_diff);
74 }
75
76 /* To use to calibrate ppm value*/
77 int co2_sensor_set_calibration_values(unsigned int zero_point_v,
78                 unsigned int second_point_ppm, unsigned int second_point_v)
79 {
80         double sec_volt = -1;
81         double x_axis_diff = -1;
82
83         retvm_if(zero_point_v <= second_point_v, -1, "%u - %u", zero_point_v, second_point_v);
84         retv_if(second_point_ppm <= 400, -1);
85
86         co2_zp_volt = __value_to_volt(zero_point_v);
87         sec_volt = __value_to_volt(second_point_v);
88         x_axis_diff = log400 - log10(second_point_ppm);
89         slope_value = __calc_slope(co2_zp_volt, sec_volt, x_axis_diff);
90
91         return 0;
92 }
93
94 double co2_sensor_value_to_voltage(unsigned int value)
95 {
96         retv_if(value > 1023, -100000000.0); // out of value range
97
98         return __value_to_volt(value);
99 }
100
101 /* Not implemented !!! */
102 unsigned int co2_sensor_voltage_to_ppm(double voltage)
103 {
104         double ppm = 0;
105         /* Make this function yourself */
106         /* Please ref. 'Theory' section in https://sandboxelectronics.com/?p=147#Theory */
107
108         /* Example */
109 #ifdef USE_EXAMPLE_CODE
110         if (slope_value > 0)
111                 co2_sensor_set_calibration_values(CO2_SENSOR_DEFAULT_VALUE_ZP,
112                                 1000, CO2_SENSOR_DEFAULT_VALUE_1000);
113
114         if (voltage >= co2_zp_volt)
115                 return 400;
116
117         ppm = pow(10, (voltage - co2_zp_volt)/slope_value + log400);
118 //      if (ppm > 10000)
119 //              ppm = 10000;
120 #endif
121
122         return (unsigned int)ppm;
123 }
124
125 unsigned int co2_sensor_value_to_ppm(unsigned int value)
126 {
127         /* You can use this function after implementing co2_sensor_voltage_to_ppm() function */
128         return co2_sensor_voltage_to_ppm(co2_sensor_value_to_voltage(value));
129 }
130