1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

drivers: add grove ledbar driver

This commit is contained in:
smlng 2017-06-12 22:31:04 +02:00
parent aa554d7cce
commit e5e933e332
6 changed files with 247 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base

View File

@ -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 <s@mlng.net>
*
* @}
*/
#include <assert.h>
#include <stdint.h>
#include <string.h>
#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);
}

View File

@ -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 <s@mlng.net>
*/
#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 */
/** @} */

View File

@ -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 <s@mlng.net>
*/
#ifndef GROVE_LEDBAR_H
#define GROVE_LEDBAR_H
#include <stdint.h>
#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 */
/** @} */