Skip to content

Commit

Permalink
drivers/mq3: avoid use of floats
Browse files Browse the repository at this point in the history
  • Loading branch information
maribu committed May 31, 2023
1 parent d742513 commit 6275068
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
8 changes: 4 additions & 4 deletions drivers/include/mq3.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ typedef struct {
* @param[out] dev device descriptor of an MQ-3 sensor
* @param[in] adc_line the ADC device the sensor is connected to
*
* @return 0 on success
* @return -1 on error
* @retval 0 success
* @retval -1 failure
*/
int mq3_init(mq3_t *dev, adc_t adc_line);

Expand All @@ -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
*/
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
Expand All @@ -71,7 +71,7 @@ int mq3_read_raw(const mq3_t *dev);
*
* @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
}
Expand Down
13 changes: 7 additions & 6 deletions drivers/mq3/mq3.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,24 @@
#include "mq3.h"

#define PRECISION ADC_RES_10BIT
#define MIN (100U) /* TODO: calibrate to useful value */
#define FACTOR (2.33f) /* TODO: calibrate to useful value */
#define MIN (100) /* TODO: calibrate to useful value */
#define FACTOR (233) /* TODO: calibrate to useful value */
#define DIVISOR (100)

int mq3_init(mq3_t *dev, adc_t adc_line)
{
dev->adc_line = 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);
}

int mq3_read(const mq3_t *dev)
int16_t mq3_read(const mq3_t *dev)
{
float res = mq3_read_raw(dev);
int32_t res = mq3_read_raw(dev);
res = (res > MIN) ? res - MIN : 0;
return (int)(res * FACTOR);
return res * FACTOR / DIVISOR;
}

0 comments on commit 6275068

Please sign in to comment.