1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/drivers/mq3/mq3.c

49 lines
1.1 KiB
C
Raw Normal View History

/*
* Copyright (C) 2014 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more
* details.
*/
/**
2017-08-29 18:00:46 +02:00
* @ingroup drivers_mq3
* @{
*
* @file
* @brief Device driver implementation for the MQ-3 alcohol sensor
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*
* @}
*/
#include "mq3.h"
2023-05-31 20:05:11 +02:00
#include "macros/math.h"
#define PRECISION ADC_RES_10BIT
2023-05-31 20:05:11 +02:00
/* 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)
{
dev->adc_line = adc_line;
return adc_init(dev->adc_line);
}
2023-05-31 20:05:11 +02:00
int16_t mq3_read_raw(const mq3_t *dev)
{
return adc_sample(dev->adc_line, PRECISION);
}
2023-05-31 20:05:11 +02:00
int16_t mq3_read(const mq3_t *dev)
{
2023-05-31 20:05:11 +02:00
uint32_t res = mq3_read_raw(dev);
res = (res > MIN) ? res - MIN : 0;
2023-05-31 20:05:11 +02:00
/* same as `(int16_t)(res * 2.33)` */
return (res * FACTOR) >> SHIFT;
}