diff options
author | Nick Brassel <nick@tzarc.org> | 2023-08-29 21:24:43 +1000 |
---|---|---|
committer | Nick Brassel <nick@tzarc.org> | 2023-08-29 21:24:43 +1000 |
commit | 31a91add168c956655ace8ec4cf9750db1e2cfc6 (patch) | |
tree | 12cea9b5f8e769f9611a31cbaede9537edad71cc /drivers/led | |
parent | f07490bc092e365ba03dc685b3fc30ad0bf0b752 (diff) | |
parent | edaf8a87ef3164f8986b0a8eb171d4879b45414c (diff) |
Merge branch 'develop'
Diffstat (limited to 'drivers/led')
25 files changed, 465 insertions, 415 deletions
diff --git a/drivers/led/apa102.c b/drivers/led/apa102.c index 40fc68e4f1..766d8cd2eb 100644 --- a/drivers/led/apa102.c +++ b/drivers/led/apa102.c @@ -16,14 +16,14 @@ */ #include "apa102.h" -#include "quantum.h" +#include "gpio.h" #ifndef APA102_NOPS # if defined(__AVR__) # define APA102_NOPS 0 // AVR at 16 MHz already spends 62.5 ns per clock, so no extra delay is needed # elif defined(PROTOCOL_CHIBIOS) - # include "hal.h" +# include "chibios_config.h" # if defined(STM32F0XX) || defined(STM32F1XX) || defined(STM32F3XX) || defined(STM32F4XX) || defined(STM32L0XX) || defined(GD32VF103) # define APA102_NOPS (100 / (1000000000L / (CPU_CLOCK / 4))) // This calculates how many loops of 4 nops to run to delay 100 ns # else diff --git a/drivers/led/aw20216.c b/drivers/led/aw20216.c index 7895f1497b..479643add4 100644 --- a/drivers/led/aw20216.c +++ b/drivers/led/aw20216.c @@ -71,7 +71,7 @@ uint8_t g_pwm_buffer[DRIVER_COUNT][AW_PWM_REGISTER_COUNT]; bool g_pwm_buffer_update_required[DRIVER_COUNT] = {false}; -bool AW20216_write(pin_t cs_pin, uint8_t page, uint8_t reg, uint8_t* data, uint8_t len) { +bool aw20216_write(pin_t cs_pin, uint8_t page, uint8_t reg, uint8_t* data, uint8_t len) { static uint8_t s_spi_transfer_buffer[2] = {0}; if (!spi_start(cs_pin, false, AW_SPI_MODE, AW_SPI_DIVISOR)) { @@ -96,70 +96,73 @@ bool AW20216_write(pin_t cs_pin, uint8_t page, uint8_t reg, uint8_t* data, uint8 return true; } -static inline bool AW20216_write_register(pin_t cs_pin, uint8_t page, uint8_t reg, uint8_t value) { +static inline bool aw20216_write_register(pin_t cs_pin, uint8_t page, uint8_t reg, uint8_t value) { // Little wrapper so callers need not care about sending a buffer - return AW20216_write(cs_pin, page, reg, &value, 1); + return aw20216_write(cs_pin, page, reg, &value, 1); } -void AW20216_soft_reset(pin_t cs_pin) { - AW20216_write_register(cs_pin, AW_PAGE_FUNCTION, AW_REG_RESET, AW_RESET_CMD); +void aw20216_soft_reset(pin_t cs_pin) { + aw20216_write_register(cs_pin, AW_PAGE_FUNCTION, AW_REG_RESET, AW_RESET_CMD); } -static void AW20216_init_scaling(pin_t cs_pin) { +static void aw20216_init_scaling(pin_t cs_pin) { // Set constant current to the max, control brightness with PWM for (uint8_t i = 0; i < AW_PWM_REGISTER_COUNT; i++) { - AW20216_write_register(cs_pin, AW_PAGE_SCALING, i, AW_SCALING_MAX); + aw20216_write_register(cs_pin, AW_PAGE_SCALING, i, AW_SCALING_MAX); } } -static inline void AW20216_init_current_limit(pin_t cs_pin) { +static inline void aw20216_init_current_limit(pin_t cs_pin) { // Push config - AW20216_write_register(cs_pin, AW_PAGE_FUNCTION, AW_REG_GLOBALCURRENT, AW_GLOBAL_CURRENT_MAX); + aw20216_write_register(cs_pin, AW_PAGE_FUNCTION, AW_REG_GLOBALCURRENT, AW_GLOBAL_CURRENT_MAX); } -static inline void AW20216_soft_enable(pin_t cs_pin) { +static inline void aw20216_soft_enable(pin_t cs_pin) { // Push config - AW20216_write_register(cs_pin, AW_PAGE_FUNCTION, AW_REG_CONFIGURATION, AW_CONFIG_DEFAULT | AW_CHIPEN); + aw20216_write_register(cs_pin, AW_PAGE_FUNCTION, AW_REG_CONFIGURATION, AW_CONFIG_DEFAULT | AW_CHIPEN); } -static inline void AW20216_auto_lowpower(pin_t cs_pin) { - AW20216_write_register(cs_pin, AW_PAGE_FUNCTION, AW_REG_MIXFUNCTION, AW_MIXCR_DEFAULT | AW_LPEN); +static inline void aw20216_auto_lowpower(pin_t cs_pin) { + aw20216_write_register(cs_pin, AW_PAGE_FUNCTION, AW_REG_MIXFUNCTION, AW_MIXCR_DEFAULT | AW_LPEN); } -void AW20216_init(pin_t cs_pin, pin_t en_pin) { +void aw20216_init(pin_t cs_pin, pin_t en_pin) { setPinOutput(en_pin); writePinHigh(en_pin); - AW20216_soft_reset(cs_pin); + aw20216_soft_reset(cs_pin); wait_ms(2); // Drivers should start with all scaling and PWM registers as off - AW20216_init_current_limit(cs_pin); - AW20216_init_scaling(cs_pin); + aw20216_init_current_limit(cs_pin); + aw20216_init_scaling(cs_pin); - AW20216_soft_enable(cs_pin); - AW20216_auto_lowpower(cs_pin); + aw20216_soft_enable(cs_pin); + aw20216_auto_lowpower(cs_pin); } -void AW20216_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { +void aw20216_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { aw_led led; memcpy_P(&led, (&g_aw_leds[index]), sizeof(led)); + if (g_pwm_buffer[led.driver][led.r] == red && g_pwm_buffer[led.driver][led.g] == green && g_pwm_buffer[led.driver][led.b] == blue) { + return; + } g_pwm_buffer[led.driver][led.r] = red; g_pwm_buffer[led.driver][led.g] = green; g_pwm_buffer[led.driver][led.b] = blue; g_pwm_buffer_update_required[led.driver] = true; } -void AW20216_set_color_all(uint8_t red, uint8_t green, uint8_t blue) { +void aw20216_set_color_all(uint8_t red, uint8_t green, uint8_t blue) { for (uint8_t i = 0; i < RGB_MATRIX_LED_COUNT; i++) { - AW20216_set_color(i, red, green, blue); + aw20216_set_color(i, red, green, blue); } } -void AW20216_update_pwm_buffers(pin_t cs_pin, uint8_t index) { +void aw20216_update_pwm_buffers(pin_t cs_pin, uint8_t index) { if (g_pwm_buffer_update_required[index]) { - AW20216_write(cs_pin, AW_PAGE_PWM, 0, g_pwm_buffer[index], AW_PWM_REGISTER_COUNT); + aw20216_write(cs_pin, AW_PAGE_PWM, 0, g_pwm_buffer[index], AW_PWM_REGISTER_COUNT); } g_pwm_buffer_update_required[index] = false; } diff --git a/drivers/led/aw20216.h b/drivers/led/aw20216.h index c6e71b4b4e..e342cb6bac 100644 --- a/drivers/led/aw20216.h +++ b/drivers/led/aw20216.h @@ -30,10 +30,10 @@ typedef struct aw_led { extern const aw_led PROGMEM g_aw_leds[RGB_MATRIX_LED_COUNT]; -void AW20216_init(pin_t cs_pin, pin_t en_pin); -void AW20216_set_color(int index, uint8_t red, uint8_t green, uint8_t blue); -void AW20216_set_color_all(uint8_t red, uint8_t green, uint8_t blue); -void AW20216_update_pwm_buffers(pin_t cs_pin, uint8_t index); +void aw20216_init(pin_t cs_pin, pin_t en_pin); +void aw20216_set_color(int index, uint8_t red, uint8_t green, uint8_t blue); +void aw20216_set_color_all(uint8_t red, uint8_t green, uint8_t blue); +void aw20216_update_pwm_buffers(pin_t cs_pin, uint8_t index); #define CS1_SW1 0x00 #define CS2_SW1 0x01 diff --git a/drivers/led/ckled2001-simple.c b/drivers/led/ckled2001-simple.c index 6c4ffd398e..c4d4c0a4cc 100644 --- a/drivers/led/ckled2001-simple.c +++ b/drivers/led/ckled2001-simple.c @@ -42,7 +42,7 @@ uint8_t g_twi_transfer_buffer[20]; // The control buffers match the PG0 LED On/Off registers. // Storing them like this is optimal for I2C transfers to the registers. // We could optimize this and take out the unused registers from these -// buffers and the transfers in CKLED2001_write_pwm_buffer() but it's +// buffers and the transfers in ckled2001_write_pwm_buffer() but it's // probably not worth the extra complexity. uint8_t g_pwm_buffer[DRIVER_COUNT][192]; bool g_pwm_buffer_update_required[DRIVER_COUNT] = {false}; @@ -50,7 +50,7 @@ bool g_pwm_buffer_update_required[DRIVER_COUNT] = {false}; uint8_t g_led_control_registers[DRIVER_COUNT][24] = {0}; bool g_led_control_registers_update_required[DRIVER_COUNT] = {false}; -bool CKLED2001_write_register(uint8_t addr, uint8_t reg, uint8_t data) { +bool ckled2001_write_register(uint8_t addr, uint8_t reg, uint8_t data) { // If the transaction fails function returns false. g_twi_transfer_buffer[0] = reg; g_twi_transfer_buffer[1] = data; @@ -69,7 +69,7 @@ bool CKLED2001_write_register(uint8_t addr, uint8_t reg, uint8_t data) { return true; } -bool CKLED2001_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { +bool ckled2001_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { // Assumes PG1 is already selected. // If any of the transactions fails function returns false. // Transmit PWM registers in 12 transfers of 16 bytes. @@ -100,69 +100,72 @@ bool CKLED2001_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { return true; } -void CKLED2001_init(uint8_t addr) { +void ckled2001_init(uint8_t addr) { // Select to function page - CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, FUNCTION_PAGE); + ckled2001_write_register(addr, CONFIGURE_CMD_PAGE, FUNCTION_PAGE); // Setting LED driver to shutdown mode - CKLED2001_write_register(addr, CONFIGURATION_REG, MSKSW_SHUT_DOWN_MODE); + ckled2001_write_register(addr, CONFIGURATION_REG, MSKSW_SHUT_DOWN_MODE); // Setting internal channel pulldown/pullup - CKLED2001_write_register(addr, PDU_REG, MSKSET_CA_CB_CHANNEL); + ckled2001_write_register(addr, PDU_REG, MSKSET_CA_CB_CHANNEL); // Select number of scan phase - CKLED2001_write_register(addr, SCAN_PHASE_REG, PHASE_CHANNEL); + ckled2001_write_register(addr, SCAN_PHASE_REG, PHASE_CHANNEL); // Setting PWM Delay Phase - CKLED2001_write_register(addr, SLEW_RATE_CONTROL_MODE1_REG, MSKPWM_DELAY_PHASE_ENABLE); + ckled2001_write_register(addr, SLEW_RATE_CONTROL_MODE1_REG, MSKPWM_DELAY_PHASE_ENABLE); // Setting Driving/Sinking Channel Slew Rate - CKLED2001_write_register(addr, SLEW_RATE_CONTROL_MODE2_REG, MSKDRIVING_SINKING_CHHANNEL_SLEWRATE_ENABLE); + ckled2001_write_register(addr, SLEW_RATE_CONTROL_MODE2_REG, MSKDRIVING_SINKING_CHHANNEL_SLEWRATE_ENABLE); // Setting Iref - CKLED2001_write_register(addr, SOFTWARE_SLEEP_REG, MSKSLEEP_DISABLE); + ckled2001_write_register(addr, SOFTWARE_SLEEP_REG, MSKSLEEP_DISABLE); // Set LED CONTROL PAGE (Page 0) - CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, LED_CONTROL_PAGE); + ckled2001_write_register(addr, CONFIGURE_CMD_PAGE, LED_CONTROL_PAGE); for (int i = 0; i < LED_CONTROL_ON_OFF_LENGTH; i++) { - CKLED2001_write_register(addr, i, 0x00); + ckled2001_write_register(addr, i, 0x00); } // Set PWM PAGE (Page 1) - CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, LED_PWM_PAGE); + ckled2001_write_register(addr, CONFIGURE_CMD_PAGE, LED_PWM_PAGE); for (int i = 0; i < LED_CURRENT_TUNE_LENGTH; i++) { - CKLED2001_write_register(addr, i, 0x00); + ckled2001_write_register(addr, i, 0x00); } // Set CURRENT PAGE (Page 4) uint8_t current_tuen_reg_list[LED_CURRENT_TUNE_LENGTH] = CKLED2001_CURRENT_TUNE; - CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, CURRENT_TUNE_PAGE); + ckled2001_write_register(addr, CONFIGURE_CMD_PAGE, CURRENT_TUNE_PAGE); for (int i = 0; i < LED_CURRENT_TUNE_LENGTH; i++) { - CKLED2001_write_register(addr, i, current_tuen_reg_list[i]); + ckled2001_write_register(addr, i, current_tuen_reg_list[i]); } // Enable LEDs ON/OFF - CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, LED_CONTROL_PAGE); + ckled2001_write_register(addr, CONFIGURE_CMD_PAGE, LED_CONTROL_PAGE); for (int i = 0; i < LED_CONTROL_ON_OFF_LENGTH; i++) { - CKLED2001_write_register(addr, i, 0xFF); + ckled2001_write_register(addr, i, 0xFF); } // Select to function page - CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, FUNCTION_PAGE); + ckled2001_write_register(addr, CONFIGURE_CMD_PAGE, FUNCTION_PAGE); // Setting LED driver to normal mode - CKLED2001_write_register(addr, CONFIGURATION_REG, MSKSW_NORMAL_MODE); + ckled2001_write_register(addr, CONFIGURATION_REG, MSKSW_NORMAL_MODE); } -void CKLED2001_set_value(int index, uint8_t value) { +void ckled2001_set_value(int index, uint8_t value) { ckled2001_led led; if (index >= 0 && index < LED_MATRIX_LED_COUNT) { memcpy_P(&led, (&g_ckled2001_leds[index]), sizeof(led)); + if (g_pwm_buffer[led.driver][led.v] == value) { + return; + } g_pwm_buffer[led.driver][led.v] = value; g_pwm_buffer_update_required[led.driver] = true; } } -void CKLED2001_set_value_all(uint8_t value) { +void ckled2001_set_value_all(uint8_t value) { for (int i = 0; i < LED_MATRIX_LED_COUNT; i++) { - CKLED2001_set_value(i, value); + ckled2001_set_value(i, value); } } -void CKLED2001_set_led_control_register(uint8_t index, bool value) { +void ckled2001_set_led_control_register(uint8_t index, bool value) { ckled2001_led led; memcpy_P(&led, (&g_ckled2001_leds[index]), sizeof(led)); @@ -178,41 +181,41 @@ void CKLED2001_set_led_control_register(uint8_t index, bool value) { g_led_control_registers_update_required[led.driver] = true; } -void CKLED2001_update_pwm_buffers(uint8_t addr, uint8_t index) { +void ckled2001_update_pwm_buffers(uint8_t addr, uint8_t index) { if (g_pwm_buffer_update_required[index]) { - CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, LED_PWM_PAGE); + ckled2001_write_register(addr, CONFIGURE_CMD_PAGE, LED_PWM_PAGE); // If any of the transactions fail we risk writing dirty PG0, // refresh page 0 just in case. - if (!CKLED2001_write_pwm_buffer(addr, g_pwm_buffer[index])) { + if (!ckled2001_write_pwm_buffer(addr, g_pwm_buffer[index])) { g_led_control_registers_update_required[index] = true; } } g_pwm_buffer_update_required[index] = false; } -void CKLED2001_update_led_control_registers(uint8_t addr, uint8_t index) { +void ckled2001_update_led_control_registers(uint8_t addr, uint8_t index) { if (g_led_control_registers_update_required[index]) { - CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, LED_CONTROL_PAGE); + ckled2001_write_register(addr, CONFIGURE_CMD_PAGE, LED_CONTROL_PAGE); for (int i = 0; i < 24; i++) { - CKLED2001_write_register(addr, i, g_led_control_registers[index][i]); + ckled2001_write_register(addr, i, g_led_control_registers[index][i]); } } g_led_control_registers_update_required[index] = false; } -void CKLED2001_sw_return_normal(uint8_t addr) { +void ckled2001_sw_return_normal(uint8_t addr) { // Select to function page - CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, FUNCTION_PAGE); + ckled2001_write_register(addr, CONFIGURE_CMD_PAGE, FUNCTION_PAGE); // Setting LED driver to normal mode - CKLED2001_write_register(addr, CONFIGURATION_REG, MSKSW_NORMAL_MODE); + ckled2001_write_register(addr, CONFIGURATION_REG, MSKSW_NORMAL_MODE); } -void CKLED2001_sw_shutdown(uint8_t addr) { +void ckled2001_sw_shutdown(uint8_t addr) { // Select to function page - CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, FUNCTION_PAGE); + ckled2001_write_register(addr, CONFIGURE_CMD_PAGE, FUNCTION_PAGE); // Setting LED driver to shutdown mode - CKLED2001_write_register(addr, CONFIGURATION_REG, MSKSW_SHUT_DOWN_MODE); + ckled2001_write_register(addr, CONFIGURATION_REG, MSKSW_SHUT_DOWN_MODE); // Write SW Sleep Register - CKLED2001_write_register(addr, SOFTWARE_SLEEP_REG, MSKSLEEP_ENABLE); + ckled2001_write_register(addr, SOFTWARE_SLEEP_REG, MSKSLEEP_ENABLE); } diff --git a/drivers/led/ckled2001-simple.h b/drivers/led/ckled2001-simple.h index a56cad3461..c94df62dd2 100644 --- a/drivers/led/ckled2001-simple.h +++ b/drivers/led/ckled2001-simple.h @@ -27,24 +27,24 @@ typedef struct ckled2001_led { extern const ckled2001_led PROGMEM g_ckled2001_leds[LED_MATRIX_LED_COUNT]; -void CKLED2001_init(uint8_t addr); -bool CKLED2001_write_register(uint8_t addr, uint8_t reg, uint8_t data); -bool CKLED2001_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer); +void ckled2001_init(uint8_t addr); +bool ckled2001_write_register(uint8_t addr, uint8_t reg, uint8_t data); +bool ckled2001_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer); -void CKLED2001_set_value(int index, uint8_t value); -void CKLED2001_set_value_all(uint8_t value); +void ckled2001_set_value(int index, uint8_t value); +void ckled2001_set_value_all(uint8_t value); -void CKLED2001_set_led_control_register(uint8_t index, bool value); +void ckled2001_set_led_control_register(uint8_t index, bool value); // This should not be called from an interrupt // (eg. from a timer interrupt). // Call this while idle (in between matrix scans). // If the buffer is dirty, it will update the driver with the buffer. -void CKLED2001_update_pwm_buffers(uint8_t addr, uint8_t index); -void CKLED2001_update_led_control_registers(uint8_t addr, uint8_t index); +void ckled2001_update_pwm_buffers(uint8_t addr, uint8_t index); +void ckled2001_update_led_control_registers(uint8_t addr, uint8_t index); -void CKLED2001_sw_return_normal(uint8_t addr); -void CKLED2001_sw_shutdown(uint8_t addr); +void ckled2001_sw_return_normal(uint8_t addr); +void ckled2001_sw_shutdown(uint8_t addr); // Registers Page Define #define CONFIGURE_CMD_PAGE 0xFD diff --git a/drivers/led/ckled2001.c b/drivers/led/ckled2001.c index a99b479d1c..6ababf55e9 100644 --- a/drivers/led/ckled2001.c +++ b/drivers/led/ckled2001.c @@ -42,7 +42,7 @@ uint8_t g_twi_transfer_buffer[65]; // The control buffers match the PG0 LED On/Off registers. // Storing them like this is optimal for I2C transfers to the registers. // We could optimize this and take out the unused registers from these -// buffers and the transfers in CKLED2001_write_pwm_buffer() but it's +// buffers and the transfers in ckled2001_write_pwm_buffer() but it's // probably not worth the extra complexity. uint8_t g_pwm_buffer[DRIVER_COUNT][192]; bool g_pwm_buffer_update_required[DRIVER_COUNT] = {false}; @@ -50,7 +50,7 @@ bool g_pwm_buffer_update_required[DRIVER_COUNT] = {false}; uint8_t g_led_control_registers[DRIVER_COUNT][24] = {0}; bool g_led_control_registers_update_required[DRIVER_COUNT] = {false}; -bool CKLED2001_write_register(uint8_t addr, uint8_t reg, uint8_t data) { +bool ckled2001_write_register(uint8_t addr, uint8_t reg, uint8_t data) { // If the transaction fails function returns false. g_twi_transfer_buffer[0] = reg; g_twi_transfer_buffer[1] = data; @@ -69,7 +69,7 @@ bool CKLED2001_write_register(uint8_t addr, uint8_t reg, uint8_t data) { return true; } -bool CKLED2001_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { +bool ckled2001_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { // Assumes PG1 is already selected. // If any of the transactions fails function returns false. // Transmit PWM registers in 3 transfers of 64 bytes. @@ -99,57 +99,60 @@ bool CKLED2001_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { return true; } -void CKLED2001_init(uint8_t addr) { +void ckled2001_init(uint8_t addr) { // Select to function page - CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, FUNCTION_PAGE); + ckled2001_write_register(addr, CONFIGURE_CMD_PAGE, FUNCTION_PAGE); // Setting LED driver to shutdown mode - CKLED2001_write_register(addr, CONFIGURATION_REG, MSKSW_SHUT_DOWN_MODE); + ckled2001_write_register(addr, CONFIGURATION_REG, MSKSW_SHUT_DOWN_MODE); // Setting internal channel pulldown/pullup - CKLED2001_write_register(addr, PDU_REG, MSKSET_CA_CB_CHANNEL); + ckled2001_write_register(addr, PDU_REG, MSKSET_CA_CB_CHANNEL); // Select number of scan phase - CKLED2001_write_register(addr, SCAN_PHASE_REG, PHASE_CHANNEL); + ckled2001_write_register(addr, SCAN_PHASE_REG, PHASE_CHANNEL); // Setting PWM Delay Phase - CKLED2001_write_register(addr, SLEW_RATE_CONTROL_MODE1_REG, MSKPWM_DELAY_PHASE_ENABLE); + ckled2001_write_register(addr, SLEW_RATE_CONTROL_MODE1_REG, MSKPWM_DELAY_PHASE_ENABLE); // Setting Driving/Sinking Channel Slew Rate - CKLED2001_write_register(addr, SLEW_RATE_CONTROL_MODE2_REG, MSKDRIVING_SINKING_CHHANNEL_SLEWRATE_ENABLE); + ckled2001_write_register(addr, SLEW_RATE_CONTROL_MODE2_REG, MSKDRIVING_SINKING_CHHANNEL_SLEWRATE_ENABLE); // Setting Iref - CKLED2001_write_register(addr, SOFTWARE_SLEEP_REG, MSKSLEEP_DISABLE); + ckled2001_write_register(addr, SOFTWARE_SLEEP_REG, MSKSLEEP_DISABLE); // Set LED CONTROL PAGE (Page 0) - CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, LED_CONTROL_PAGE); + ckled2001_write_register(addr, CONFIGURE_CMD_PAGE, LED_CONTROL_PAGE); for (int i = 0; i < LED_CONTROL_ON_OFF_LENGTH; i++) { - CKLED2001_write_register(addr, i, 0x00); + ckled2001_write_register(addr, i, 0x00); } // Set PWM PAGE (Page 1) - CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, LED_PWM_PAGE); + ckled2001_write_register(addr, CONFIGURE_CMD_PAGE, LED_PWM_PAGE); for (int i = 0; i < LED_CURRENT_TUNE_LENGTH; i++) { - CKLED2001_write_register(addr, i, 0x00); + ckled2001_write_register(addr, i, 0x00); } // Set CURRENT PAGE (Page 4) uint8_t current_tuen_reg_list[LED_CURRENT_TUNE_LENGTH] = CKLED2001_CURRENT_TUNE; - CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, CURRENT_TUNE_PAGE); + ckled2001_write_register(addr, CONFIGURE_CMD_PAGE, CURRENT_TUNE_PAGE); for (int i = 0; i < LED_CURRENT_TUNE_LENGTH; i++) { - CKLED2001_write_register(addr, i, current_tuen_reg_list[i]); + ckled2001_write_register(addr, i, current_tuen_reg_list[i]); } // Enable LEDs ON/OFF - CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, LED_CONTROL_PAGE); + ckled2001_write_register(addr, CONFIGURE_CMD_PAGE, LED_CONTROL_PAGE); for (int i = 0; i < LED_CONTROL_ON_OFF_LENGTH; i++) { - CKLED2001_write_register(addr, i, 0xFF); + ckled2001_write_register(addr, i, 0xFF); } // Select to function page - CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, FUNCTION_PAGE); + ckled2001_write_register(addr, CONFIGURE_CMD_PAGE, FUNCTION_PAGE); // Setting LED driver to normal mode - CKLED2001_write_register(addr, CONFIGURATION_REG, MSKSW_NORMAL_MODE); + ckled2001_write_register(addr, CONFIGURATION_REG, MSKSW_NORMAL_MODE); } -void CKLED2001_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { +void ckled2001_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { ckled2001_led led; if (index >= 0 && index < RGB_MATRIX_LED_COUNT) { memcpy_P(&led, (&g_ckled2001_leds[index]), sizeof(led)); + if (g_pwm_buffer[led.driver][led.r] == red && g_pwm_buffer[led.driver][led.g] == green && g_pwm_buffer[led.driver][led.b] == blue) { + return; + } g_pwm_buffer[led.driver][led.r] = red; g_pwm_buffer[led.driver][led.g] = green; g_pwm_buffer[led.driver][led.b] = blue; @@ -157,13 +160,13 @@ void CKLED2001_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { } } -void CKLED2001_set_color_all(uint8_t red, uint8_t green, uint8_t blue) { +void ckled2001_set_color_all(uint8_t red, uint8_t green, uint8_t blue) { for (int i = 0; i < RGB_MATRIX_LED_COUNT; i++) { - CKLED2001_set_color(i, red, green, blue); + ckled2001_set_color(i, red, green, blue); } } -void CKLED2001_set_led_control_register(uint8_t index, bool red, bool green, bool blue) { +void ckled2001_set_led_control_register(uint8_t index, bool red, bool green, bool blue) { ckled2001_led led; memcpy_P(&led, (&g_ckled2001_leds[index]), sizeof(led)); @@ -193,41 +196,41 @@ void CKLED2001_set_led_control_register(uint8_t index, bool red, bool green, boo g_led_control_registers_update_required[led.driver] = true; } -void CKLED2001_update_pwm_buffers(uint8_t addr, uint8_t index) { +void ckled2001_update_pwm_buffers(uint8_t addr, uint8_t index) { if (g_pwm_buffer_update_required[index]) { - CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, LED_PWM_PAGE); + ckled2001_write_register(addr, CONFIGURE_CMD_PAGE, LED_PWM_PAGE); // If any of the transactions fail we risk writing dirty PG0, // refresh page 0 just in case. - if (!CKLED2001_write_pwm_buffer(addr, g_pwm_buffer[index])) { + if (!ckled2001_write_pwm_buffer(addr, g_pwm_buffer[index])) { g_led_control_registers_update_required[index] = true; } } g_pwm_buffer_update_required[index] = false; } -void CKLED2001_update_led_control_registers(uint8_t addr, uint8_t index) { +void ckled2001_update_led_control_registers(uint8_t addr, uint8_t index) { if (g_led_control_registers_update_required[index]) { - CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, LED_CONTROL_PAGE); + ckled2001_write_register(addr, CONFIGURE_CMD_PAGE, LED_CONTROL_PAGE); for (int i = 0; i < 24; i++) { - CKLED2001_write_register(addr, i, g_led_control_registers[index][i]); + ckled2001_write_register(addr, i, g_led_control_registers[index][i]); } } g_led_control_registers_update_required[index] = false; } -void CKLED2001_sw_return_normal(uint8_t addr) { +void ckled2001_sw_return_normal(uint8_t addr) { // Select to function page - CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, FUNCTION_PAGE); + ckled2001_write_register(addr, CONFIGURE_CMD_PAGE, FUNCTION_PAGE); // Setting LED driver to normal mode - CKLED2001_write_register(addr, CONFIGURATION_REG, MSKSW_NORMAL_MODE); + ckled2001_write_register(addr, CONFIGURATION_REG, MSKSW_NORMAL_MODE); } -void CKLED2001_sw_shutdown(uint8_t addr) { +void ckled2001_sw_shutdown(uint8_t addr) { // Select to function page - CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, FUNCTION_PAGE); + ckled2001_write_register(addr, CONFIGURE_CMD_PAGE, FUNCTION_PAGE); // Setting LED driver to shutdown mode - CKLED2001_write_register(addr, CONFIGURATION_REG, MSKSW_SHUT_DOWN_MODE); + ckled2001_write_register(addr, CONFIGURATION_REG, MSKSW_SHUT_DOWN_MODE); // Write SW Sleep Register - CKLED2001_write_register(addr, SOFTWARE_SLEEP_REG, MSKSLEEP_ENABLE); + ckled2001_write_register(addr, SOFTWARE_SLEEP_REG, MSKSLEEP_ENABLE); } diff --git a/drivers/led/ckled2001.h b/drivers/led/ckled2001.h index aa70a0623f..32da137fb7 100644 --- a/drivers/led/ckled2001.h +++ b/drivers/led/ckled2001.h @@ -29,24 +29,24 @@ typedef struct ckled2001_led { extern const ckled2001_led PROGMEM g_ckled2001_leds[RGB_MATRIX_LED_COUNT]; -void CKLED2001_init(uint8_t addr); -bool CKLED2001_write_register(uint8_t addr, uint8_t reg, uint8_t data); -bool CKLED2001_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer); +void ckled2001_init(uint8_t addr); +bool ckled2001_write_register(uint8_t addr, uint8_t reg, uint8_t data); +bool ckled2001_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer); -void CKLED2001_set_color(int index, uint8_t red, uint8_t green, uint8_t blue); -void CKLED2001_set_color_all(uint8_t red, uint8_t green, uint8_t blue); +void ckled2001_set_color(int index, uint8_t red, uint8_t green, uint8_t blue); +void ckled2001_set_color_all(uint8_t red, uint8_t green, uint8_t blue); -void CKLED2001_set_led_control_register(uint8_t index, bool red, bool green, bool blue); +void ckled2001_set_led_control_register(uint8_t index, bool red, bool green, bool blue); // This should not be called from an interrupt // (eg. from a timer interrupt). // Call this while idle (in between matrix scans). // If the buffer is dirty, it will update the driver with the buffer. -void CKLED2001_update_pwm_buffers(uint8_t addr, uint8_t index); -void CKLED2001_update_led_control_registers(uint8_t addr, uint8_t index); +void ckled2001_update_pwm_buffers(uint8_t addr, uint8_t index); +void ckled2001_update_led_control_registers(uint8_t addr, uint8_t index); -void CKLED2001_sw_return_normal(uint8_t addr); -void CKLED2001_sw_shutdown(uint8_t addr); +void ckled2001_sw_return_normal(uint8_t addr); +void ckled2001_sw_shutdown(uint8_t addr); // Registers Page Define #define CONFIGURE_CMD_PAGE 0xFD diff --git a/drivers/led/issi/is31fl3218.c b/drivers/led/issi/is31fl3218.c index c2300ebe89..970e9a0be9 100644 --- a/drivers/led/issi/is31fl3218.c +++ b/drivers/led/issi/is31fl3218.c @@ -37,58 +37,61 @@ uint8_t g_twi_transfer_buffer[20]; uint8_t g_pwm_buffer[18]; bool g_pwm_buffer_update_required = false; -void IS31FL3218_write_register(uint8_t reg, uint8_t data) { +void is31fl3218_write_register(uint8_t reg, uint8_t data) { g_twi_transfer_buffer[0] = reg; g_twi_transfer_buffer[1] = data; i2c_transmit(ISSI_ADDRESS, g_twi_transfer_buffer, 2, ISSI_TIMEOUT); } -void IS31FL3218_write_pwm_buffer(uint8_t *pwm_buffer) { +void is31fl3218_write_pwm_buffer(uint8_t *pwm_buffer) { g_twi_transfer_buffer[0] = ISSI_REG_PWM; memcpy(g_twi_transfer_buffer + 1, pwm_buffer, 18); i2c_transmit(ISSI_ADDRESS, g_twi_transfer_buffer, 19, ISSI_TIMEOUT); } -void IS31FL3218_init(void) { +void is31fl3218_init(void) { // In case we ever want to reinitialize (?) - IS31FL3218_write_register(ISSI_REG_RESET, 0x00); + is31fl3218_write_register(ISSI_REG_RESET, 0x00); // Turn off software shutdown - IS31FL3218_write_register(ISSI_REG_SHUTDOWN, 0x01); + is31fl3218_write_register(ISSI_REG_SHUTDOWN, 0x01); // Set all PWM values to zero for (uint8_t i = 0; i < 18; i++) { - IS31FL3218_write_register(ISSI_REG_PWM + i, 0x00); + is31fl3218_write_register(ISSI_REG_PWM + i, 0x00); } // Enable all channels for (uint8_t i = 0; i < 3; i++) { - IS31FL3218_write_register(ISSI_REG_CONTROL + i, 0b00111111); + is31fl3218_write_register(ISSI_REG_CONTROL + i, 0b00111111); } // Load PWM registers and LED Control register data - IS31FL3218_write_register(ISSI_REG_UPDATE, 0x01); + is31fl3218_write_register(ISSI_REG_UPDATE, 0x01); } -void IS31FL3218_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { +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) { + 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; } -void IS31FL3218_set_color_all(uint8_t red, uint8_t green, uint8_t blue) { +void is31fl3218_set_color_all(uint8_t red, uint8_t green, uint8_t blue) { for (int i = 0; i < 6; i++) { - IS31FL3218_set_color(i, red, green, blue); + is31fl3218_set_color(i, red, green, blue); } } -void IS31FL3218_update_pwm_buffers(void) { +void is31fl3218_update_pwm_buffers(void) { if (g_pwm_buffer_update_required) { - IS31FL3218_write_pwm_buffer(g_pwm_buffer); + is31fl3218_write_pwm_buffer(g_pwm_buffer); // Load PWM registers and LED Control register data - IS31FL3218_write_register(ISSI_REG_UPDATE, 0x01); + is31fl3218_write_register(ISSI_REG_UPDATE, 0x01); } g_pwm_buffer_update_required = false; } diff --git a/drivers/led/issi/is31fl3218.h b/drivers/led/issi/is31fl3218.h index 26bb01a014..2fe3768432 100644 --- a/drivers/led/issi/is31fl3218.h +++ b/drivers/led/issi/is31fl3218.h @@ -20,7 +20,7 @@ #include <stdbool.h> #include <string.h> -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_update_pwm_buffers(void); +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_update_pwm_buffers(void); diff --git a/drivers/led/issi/is31fl3731-simple.c b/drivers/led/issi/is31fl3731-simple.c index a62b21cc6b..f7f6980a3b 100644 --- a/drivers/led/issi/is31fl3731-simple.c +++ b/drivers/led/issi/is31fl3731-simple.c @@ -64,7 +64,7 @@ uint8_t g_twi_transfer_buffer[20]; // These buffers match the IS31FL3731 |