1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
19696: drivers/mq3: avoid use of floats r=maribu a=maribu



19698: tests/pkg/lvgl: avoid using floats r=maribu a=maribu



Co-authored-by: Marian Buschsieweke <marian.buschsieweke@posteo.net>
This commit is contained in:
bors[bot] 2023-06-02 05:48:07 +00:00 committed by GitHub
commit d16f8f1032
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 12 deletions

View File

@ -50,8 +50,8 @@ typedef struct {
* @param[out] dev device descriptor of an MQ-3 sensor * @param[out] dev device descriptor of an MQ-3 sensor
* @param[in] adc_line the ADC device the sensor is connected to * @param[in] adc_line the ADC device the sensor is connected to
* *
* @return 0 on success * @retval 0 success
* @return -1 on error * @retval -1 failure
*/ */
int mq3_init(mq3_t *dev, adc_t adc_line); int mq3_init(mq3_t *dev, adc_t adc_line);
@ -62,7 +62,7 @@ int mq3_init(mq3_t *dev, adc_t adc_line);
* *
* @return the raw sensor value, between 0 and MQ3_MAX_RAW_VALUE * @return the raw sensor value, between 0 and MQ3_MAX_RAW_VALUE
*/ */
int mq3_read_raw(const mq3_t *dev); int16_t mq3_read_raw(const mq3_t *dev);
/** /**
* @brief Read the scaled sensor value of PPM of alcohol * @brief Read the scaled sensor value of PPM of alcohol
@ -71,7 +71,7 @@ int mq3_read_raw(const mq3_t *dev);
* *
* @return the scaled sensor value in PPM of alcohol * @return the scaled sensor value in PPM of alcohol
*/ */
int mq3_read(const mq3_t *dev); int16_t mq3_read(const mq3_t *dev);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -19,10 +19,14 @@
*/ */
#include "mq3.h" #include "mq3.h"
#include "macros/math.h"
#define PRECISION ADC_RES_10BIT #define PRECISION ADC_RES_10BIT
#define MIN (100U) /* TODO: calibrate to useful value */ /* TODO: calibrate to useful value */
#define FACTOR (2.33f) /* TODO: calibrate to useful value */ #define MIN (100U)
/* TODO: calibrate to useful value */
#define SHIFT (12U)
#define FACTOR DIV_ROUND(233UL << SHIFT, 100)
int mq3_init(mq3_t *dev, adc_t adc_line) int mq3_init(mq3_t *dev, adc_t adc_line)
{ {
@ -30,14 +34,15 @@ int mq3_init(mq3_t *dev, adc_t adc_line)
return adc_init(dev->adc_line); return adc_init(dev->adc_line);
} }
int mq3_read_raw(const mq3_t *dev) int16_t mq3_read_raw(const mq3_t *dev)
{ {
return adc_sample(dev->adc_line, PRECISION); return adc_sample(dev->adc_line, PRECISION);
} }
int mq3_read(const mq3_t *dev) int16_t mq3_read(const mq3_t *dev)
{ {
float res = mq3_read_raw(dev); uint32_t res = mq3_read_raw(dev);
res = (res > MIN) ? res - MIN : 0; res = (res > MIN) ? res - MIN : 0;
return (int)(res * FACTOR); /* same as `(int16_t)(res * 2.33)` */
return (res * FACTOR) >> SHIFT;
} }

View File

@ -92,7 +92,7 @@ void sysmon_create(void)
/* Create a chart with two data lines */ /* Create a chart with two data lines */
chart = lv_chart_create(cont); chart = lv_chart_create(cont);
lv_obj_set_size(chart, hres / 2.5, vres / 2); lv_obj_set_size(chart, hres * 10L / 25, vres / 2);
lv_obj_set_pos(chart, LV_DPI_DEF / 10, LV_DPI_DEF / 10); lv_obj_set_pos(chart, LV_DPI_DEF / 10, LV_DPI_DEF / 10);
lv_chart_set_point_count(chart, CHART_POINT_NUM); lv_chart_set_point_count(chart, CHART_POINT_NUM);
lv_chart_set_range(chart, LV_CHART_AXIS_PRIMARY_Y, 0, 100); lv_chart_set_range(chart, LV_CHART_AXIS_PRIMARY_Y, 0, 100);
@ -102,7 +102,7 @@ void sysmon_create(void)
/* Set the data series to zero */ /* Set the data series to zero */
uint16_t i; uint16_t i;
for(i = 0; i < CHART_POINT_NUM; i++) { for (i = 0; i < CHART_POINT_NUM; i++) {
lv_chart_set_next_value(chart, cpu_ser, 0); lv_chart_set_next_value(chart, cpu_ser, 0);
lv_chart_set_next_value(chart, mem_ser, 0); lv_chart_set_next_value(chart, mem_ser, 0);
} }