a752c7ab
elopes
add first test an...
|
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
/*
* 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_my9221
*
* @{
* @file
* @brief Driver for the MY9221 LED controller
*
* @author Sebastian Meiling <s@mlng.net>
*
* @}
*/
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include "log.h"
#include "periph/gpio.h"
#include "xtimer.h"
#include "my9221.h"
#include "my9221_internal.h"
#define DEV_DIR (dev->params.dir)
#define DEV_LEDS (dev->params.leds)
#define DEV_STATE(x) (dev->state[x])
#define PIN_CLK (dev->params.clk)
#define PIN_DAT (dev->params.dat)
/**
* @brief Write a single data to the LED controller
*/
void _write(my9221_t *dev, uint16_t data)
{
assert(dev);
for (unsigned i = 0; i < 16; ++i) {
(data & 0x8000) ? gpio_set(PIN_DAT) : gpio_clear(PIN_DAT);
gpio_toggle(PIN_CLK);
data <<= 1;
}
}
/**
* @brief Load data into the latch register of the LED controller
*/
void _latch(my9221_t *dev)
{
assert(dev);
gpio_clear(PIN_DAT);
xtimer_usleep(MY9221_LATCH_WAIT);
for (unsigned i = 0; i < MY9221_LATCH_LOOP; ++i) {
gpio_set(PIN_DAT);
gpio_clear(PIN_DAT);
}
}
/**
* @brief Write state data of all LEDs to the controller
*/
void _set_state(my9221_t *dev)
{
assert(dev);
_write(dev, MY9221_CMDMODE);
for (unsigned i = 0; i < DEV_LEDS; ++i) {
if (DEV_DIR == MY9221_DIR_REV) {
/* Write LED state in reverse order */
_write(dev, DEV_STATE(DEV_LEDS-i-1));
}
else {
/* Write LED state in forward order */
_write(dev, DEV_STATE(i));
}
}
/* set unused LED pins to off */
for (unsigned j = DEV_LEDS; j < MY9221_LED_MAX; ++j) {
_write(dev, MY9221_LED_OFF);
}
_latch(dev);
}
int my9221_init(my9221_t *dev, const my9221_params_t *params)
{
assert(dev);
assert(params);
/* write config params to device descriptor */
memcpy(&dev->params, params, sizeof(my9221_params_t));
/* init clock and data pins as output */
gpio_init(PIN_CLK, GPIO_OUT);
gpio_init(PIN_DAT, GPIO_OUT);
/* reset state, i.e., all LEDs off */
memset(dev->state, 0, sizeof(dev->state));
_set_state(dev);
return MY9221_OK;
}
void my9221_set_state(my9221_t *dev, const uint8_t *state, uint8_t len)
{
if ((state != NULL) && (len > 0)) {
if (len > MY9221_LED_MAX) {
len = MY9221_LED_MAX;
}
memcpy(dev->state, state, len);
}
_set_state(dev);
}
void my9221_set_led(my9221_t *dev, const uint8_t led, const uint8_t alpha)
{
assert(dev);
assert(led < DEV_LEDS);
DEV_STATE(led) = ~(MY9221_LED_ON << ((uint16_t)(alpha << 3) >> 8));
_set_state(dev);
}
void my9221_toggle_led(my9221_t *dev, const uint8_t led)
{
assert(dev);
assert(led < DEV_LEDS);
DEV_STATE(led) = DEV_STATE(led) ? MY9221_LED_OFF : MY9221_LED_ON;
_set_state(dev);
}
|