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 /* 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)
33 #define USE_EXAMPLE_CODE
35 static const double log400 = 2.602;
37 static bool initialized = false;
38 static double co2_zp_volt = -1;
39 static double slope_value = 10000;
41 void co2_sensor_close(void)
47 int co2_sensor_read(int ch_num, unsigned int *out_value)
49 unsigned int read_value = 0;
53 ret = adc_mcp3008_init();
54 retv_if(ret != 0, -1);
58 ret = adc_mcp3008_read(ch_num, &read_value);
59 retv_if(ret != 0, -1);
61 *out_value = read_value;
66 static inline double __value_to_volt(unsigned int value)
68 return (double)value * CO2_SENSOR_REF_VOLTAGE / CO2_SENSOR_VALUE_MAX / CO2_SENSOR_VOLTAGE_GAIN;
71 static double __calc_slope(double zp_volt, double sec_volt, double x_axis_diff)
73 return (zp_volt - sec_volt) / (x_axis_diff);
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)
81 double x_axis_diff = -1;
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);
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);
94 double co2_sensor_value_to_voltage(unsigned int value)
96 retv_if(value > 1023, -100000000.0); // out of value range
98 return __value_to_volt(value);
101 /* Not implemented !!! */
102 unsigned int co2_sensor_voltage_to_ppm(double voltage)
105 /* Make this function yourself */
106 /* Please ref. 'Theory' section in https://sandboxelectronics.com/?p=147#Theory */
109 #ifdef USE_EXAMPLE_CODE
111 co2_sensor_set_calibration_values(CO2_SENSOR_DEFAULT_VALUE_ZP,
112 1000, CO2_SENSOR_DEFAULT_VALUE_1000);
114 if (voltage >= co2_zp_volt)
117 ppm = pow(10, (voltage - co2_zp_volt)/slope_value + log400);
122 return (unsigned int)ppm;
125 unsigned int co2_sensor_value_to_ppm(unsigned int value)
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));