grove_ledbar.c
1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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);
}