1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/drivers/hm330x/hm330x_saul.c
Marian Buschsieweke 16f859dafd
drivers/saul: use const qualifier for data to write
This makes life easier when calling e.g. `saul_reg_write()` with data
stored in flash.

As now the signatures for reading and writing differ (in that `const`
qualifier only), `saul_notsup()` is split into `saul_write_notsup()`
and `saul_read_notsup()`. However, one is implemented as a symbol alias
of the other, so that ROM consumption remains unchanged.
2022-05-23 08:35:27 +02:00

176 lines
3.6 KiB
C

/*
* Copyright (C) 2021 Inria
*
* 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.
*/
/**
* @ingroup drivers_hm330x
* @{
* @file
* @brief SAUL adaption of the HM330X particulate matter sensor
*
* @author Francisco Molina <francois-xavier.molina@inria.fr>
* @}
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "phydat.h"
#include "saul.h"
#include "hm330x.h"
#include "hm330x_params.h"
#include "hm330x_constants.h"
static int read_mc_pm_1(const void *_dev, phydat_t *data)
{
hm330x_t *dev = (hm330x_t *)_dev;
hm330x_data_t values;
hm330x_read(dev, &values);
data->unit = UNIT_GPM3;
data->scale = -6;
uint32_t value;
if (IS_ACTIVE(CONFIG_HM330X_INDOOR_ENVIRONMENT)) {
value = values.mc_pm_1;
}
else {
value = values.amc_pm_1;
}
phydat_fit(data, (int32_t *)&value, 1);
return 1;
}
static int read_mc_pm_2p5(const void *_dev, phydat_t *data)
{
hm330x_t *dev = (hm330x_t *)_dev;
hm330x_data_t values;
hm330x_read(dev, &values);
data->unit = UNIT_GPM3;
data->scale = -6;
uint32_t value;
if (IS_ACTIVE(CONFIG_HM330X_INDOOR_ENVIRONMENT)) {
value = values.mc_pm_2p5;
}
else {
value = values.amc_pm_2p5;
}
phydat_fit(data, (int32_t *)&value, 1);
return 1;
}
static int read_mc_pm_10(const void *_dev, phydat_t *data)
{
hm330x_t *dev = (hm330x_t *)_dev;
hm330x_data_t values;
hm330x_read(dev, &values);
data->unit = UNIT_GPM3;
data->scale = -6;
uint32_t value;
if (IS_ACTIVE(CONFIG_HM330X_INDOOR_ENVIRONMENT)) {
value = values.mc_pm_10;
}
else {
value = values.amc_pm_10;
}
phydat_fit(data, (int32_t *)&value, 1);
return 1;
}
#if IS_USED(MODULE_HM3302)
static int read_nc_pm_1(const void *_dev, phydat_t *data)
{
hm330x_t *dev = (hm330x_t *)_dev;
hm330x_data_t values;
hm330x_read(dev, &values);
data->unit = UNIT_CPM3;
data->scale = 4;
uint32_t value = values.nc_pm_1;
phydat_fit(data, (int32_t *)&value, 1);
return 1;
}
static int read_nc_pm_2p5(const void *_dev, phydat_t *data)
{
hm330x_t *dev = (hm330x_t *)_dev;
hm330x_data_t values;
hm330x_read(dev, &values);
data->unit = UNIT_CPM3;
data->scale = 4;
uint32_t value = values.nc_pm_2p5;
phydat_fit(data, (int32_t *)&value, 1);
return 1;
}
static int read_nc_pm_10(const void *_dev, phydat_t *data)
{
hm330x_t *dev = (hm330x_t *)_dev;
hm330x_data_t values;
hm330x_read(dev, &values);
data->unit = UNIT_CPM3;
data->scale = 4;
uint32_t value = values.nc_pm_10;
phydat_fit(data, (int32_t *)&value, 1);
return 1;
}
#endif
const saul_driver_t hm330x_saul_driver_mc_pm_1 = {
.read = read_mc_pm_1,
.write = saul_write_notsup,
.type = SAUL_SENSE_PM
};
const saul_driver_t hm330x_saul_driver_mc_pm_2p5 = {
.read = read_mc_pm_2p5,
.write = saul_write_notsup,
.type = SAUL_SENSE_PM
};
const saul_driver_t hm330x_saul_driver_mc_pm_10 = {
.read = read_mc_pm_10,
.write = saul_write_notsup,
.type = SAUL_SENSE_PM
};
#if IS_USED(MODULE_HM3302)
const saul_driver_t hm330x_saul_driver_nc_pm_1 = {
.read = read_nc_pm_1,
.write = saul_write_notsup,
.type = SAUL_SENSE_COUNT
};
const saul_driver_t hm330x_saul_driver_nc_pm_2p5 = {
.read = read_nc_pm_2p5,
.write = saul_write_notsup,
.type = SAUL_SENSE_COUNT
};
const saul_driver_t hm330x_saul_driver_nc_pm_10 = {
.read = read_nc_pm_10,
.write = saul_write_notsup,
.type = SAUL_SENSE_COUNT
};
#endif