summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeonghoon Park <jh1979.park@samsung.com>2018-08-10 12:20:32 +0900
committerJeonghoon Park <jh1979.park@samsung.com>2018-08-10 12:20:32 +0900
commit7965e6ab204c4e88094b518e3f627589f39e21fa (patch)
treebe498dd9bd5f90654f50c50bb5fa12fc251e66a3
parentb0624e230380125902edc78fbd98dfa962b31ccd (diff)
downloadst-things-co2-meter-7965e6ab204c4e88094b518e3f627589f39e21fa.tar.gz
st-things-co2-meter-7965e6ab204c4e88094b518e3f627589f39e21fa.tar.bz2
st-things-co2-meter-7965e6ab204c4e88094b518e3f627589f39e21fa.zip
bug fix for calculating ppm
-rw-r--r--src/co2-sensor.c17
1 files changed, 8 insertions, 9 deletions
diff --git a/src/co2-sensor.c b/src/co2-sensor.c
index ce85c21..7b6cad5 100644
--- a/src/co2-sensor.c
+++ b/src/co2-sensor.c
@@ -36,7 +36,7 @@ static const double log400 = 2.602;
static bool initialized = false;
static double co2_zp_volt = -1;
-static double slope_value = -1;
+static double slope_value = 10000;
void co2_sensor_close(void)
{
@@ -65,9 +65,7 @@ int co2_sensor_read(int ch_num, unsigned int *out_value)
static inline double __value_to_volt(unsigned int value)
{
- return (double)value
- * (double)(CO2_SENSOR_REF_VOLTAGE / CO2_SENSOR_VALUE_MAX)
- / CO2_SENSOR_VOLTAGE_GAIN;
+ return (double)value * CO2_SENSOR_REF_VOLTAGE / CO2_SENSOR_VALUE_MAX / CO2_SENSOR_VOLTAGE_GAIN;
}
static double __calc_slope(double zp_volt, double sec_volt, double x_axis_diff)
@@ -82,13 +80,12 @@ int co2_sensor_set_calibration_values(unsigned int zero_point_v,
double sec_volt = -1;
double x_axis_diff = -1;
- retv_if(zero_point_v <= second_point_v, -1);
+ retvm_if(zero_point_v <= second_point_v, -1, "%u - %u", zero_point_v, second_point_v);
retv_if(second_point_ppm <= 400, -1);
co2_zp_volt = __value_to_volt(zero_point_v);
sec_volt = __value_to_volt(second_point_v);
- x_axis_diff = log400 - log(second_point_ppm);
-
+ x_axis_diff = log400 - log10(second_point_ppm);
slope_value = __calc_slope(co2_zp_volt, sec_volt, x_axis_diff);
return 0;
@@ -110,14 +107,16 @@ unsigned int co2_sensor_voltage_to_ppm(double voltage)
/* Example */
#ifdef USE_EXAMPLE_CODE
- if (slope_value < 0)
+ if (slope_value > 0)
co2_sensor_set_calibration_values(CO2_SENSOR_DEFAULT_VALUE_ZP,
- CO2_SENSOR_DEFAULT_VALUE_1000, 1000);
+ 1000, CO2_SENSOR_DEFAULT_VALUE_1000);
if (voltage >= co2_zp_volt)
return 400;
ppm = pow(10, (voltage - co2_zp_volt)/slope_value + log400);
+// if (ppm > 10000)
+// ppm = 10000;
#endif
return (unsigned int)ppm;