From bba4d5b39b988831abf9afaa7552bbe1e55172b0 Mon Sep 17 00:00:00 2001 From: Michel Rottleuthner Date: Thu, 22 Nov 2018 19:17:22 +0100 Subject: [PATCH] drivers/sds011: add saul integration --- drivers/sds011/sds011_saul.c | 46 ++++++++++++++ sys/auto_init/auto_init.c | 4 ++ sys/auto_init/saul/auto_init_sds011.c | 88 +++++++++++++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 drivers/sds011/sds011_saul.c create mode 100644 sys/auto_init/saul/auto_init_sds011.c diff --git a/drivers/sds011/sds011_saul.c b/drivers/sds011/sds011_saul.c new file mode 100644 index 0000000000..d57e125f9e --- /dev/null +++ b/drivers/sds011/sds011_saul.c @@ -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 + * + * @} + */ + +#include + +#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 +}; diff --git a/sys/auto_init/auto_init.c b/sys/auto_init/auto_init.c index a5c45badec..b4e389d95d 100644 --- a/sys/auto_init/auto_init.c +++ b/sys/auto_init/auto_init.c @@ -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(); diff --git a/sys/auto_init/saul/auto_init_sds011.c b/sys/auto_init/saul/auto_init_sds011.c new file mode 100644 index 0000000000..f5874b6f49 --- /dev/null +++ b/sys/auto_init/saul/auto_init_sds011.c @@ -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 + * + * @} + */ + +#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 */