Complete RGB Matrix support for IS31FL3218 (#22004)
This commit is contained in:
parent
32126dc4f0
commit
1ea54a2d8d
9 changed files with 178 additions and 33 deletions
|
@ -14,11 +14,9 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "is31fl3218.h"
|
||||
#include <string.h>
|
||||
#include "i2c_master.h"
|
||||
|
||||
// This is the full 8-bit address
|
||||
#define IS31FL3218_I2C_ADDRESS 0xA8
|
||||
|
||||
// These are the register addresses
|
||||
#define IS31FL3218_REG_SHUTDOWN 0x00
|
||||
#define IS31FL3218_REG_PWM 0x01
|
||||
|
@ -26,28 +24,47 @@
|
|||
#define IS31FL3218_REG_UPDATE 0x16
|
||||
#define IS31FL3218_REG_RESET 0x17
|
||||
|
||||
// Default timeout if no I2C response
|
||||
#define IS31FL3218_I2C_TIMEOUT 100
|
||||
#ifndef IS31FL3218_I2C_TIMEOUT
|
||||
# define IS31FL3218_I2C_TIMEOUT 100
|
||||
#endif
|
||||
|
||||
#ifndef IS31FL3218_I2C_PERSISTENCE
|
||||
# define IS31FL3218_I2C_PERSISTENCE 0
|
||||
#endif
|
||||
|
||||
// Reusable buffer for transfers
|
||||
uint8_t g_twi_transfer_buffer[20];
|
||||
|
||||
// IS31FL3218 has 18 PWM outputs and a fixed I2C address, so no chaining.
|
||||
// If used as RGB LED driver, LEDs are assigned RGB,RGB,RGB,RGB,RGB,RGB
|
||||
uint8_t g_pwm_buffer[18];
|
||||
bool g_pwm_buffer_update_required = false;
|
||||
|
||||
uint8_t g_led_control_registers[3] = {0};
|
||||
bool g_led_control_registers_update_required = false;
|
||||
|
||||
void is31fl3218_write_register(uint8_t reg, uint8_t data) {
|
||||
g_twi_transfer_buffer[0] = reg;
|
||||
g_twi_transfer_buffer[1] = data;
|
||||
i2c_transmit(IS31FL3218_I2C_ADDRESS, g_twi_transfer_buffer, 2, IS31FL3218_I2C_TIMEOUT);
|
||||
#if IS31FL3218_I2C_PERSISTENCE > 0
|
||||
for (uint8_t i = 0; i < IS31FL3218_I2C_PERSISTENCE; i++) {
|
||||
if (i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, g_twi_transfer_buffer, 2, IS31FL3218_I2C_TIMEOUT) == 0) break;
|
||||
}
|
||||
#else
|
||||
i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, g_twi_transfer_buffer, 2, IS31FL3218_I2C_TIMEOUT);
|
||||
#endif
|
||||
}
|
||||
|
||||
void is31fl3218_write_pwm_buffer(uint8_t *pwm_buffer) {
|
||||
g_twi_transfer_buffer[0] = IS31FL3218_REG_PWM;
|
||||
memcpy(g_twi_transfer_buffer + 1, pwm_buffer, 18);
|
||||
|
||||
i2c_transmit(IS31FL3218_I2C_ADDRESS, g_twi_transfer_buffer, 19, IS31FL3218_I2C_TIMEOUT);
|
||||
#if IS31FL3218_I2C_PERSISTENCE > 0
|
||||
for (uint8_t i = 0; i < IS31FL3218_I2C_PERSISTENCE; i++) {
|
||||
i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, g_twi_transfer_buffer, 19, IS31FL3218_I2C_TIMEOUT);
|
||||
}
|
||||
#else
|
||||
i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, g_twi_transfer_buffer, 19, IS31FL3218_I2C_TIMEOUT);
|
||||
#endif
|
||||
}
|
||||
|
||||
void is31fl3218_init(void) {
|
||||
|
@ -62,9 +79,9 @@ void is31fl3218_init(void) {
|
|||
is31fl3218_write_register(IS31FL3218_REG_PWM + i, 0x00);
|
||||
}
|
||||
|
||||
// Enable all channels
|
||||
// turn off all LEDs in the LED control register
|
||||
for (uint8_t i = 0; i < 3; i++) {
|
||||
is31fl3218_write_register(IS31FL3218_REG_CONTROL + i, 0b00111111);
|
||||
is31fl3218_write_register(IS31FL3218_REG_CONTROL + i, 0x00);
|
||||
}
|
||||
|
||||
// Load PWM registers and LED Control register data
|
||||
|
@ -72,26 +89,71 @@ void is31fl3218_init(void) {
|
|||
}
|
||||
|
||||
void is31fl3218_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
|
||||
if (g_pwm_buffer[index * 3 + 0] == red && g_pwm_buffer[index * 3 + 1] == green && g_pwm_buffer[index * 3 + 2] == blue) {
|
||||
is31fl3218_led_t led;
|
||||
if (index >= 0 && index < RGB_MATRIX_LED_COUNT) {
|
||||
memcpy_P(&led, (&g_is31fl3218_leds[index]), sizeof(led));
|
||||
}
|
||||
if (g_pwm_buffer[led.r - IS31FL3218_REG_PWM] == red && g_pwm_buffer[led.g - IS31FL3218_REG_PWM] == green && g_pwm_buffer[led.b - IS31FL3218_REG_PWM] == blue) {
|
||||
return;
|
||||
}
|
||||
g_pwm_buffer[index * 3 + 0] = red;
|
||||
g_pwm_buffer[index * 3 + 1] = green;
|
||||
g_pwm_buffer[index * 3 + 2] = blue;
|
||||
g_pwm_buffer_update_required = true;
|
||||
g_pwm_buffer[led.r - IS31FL3218_REG_PWM] = red;
|
||||
g_pwm_buffer[led.g - IS31FL3218_REG_PWM] = green;
|
||||
g_pwm_buffer[led.b - IS31FL3218_REG_PWM] = blue;
|
||||
g_pwm_buffer_update_required = true;
|
||||
}
|
||||
|
||||
void is31fl3218_set_color_all(uint8_t red, uint8_t green, uint8_t blue) {
|
||||
for (int i = 0; i < 6; i++) {
|
||||
for (int i = 0; i < RGB_MATRIX_LED_COUNT; i++) {
|
||||
is31fl3218_set_color(i, red, green, blue);
|
||||
}
|
||||
}
|
||||
|
||||
void is31fl3218_set_led_control_register(uint8_t index, bool red, bool green, bool blue) {
|
||||
is31fl3218_led_t led;
|
||||
memcpy_P(&led, (&g_is31fl3218_leds[index]), sizeof(led));
|
||||
|
||||
uint8_t control_register_r = (led.r - IS31FL3218_REG_PWM) / 6;
|
||||
uint8_t control_register_g = (led.g - IS31FL3218_REG_PWM) / 6;
|
||||
uint8_t control_register_b = (led.b - IS31FL3218_REG_PWM) / 6;
|
||||
uint8_t bit_r = (led.r - IS31FL3218_REG_PWM) % 6;
|
||||
uint8_t bit_g = (led.g - IS31FL3218_REG_PWM) % 6;
|
||||
uint8_t bit_b = (led.b - IS31FL3218_REG_PWM) % 6;
|
||||
|
||||
if (red) {
|
||||
g_led_control_registers[control_register_r] |= (1 << bit_r);
|
||||
} else {
|
||||
g_led_control_registers[control_register_r] &= ~(1 << bit_r);
|
||||
}
|
||||
if (green) {
|
||||
g_led_control_registers[control_register_g] |= (1 << bit_g);
|
||||
} else {
|
||||
g_led_control_registers[control_register_g] &= ~(1 << bit_g);
|
||||
}
|
||||
if (blue) {
|
||||
g_led_control_registers[control_register_b] |= (1 << bit_b);
|
||||
} else {
|
||||
g_led_control_registers[control_register_b] &= ~(1 << bit_b);
|
||||
}
|
||||
|
||||
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 (int i = 0; i < 3; i++) {
|
||||
is31fl3218_write_register(IS31FL3218_REG_CONTROL + i, g_led_control_registers[i]);
|
||||
}
|
||||
|
||||
g_led_control_registers_update_required = false;
|
||||
}
|
||||
g_pwm_buffer_update_required = false;
|
||||
}
|
||||
|
|
|
@ -18,9 +18,45 @@
|
|||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include "progmem.h"
|
||||
|
||||
#define IS31FL3218_I2C_ADDRESS 0x54
|
||||
|
||||
typedef struct is31fl3218_led_t {
|
||||
uint8_t r;
|
||||
uint8_t g;
|
||||
uint8_t b;
|
||||
} __attribute__((packed)) is31fl3218_led_t;
|
||||
|
||||
extern const is31fl3218_led_t PROGMEM g_is31fl3218_leds[RGB_MATRIX_LED_COUNT];
|
||||
|
||||
void is31fl3218_init(void);
|
||||
|
||||
void is31fl3218_set_color(int index, uint8_t red, uint8_t green, uint8_t blue);
|
||||
|
||||
void is31fl3218_set_color_all(uint8_t red, uint8_t green, uint8_t blue);
|
||||
|
||||
void is31fl3218_set_led_control_register(uint8_t index, bool red, bool green, bool blue);
|
||||
|
||||
void is31fl3218_update_pwm_buffers(void);
|
||||
|
||||
void is31fl3218_update_led_control_registers(void);
|
||||
|
||||
#define OUT1 0x01
|
||||
#define OUT2 0x02
|
||||
#define OUT3 0x03
|
||||
#define OUT4 0x04
|
||||
#define OUT5 0x05
|
||||
#define OUT6 0x06
|
||||
#define OUT7 0x07
|
||||
#define OUT8 0x08
|
||||
#define OUT9 0x09
|
||||
#define OUT10 0x0A
|
||||
#define OUT11 0x0B
|
||||
#define OUT12 0x0C
|
||||
#define OUT13 0x0D
|
||||
#define OUT14 0x0E
|
||||
#define OUT15 0x0F
|
||||
#define OUT16 0x10
|
||||
#define OUT17 0x11
|
||||
#define OUT18 0x12
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue