diff --git a/boards/pba-d-01-kw2x/Makefile.dep b/boards/pba-d-01-kw2x/Makefile.dep index aa4f35065..415f682bf 100644 --- a/boards/pba-d-01-kw2x/Makefile.dep +++ b/boards/pba-d-01-kw2x/Makefile.dep @@ -4,8 +4,9 @@ endif ifneq (,$(filter saul_default,$(USEMODULE))) USEMODULE += saul_gpio + USEMODULE += hdc1000 USEMODULE += mag3110 USEMODULE += mma8x5x - USEMODULE += hdc1000 + USEMODULE += tmp006 USEMODULE += tcs37727 endif diff --git a/drivers/include/tmp006.h b/drivers/include/tmp006.h index 2f5399b5a..d96621c60 100644 --- a/drivers/include/tmp006.h +++ b/drivers/include/tmp006.h @@ -222,6 +222,17 @@ int tmp006_read(tmp006_t *dev, int16_t *rawv, int16_t *rawt, uint8_t *drdy); */ void tmp006_convert(int16_t rawv, int16_t rawt, float *tamb, float *tobj); +/** + * @brief Convenience function to get ambient and object temperatures in [°C] + * + * @note Temperature scaled by x100 for accuracy and avoid floats + * + * @param[in] dev device descriptor of sensor + * @param[out] ta converted ambient temperature + * @param[out] to converted object temperature + */ +int tmp006_read_temperature(tmp006_t *dev, int16_t *ta, int16_t *to); + #ifdef __cplusplus } #endif diff --git a/drivers/tmp006/include/tmp006_params.h b/drivers/tmp006/include/tmp006_params.h index d3ebaf436..bfbbf88ab 100644 --- a/drivers/tmp006/include/tmp006_params.h +++ b/drivers/tmp006/include/tmp006_params.h @@ -21,6 +21,7 @@ #include "board.h" #include "tmp006.h" +#include "saul_reg.h" #ifdef __cplusplus extern "C" { @@ -57,6 +58,14 @@ static const tmp006_params_t tmp006_params[] = #endif }; +/** + * @brief Additional meta information to keep in the SAUL registry + */ +static const saul_reg_info_t tmp006_saul_info[] = +{ + { .name = "tmp006" } +}; + #ifdef __cplusplus } #endif diff --git a/drivers/tmp006/tmp006.c b/drivers/tmp006/tmp006.c index 922c5e8b8..e74f0d579 100644 --- a/drivers/tmp006/tmp006.c +++ b/drivers/tmp006/tmp006.c @@ -223,3 +223,20 @@ void tmp006_convert(int16_t rawv, int16_t rawt, float *tamb, float *tobj) /* calculate object temperature in Celsius */ *tobj = (t - 273.15); } + +int tmp006_read_temperature(tmp006_t *dev, int16_t *ta, int16_t *to) +{ + int16_t rawtemp, rawvolt; + float tamb, tobj; + uint8_t drdy; + tmp006_read(dev, &rawvolt, &rawtemp, &drdy); + + if (!drdy) { + return TMP006_ERROR; + } + tmp006_convert(rawvolt, rawtemp, &tamb, &tobj); + *ta = (int16_t)(tamb*100); + *to = (int16_t)(tobj*100); + + return TMP006_OK; +} diff --git a/drivers/tmp006/tmp006_saul.c b/drivers/tmp006/tmp006_saul.c new file mode 100644 index 000000000..bc817c6f6 --- /dev/null +++ b/drivers/tmp006/tmp006_saul.c @@ -0,0 +1,42 @@ +/* + * 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 driver_tmp006 + * @{ + * + * @file + * @brief TMP006 adaption to the RIOT actuator/sensor interface + * + * @author Sebastian Meiling + * + * @} + */ + +#include + +#include "saul.h" +#include "tmp006.h" + +static int read_temp(void *dev, phydat_t *res) +{ + if (tmp006_read_temperature((tmp006_t *)dev, &res->val[0], &res->val[1]) != TMP006_OK) { + return -ECANCELED; + } + res->val[2] = 0; + res->unit = UNIT_TEMP_C; + res->scale = -2; + + return 2; +} + +const saul_driver_t tmp006_saul_driver = { + .read = read_temp, + .write = saul_notsup, + .type = SAUL_SENSE_TEMP, +}; diff --git a/sys/auto_init/auto_init.c b/sys/auto_init/auto_init.c index 6b868eb9d..c6644a507 100644 --- a/sys/auto_init/auto_init.c +++ b/sys/auto_init/auto_init.c @@ -315,6 +315,10 @@ void auto_init(void) extern void auto_init_dht(void); auto_init_dht(); #endif +#ifdef MODULE_TMP006 + extern void auto_init_tmp006(void); + auto_init_tmp006(); +#endif #ifdef MODULE_TCS37727 extern void auto_init_tcs37727(void); auto_init_tcs37727(); diff --git a/sys/auto_init/saul/auto_init_tmp006.c b/sys/auto_init/saul/auto_init_tmp006.c new file mode 100644 index 000000000..fe8ec267e --- /dev/null +++ b/sys/auto_init/saul/auto_init_tmp006.c @@ -0,0 +1,73 @@ +/* + * 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 of TMP006 temperature sensor + * + * @author Sebastian Meiling + * + * @} + */ + +#ifdef MODULE_TMP006 + +#include "log.h" +#include "saul_reg.h" + +#include "tmp006.h" +#include "tmp006_params.h" + +/** + * @brief Define the number of configured sensors + */ +#define TMP006_NUM (sizeof(tmp006_params) / sizeof(tmp006_params[0])) + +/** + * @brief Allocate memory for the device descriptors + */ +static tmp006_t tmp006_devs[TMP006_NUM]; + +/** + * @brief Memory for the SAUL registry entries + */ +static saul_reg_t saul_entries[TMP006_NUM]; + +/** + * @brief Reference the driver struct + */ +extern const saul_driver_t tmp006_saul_driver; + +void auto_init_tmp006(void) +{ + for (unsigned i = 0; i < TMP006_NUM; i++) { + LOG_DEBUG("[auto_init_saul] initializing tmp006 #%u\n", i); + + if (tmp006_init(&tmp006_devs[i], &tmp006_params[i]) != TMP006_OK) { + LOG_ERROR("[auto_init_saul] error initializing tmp006 #%u\n", i); + continue; + } + if (tmp006_set_active(&tmp006_devs[i]) != TMP006_OK) { + LOG_ERROR("[auto_init_saul] error set active tmp006 #%u\n", i); + continue; + } + + saul_entries[i].dev = &(tmp006_devs[i]); + saul_entries[i].name = tmp006_saul_info[i].name; + saul_entries[i].driver = &tmp006_saul_driver; + saul_reg_add(&(saul_entries[i])); + } +} + +#else +typedef int dont_be_pedantic; +#endif /* MODULE_TMP006 */