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
138
139
140
141
142
|
/* Copyright 2018 Jason Williams (Wilba)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "is31fl3218-mono.h"
#include "i2c_master.h"
#define IS31FL3218_PWM_REGISTER_COUNT 18
#define IS31FL3218_LED_CONTROL_REGISTER_COUNT 3
#ifndef IS31FL3218_I2C_TIMEOUT
# define IS31FL3218_I2C_TIMEOUT 100
#endif
#ifndef IS31FL3218_I2C_PERSISTENCE
# define IS31FL3218_I2C_PERSISTENCE 0
#endif
// IS31FL3218 has 18 PWM outputs and a fixed I2C address, so no chaining.
uint8_t g_pwm_buffer[IS31FL3218_PWM_REGISTER_COUNT];
bool g_pwm_buffer_update_required = false;
uint8_t g_led_control_registers[IS31FL3218_LED_CONTROL_REGISTER_COUNT] = {0};
bool g_led_control_registers_update_required = false;
void is31fl3218_write_register(uint8_t reg, uint8_t data) {
#if IS31FL3218_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3218_I2C_PERSISTENCE; i++) {
if (i2c_writeReg(IS31FL3218_I2C_ADDRESS << 1, reg, &data, 1, IS31FL3218_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_writeReg(IS31FL3218_I2C_ADDRESS << 1, reg, &data, 1, IS31FL3218_I2C_TIMEOUT);
#endif
}
void is31fl3218_write_pwm_buffer(uint8_t *pwm_buffer) {
#if IS31FL3218_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3218_I2C_PERSISTENCE; i++) {
if (i2c_writeReg(IS31FL3218_I2C_ADDRESS << 1, IS31FL3218_REG_PWM, pwm_buffer, 18, IS31FL3218_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_writeReg(IS31FL3218_I2C_ADDRESS << 1, IS31FL3218_REG_PWM, pwm_buffer, 18, IS31FL3218_I2C_TIMEOUT);
#endif
}
void is31fl3218_init(void) {
i2c_init();
// In case we ever want to reinitialize (?)
is31fl3218_write_register(IS31FL3218_REG_RESET, 0x00);
// Turn off software shutdown
is31fl3218_write_register(IS31FL3218_REG_SHUTDOWN, 0x01);
// Set all PWM values to zero
for (uint8_t i = 0; i < IS31FL3218_PWM_REGISTER_COUNT; i++) {
is31fl3218_write_register(IS31FL3218_REG_PWM + i, 0x00);
}
// turn off all LEDs in the LED control register
for (uint8_t i = 0; i < IS31FL3218_LED_CONTROL_REGISTER_COUNT; i++) {
is31fl3218_write_register(IS31FL3218_REG_LED_CONTROL_1 + i, 0x00);
}
// Load PWM registers and LED Control register data
is31fl3218_write_register(IS31FL3218_REG_UPDATE, 0x01);
for (int i = 0; i < IS31FL3218_LED_COUNT; i++) {
is31fl3218_set_led_control_register(i, true);
}
is31fl3218_update_led_control_registers();
}
void is31fl3218_set_value(int index, uint8_t value) {
is31fl3218_led_t led;
if (index >= 0 && index < IS31FL3218_LED_COUNT) {
memcpy_P(&led, (&g_is31fl3218_leds[index]), sizeof(led));
if (g_pwm_buffer[led.v] == value) {
return;
}
g_pwm_buffer[led.v] = value;
g_pwm_buffer_update_required = true;
}
}
void is31fl3218_set_value_all(uint8_t value) {
for (int i = 0; i < IS31FL3218_LED_COUNT; i++) {
is31fl3218_set_value(i, value);
}
}
void is31fl3218_set_led_control_register(uint8_t index, bool value) {
is31fl3218_led_t led;
memcpy_P(&led, (&g_is31fl3218_leds[index]), sizeof(led));
uint8_t control_register = led.v / 6;
uint8_t bit_value = led.v % 6;
if (value) {
g_led_control_registers[control_register] |= (1 << bit_value);
} else {
g_led_control_registers[control_register] &= ~(1 << bit_value);
}
g_led_control_registers_update_required = true;
}
void is31fl3218_update_pwm_buffers(void) {
if (g_pwm_buffer_update_required) {
is31fl3218_write_pwm_buffer(g_pwm_buffer);
// Load PWM registers and LED Control register data
is31fl3218_write_register(IS31FL3218_REG_UPDATE, 0x01);
g_pwm_buffer_update_required = false;
}
}
void is31fl3218_update_led_control_registers(void) {
if (g_led_control_registers_update_required) {
for (uint8_t i = 0; i < IS31FL3218_LED_CONTROL_REGISTER_COUNT; i++) {
is31fl3218_write_register(IS31FL3218_REG_LED_CONTROL_1 + i, g_led_control_registers[i]);
}
g_led_control_registers_update_required = false;
}
}
|