diff --git a/drivers/hts221/hts221_saul.c b/drivers/hts221/hts221_saul.c new file mode 100644 index 0000000000..0c1e204f1b --- /dev/null +++ b/drivers/hts221/hts221_saul.c @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2017 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_hts221 + * @{ + * + * @file + * @brief HTS221 adaption to the RIOT SAUL interface + * + * @author Sebastian Meiling + * + * @} + */ + +#include + +#include "saul.h" +#include "hts221.h" + +static int read_temp(const void *dev, phydat_t *res) +{ + if (hts221_read_temperature((const hts221_t *)dev, &res->val[0]) != HTS221_OK) { + return -ECANCELED; + } + res->unit = UNIT_TEMP_C; + res->scale = -1; + + return 1; +} + +static int read_hum(const void *dev, phydat_t *res) +{ + if (hts221_read_humidity((const hts221_t *)dev, (uint16_t *)&res->val[0]) != HTS221_OK) { + return -ECANCELED; + } + res->unit = UNIT_PERCENT; + res->scale = -1; + + return 1; +} + +const saul_driver_t hts221_saul_temp_driver = { + .read = read_temp, + .write = saul_notsup, + .type = SAUL_SENSE_TEMP, +}; + +const saul_driver_t hts221_saul_hum_driver = { + .read = read_hum, + .write = saul_notsup, + .type = SAUL_SENSE_HUM, +}; diff --git a/drivers/hts221/include/hts221_params.h b/drivers/hts221/include/hts221_params.h index 127a659f9b..d4d7ab8085 100644 --- a/drivers/hts221/include/hts221_params.h +++ b/drivers/hts221/include/hts221_params.h @@ -51,6 +51,10 @@ extern "C" { .avgx = HTS221_PARAM_AVGX, \ .rate = HTS221_PARAM_RATE } #endif /* HTS221_PARAMS */ + +#ifndef HTS221_SAULINFO +#define HTS221_SAULINFO { .name = "hts221" } +#endif /**@}*/ /** @@ -61,6 +65,14 @@ static const hts221_params_t hts221_params[] = HTS221_PARAMS, }; +/** + * @brief Additional meta information to keep in the SAUL registry + */ +static const saul_reg_info_t hts221_saul_info[] = +{ + HTS221_SAULINFO +}; + #ifdef __cplusplus } #endif diff --git a/sys/auto_init/auto_init.c b/sys/auto_init/auto_init.c index 582214fe2d..6983d2598e 100644 --- a/sys/auto_init/auto_init.c +++ b/sys/auto_init/auto_init.c @@ -317,6 +317,10 @@ void auto_init(void) extern void auto_init_hdc1000(void); auto_init_hdc1000(); #endif +#ifdef MODULE_HTS221 + extern void auto_init_hts221(void); + auto_init_hts221(); +#endif #ifdef MODULE_DHT extern void auto_init_dht(void); auto_init_dht(); diff --git a/sys/auto_init/saul/auto_init_hts221.c b/sys/auto_init/saul/auto_init_hts221.c new file mode 100644 index 0000000000..32480a8e4b --- /dev/null +++ b/sys/auto_init/saul/auto_init_hts221.c @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2017 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 auto_init_saul + * @{ + * + * @file + * @brief Auto initialization for HTS221 devices + * + * @author Sebastian Meiling + * + * @} + */ + +#ifdef MODULE_HTS221 + +#include "log.h" +#include "saul_reg.h" +#include "hts221.h" +#include "hts221_params.h" + +/** + * @brief Define the number of configured sensors + */ +#define HTS221_NUM (sizeof(hts221_params)/sizeof(hts221_params[0])) + +#define HTS221_SAUL_NUM (sizeof(hts221_saul_info)/sizeof(hts221_saul_info[0])) +/** + * @brief Allocate memory for the device descriptors + */ +static hts221_t hts221_devs[HTS221_NUM]; + +/** + * @brief Memory for the SAUL registry entries + */ +static saul_reg_t saul_entries[HTS221_NUM * 2]; + +/** + * @brief Reference the driver struct + * @{ + */ +extern saul_driver_t hts221_saul_temp_driver; +extern saul_driver_t hts221_saul_hum_driver; +/** @} */ + + +void auto_init_hts221(void) +{ + assert(HTS221_NUM == HTS221_SAUL_NUM); + + for (unsigned i = 0; i < HTS221_NUM; i++) { + LOG_DEBUG("[auto_init_saul] initializing hts221 #%u\n", i); + + hts221_t *dev = &hts221_devs[i]; + if (hts221_init(dev, &hts221_params[i]) != HTS221_OK) { + LOG_ERROR("[auto_init_saul] error initializing hts221 (#%u)\n", i); + continue; + } + if (hts221_power_on(dev) != HTS221_OK) { + LOG_ERROR("[auto_init_saul] error set power on (#%u)!\n", i); + continue; + } + if (hts221_set_rate(dev, dev->p.rate) != HTS221_OK) { + LOG_ERROR("[auto_init_saul] error set continuous mode (#%u)!\n", i); + continue; + } + + saul_entries[(i * 2)].dev = &(hts221_devs[i]); + saul_entries[(i * 2)].name = hts221_saul_info[i].name; + saul_entries[(i * 2)].driver = &hts221_saul_temp_driver; + saul_entries[(i * 2) + 1].dev = &(hts221_devs[i]); + saul_entries[(i * 2) + 1].name = hts221_saul_info[i].name; + saul_entries[(i * 2) + 1].driver = &hts221_saul_hum_driver; + saul_reg_add(&(saul_entries[(i * 2)])); + saul_reg_add(&(saul_entries[(i * 2) + 1])); + } +} + +#else +typedef int dont_be_pedantic; +#endif /* MODULE_HTS221 */