diff --git a/drivers/sht1x/include/sht1x_params.h b/drivers/sht1x/include/sht1x_params.h index f7cd54f8a7..02b94c784d 100644 --- a/drivers/sht1x/include/sht1x_params.h +++ b/drivers/sht1x/include/sht1x_params.h @@ -47,6 +47,28 @@ extern "C" { #endif /**@}*/ +/** + * @name Set default SAUL info text depending on used pseudo module + * @{ + */ +#ifndef SHT1X_SAULINFO +#ifdef MODULE_SHT15 +#define SHT1X_SAULINFO { .name = "SHT15 temperature" }, \ + { .name = "SHT15 humidity" } +#else +#ifdef MODULE_SHT10 +#define SHT1X_SAULINFO { .name = "SHT10 temperature" }, \ + { .name = "SHT10 humidity" } +#else +/* SHT11 is the most commonly used, so use that as default */ +#define SHT1X_SAULINFO { .name = "SHT11 temperature" }, \ + { .name = "SHT11 humidity" } +#endif /* MODULE_SHT10 */ +#endif /* MODULE_SHT15 */ +#endif /* SHT1X_SAULINFO */ + +/**@}*/ + /** * @brief Configure SHT1X devices */ @@ -55,6 +77,14 @@ static const sht1x_params_t sht1x_params[] = SHT1X_PARAMS }; +/** + * @brief Allocate and configure entries to the SAUL registry + */ +static const saul_reg_info_t sht1x_saul_info[] = +{ + SHT1X_SAULINFO +}; + #ifdef __cplusplus } #endif diff --git a/drivers/sht1x/sht1x_saul.c b/drivers/sht1x/sht1x_saul.c new file mode 100644 index 0000000000..7c0abd9255 --- /dev/null +++ b/drivers/sht1x/sht1x_saul.c @@ -0,0 +1,87 @@ +/* + * Copyright 2018 Otto-von-Guericke-Universität Magdeburg + * + * 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_sht1x + * @{ + * + * @file + * @brief SAUL adaption for SHT10/SHT11/SHT15 devices + * + * @author Marian Buschsieweke + * + * @} + */ + +#include +#include +#include + +#include "phydat.h" +#include "saul.h" +#include "sht1x_defines.h" +#include "sht1x.h" + +static int read(const sht1x_dev_t *dev, int16_t *temp, int16_t *hum) +{ + for (int retries = 0; retries < SHT1X_SAUL_RETRIES; retries++) { + switch (sht1x_read(dev, temp, hum)) { + case 0: + return 0; + case -EBADMSG: + puts("[sht1x] CRC"); + continue; + case -EPROTO: + puts("[sht1x] Sensor did not acknowledge measurement command"); + continue; + case -ECANCELED: + puts("[sht1x] Measurement times out"); + default: + /* Other failure, cannot recover so giving up */ + return -1; + } + } + + printf("[sht1x] Giving up after %i tries\n", SHT1X_SAUL_RETRIES); + + return -1; +} + +static int read_temp(const void *dev, phydat_t *res) +{ + if (read(dev, &res->val[0], NULL) == 0) { + res->unit = UNIT_TEMP_C; + res->scale = -2; + return 1; + } + + return -ECANCELED; +} + +static int read_hum(const void *dev, phydat_t *res) +{ + if (read(dev, NULL, &res->val[0]) == 0) { + res->unit = UNIT_PERCENT; + res->scale = -2; + return 1; + } + + return -ECANCELED; +} + +const saul_driver_t sht1x_saul_temp_driver = { + .read = read_temp, + .write = saul_notsup, + .type = SAUL_SENSE_TEMP +}; + +const saul_driver_t sht1x_saul_hum_driver = { + .read = read_hum, + .write = saul_notsup, + .type = SAUL_SENSE_HUM +}; diff --git a/sys/auto_init/auto_init_sht1x.c b/sys/auto_init/auto_init_sht1x.c index 49d9e6f5c9..3264ab9b45 100644 --- a/sys/auto_init/auto_init_sht1x.c +++ b/sys/auto_init/auto_init_sht1x.c @@ -38,6 +38,23 @@ */ sht1x_dev_t sht1x_devs[SHT1X_NUM]; +#ifdef MODULE_AUTO_INIT_SAUL + +/** + * @brief Memory for the SAUL registry entries + */ +static saul_reg_t saul_entries[SHT1X_NUM * 2]; + +/** + * @name Import SAUL endpoints + * @{ + */ +extern const saul_driver_t sht1x_saul_temp_driver; +extern const saul_driver_t sht1x_saul_hum_driver; +/** @} */ + +#endif /* MODULE_AUTO_INIT_SAUL */ + static void sht1x_error(unsigned int num, const char *reason) { LOG_ERROR("[auto_init] error initializing SHT10/SHT11/SHT15 sensor " @@ -66,6 +83,17 @@ void auto_init_sht1x(void) sht1x_error(i, "?"); continue; } + +#ifdef MODULE_AUTO_INIT_SAUL + saul_entries[(i * 2) ].dev = &(sht1x_devs[i]); + saul_entries[(i * 2) + 1].dev = &(sht1x_devs[i]); + saul_entries[(i * 2) ].name = sht1x_saul_info[(i * 2) ].name; + saul_entries[(i * 2) + 1].name = sht1x_saul_info[(i * 2) + 1].name; + saul_entries[(i * 2) ].driver = &sht1x_saul_temp_driver; + saul_entries[(i * 2) + 1].driver = &sht1x_saul_hum_driver; + saul_reg_add(&(saul_entries[(i * 2) ])); + saul_reg_add(&(saul_entries[(i * 2) + 1])); +#endif /* MODULE_AUTO_INIT_SAUL */ } }