add functions to store boolean type sensor data
[apps/native/st-things-co2-meter.git] / src / co2-sensor.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Flora License, Version 1.1 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <peripheral_io.h>
18 #include "adc-mcp3008.h"
19 #include "log.h"
20
21 #define CO2_SENSOR_REF_VOLTAGE (5)
22 #define CO2_SENSOR_VALUE_MAX (1024)
23
24 static bool initialized = false;
25
26 void co2_sensor_close(void)
27 {
28         adc_mcp3008_fini();
29         initialized = false;
30 }
31
32 int co2_sensor_read(int ch_num, unsigned int *out_value)
33 {
34         unsigned int read_value = 0;
35         int ret = 0;
36
37         if (!initialized) {
38                 ret = adc_mcp3008_init();
39                 retv_if(ret != 0, -1);
40                 initialized = true;
41         }
42         ret = adc_mcp3008_read(ch_num, &read_value);
43         retv_if(ret != 0, -1);
44
45         *out_value = read_value;
46
47         return 0;
48 }
49
50 double co2_sensor_value_to_voltage(unsigned int value)
51 {
52         double v = 0;
53
54         v = (double)value * CO2_SENSOR_REF_VOLTAGE / CO2_SENSOR_VALUE_MAX;
55
56         return v;
57 }
58
59 /* Not implemented, please see c source code */
60 unsigned int co2_sensor_voltage_to_ppm(double voltage)
61 {
62         int ppm = 0;
63
64         /* Make this function yourself */
65         /* Please ref. 'Theory' section in https://sandboxelectronics.com/?p=147#Theory */
66
67         return ppm;
68 }
69
70 unsigned int co2_sensor_value_to_ppm(unsigned int value)
71 {
72         /* You can use this function after implementing co2_sensor_voltage_to_ppm() function */
73         return co2_sensor_voltage_to_ppm(co2_sensor_value_to_voltage(value));
74 }
75