diff --git a/drivers/Makefile.dep b/drivers/Makefile.dep index 923d8072ef..34e91a56fb 100644 --- a/drivers/Makefile.dep +++ b/drivers/Makefile.dep @@ -168,6 +168,10 @@ ifneq (,$(filter mtd_sdcard,$(USEMODULE))) USEMODULE += sdcard_spi endif +ifneq (,$(filter grove_ledbar,$(USEMODULE))) + USEMODULE += my9221 +endif + ifneq (,$(filter my9221,$(USEMODULE))) FEATURES_REQUIRED += periph_gpio USEMODULE += xtimer diff --git a/drivers/Makefile.include b/drivers/Makefile.include index 6ed1123fcb..dcbd298cfa 100644 --- a/drivers/Makefile.include +++ b/drivers/Makefile.include @@ -13,6 +13,9 @@ endif ifneq (,$(filter ds1307,$(USEMODULE))) USEMODULE_INCLUDES += $(RIOTBASE)/drivers/ds1307/include endif +ifneq (,$(filter grove_ledbar,$(USEMODULE))) + USEMODULE_INCLUDES += $(RIOTBASE)/drivers/grove_ledbar/include +endif ifneq (,$(filter kw2xrf,$(USEMODULE))) USEMODULE_INCLUDES += $(RIOTBASE)/drivers/kw2xrf/include endif diff --git a/drivers/grove_ledbar/Makefile b/drivers/grove_ledbar/Makefile new file mode 100644 index 0000000000..48422e909a --- /dev/null +++ b/drivers/grove_ledbar/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/drivers/grove_ledbar/grove_ledbar.c b/drivers/grove_ledbar/grove_ledbar.c new file mode 100644 index 0000000000..b50129c20c --- /dev/null +++ b/drivers/grove_ledbar/grove_ledbar.c @@ -0,0 +1,68 @@ +/* + * 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_grove_ledbar + * + * @{ + * @file + * @brief Driver for the Grove ledbar + * + * @author Sebastian Meiling + * + * @} + */ + +#include +#include +#include + +#include "log.h" +#include "xtimer.h" + +#include "grove_ledbar.h" +#include "my9221.h" + +#define DEV_LEDS (dev->params.leds) +#define DEV_STATE(x) (dev->state[x]) + +int grove_ledbar_init(grove_ledbar_t *dev, const grove_ledbar_params_t *params) +{ + assert(dev && params); + + return my9221_init((my9221_t *)dev, (my9221_params_t *)params); +} + +void grove_ledbar_set(grove_ledbar_t *dev, uint8_t level) +{ + assert(dev); + + uint8_t frac = GROVE_LEDBAR_MAX / DEV_LEDS; + + for (unsigned i = 0; i < DEV_LEDS; ++i) { + if (level > frac) { + DEV_STATE(i) = MY9221_LED_ON; + level -= frac; + } + else if (level > 0) { + DEV_STATE(i) = ~(MY9221_LED_ON << ((level << 3) / frac)); + level = 0; + } + else { + DEV_STATE(i) = MY9221_LED_OFF; + } + } + my9221_set_state((my9221_t *)dev, NULL, 0); +} + +void grove_ledbar_clear(grove_ledbar_t *dev) +{ + assert(dev); + + grove_ledbar_set(dev, 0); +} diff --git a/drivers/grove_ledbar/include/grove_ledbar_params.h b/drivers/grove_ledbar/include/grove_ledbar_params.h new file mode 100644 index 0000000000..ac3c13677b --- /dev/null +++ b/drivers/grove_ledbar/include/grove_ledbar_params.h @@ -0,0 +1,80 @@ +/* + * 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_grove_ledbar + * + * @{ + * @file + * @brief Config for the Grove LED bar based on MY9221 LED controller + * + * @author Sebastian Meiling + */ +#ifndef GROVE_LEDBAR_PARAMS_H +#define GROVE_LEDBAR_PARAMS_H + +#include "board.h" +#include "periph/gpio.h" + +#include "grove_ledbar.h" +#include "my9221.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +/** + * @brief Clock GPIO pin + */ +#ifndef GROVE_LEDBAR_CLK +#define GROVE_LEDBAR_CLK (GPIO_PIN(0,1)) +#endif + +/** + * @brief Data GPIO pin + */ +#ifndef GROVE_LEDBAR_DAT +#define GROVE_LEDBAR_DAT (GPIO_PIN(0,2)) +#endif + +/** + * @brief Direction of LEDs + */ +#ifndef GROVE_LEDBAR_DIR +#define GROVE_LEDBAR_DIR GROVE_LEDBAR_G2R +#endif + +/** + * @brief Default parameter settings + */ +#define GROVE_LEDBAR_PARAMS { \ + .leds = 10, \ + .dir = GROVE_LEDBAR_DIR, \ + .clk = GROVE_LEDBAR_CLK, \ + .dat = GROVE_LEDBAR_DAT, \ +} + +/** + * @brief Grove LED bar configuration + */ +static const grove_ledbar_params_t grove_ledbar_params[] = +{ +#ifdef GROVE_LEDBAR_CUSTOM + GROVE_LEDBAR_CUSTOM, +#else + GROVE_LEDBAR_PARAMS, +#endif +}; + +#ifdef __cplusplus +} +#endif + +#endif /* GROVE_LEDBAR_PARAMS_H */ +/** @} */ diff --git a/drivers/include/grove_ledbar.h b/drivers/include/grove_ledbar.h new file mode 100644 index 0000000000..a28d97a83d --- /dev/null +++ b/drivers/include/grove_ledbar.h @@ -0,0 +1,91 @@ +/* + * 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. + */ + +/** + * @defgroup drivers_grove_ledbar Grove ledbar + * @ingroup drivers_actuators + * @brief Driver for the Grove ledbar + * + * @{ + * @file + * @brief Interface for the Grove ledbar driver + * + * @author Sebastian Meiling + */ + +#ifndef GROVE_LEDBAR_H +#define GROVE_LEDBAR_H + +#include + +#include "my9221.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @name Ledbar direction, either red to green, or vice versa. + * @{ + */ +#define GROVE_LEDBAR_R2G MY9221_DIR_FWD +#define GROVE_LEDBAR_G2R MY9221_DIR_REV +/** @} */ + +/** + * @brief Maximum value for ledbar level + */ +#define GROVE_LEDBAR_MAX UINT8_MAX + +/** + * @brief Parameters needed for device initialization + */ +typedef my9221_params_t grove_ledbar_params_t; + +/** + * @brief Device descriptor for grove ledbar, alias for MY9221 LED controller + */ +typedef my9221_t grove_ledbar_t; + +/** + * @brief Initialize the given driver + * + * @pre `dev != NULL` and `params != NULL` + * + * @param[out] dev device descriptor grove ledbar + * @param[in] params configuration parameters + * + * @return 0 on success, otherwise error + */ +int grove_ledbar_init(grove_ledbar_t *dev, const grove_ledbar_params_t *params); + +/** + * @brief Set overall (brightness) level distributed over all LEDs + * + * @pre `dev != NULL` + * + * @param[in] dev device descriptor grove ledbar + * @param[in] level overall brightness level + */ +void grove_ledbar_set(grove_ledbar_t *dev, uint8_t level); + +/** + * @brief Clear ledbar, i.e. set all LEDs off + * + * @pre `dev != NULL` + * + * @param[in] dev device descriptor grove ledbar + */ +void grove_ledbar_clear(grove_ledbar_t *dev); + +#ifdef __cplusplus +} +#endif + +#endif /* GROVE_LEDBAR_H */ +/** @} */