1 /* ****************************************************************
3 * Copyright 2017 Samsung Electronics All Rights Reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************/
19 #include <peripheral_io.h>
21 #include "adc-mcp3008.h"
24 #define CO2_SENSOR_VOLTAGE_GAIN (8.5)
26 #define CO2_SENSOR_REF_VOLTAGE (5)
27 #define CO2_SENSOR_VALUE_MAX (1024)
29 //#define USE_EXAMPLE_CODE
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 */
37 static const double log400 = 2.602;
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.
43 void co2_sensor_close(void)
49 int co2_sensor_read(int ch_num, unsigned int *out_value)
51 unsigned int read_value = 0;
55 ret = adc_mcp3008_init();
56 retv_if(ret != 0, -1);
60 ret = adc_mcp3008_read(ch_num, &read_value);
61 retv_if(ret != 0, -1);
63 *out_value = read_value;
68 static inline double __value_to_volt(unsigned int value)
70 return (double)value * CO2_SENSOR_REF_VOLTAGE / CO2_SENSOR_VALUE_MAX / CO2_SENSOR_VOLTAGE_GAIN;
73 static double __calc_slope(double zp_volt, double sec_volt, double x_axis_diff)
75 return (zp_volt - sec_volt) / (x_axis_diff);
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)
83 double x_axis_diff = -1;
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);
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);
96 double co2_sensor_value_to_voltage(unsigned int value)
98 retv_if(value > 1023, -100000000.0); // out of value range
100 return __value_to_volt(value);
103 /* Not implemented !!! */
104 unsigned int co2_sensor_voltage_to_ppm(double voltage)
107 /* Make this function yourself */
108 /* Please ref. 'Theory' section in https://sandboxelectronics.com/?p=147#Theory */
111 #ifdef USE_EXAMPLE_CODE
113 co2_sensor_set_calibration_values(CO2_SENSOR_DEFAULT_VALUE_ZP,
114 1000, CO2_SENSOR_DEFAULT_VALUE_1000);
116 if (voltage >= co2_zp_volt)
119 ppm = pow(10, (voltage - co2_zp_volt)/slope_value + log400);
121 _W("ppm value[%lf] is too large - limiting to 10000ppm", ppm);
124 #endif /* USE_EXAMPLE_CODE */
126 return (unsigned int)ppm;
129 unsigned int co2_sensor_value_to_ppm(unsigned int value)
131 /* You can use this function after implementing co2_sensor_voltage_to_ppm() function */
132 return co2_sensor_voltage_to_ppm(co2_sensor_value_to_voltage(value));