mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
drivers/sds011: add saul integration
This commit is contained in:
parent
61dc1920d3
commit
bba4d5b39b
46
drivers/sds011/sds011_saul.c
Normal file
46
drivers/sds011/sds011_saul.c
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (C) 2018 HAW-Hamburg
|
||||
*
|
||||
* 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_sds011
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief SAUL adaption for SDS011 sensor
|
||||
*
|
||||
* @author Michel Rottleuthner <michel.rottleuthner@haw-hamburg.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "saul.h"
|
||||
#include "sds011.h"
|
||||
#include "xtimer.h"
|
||||
|
||||
static int _read(const void *dev, phydat_t *res)
|
||||
{
|
||||
sds011_data_t data;
|
||||
|
||||
if (sds011_read((sds011_t *)dev, &data) == SDS011_OK) {
|
||||
res->val[0] = data.pm_2_5;
|
||||
res->val[1] = data.pm_10;
|
||||
res->unit = UNIT_GPM3;
|
||||
res->scale = -7;
|
||||
return 2;
|
||||
}
|
||||
return ECANCELED;
|
||||
}
|
||||
|
||||
const saul_driver_t sds011_saul_driver = {
|
||||
.read = _read,
|
||||
.write = saul_notsup,
|
||||
.type = SAUL_SENSE_PM
|
||||
};
|
@ -449,6 +449,10 @@ void auto_init(void)
|
||||
extern void auto_init_sht3x(void);
|
||||
auto_init_sht3x();
|
||||
#endif
|
||||
#ifdef MODULE_SDS011
|
||||
extern void auto_init_sds011(void);
|
||||
auto_init_sds011();
|
||||
#endif
|
||||
#ifdef MODULE_SI114X
|
||||
extern void auto_init_si114x(void);
|
||||
auto_init_si114x();
|
||||
|
88
sys/auto_init/saul/auto_init_sds011.c
Normal file
88
sys/auto_init/saul/auto_init_sds011.c
Normal file
@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright (C) 2018 HAW-Hamburg
|
||||
*
|
||||
* 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 sys_auto_init_saul
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Auto initialization for SDS011 particulate matter sensor
|
||||
*
|
||||
* @author Michel Rottleuthner <michel.rottleuthner@haw-hamburg.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef MODULE_SDS011
|
||||
|
||||
#include "assert.h"
|
||||
#include "log.h"
|
||||
#include "saul_reg.h"
|
||||
#include "sds011.h"
|
||||
#include "sds011_params.h"
|
||||
|
||||
/**
|
||||
* @brief Define the number of configured sensors
|
||||
*/
|
||||
#define SDS011_NUM (sizeof(sds011_params) / sizeof(sds011_params[0]))
|
||||
|
||||
/**
|
||||
* @brief Allocate memory for the device descriptors
|
||||
*/
|
||||
static sds011_t sds011_devs[SDS011_NUM];
|
||||
|
||||
/**
|
||||
* @brief Memory for the SAUL registry entries
|
||||
*/
|
||||
static saul_reg_t saul_entries[SDS011_NUM];
|
||||
|
||||
/**
|
||||
* @brief Define the number of saul info
|
||||
*/
|
||||
#define SDS011_INFO_NUM (sizeof(sds011_saul_info) / sizeof(sds011_saul_info[0]))
|
||||
|
||||
/**
|
||||
* @name Import SAUL endpoint
|
||||
* @{
|
||||
*/
|
||||
extern const saul_driver_t sds011_saul_driver;
|
||||
/** @} */
|
||||
|
||||
void auto_init_sds011(void)
|
||||
{
|
||||
assert(SDS011_INFO_NUM == SDS011_NUM);
|
||||
|
||||
for (unsigned int i = 0; i < SDS011_NUM; i++) {
|
||||
LOG_DEBUG("[auto_init_saul] initializing sds011 #%u\n", i);
|
||||
|
||||
if (sds011_init(&sds011_devs[i], &sds011_params[i]) != SDS011_OK) {
|
||||
LOG_ERROR("[auto_init_saul] error initializing sds011 #%u\n", i);
|
||||
continue;
|
||||
}
|
||||
|
||||
int retries = 0;
|
||||
|
||||
/* sensor must be set to query mode for manual reading by saul */
|
||||
while (sds011_set_reporting_mode(&sds011_devs[i], SDS011_RMODE_QUERY) != SDS011_OK) {
|
||||
if (retries++ >= 3) {
|
||||
LOG_ERROR("[auto_init_saul] error setting sds011 to query mode #%u\n", i);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
saul_entries[i].dev = &(sds011_devs[i]);
|
||||
saul_entries[i].name = sds011_saul_info[i].name;
|
||||
saul_entries[i].driver = &sds011_saul_driver;
|
||||
saul_reg_add(&saul_entries[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
typedef int dont_be_pedantic;
|
||||
#endif /* MODULE_SDS011 */
|
Loading…
Reference in New Issue
Block a user