From 66015f164507022e376a15b45f41b58c0fb5ee97 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 10 Feb 2024 18:48:13 +1100 Subject: [PATCH] LED drivers: create structs to hold PWM/scaling buffers (#22955) --- drivers/led/aw20216s.c | 27 ++++--- drivers/led/issi/is31fl3218-mono.c | 44 ++++++----- drivers/led/issi/is31fl3218.c | 56 ++++++++------ drivers/led/issi/is31fl3731-mono.c | 42 +++++----- drivers/led/issi/is31fl3731.c | 54 +++++++------ drivers/led/issi/is31fl3733-mono.c | 42 +++++----- drivers/led/issi/is31fl3733.c | 54 +++++++------ drivers/led/issi/is31fl3736-mono.c | 42 +++++----- drivers/led/issi/is31fl3736.c | 54 +++++++------ drivers/led/issi/is31fl3737-mono.c | 43 ++++++----- drivers/led/issi/is31fl3737.c | 55 ++++++++------ drivers/led/issi/is31fl3741-mono.c | 56 ++++++++------ drivers/led/issi/is31fl3741.c | 76 ++++++++++--------- drivers/led/issi/is31fl3742a-mono.c | 40 ++++++---- drivers/led/issi/is31fl3742a.c | 48 +++++++----- drivers/led/issi/is31fl3743a-mono.c | 40 ++++++---- drivers/led/issi/is31fl3743a.c | 48 +++++++----- drivers/led/issi/is31fl3745-mono.c | 40 ++++++---- drivers/led/issi/is31fl3745.c | 48 +++++++----- drivers/led/issi/is31fl3746a-mono.c | 40 ++++++---- drivers/led/issi/is31fl3746a.c | 48 +++++++----- drivers/led/snled27351-mono.c | 42 +++++----- drivers/led/snled27351.c | 54 +++++++------ keyboards/input_club/k_type/is31fl3733-dual.c | 54 +++++++------ 24 files changed, 668 insertions(+), 479 deletions(-) diff --git a/drivers/led/aw20216s.c b/drivers/led/aw20216s.c index 49b059186d..9a05d72ca9 100644 --- a/drivers/led/aw20216s.c +++ b/drivers/led/aw20216s.c @@ -45,8 +45,15 @@ # define AW20216S_SPI_DIVISOR 4 #endif -uint8_t g_pwm_buffer[AW20216S_DRIVER_COUNT][AW20216S_PWM_REGISTER_COUNT]; -bool g_pwm_buffer_update_required[AW20216S_DRIVER_COUNT] = {false}; +typedef struct aw20216s_driver_t { + uint8_t pwm_buffer[AW20216S_PWM_REGISTER_COUNT]; + bool pwm_buffer_dirty; +} PACKED aw20216s_driver_t; + +aw20216s_driver_t driver_buffers[AW20216S_DRIVER_COUNT] = {{ + .pwm_buffer = {0}, + .pwm_buffer_dirty = false, +}}; bool aw20216s_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}; @@ -131,14 +138,14 @@ void aw20216s_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { aw20216s_led_t led; memcpy_P(&led, (&g_aw20216s_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) { + if (driver_buffers[led.driver].pwm_buffer[led.r] == red && driver_buffers[led.driver].pwm_buffer[led.g] == green && driver_buffers[led.driver].pwm_buffer[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; + driver_buffers[led.driver].pwm_buffer[led.r] = red; + driver_buffers[led.driver].pwm_buffer[led.g] = green; + driver_buffers[led.driver].pwm_buffer[led.b] = blue; + driver_buffers[led.driver].pwm_buffer_dirty = true; } void aw20216s_set_color_all(uint8_t red, uint8_t green, uint8_t blue) { @@ -148,9 +155,9 @@ void aw20216s_set_color_all(uint8_t red, uint8_t green, uint8_t blue) { } void aw20216s_update_pwm_buffers(pin_t cs_pin, uint8_t index) { - if (g_pwm_buffer_update_required[index]) { - aw20216s_write(cs_pin, AW20216S_PAGE_PWM, 0, g_pwm_buffer[index], AW20216S_PWM_REGISTER_COUNT); - g_pwm_buffer_update_required[index] = false; + if (driver_buffers[index].pwm_buffer_dirty) { + aw20216s_write(cs_pin, AW20216S_PAGE_PWM, 0, driver_buffers[index].pwm_buffer, AW20216S_PWM_REGISTER_COUNT); + driver_buffers[index].pwm_buffer_dirty = false; } } diff --git a/drivers/led/issi/is31fl3218-mono.c b/drivers/led/issi/is31fl3218-mono.c index 1c5e4c055c..5d3c22d7d9 100644 --- a/drivers/led/issi/is31fl3218-mono.c +++ b/drivers/led/issi/is31fl3218-mono.c @@ -28,12 +28,20 @@ # 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; +typedef struct is31fl3218_driver_t { + uint8_t pwm_buffer[IS31FL3218_PWM_REGISTER_COUNT]; + bool pwm_buffer_dirty; + uint8_t led_control_buffer[IS31FL3218_LED_CONTROL_REGISTER_COUNT]; + bool led_control_buffer_dirty; +} PACKED is31fl3218_driver_t; -uint8_t g_led_control_registers[IS31FL3218_LED_CONTROL_REGISTER_COUNT] = {0}; -bool g_led_control_registers_update_required = false; +// IS31FL3218 has 18 PWM outputs and a fixed I2C address, so no chaining. +is31fl3218_driver_t driver_buffers = { + .pwm_buffer = {0}, + .pwm_buffer_dirty = false, + .led_control_buffer = {0}, + .led_control_buffer_dirty = false, +}; void is31fl3218_write_register(uint8_t reg, uint8_t data) { #if IS31FL3218_I2C_PERSISTENCE > 0 @@ -48,10 +56,10 @@ void is31fl3218_write_register(uint8_t reg, uint8_t data) { void is31fl3218_write_pwm_buffer(void) { #if IS31FL3218_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3218_I2C_PERSISTENCE; i++) { - if (i2c_write_register(IS31FL3218_I2C_ADDRESS << 1, IS31FL3218_REG_PWM, g_pwm_buffer, 18, IS31FL3218_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; + if (i2c_write_register(IS31FL3218_I2C_ADDRESS << 1, IS31FL3218_REG_PWM, driver_buffers.pwm_buffer, 18, IS31FL3218_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; } #else - i2c_write_register(IS31FL3218_I2C_ADDRESS << 1, IS31FL3218_REG_PWM, g_pwm_buffer, 18, IS31FL3218_I2C_TIMEOUT); + i2c_write_register(IS31FL3218_I2C_ADDRESS << 1, IS31FL3218_REG_PWM, driver_buffers.pwm_buffer, 18, IS31FL3218_I2C_TIMEOUT); #endif } @@ -90,12 +98,12 @@ void is31fl3218_set_value(int index, uint8_t value) { if (index >= 0 && index < IS31FL3218_LED_COUNT) { memcpy_P(&led, (&g_is31fl3218_leds[index]), sizeof(led)); - if (g_pwm_buffer[led.v] == value) { + if (driver_buffers.pwm_buffer[led.v] == value) { return; } - g_pwm_buffer[led.v] = value; - g_pwm_buffer_update_required = true; + driver_buffers.pwm_buffer[led.v] = value; + driver_buffers.pwm_buffer_dirty = true; } } @@ -113,30 +121,30 @@ void is31fl3218_set_led_control_register(uint8_t index, bool value) { uint8_t bit_value = led.v % 6; if (value) { - g_led_control_registers[control_register] |= (1 << bit_value); + driver_buffers.led_control_buffer[control_register] |= (1 << bit_value); } else { - g_led_control_registers[control_register] &= ~(1 << bit_value); + driver_buffers.led_control_buffer[control_register] &= ~(1 << bit_value); } - g_led_control_registers_update_required = true; + driver_buffers.led_control_buffer_dirty = true; } void is31fl3218_update_pwm_buffers(void) { - if (g_pwm_buffer_update_required) { + if (driver_buffers.pwm_buffer_dirty) { is31fl3218_write_pwm_buffer(); // Load PWM registers and LED Control register data is31fl3218_write_register(IS31FL3218_REG_UPDATE, 0x01); - g_pwm_buffer_update_required = false; + driver_buffers.pwm_buffer_dirty = false; } } void is31fl3218_update_led_control_registers(void) { - if (g_led_control_registers_update_required) { + if (driver_buffers.led_control_buffer_dirty) { 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]); + is31fl3218_write_register(IS31FL3218_REG_LED_CONTROL_1 + i, driver_buffers.led_control_buffer[i]); } - g_led_control_registers_update_required = false; + driver_buffers.led_control_buffer_dirty = false; } } diff --git a/drivers/led/issi/is31fl3218.c b/drivers/led/issi/is31fl3218.c index 5099480023..c591e22a51 100644 --- a/drivers/led/issi/is31fl3218.c +++ b/drivers/led/issi/is31fl3218.c @@ -28,12 +28,20 @@ # 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; +typedef struct is31fl3218_driver_t { + uint8_t pwm_buffer[IS31FL3218_PWM_REGISTER_COUNT]; + bool pwm_buffer_dirty; + uint8_t led_control_buffer[IS31FL3218_LED_CONTROL_REGISTER_COUNT]; + bool led_control_buffer_dirty; +} PACKED is31fl3218_driver_t; -uint8_t g_led_control_registers[IS31FL3218_LED_CONTROL_REGISTER_COUNT] = {0}; -bool g_led_control_registers_update_required = false; +// IS31FL3218 has 18 PWM outputs and a fixed I2C address, so no chaining. +is31fl3218_driver_t driver_buffers = { + .pwm_buffer = {0}, + .pwm_buffer_dirty = false, + .led_control_buffer = {0}, + .led_control_buffer_dirty = false, +}; void is31fl3218_write_register(uint8_t reg, uint8_t data) { #if IS31FL3218_I2C_PERSISTENCE > 0 @@ -48,10 +56,10 @@ void is31fl3218_write_register(uint8_t reg, uint8_t data) { void is31fl3218_write_pwm_buffer(void) { #if IS31FL3218_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3218_I2C_PERSISTENCE; i++) { - if (i2c_write_register(IS31FL3218_I2C_ADDRESS << 1, IS31FL3218_REG_PWM, g_pwm_buffer, 18, IS31FL3218_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; + if (i2c_write_register(IS31FL3218_I2C_ADDRESS << 1, IS31FL3218_REG_PWM, driver_buffers.pwm_buffer, 18, IS31FL3218_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; } #else - i2c_write_register(IS31FL3218_I2C_ADDRESS << 1, IS31FL3218_REG_PWM, g_pwm_buffer, 18, IS31FL3218_I2C_TIMEOUT); + i2c_write_register(IS31FL3218_I2C_ADDRESS << 1, IS31FL3218_REG_PWM, driver_buffers.pwm_buffer, 18, IS31FL3218_I2C_TIMEOUT); #endif } @@ -90,14 +98,14 @@ void is31fl3218_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { if (index >= 0 && index < IS31FL3218_LED_COUNT) { memcpy_P(&led, (&g_is31fl3218_leds[index]), sizeof(led)); - if (g_pwm_buffer[led.r] == red && g_pwm_buffer[led.g] == green && g_pwm_buffer[led.b] == blue) { + if (driver_buffers.pwm_buffer[led.r] == red && driver_buffers.pwm_buffer[led.g] == green && driver_buffers.pwm_buffer[led.b] == blue) { return; } - g_pwm_buffer[led.r] = red; - g_pwm_buffer[led.g] = green; - g_pwm_buffer[led.b] = blue; - g_pwm_buffer_update_required = true; + driver_buffers.pwm_buffer[led.r] = red; + driver_buffers.pwm_buffer[led.g] = green; + driver_buffers.pwm_buffer[led.b] = blue; + driver_buffers.pwm_buffer_dirty = true; } } @@ -119,40 +127,40 @@ void is31fl3218_set_led_control_register(uint8_t index, bool red, bool green, bo uint8_t bit_b = led.b % 6; if (red) { - g_led_control_registers[control_register_r] |= (1 << bit_r); + driver_buffers.led_control_buffer[control_register_r] |= (1 << bit_r); } else { - g_led_control_registers[control_register_r] &= ~(1 << bit_r); + driver_buffers.led_control_buffer[control_register_r] &= ~(1 << bit_r); } if (green) { - g_led_control_registers[control_register_g] |= (1 << bit_g); + driver_buffers.led_control_buffer[control_register_g] |= (1 << bit_g); } else { - g_led_control_registers[control_register_g] &= ~(1 << bit_g); + driver_buffers.led_control_buffer[control_register_g] &= ~(1 << bit_g); } if (blue) { - g_led_control_registers[control_register_b] |= (1 << bit_b); + driver_buffers.led_control_buffer[control_register_b] |= (1 << bit_b); } else { - g_led_control_registers[control_register_b] &= ~(1 << bit_b); + driver_buffers.led_control_buffer[control_register_b] &= ~(1 << bit_b); } - g_led_control_registers_update_required = true; + driver_buffers.led_control_buffer_dirty = true; } void is31fl3218_update_pwm_buffers(void) { - if (g_pwm_buffer_update_required) { + if (driver_buffers.pwm_buffer_dirty) { is31fl3218_write_pwm_buffer(); // Load PWM registers and LED Control register data is31fl3218_write_register(IS31FL3218_REG_UPDATE, 0x01); - g_pwm_buffer_update_required = false; + driver_buffers.pwm_buffer_dirty = false; } } void is31fl3218_update_led_control_registers(void) { - if (g_led_control_registers_update_required) { + if (driver_buffers.led_control_buffer_dirty) { 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]); + is31fl3218_write_register(IS31FL3218_REG_LED_CONTROL_1 + i, driver_buffers.led_control_buffer[i]); } - g_led_control_registers_update_required = false; + driver_buffers.led_control_buffer_dirty = false; } } diff --git a/drivers/led/issi/is31fl3731-mono.c b/drivers/led/issi/is31fl3731-mono.c index 0ffc87765d..d3be496ec3 100644 --- a/drivers/led/issi/is31fl3731-mono.c +++ b/drivers/led/issi/is31fl3731-mono.c @@ -50,11 +50,19 @@ const uint8_t i2c_addresses[IS31FL3731_DRIVER_COUNT] = { // We could optimize this and take out the unused registers from these // buffers and the transfers in is31fl3731_write_pwm_buffer() but it's // probably not worth the extra complexity. -uint8_t g_pwm_buffer[IS31FL3731_DRIVER_COUNT][IS31FL3731_PWM_REGISTER_COUNT]; -bool g_pwm_buffer_update_required[IS31FL3731_DRIVER_COUNT] = {false}; +typedef struct is31fl3731_driver_t { + uint8_t pwm_buffer[IS31FL3731_PWM_REGISTER_COUNT]; + bool pwm_buffer_dirty; + uint8_t led_control_buffer[IS31FL3731_LED_CONTROL_REGISTER_COUNT]; + bool led_control_buffer_dirty; +} PACKED is31fl3731_driver_t; -uint8_t g_led_control_registers[IS31FL3731_DRIVER_COUNT][IS31FL3731_LED_CONTROL_REGISTER_COUNT] = {0}; -bool g_led_control_registers_update_required[IS31FL3731_DRIVER_COUNT] = {false}; +is31fl3731_driver_t driver_buffers[IS31FL3731_DRIVER_COUNT] = {{ + .pwm_buffer = {0}, + .pwm_buffer_dirty = false, + .led_control_buffer = {0}, + .led_control_buffer_dirty = false, +}}; void is31fl3731_write_register(uint8_t index, uint8_t reg, uint8_t data) { #if IS31FL3731_I2C_PERSISTENCE > 0 @@ -78,10 +86,10 @@ void is31fl3731_write_pwm_buffer(uint8_t index) { for (uint8_t i = 0; i < IS31FL3731_PWM_REGISTER_COUNT; i += 16) { #if IS31FL3731_I2C_PERSISTENCE > 0 for (uint8_t j = 0; j < IS31FL3731_I2C_PERSISTENCE; j++) { - if (i2c_write_register(i2c_addresses[index] << 1, IS31FL3731_FRAME_REG_PWM + i, g_pwm_buffer[index] + i, 16, IS31FL3731_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; + if (i2c_write_register(i2c_addresses[index] << 1, IS31FL3731_FRAME_REG_PWM + i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3731_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; } #else - i2c_write_register(i2c_addresses[index] << 1, IS31FL3731_FRAME_REG_PWM + i, g_pwm_buffer[index] + i, 16, IS31FL3731_I2C_TIMEOUT); + i2c_write_register(i2c_addresses[index] << 1, IS31FL3731_FRAME_REG_PWM + i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3731_I2C_TIMEOUT); #endif } } @@ -160,12 +168,12 @@ void is31fl3731_set_value(int index, uint8_t value) { if (index >= 0 && index < IS31FL3731_LED_COUNT) { memcpy_P(&led, (&g_is31fl3731_leds[index]), sizeof(led)); - if (g_pwm_buffer[led.driver][led.v] == value) { + if (driver_buffers[led.driver].pwm_buffer[led.v] == value) { return; } - g_pwm_buffer[led.driver][led.v] = value; - g_pwm_buffer_update_required[led.driver] = true; + driver_buffers[led.driver].pwm_buffer[led.v] = value; + driver_buffers[led.driver].pwm_buffer_dirty = true; } } @@ -183,29 +191,29 @@ void is31fl3731_set_led_control_register(uint8_t index, bool value) { uint8_t bit_value = led.v % 8; if (value) { - g_led_control_registers[led.driver][control_register] |= (1 << bit_value); + driver_buffers[led.driver].led_control_buffer[control_register] |= (1 << bit_value); } else { - g_led_control_registers[led.driver][control_register] &= ~(1 << bit_value); + driver_buffers[led.driver].led_control_buffer[control_register] &= ~(1 << bit_value); } - g_led_control_registers_update_required[led.driver] = true; + driver_buffers[led.driver].led_control_buffer_dirty = true; } void is31fl3731_update_pwm_buffers(uint8_t index) { - if (g_pwm_buffer_update_required[index]) { + if (driver_buffers[index].pwm_buffer_dirty) { is31fl3731_write_pwm_buffer(index); - g_pwm_buffer_update_required[index] = false; + driver_buffers[index].pwm_buffer_dirty = false; } } void is31fl3731_update_led_control_registers(uint8_t index) { - if (g_led_control_registers_update_required[index]) { + if (driver_buffers[index].led_control_buffer_dirty) { for (uint8_t i = 0; i < IS31FL3731_LED_CONTROL_REGISTER_COUNT; i++) { - is31fl3731_write_register(index, i, g_led_control_registers[index][i]); + is31fl3731_write_register(index, i, driver_buffers[index].led_control_buffer[i]); } - g_led_control_registers_update_required[index] = false; + driver_buffers[index].led_control_buffer_dirty = false; } } diff --git a/drivers/led/issi/is31fl3731.c b/drivers/led/issi/is31fl3731.c index bb6f7e4028..09a4afec90 100644 --- a/drivers/led/issi/is31fl3731.c +++ b/drivers/led/issi/is31fl3731.c @@ -49,11 +49,19 @@ const uint8_t i2c_addresses[IS31FL3731_DRIVER_COUNT] = { // We could optimize this and take out the unused registers from these // buffers and the transfers in is31fl3731_write_pwm_buffer() but it's // probably not worth the extra complexity. -uint8_t g_pwm_buffer[IS31FL3731_DRIVER_COUNT][IS31FL3731_PWM_REGISTER_COUNT]; -bool g_pwm_buffer_update_required[IS31FL3731_DRIVER_COUNT] = {false}; +typedef struct is31fl3731_driver_t { + uint8_t pwm_buffer[IS31FL3731_PWM_REGISTER_COUNT]; + bool pwm_buffer_dirty; + uint8_t led_control_buffer[IS31FL3731_LED_CONTROL_REGISTER_COUNT]; + bool led_control_buffer_dirty; +} PACKED is31fl3731_driver_t; -uint8_t g_led_control_registers[IS31FL3731_DRIVER_COUNT][IS31FL3731_LED_CONTROL_REGISTER_COUNT] = {0}; -bool g_led_control_registers_update_required[IS31FL3731_DRIVER_COUNT] = {false}; +is31fl3731_driver_t driver_buffers[IS31FL3731_DRIVER_COUNT] = {{ + .pwm_buffer = {0}, + .pwm_buffer_dirty = false, + .led_control_buffer = {0}, + .led_control_buffer_dirty = false, +}}; void is31fl3731_write_register(uint8_t index, uint8_t reg, uint8_t data) { #if IS31FL3731_I2C_PERSISTENCE > 0 @@ -77,10 +85,10 @@ void is31fl3731_write_pwm_buffer(uint8_t index) { for (uint8_t i = 0; i < IS31FL3731_PWM_REGISTER_COUNT; i += 16) { #if IS31FL3731_I2C_PERSISTENCE > 0 for (uint8_t j = 0; j < IS31FL3731_I2C_PERSISTENCE; j++) { - if (i2c_write_register(i2c_addresses[index] << 1, IS31FL3731_FRAME_REG_PWM + i, g_pwm_buffer[index] + i, 16, IS31FL3731_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; + if (i2c_write_register(i2c_addresses[index] << 1, IS31FL3731_FRAME_REG_PWM + i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3731_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; } #else - i2c_write_register(i2c_addresses[index] << 1, IS31FL3731_FRAME_REG_PWM + i, g_pwm_buffer[index] + i, 16, IS31FL3731_I2C_TIMEOUT); + i2c_write_register(i2c_addresses[index] << 1, IS31FL3731_FRAME_REG_PWM + i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3731_I2C_TIMEOUT); #endif } } @@ -159,14 +167,14 @@ void is31fl3731_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { if (index >= 0 && index < IS31FL3731_LED_COUNT) { memcpy_P(&led, (&g_is31fl3731_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) { + if (driver_buffers[led.driver].pwm_buffer[led.r] == red && driver_buffers[led.driver].pwm_buffer[led.g] == green && driver_buffers[led.driver].pwm_buffer[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; + driver_buffers[led.driver].pwm_buffer[led.r] = red; + driver_buffers[led.driver].pwm_buffer[led.g] = green; + driver_buffers[led.driver].pwm_buffer[led.b] = blue; + driver_buffers[led.driver].pwm_buffer_dirty = true; } } @@ -188,39 +196,39 @@ void is31fl3731_set_led_control_register(uint8_t index, bool red, bool green, bo uint8_t bit_b = led.b % 8; if (red) { - g_led_control_registers[led.driver][control_register_r] |= (1 << bit_r); + driver_buffers[led.driver].led_control_buffer[control_register_r] |= (1 << bit_r); } else { - g_led_control_registers[led.driver][control_register_r] &= ~(1 << bit_r); + driver_buffers[led.driver].led_control_buffer[control_register_r] &= ~(1 << bit_r); } if (green) { - g_led_control_registers[led.driver][control_register_g] |= (1 << bit_g); + driver_buffers[led.driver].led_control_buffer[control_register_g] |= (1 << bit_g); } else { - g_led_control_registers[led.driver][control_register_g] &= ~(1 << bit_g); + driver_buffers[led.driver].led_control_buffer[control_register_g] &= ~(1 << bit_g); } if (blue) { - g_led_control_registers[led.driver][control_register_b] |= (1 << bit_b); + driver_buffers[led.driver].led_control_buffer[control_register_b] |= (1 << bit_b); } else { - g_led_control_registers[led.driver][control_register_b] &= ~(1 << bit_b); + driver_buffers[led.driver].led_control_buffer[control_register_b] &= ~(1 << bit_b); } - g_led_control_registers_update_required[led.driver] = true; + driver_buffers[led.driver].led_control_buffer_dirty = true; } void is31fl3731_update_pwm_buffers(uint8_t index) { - if (g_pwm_buffer_update_required[index]) { + if (driver_buffers[index].pwm_buffer_dirty) { is31fl3731_write_pwm_buffer(index); - g_pwm_buffer_update_required[index] = false; + driver_buffers[index].pwm_buffer_dirty = false; } } void is31fl3731_update_led_control_registers(uint8_t index) { - if (g_led_control_registers_update_required[index]) { + if (driver_buffers[index].led_control_buffer_dirty) { for (uint8_t i = 0; i < IS31FL3731_LED_CONTROL_REGISTER_COUNT; i++) { - is31fl3731_write_register(index, i, g_led_control_registers[index][i]); + is31fl3731_write_register(index, i, driver_buffers[index].led_control_buffer[i]); } - g_led_control_registers_update_required[index] = false; + driver_buffers[index].led_control_buffer_dirty = false; } } diff --git a/drivers/led/issi/is31fl3733-mono.c b/drivers/led/issi/is31fl3733-mono.c index bbe799bc90..ecbfa4b7b1 100644 --- a/drivers/led/issi/is31fl3733-mono.c +++ b/drivers/led/issi/is31fl3733-mono.c @@ -94,11 +94,19 @@ const uint8_t driver_sync[IS31FL3733_DRIVER_COUNT] = { // We could optimize this and take out the unused registers from these // buffers and the transfers in is31fl3733_write_pwm_buffer() but it's // probably not worth the extra complexity. -uint8_t g_pwm_buffer[IS31FL3733_DRIVER_COUNT][IS31FL3733_PWM_REGISTER_COUNT]; -bool g_pwm_buffer_update_required[IS31FL3733_DRIVER_COUNT] = {false}; +typedef struct is31fl3733_driver_t { + uint8_t pwm_buffer[IS31FL3733_PWM_REGISTER_COUNT]; + bool pwm_buffer_dirty; + uint8_t led_control_buffer[IS31FL3733_LED_CONTROL_REGISTER_COUNT]; + bool led_control_buffer_dirty; +} PACKED is31fl3733_driver_t; -uint8_t g_led_control_registers[IS31FL3733_DRIVER_COUNT][IS31FL3733_LED_CONTROL_REGISTER_COUNT] = {0}; -bool g_led_control_registers_update_required[IS31FL3733_DRIVER_COUNT] = {false}; +is31fl3733_driver_t driver_buffers[IS31FL3733_DRIVER_COUNT] = {{ + .pwm_buffer = {0}, + .pwm_buffer_dirty = false, + .led_control_buffer = {0}, + .led_control_buffer_dirty = false, +}}; void is31fl3733_write_register(uint8_t index, uint8_t reg, uint8_t data) { #if IS31FL3733_I2C_PERSISTENCE > 0 @@ -123,10 +131,10 @@ void is31fl3733_write_pwm_buffer(uint8_t index) { for (uint8_t i = 0; i < IS31FL3733_PWM_REGISTER_COUNT; i += 16) { #if IS31FL3733_I2C_PERSISTENCE > 0 for (uint8_t j = 0; j < IS31FL3733_I2C_PERSISTENCE; j++) { - if (i2c_write_register(i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3733_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; + if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3733_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; } #else - i2c_write_register(i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3733_I2C_TIMEOUT); + i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3733_I2C_TIMEOUT); #endif } } @@ -191,12 +199,12 @@ void is31fl3733_set_value(int index, uint8_t value) { if (index >= 0 && index < IS31FL3733_LED_COUNT) { memcpy_P(&led, (&g_is31fl3733_leds[index]), sizeof(led)); - if (g_pwm_buffer[led.driver][led.v] == value) { + if (driver_buffers[led.driver].pwm_buffer[led.v] == value) { return; } - g_pwm_buffer[led.driver][led.v] = value; - g_pwm_buffer_update_required[led.driver] = true; + driver_buffers[led.driver].pwm_buffer[led.v] = value; + driver_buffers[led.driver].pwm_buffer_dirty = true; } } @@ -214,33 +222,33 @@ void is31fl3733_set_led_control_register(uint8_t index, bool value) { uint8_t bit_value = led.v % 8; if (value) { - g_led_control_registers[led.driver][control_register] |= (1 << bit_value); + driver_buffers[led.driver].led_control_buffer[control_register] |= (1 << bit_value); } else { - g_led_control_registers[led.driver][control_register] &= ~(1 << bit_value); + driver_buffers[led.driver].led_control_buffer[control_register] &= ~(1 << bit_value); } - g_led_control_registers_update_required[led.driver] = true; + driver_buffers[led.driver].led_control_buffer_dirty = true; } void is31fl3733_update_pwm_buffers(uint8_t index) { - if (g_pwm_buffer_update_required[index]) { + if (driver_buffers[index].pwm_buffer_dirty) { is31fl3733_select_page(index, IS31FL3733_COMMAND_PWM); is31fl3733_write_pwm_buffer(index); - g_pwm_buffer_update_required[index] = false; + driver_buffers[index].pwm_buffer_dirty = false; } } void is31fl3733_update_led_control_registers(uint8_t index) { - if (g_led_control_registers_update_required[index]) { + if (driver_buffers[index].led_control_buffer_dirty) { is31fl3733_select_page(index, IS31FL3733_COMMAND_LED_CONTROL); for (uint8_t i = 0; i < IS31FL3733_LED_CONTROL_REGISTER_COUNT; i++) { - is31fl3733_write_register(index, i, g_led_control_registers[index][i]); + is31fl3733_write_register(index, i, driver_buffers[index].led_control_buffer[i]); } - g_led_control_registers_update_required[index] = false; + driver_buffers[index].led_control_buffer_dirty = false; } } diff --git a/drivers/led/issi/is31fl3733.c b/drivers/led/issi/is31fl3733.c index 9637746d22..236eed72eb 100644 --- a/drivers/led/issi/is31fl3733.c +++ b/drivers/led/issi/is31fl3733.c @@ -93,11 +93,19 @@ const uint8_t driver_sync[IS31FL3733_DRIVER_COUNT] = { // We could optimize this and take out the unused registers from these // buffers and the transfers in is31fl3733_write_pwm_buffer() but it's // probably not worth the extra complexity. -uint8_t g_pwm_buffer[IS31FL3733_DRIVER_COUNT][IS31FL3733_PWM_REGISTER_COUNT]; -bool g_pwm_buffer_update_required[IS31FL3733_DRIVER_COUNT] = {false}; +typedef struct is31fl3733_driver_t { + uint8_t pwm_buffer[IS31FL3733_PWM_REGISTER_COUNT]; + bool pwm_buffer_dirty; + uint8_t led_control_buffer[IS31FL3733_LED_CONTROL_REGISTER_COUNT]; + bool led_control_buffer_dirty; +} PACKED is31fl3733_driver_t; -uint8_t g_led_control_registers[IS31FL3733_DRIVER_COUNT][IS31FL3733_LED_CONTROL_REGISTER_COUNT] = {0}; -bool g_led_control_registers_update_required[IS31FL3733_DRIVER_COUNT] = {false}; +is31fl3733_driver_t driver_buffers[IS31FL3733_DRIVER_COUNT] = {{ + .pwm_buffer = {0}, + .pwm_buffer_dirty = false, + .led_control_buffer = {0}, + .led_control_buffer_dirty = false, +}}; void is31fl3733_write_register(uint8_t index, uint8_t reg, uint8_t data) { #if IS31FL3733_I2C_PERSISTENCE > 0 @@ -122,10 +130,10 @@ void is31fl3733_write_pwm_buffer(uint8_t index) { for (uint8_t i = 0; i < IS31FL3733_PWM_REGISTER_COUNT; i += 16) { #if IS31FL3733_I2C_PERSISTENCE > 0 for (uint8_t j = 0; j < IS31FL3733_I2C_PERSISTENCE; j++) { - if (i2c_write_register(i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3733_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; + if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3733_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; } #else - i2c_write_register(i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3733_I2C_TIMEOUT); + i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3733_I2C_TIMEOUT); #endif } } @@ -190,14 +198,14 @@ void is31fl3733_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { if (index >= 0 && index < IS31FL3733_LED_COUNT) { memcpy_P(&led, (&g_is31fl3733_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) { + if (driver_buffers[led.driver].pwm_buffer[led.r] == red && driver_buffers[led.driver].pwm_buffer[led.g] == green && driver_buffers[led.driver].pwm_buffer[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; + driver_buffers[led.driver].pwm_buffer[led.r] = red; + driver_buffers[led.driver].pwm_buffer[led.g] = green; + driver_buffers[led.driver].pwm_buffer[led.b] = blue; + driver_buffers[led.driver].pwm_buffer_dirty = true; } } @@ -219,43 +227,43 @@ void is31fl3733_set_led_control_register(uint8_t index, bool red, bool green, bo uint8_t bit_b = led.b % 8; if (red) { - g_led_control_registers[led.driver][control_register_r] |= (1 << bit_r); + driver_buffers[led.driver].led_control_buffer[control_register_r] |= (1 << bit_r); } else { - g_led_control_registers[led.driver][control_register_r] &= ~(1 << bit_r); + driver_buffers[led.driver].led_control_buffer[control_register_r] &= ~(1 << bit_r); } if (green) { - g_led_control_registers[led.driver][control_register_g] |= (1 << bit_g); + driver_buffers[led.driver].led_control_buffer[control_register_g] |= (1 << bit_g); } else { - g_led_control_registers[led.driver][control_register_g] &= ~(1 << bit_g); + driver_buffers[led.driver].led_control_buffer[control_register_g] &= ~(1 << bit_g); } if (blue) { - g_led_control_registers[led.driver][control_register_b] |= (1 << bit_b); + driver_buffers[led.driver].led_control_buffer[control_register_b] |= (1 << bit_b); } else { - g_led_control_registers[led.driver][control_register_b] &= ~(1 << bit_b); + driver_buffers[led.driver].led_control_buffer[control_register_b] &= ~(1 << bit_b); } - g_led_control_registers_update_required[led.driver] = true; + driver_buffers[led.driver].led_control_buffer_dirty = true; } void is31fl3733_update_pwm_buffers(uint8_t index) { - if (g_pwm_buffer_update_required[index]) { + if (driver_buffers[index].pwm_buffer_dirty) { is31fl3733_select_page(index, IS31FL3733_COMMAND_PWM); is31fl3733_write_pwm_buffer(index); - g_pwm_buffer_update_required[index] = false; + driver_buffers[index].pwm_buffer_dirty = false; } } void is31fl3733_update_led_control_registers(uint8_t index) { - if (g_led_control_registers_update_required[index]) { + if (driver_buffers[index].led_control_buffer_dirty) { is31fl3733_select_page(index, IS31FL3733_COMMAND_LED_CONTROL); for (uint8_t i = 0; i < IS31FL3733_LED_CONTROL_REGISTER_COUNT; i++) { - is31fl3733_write_register(index, i, g_led_control_registers[index][i]); + is31fl3733_write_register(index, i, driver_buffers[index].led_control_buffer[i]); } - g_led_control_registers_update_required[index] = false; + driver_buffers[index].led_control_buffer_dirty = false; } } diff --git a/drivers/led/issi/is31fl3736-mono.c b/drivers/led/issi/is31fl3736-mono.c index fa9c520fc6..e1c3279772 100644 --- a/drivers/led/issi/is31fl3736-mono.c +++ b/drivers/led/issi/is31fl3736-mono.c @@ -65,11 +65,19 @@ const uint8_t i2c_addresses[IS31FL3736_DRIVER_COUNT] = { // We could optimize this and take out the unused registers from these // buffers and the transfers in is31fl3736_write_pwm_buffer() but it's // probably not worth the extra complexity. -uint8_t g_pwm_buffer[IS31FL3736_DRIVER_COUNT][IS31FL3736_PWM_REGISTER_COUNT]; -bool g_pwm_buffer_update_required[IS31FL3736_DRIVER_COUNT] = {false}; +typedef struct is31fl3736_driver_t { + uint8_t pwm_buffer[IS31FL3736_PWM_REGISTER_COUNT]; + bool pwm_buffer_dirty; + uint8_t led_control_buffer[IS31FL3736_LED_CONTROL_REGISTER_COUNT]; + bool led_control_buffer_dirty; +} PACKED is31fl3736_driver_t; -uint8_t g_led_control_registers[IS31FL3736_DRIVER_COUNT][IS31FL3736_LED_CONTROL_REGISTER_COUNT] = {0}; -bool g_led_control_registers_update_required[IS31FL3736_DRIVER_COUNT] = {false}; +is31fl3736_driver_t driver_buffers[IS31FL3736_DRIVER_COUNT] = {{ + .pwm_buffer = {0}, + .pwm_buffer_dirty = false, + .led_control_buffer = {0}, + .led_control_buffer_dirty = false, +}}; void is31fl3736_write_register(uint8_t index, uint8_t reg, uint8_t data) { #if IS31FL3736_I2C_PERSISTENCE > 0 @@ -94,10 +102,10 @@ void is31fl3736_write_pwm_buffer(uint8_t index) { for (uint8_t i = 0; i < IS31FL3736_PWM_REGISTER_COUNT; i += 16) { #if IS31FL3736_I2C_PERSISTENCE > 0 for (uint8_t j = 0; j < IS31FL3736_I2C_PERSISTENCE; j++) { - if (i2c_write_register(i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3736_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; + if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3736_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; } #else - i2c_write_register(i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3736_I2C_TIMEOUT); + i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3736_I2C_TIMEOUT); #endif } } @@ -160,12 +168,12 @@ void is31fl3736_set_value(int index, uint8_t value) { if (index >= 0 && index < IS31FL3736_LED_COUNT) { memcpy_P(&led, (&g_is31fl3736_leds[index]), sizeof(led)); - if (g_pwm_buffer[led.driver][led.v] == value) { + if (driver_buffers[led.driver].pwm_buffer[led.v] == value) { return; } - g_pwm_buffer[led.driver][led.v] = value; - g_pwm_buffer_update_required[led.driver] = true; + driver_buffers[led.driver].pwm_buffer[led.v] = value; + driver_buffers[led.driver].pwm_buffer_dirty = true; } } @@ -189,33 +197,33 @@ void is31fl3736_set_led_control_register(uint8_t index, bool value) { uint8_t bit_value = led.v % 8; if (value) { - g_led_control_registers[led.driver][control_register] |= (1 << bit_value); + driver_buffers[led.driver].led_control_buffer[control_register] |= (1 << bit_value); } else { - g_led_control_registers[led.driver][control_register] &= ~(1 << bit_value); + driver_buffers[led.driver].led_control_buffer[control_register] &= ~(1 << bit_value); } - g_led_control_registers_update_required[led.driver] = true; + driver_buffers[led.driver].led_control_buffer_dirty = true; } void is31fl3736_update_pwm_buffers(uint8_t index) { - if (g_pwm_buffer_update_required[index]) { + if (driver_buffers[index].pwm_buffer_dirty) { is31fl3736_select_page(index, IS31FL3736_COMMAND_PWM); is31fl3736_write_pwm_buffer(index); - g_pwm_buffer_update_required[index] = false; + driver_buffers[index].pwm_buffer_dirty = false; } } void is31fl3736_update_led_control_registers(uint8_t index) { - if (g_led_control_registers_update_required[index]) { + if (driver_buffers[index].led_control_buffer_dirty) { is31fl3736_select_page(index, IS31FL3736_COMMAND_LED_CONTROL); for (uint8_t i = 0; i < IS31FL3736_LED_CONTROL_REGISTER_COUNT; i++) { - is31fl3736_write_register(index, i, g_led_control_registers[index][i]); + is31fl3736_write_register(index, i, driver_buffers[index].led_control_buffer[i]); } - g_led_control_registers_update_required[index] = false; + driver_buffers[index].led_control_buffer_dirty = false; } } diff --git a/drivers/led/issi/is31fl3736.c b/drivers/led/issi/is31fl3736.c index b546589693..1d14be382d 100644 --- a/drivers/led/issi/is31fl3736.c +++ b/drivers/led/issi/is31fl3736.c @@ -65,11 +65,19 @@ const uint8_t i2c_addresses[IS31FL3736_DRIVER_COUNT] = { // We could optimize this and take out the unused registers from these // buffers and the transfers in is31fl3736_write_pwm_buffer() but it's // probably not worth the extra complexity. -uint8_t g_pwm_buffer[IS31FL3736_DRIVER_COUNT][IS31FL3736_PWM_REGISTER_COUNT]; -bool g_pwm_buffer_update_required[IS31FL3736_DRIVER_COUNT] = {false}; +typedef struct is31fl3736_driver_t { + uint8_t pwm_buffer[IS31FL3736_PWM_REGISTER_COUNT]; + bool pwm_buffer_dirty; + uint8_t led_control_buffer[IS31FL3736_LED_CONTROL_REGISTER_COUNT]; + bool led_control_buffer_dirty; +} PACKED is31fl3736_driver_t; -uint8_t g_led_control_registers[IS31FL3736_DRIVER_COUNT][IS31FL3736_LED_CONTROL_REGISTER_COUNT] = {0}; -bool g_led_control_registers_update_required[IS31FL3736_DRIVER_COUNT] = {false}; +is31fl3736_driver_t driver_buffers[IS31FL3736_DRIVER_COUNT] = {{ + .pwm_buffer = {0}, + .pwm_buffer_dirty = false, + .led_control_buffer = {0}, + .led_control_buffer_dirty = false, +}}; void is31fl3736_write_register(uint8_t index, uint8_t reg, uint8_t data) { #if IS31FL3736_I2C_PERSISTENCE > 0 @@ -94,10 +102,10 @@ void is31fl3736_write_pwm_buffer(uint8_t index) { for (uint8_t i = 0; i < IS31FL3736_PWM_REGISTER_COUNT; i += 16) { #if IS31FL3736_I2C_PERSISTENCE > 0 for (uint8_t j = 0; j < IS31FL3736_I2C_PERSISTENCE; j++) { - if (i2c_write_register(i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3736_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; + if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3736_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; } #else - i2c_write_register(i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3736_I2C_TIMEOUT); + i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3736_I2C_TIMEOUT); #endif } } @@ -160,14 +168,14 @@ void is31fl3736_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { if (index >= 0 && index < IS31FL3736_LED_COUNT) { memcpy_P(&led, (&g_is31fl3736_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) { + if (driver_buffers[led.driver].pwm_buffer[led.r] == red && driver_buffers[led.driver].pwm_buffer[led.g] == green && driver_buffers[led.driver].pwm_buffer[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; + driver_buffers[led.driver].pwm_buffer[led.r] = red; + driver_buffers[led.driver].pwm_buffer[led.g] = green; + driver_buffers[led.driver].pwm_buffer[led.b] = blue; + driver_buffers[led.driver].pwm_buffer_dirty = true; } } @@ -196,43 +204,43 @@ void is31fl3736_set_led_control_register(uint8_t index, bool red, bool green, bo uint8_t bit_b = led.b % 8; if (red) { - g_led_control_registers[led.driver][control_register_r] |= (1 << bit_r); + driver_buffers[led.driver].led_control_buffer[control_register_r] |= (1 << bit_r); } else { - g_led_control_registers[led.driver][control_register_r] &= ~(1 << bit_r); + driver_buffers[led.driver].led_control_buffer[control_register_r] &= ~(1 << bit_r); } if (green) { - g_led_control_registers[led.driver][control_register_g] |= (1 << bit_g); + driver_buffers[led.driver].led_control_buffer[control_register_g] |= (1 << bit_g); } else { - g_led_control_registers[led.driver][control_register_g] &= ~(1 << bit_g); + driver_buffers[led.driver].led_control_buffer[control_register_g] &= ~(1 << bit_g); } if (blue) { - g_led_control_registers[led.driver][control_register_b] |= (1 << bit_b); + driver_buffers[led.driver].led_control_buffer[control_register_b] |= (1 << bit_b); } else { - g_led_control_registers[led.driver][control_register_b] &= ~(1 << bit_b); + driver_buffers[led.driver].led_control_buffer[control_register_b] &= ~(1 << bit_b); } - g_led_control_registers_update_required[led.driver] = true; + driver_buffers[led.driver].led_control_buffer_dirty = true; } void is31fl3736_update_pwm_buffers(uint8_t index) { - if (g_pwm_buffer_update_required[index]) { + if (driver_buffers[index].pwm_buffer_dirty) { is31fl3736_select_page(index, IS31FL3736_COMMAND_PWM); is31fl3736_write_pwm_buffer(index); - g_pwm_buffer_update_required[index] = false; + driver_buffers[index].pwm_buffer_dirty = false; } } void is31fl3736_update_led_control_registers(uint8_t index) { - if (g_led_control_registers_update_required[index]) { + if (driver_buffers[index].led_control_buffer_dirty) { is31fl3736_select_page(index, IS31FL3736_COMMAND_LED_CONTROL); for (uint8_t i = 0; i < IS31FL3736_LED_CONTROL_REGISTER_COUNT; i++) { - is31fl3736_write_register(index, i, g_led_control_registers[index][i]); + is31fl3736_write_register(index, i, driver_buffers[index].led_control_buffer[i]); } - g_led_control_registers_update_required[index] = false; + driver_buffers[index].led_control_buffer_dirty = false; } } diff --git a/drivers/led/issi/is31fl3737-mono.c b/drivers/led/issi/is31fl3737-mono.c index f11358b6a3..11e23acb23 100644 --- a/drivers/led/issi/is31fl3737-mono.c +++ b/drivers/led/issi/is31fl3737-mono.c @@ -67,12 +67,19 @@ const uint8_t i2c_addresses[IS31FL3737_DRIVER_COUNT] = { // We could optimize this and take out the unused registers from these // buffers and the transfers in is31fl3737_write_pwm_buffer() but it's // probably not worth the extra complexity. +typedef struct is31fl3737_driver_t { + uint8_t pwm_buffer[IS31FL3737_PWM_REGISTER_COUNT]; + bool pwm_buffer_dirty; + uint8_t led_control_buffer[IS31FL3737_LED_CONTROL_REGISTER_COUNT]; + bool led_control_buffer_dirty; +} PACKED is31fl3737_driver_t; -uint8_t g_pwm_buffer[IS31FL3737_DRIVER_COUNT][IS31FL3737_PWM_REGISTER_COUNT]; -bool g_pwm_buffer_update_required[IS31FL3737_DRIVER_COUNT] = {false}; - -uint8_t g_led_control_registers[IS31FL3737_DRIVER_COUNT][IS31FL3737_LED_CONTROL_REGISTER_COUNT] = {0}; -bool g_led_control_registers_update_required[IS31FL3737_DRIVER_COUNT] = {false}; +is31fl3737_driver_t driver_buffers[IS31FL3737_DRIVER_COUNT] = {{ + .pwm_buffer = {0}, + .pwm_buffer_dirty = false, + .led_control_buffer = {0}, + .led_control_buffer_dirty = false, +}}; void is31fl3737_write_register(uint8_t index, uint8_t reg, uint8_t data) { #if IS31FL3737_I2C_PERSISTENCE > 0 @@ -97,10 +104,10 @@ void is31fl3737_write_pwm_buffer(uint8_t index) { for (uint8_t i = 0; i < IS31FL3737_PWM_REGISTER_COUNT; i += 16) { #if IS31FL3737_I2C_PERSISTENCE > 0 for (uint8_t j = 0; j < IS31FL3737_I2C_PERSISTENCE; j++) { - if (i2c_write_register(i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3737_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; + if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3737_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; } #else - i2c_write_register(i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3737_I2C_TIMEOUT); + i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3737_I2C_TIMEOUT); #endif } } @@ -163,12 +170,12 @@ void is31fl3737_set_value(int index, uint8_t value) { if (index >= 0 && index < IS31FL3737_LED_COUNT) { memcpy_P(&led, (&g_is31fl3737_leds[index]), sizeof(led)); - if (g_pwm_buffer[led.driver][led.v] == value) { + if (driver_buffers[led.driver].pwm_buffer[led.v] == value) { return; } - g_pwm_buffer[led.driver][led.v] = value; - g_pwm_buffer_update_required[led.driver] = true; + driver_buffers[led.driver].pwm_buffer[led.v] = value; + driver_buffers[led.driver].pwm_buffer_dirty = true; } } @@ -186,33 +193,33 @@ void is31fl3737_set_led_control_register(uint8_t index, bool value) { uint8_t bit_value = led.v % 8; if (value) { - g_led_control_registers[led.driver][control_register] |= (1 << bit_value); + driver_buffers[led.driver].led_control_buffer[control_register] |= (1 << bit_value); } else { - g_led_control_registers[led.driver][control_register] &= ~(1 << bit_value); + driver_buffers[led.driver].led_control_buffer[control_register] &= ~(1 << bit_value); } - g_led_control_registers_update_required[led.driver] = true; + driver_buffers[led.driver].led_control_buffer_dirty = true; } void is31fl3737_update_pwm_buffers(uint8_t index) { - if (g_pwm_buffer_update_required[index]) { + if (driver_buffers[index].pwm_buffer_dirty) { is31fl3737_select_page(index, IS31FL3737_COMMAND_PWM); is31fl3737_write_pwm_buffer(index); - g_pwm_buffer_update_required[index] = false; + driver_buffers[index].pwm_buffer_dirty = false; } } void is31fl3737_update_led_control_registers(uint8_t index) { - if (g_led_control_registers_update_required[index]) { + if (driver_buffers[index].led_control_buffer_dirty) { is31fl3737_select_page(index, IS31FL3737_COMMAND_LED_CONTROL); for (uint8_t i = 0; i < IS31FL3737_LED_CONTROL_REGISTER_COUNT; i++) { - is31fl3737_write_register(index, i, g_led_control_registers[index][i]); + is31fl3737_write_register(index, i, driver_buffers[index].led_control_buffer[i]); } - g_led_control_registers_update_required[index] = false; + driver_buffers[index].led_control_buffer_dirty = false; } } diff --git a/drivers/led/issi/is31fl3737.c b/drivers/led/issi/is31fl3737.c index 20f068038a..2a98d7c3d0 100644 --- a/drivers/led/issi/is31fl3737.c +++ b/drivers/led/issi/is31fl3737.c @@ -67,12 +67,19 @@ const uint8_t i2c_addresses[IS31FL3737_DRIVER_COUNT] = { // We could optimize this and take out the unused registers from these // buffers and the transfers in is31fl3737_write_pwm_buffer() but it's // probably not worth the extra complexity. +typedef struct is31fl3737_driver_t { + uint8_t pwm_buffer[IS31FL3737_PWM_REGISTER_COUNT]; + bool pwm_buffer_dirty; + uint8_t led_control_buffer[IS31FL3737_LED_CONTROL_REGISTER_COUNT]; + bool led_control_buffer_dirty; +} PACKED is31fl3737_driver_t; -uint8_t g_pwm_buffer[IS31FL3737_DRIVER_COUNT][IS31FL3737_PWM_REGISTER_COUNT]; -bool g_pwm_buffer_update_required[IS31FL3737_DRIVER_COUNT] = {false}; - -uint8_t g_led_control_registers[IS31FL3737_DRIVER_COUNT][IS31FL3737_LED_CONTROL_REGISTER_COUNT] = {0}; -bool g_led_control_registers_update_required[IS31FL3737_DRIVER_COUNT] = {false}; +is31fl3737_driver_t driver_buffers[IS31FL3737_DRIVER_COUNT] = {{ + .pwm_buffer = {0}, + .pwm_buffer_dirty = false, + .led_control_buffer = {0}, + .led_control_buffer_dirty = false, +}}; void is31fl3737_write_register(uint8_t index, uint8_t reg, uint8_t data) { #if IS31FL3737_I2C_PERSISTENCE > 0 @@ -97,10 +104,10 @@ void is31fl3737_write_pwm_buffer(uint8_t index) { for (uint8_t i = 0; i < IS31FL3737_PWM_REGISTER_COUNT; i += 16) { #if IS31FL3737_I2C_PERSISTENCE > 0 for (uint8_t j = 0; j < IS31FL3737_I2C_PERSISTENCE; j++) { - if (i2c_write_register(i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3737_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; + if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3737_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; } #else - i2c_write_register(i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3737_I2C_TIMEOUT); + i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3737_I2C_TIMEOUT); #endif } } @@ -163,14 +170,14 @@ void is31fl3737_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { if (index >= 0 && index < IS31FL3737_LED_COUNT) { memcpy_P(&led, (&g_is31fl3737_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) { + if (driver_buffers[led.driver].pwm_buffer[led.r] == red && driver_buffers[led.driver].pwm_buffer[led.g] == green && driver_buffers[led.driver].pwm_buffer[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; + driver_buffers[led.driver].pwm_buffer[led.r] = red; + driver_buffers[led.driver].pwm_buffer[led.g] = green; + driver_buffers[led.driver].pwm_buffer[led.b] = blue; + driver_buffers[led.driver].pwm_buffer_dirty = true; } } @@ -192,43 +199,43 @@ void is31fl3737_set_led_control_register(uint8_t index, bool red, bool green, bo uint8_t bit_b = led.b % 8; if (red) { - g_led_control_registers[led.driver][control_register_r] |= (1 << bit_r); + driver_buffers[led.driver].led_control_buffer[control_register_r] |= (1 << bit_r); } else { - g_led_control_registers[led.driver][control_register_r] &= ~(1 << bit_r); + driver_buffers[led.driver].led_control_buffer[control_register_r] &= ~(1 << bit_r); } if (green) { - g_led_control_registers[led.driver][control_register_g] |= (1 << bit_g); + driver_buffers[led.driver].led_control_buffer[control_register_g] |= (1 << bit_g); } else { - g_led_control_registers[led.driver][control_register_g] &= ~(1 << bit_g); + driver_buffers[led.driver].led_control_buffer[control_register_g] &= ~(1 << bit_g); } if (blue) { - g_led_control_registers[led.driver][control_register_b] |= (1 << bit_b); + driver_buffers[led.driver].led_control_buffer[control_register_b] |= (1 << bit_b); } else { - g_led_control_registers[led.driver][control_register_b] &= ~(1 << bit_b); + driver_buffers[led.driver].led_control_buffer[control_register_b] &= ~(1 << bit_b); } - g_led_control_registers_update_required[led.driver] = true; + driver_buffers[led.driver].led_control_buffer_dirty = true; } void is31fl3737_update_pwm_buffers(uint8_t index) { - if (g_pwm_buffer_update_required[index]) { + if (driver_buffers[index].pwm_buffer_dirty) { is31fl3737_select_page(index, IS31FL3737_COMMAND_PWM); is31fl3737_write_pwm_buffer(index); - g_pwm_buffer_update_required[index] = false; + driver_buffers[index].pwm_buffer_dirty = false; } } void is31fl3737_update_led_control_registers(uint8_t index) { - if (g_led_control_registers_update_required[index]) { + if (driver_buffers[index].led_control_buffer_dirty) { is31fl3737_select_page(index, IS31FL3737_COMMAND_LED_CONTROL); for (uint8_t i = 0; i < IS31FL3737_LED_CONTROL_REGISTER_COUNT; i++) { - is31fl3737_write_register(index, i, g_led_control_registers[index][i]); + is31fl3737_write_register(index, i, driver_buffers[index].led_control_buffer[i]); } - g_led_control_registers_update_required[index] = false; + driver_buffers[index].led_control_buffer_dirty = false; } } diff --git a/drivers/led/issi/is31fl3741-mono.c b/drivers/led/issi/is31fl3741-mono.c index dbe99f2f88..0212f6f204 100644 --- a/drivers/led/issi/is31fl3741-mono.c +++ b/drivers/led/issi/is31fl3741-mono.c @@ -71,11 +71,19 @@ const uint8_t i2c_addresses[IS31FL3741_DRIVER_COUNT] = { // We could optimize this and take out the unused registers from these // buffers and the transfers in is31fl3741_write_pwm_buffer() but it's // probably not worth the extra complexity. -uint8_t g_pwm_buffer[IS31FL3741_DRIVER_COUNT][IS31FL3741_PWM_REGISTER_COUNT]; -bool g_pwm_buffer_update_required[IS31FL3741_DRIVER_COUNT] = {false}; -bool g_scaling_registers_update_required[IS31FL3741_DRIVER_COUNT] = {false}; +typedef struct is31fl3741_driver_t { + uint8_t pwm_buffer[IS31FL3741_PWM_REGISTER_COUNT]; + bool pwm_buffer_dirty; + uint8_t scaling_buffer[IS31FL3741_SCALING_REGISTER_COUNT]; + bool scaling_buffer_dirty; +} PACKED is31fl3741_driver_t; -uint8_t g_scaling_registers[IS31FL3741_DRIVER_COUNT][IS31FL3741_SCALING_REGISTER_COUNT]; +is31fl3741_driver_t driver_buffers[IS31FL3741_DRIVER_COUNT] = {{ + .pwm_buffer = {0}, + .pwm_buffer_dirty = false, + .scaling_buffer = {0}, + .scaling_buffer_dirty = false, +}}; void is31fl3741_write_register(uint8_t index, uint8_t reg, uint8_t data) { #if IS31FL3741_I2C_PERSISTENCE > 0 @@ -102,20 +110,20 @@ void is31fl3741_write_pwm_buffer(uint8_t index) { #if IS31FL3741_I2C_PERSISTENCE > 0 for (uint8_t j = 0; j < IS31FL3741_I2C_PERSISTENCE; j++) { - if (i2c_write_register(i2c_addresses[index] << 1, i % 180, g_pwm_buffer[index] + i, 18, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; + if (i2c_write_register(i2c_addresses[index] << 1, i % 180, driver_buffers[index].pwm_buffer + i, 18, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; } #else - i2c_write_register(i2c_addresses[index] << 1, i % 180, g_pwm_buffer[index] + i, 18, IS31FL3741_I2C_TIMEOUT); + i2c_write_register(i2c_addresses[index] << 1, i % 180, driver_buffers[index].pwm_buffer + i, 18, IS31FL3741_I2C_TIMEOUT); #endif } // transfer the left cause the total number is 351 #if IS31FL3741_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3741_I2C_PERSISTENCE; i++) { - if (i2c_write_register(i2c_addresses[index] << 1, 162, g_pwm_buffer[index] + 342, 9, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; + if (i2c_write_register(i2c_addresses[index] << 1, 162, driver_buffers[index].pwm_buffer + 342, 9, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; } #else - i2c_write_register(i2c_addresses[index] << 1, 162, g_pwm_buffer[index] + 342, 9, IS31FL3741_I2C_TIMEOUT); + i2c_write_register(i2c_addresses[index] << 1, 162, driver_buffers[index].pwm_buffer + 342, 9, IS31FL3741_I2C_TIMEOUT); #endif } @@ -166,12 +174,12 @@ void is31fl3741_set_value(int index, uint8_t value) { if (index >= 0 && index < IS31FL3741_LED_COUNT) { memcpy_P(&led, (&g_is31fl3741_leds[index]), sizeof(led)); - if (g_pwm_buffer[led.driver][led.v] == value) { + if (driver_buffers[led.driver].pwm_buffer[led.v] == value) { return; } - g_pwm_buffer_update_required[led.driver] = true; - g_pwm_buffer[led.driver][led.v] = value; + driver_buffers[led.driver].pwm_buffer[led.v] = value; + driver_buffers[led.driver].pwm_buffer_dirty = true; } } @@ -186,52 +194,52 @@ void is31fl3741_set_led_control_register(uint8_t index, bool value) { memcpy_P(&led, (&g_is31fl3741_leds[index]), sizeof(led)); if (value) { - g_scaling_registers[led.driver][led.v] = 0xFF; + driver_buffers[led.driver].scaling_buffer[led.v] = 0xFF; } else { - g_scaling_registers[led.driver][led.v] = 0x00; + driver_buffers[led.driver].scaling_buffer[led.v] = 0x00; } - g_scaling_registers_update_required[led.driver] = true; + driver_buffers[led.driver].scaling_buffer_dirty = true; } void is31fl3741_update_pwm_buffers(uint8_t index) { - if (g_pwm_buffer_update_required[index]) { + if (driver_buffers[index].pwm_buffer_dirty) { is31fl3741_select_page(index, IS31FL3741_COMMAND_PWM_0); is31fl3741_write_pwm_buffer(index); - g_pwm_buffer_update_required[index] = false; + driver_buffers[index].pwm_buffer_dirty = false; } } void is31fl3741_set_pwm_buffer(const is31fl3741_led_t *pled, uint8_t value) { - g_pwm_buffer[pled->driver][pled->v] = value; - g_pwm_buffer_update_required[pled->driver] = true; + driver_buffers[pled->driver].pwm_buffer[pled->v] = value; + driver_buffers[pled->driver].pwm_buffer_dirty = true; } void is31fl3741_update_led_control_registers(uint8_t index) { - if (g_scaling_registers_update_required[index]) { + if (driver_buffers[index].scaling_buffer_dirty) { is31fl3741_select_page(index, IS31FL3741_COMMAND_SCALING_0); // CS1_SW1 to CS30_SW6 are on page 2 for (int i = CS1_SW1; i <= CS30_SW6; ++i) { - is31fl3741_write_register(index, i, g_scaling_registers[index][i]); + is31fl3741_write_register(index, i, driver_buffers[index].scaling_buffer[i]); } is31fl3741_select_page(index, IS31FL3741_COMMAND_SCALING_1); // CS1_SW7 to CS39_SW9 are on page 3 for (int i = CS1_SW7; i <= CS39_SW9; ++i) { - is31fl3741_write_register(index, i - CS1_SW7, g_scaling_registers[index][i]); + is31fl3741_write_register(index, i - CS1_SW7, driver_buffers[index].scaling_buffer[i]); } - g_scaling_registers_update_required[index] = false; + driver_buffers[index].scaling_buffer_dirty = false; } } void is31fl3741_set_scaling_registers(const is31fl3741_led_t *pled, uint8_t value) { - g_scaling_registers[pled->driver][pled->v] = value; - g_scaling_registers_update_required[pled->driver] = true; + driver_buffers[pled->driver].scaling_buffer[pled->v] = value; + driver_buffers[pled->driver].scaling_buffer_dirty = true; } void is31fl3741_flush(void) { diff --git a/drivers/led/issi/is31fl3741.c b/drivers/led/issi/is31fl3741.c index de6415a851..6a47dadd9b 100644 --- a/drivers/led/issi/is31fl3741.c +++ b/drivers/led/issi/is31fl3741.c @@ -71,11 +71,19 @@ const uint8_t i2c_addresses[IS31FL3741_DRIVER_COUNT] = { // We could optimize this and take out the unused registers from these // buffers and the transfers in is31fl3741_write_pwm_buffer() but it's // probably not worth the extra complexity. -uint8_t g_pwm_buffer[IS31FL3741_DRIVER_COUNT][IS31FL3741_PWM_REGISTER_COUNT]; -bool g_pwm_buffer_update_required[IS31FL3741_DRIVER_COUNT] = {false}; -bool g_scaling_registers_update_required[IS31FL3741_DRIVER_COUNT] = {false}; +typedef struct is31fl3741_driver_t { + uint8_t pwm_buffer[IS31FL3741_PWM_REGISTER_COUNT]; + bool pwm_buffer_dirty; + uint8_t scaling_buffer[IS31FL3741_SCALING_REGISTER_COUNT]; + bool scaling_buffer_dirty; +} PACKED is31fl3741_driver_t; -uint8_t g_scaling_registers[IS31FL3741_DRIVER_COUNT][IS31FL3741_SCALING_REGISTER_COUNT]; +is31fl3741_driver_t driver_buffers[IS31FL3741_DRIVER_COUNT] = {{ + .pwm_buffer = {0}, + .pwm_buffer_dirty = false, + .scaling_buffer = {0}, + .scaling_buffer_dirty = false, +}}; void is31fl3741_write_register(uint8_t index, uint8_t reg, uint8_t data) { #if IS31FL3741_I2C_PERSISTENCE > 0 @@ -102,20 +110,20 @@ void is31fl3741_write_pwm_buffer(uint8_t index) { #if IS31FL3741_I2C_PERSISTENCE > 0 for (uint8_t j = 0; j < IS31FL3741_I2C_PERSISTENCE; j++) { - if (i2c_write_register(i2c_addresses[index] << 1, i % 180, g_pwm_buffer[index] + i, 18, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; + if (i2c_write_register(i2c_addresses[index] << 1, i % 180, driver_buffers[index].pwm_buffer + i, 18, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; } #else - i2c_write_register(i2c_addresses[index] << 1, i % 180, g_pwm_buffer[index] + i, 18, IS31FL3741_I2C_TIMEOUT); + i2c_write_register(i2c_addresses[index] << 1, i % 180, driver_buffers[index].pwm_buffer + i, 18, IS31FL3741_I2C_TIMEOUT); #endif } // transfer the left cause the total number is 351 #if IS31FL3741_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3741_I2C_PERSISTENCE; i++) { - if (i2c_write_register(i2c_addresses[index] << 1, 162, g_pwm_buffer[index] + 342, 9, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; + if (i2c_write_register(i2c_addresses[index] << 1, 162, driver_buffers[index].pwm_buffer + 342, 9, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; } #else - i2c_write_register(i2c_addresses[index] << 1, 162, g_pwm_buffer[index] + 342, 9, IS31FL3741_I2C_TIMEOUT); + i2c_write_register(i2c_addresses[index] << 1, 162, driver_buffers[index].pwm_buffer + 342, 9, IS31FL3741_I2C_TIMEOUT); #endif } @@ -166,14 +174,14 @@ void is31fl3741_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { if (index >= 0 && index < IS31FL3741_LED_COUNT) { memcpy_P(&led, (&g_is31fl3741_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) { + if (driver_buffers[led.driver].pwm_buffer[led.r] == red && driver_buffers[led.driver].pwm_buffer[led.g] == green && driver_buffers[led.driver].pwm_buffer[led.b] == blue) { return; } - g_pwm_buffer_update_required[led.driver] = true; - g_pwm_buffer[led.driver][led.r] = red; - g_pwm_buffer[led.driver][led.g] = green; - g_pwm_buffer[led.driver][led.b] = blue; + driver_buffers[led.driver].pwm_buffer[led.r] = red; + driver_buffers[led.driver].pwm_buffer[led.g] = green; + driver_buffers[led.driver].pwm_buffer[led.b] = blue; + driver_buffers[led.driver].pwm_buffer_dirty = true; } } @@ -188,68 +196,68 @@ void is31fl3741_set_led_control_register(uint8_t index, bool red, bool green, bo memcpy_P(&led, (&g_is31fl3741_leds[index]), sizeof(led)); if (red) { - g_scaling_registers[led.driver][led.r] = 0xFF; + driver_buffers[led.driver].scaling_buffer[led.r] = 0xFF; } else { - g_scaling_registers[led.driver][led.r] = 0x00; + driver_buffers[led.driver].scaling_buffer[led.r] = 0x00; } if (green) { - g_scaling_registers[led.driver][led.g] = 0xFF; + driver_buffers[led.driver].scaling_buffer[led.g] = 0xFF; } else { - g_scaling_registers[led.driver][led.g] = 0x00; + driver_buffers[led.driver].scaling_buffer[led.g] = 0x00; } if (blue) { - g_scaling_registers[led.driver][led.b] = 0xFF; + driver_buffers[led.driver].scaling_buffer[led.b] = 0xFF; } else { - g_scaling_registers[led.driver][led.b] = 0x00; + driver_buffers[led.driver].scaling_buffer[led.b] = 0x00; } - g_scaling_registers_update_required[led.driver] = true; + driver_buffers[led.driver].scaling_buffer_dirty = true; } void is31fl3741_update_pwm_buffers(uint8_t index) { - if (g_pwm_buffer_update_required[index]) { + if (driver_buffers[index].pwm_buffer_dirty) { is31fl3741_select_page(index, IS31FL3741_COMMAND_PWM_0); is31fl3741_write_pwm_buffer(index); - g_pwm_buffer_update_required[index] = false; + driver_buffers[index].pwm_buffer_dirty = false; } } void is31fl3741_set_pwm_buffer(const is31fl3741_led_t *pled, uint8_t red, uint8_t green, uint8_t blue) { - g_pwm_buffer[pled->driver][pled->r] = red; - g_pwm_buffer[pled->driver][pled->g] = green; - g_pwm_buffer[pled->driver][pled->b] = blue; - g_pwm_buffer_update_required[pled->driver] = true; + driver_buffers[pled->driver].pwm_buffer[pled->r] = red; + driver_buffers[pled->driver].pwm_buffer[pled->g] = green; + driver_buffers[pled->driver].pwm_buffer[pled->b] = blue; + driver_buffers[pled->driver].pwm_buffer_dirty = true; } void is31fl3741_update_led_control_registers(uint8_t index) { - if (g_scaling_registers_update_required[index]) { + if (driver_buffers[index].scaling_buffer_dirty) { is31fl3741_select_page(index, IS31FL3741_COMMAND_SCALING_0); // CS1_SW1 to CS30_SW6 are on page 2 for (int i = CS1_SW1; i <= CS30_SW6; ++i) { - is31fl3741_write_register(index, i, g_scaling_registers[index][i]); + is31fl3741_write_register(index, i, driver_buffers[index].scaling_buffer[i]); } is31fl3741_select_page(index, IS31FL3741_COMMAND_SCALING_1); // CS1_SW7 to CS39_SW9 are on page 3 for (int i = CS1_SW7; i <= CS39_SW9; ++i) { - is31fl3741_write_register(index, i - CS1_SW7, g_scaling_registers[index][i]); + is31fl3741_write_register(index, i - CS1_SW7, driver_buffers[index].scaling_buffer[i]); } - g_scaling_registers_update_required[index] = false; + driver_buffers[index].scaling_buffer_dirty = false; } } void is31fl3741_set_scaling_registers(const is31fl3741_led_t *pled, uint8_t red, uint8_t green, uint8_t blue) { - g_scaling_registers[pled->driver][pled->r] = red; - g_scaling_registers[pled->driver][pled->g] = green; - g_scaling_registers[pled->driver][pled->b] = blue; - g_scaling_registers_update_required[pled->driver] = true; + driver_buffers[pled->driver].scaling_buffer[pled->r] = red; + driver_buffers[pled->driver].scaling_buffer[pled->g] = green; + driver_buffers[pled->driver].scaling_buffer[pled->b] = blue; + driver_buffers[pled->driver].scaling_buffer_dirty = true; } void is31fl3741_flush(void) { diff --git a/drivers/led/issi/is31fl3742a-mono.c b/drivers/led/issi/is31fl3742a-mono.c index 3bef22aabc..1eebbb72cb 100644 --- a/drivers/led/issi/is31fl3742a-mono.c +++ b/drivers/led/issi/is31fl3742a-mono.c @@ -66,11 +66,19 @@ const uint8_t i2c_addresses[IS31FL3742A_DRIVER_COUNT] = { #endif }; -uint8_t g_pwm_buffer[IS31FL3742A_DRIVER_COUNT][IS31FL3742A_PWM_REGISTER_COUNT]; -bool g_pwm_buffer_update_required[IS31FL3742A_DRIVER_COUNT] = {false}; -bool g_scaling_registers_update_required[IS31FL3742A_DRIVER_COUNT] = {false}; +typedef struct is31fl3742a_driver_t { + uint8_t pwm_buffer[IS31FL3742A_PWM_REGISTER_COUNT]; + bool pwm_buffer_dirty; + uint8_t scaling_buffer[IS31FL3742A_SCALING_REGISTER_COUNT]; + bool scaling_buffer_dirty; +} PACKED is31fl3742a_driver_t; -uint8_t g_scaling_registers[IS31FL3742A_DRIVER_COUNT][IS31FL3742A_SCALING_REGISTER_COUNT]; +is31fl3742a_driver_t driver_buffers[IS31FL3742A_DRIVER_COUNT] = {{ + .pwm_buffer = {0}, + .pwm_buffer_dirty = false, + .scaling_buffer = {0}, + .scaling_buffer_dirty = false, +}}; void is31fl3742a_write_register(uint8_t index, uint8_t reg, uint8_t data) { #if IS31FL3742A_I2C_PERSISTENCE > 0 @@ -95,10 +103,10 @@ void is31fl3742a_write_pwm_buffer(uint8_t index) { for (uint8_t i = 0; i < IS31FL3742A_PWM_REGISTER_COUNT; i += 30) { #if IS31FL3742A_I2C_PERSISTENCE > 0 for (uint8_t j = 0; j < IS31FL3742A_I2C_PERSISTENCE; j++) { - if (i2c_write_register(i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 30, IS31FL3742A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; + if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 30, IS31FL3742A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; } #else - i2c_write_register(i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 30, IS31FL3742A_I2C_TIMEOUT); + i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 30, IS31FL3742A_I2C_TIMEOUT); #endif } } @@ -155,12 +163,12 @@ void is31fl3742a_set_value(int index, uint8_t value) { if (index >= 0 && index < IS31FL3742A_LED_COUNT) { memcpy_P(&led, (&g_is31fl3742a_leds[index]), sizeof(led)); - if (g_pwm_buffer[led.driver][led.v] == value) { + if (driver_buffers[led.driver].pwm_buffer[led.v] == value) { return; } - g_pwm_buffer[led.driver][led.v] = value; - g_pwm_buffer_update_required[led.driver] = true; + driver_buffers[led.driver].pwm_buffer[led.v] = value; + driver_buffers[led.driver].pwm_buffer_dirty = true; } } @@ -174,29 +182,29 @@ void is31fl3742a_set_scaling_register(uint8_t index, uint8_t value) { is31fl3742a_led_t led; memcpy_P(&led, (&g_is31fl3742a_leds[index]), sizeof(led)); - g_scaling_registers[led.driver][led.v] = value; - g_scaling_registers_update_required[led.driver] = true; + driver_buffers[led.driver].scaling_buffer[led.v] = value; + driver_buffers[led.driver].scaling_buffer_dirty = true; } void is31fl3742a_update_pwm_buffers(uint8_t index) { - if (g_pwm_buffer_update_required[index]) { + if (driver_buffers[index].pwm_buffer_dirty) { is31fl3742a_select_page(index, IS31FL3742A_COMMAND_PWM); is31fl3742a_write_pwm_buffer(index); - g_pwm_buffer_update_required[index] = false; + driver_buffers[index].pwm_buffer_dirty = false; } } void is31fl3742a_update_scaling_registers(uint8_t index) { - if (g_scaling_registers_update_required[index]) { + if (driver_buffers[index].scaling_buffer_dirty) { is31fl3742a_select_page(index, IS31FL3742A_COMMAND_SCALING); for (uint8_t i = 0; i < IS31FL3742A_SCALING_REGISTER_COUNT; i++) { - is31fl3742a_write_register(index, i, g_scaling_registers[index][i]); + is31fl3742a_write_register(index, i, driver_buffers[index].scaling_buffer[i]); } - g_scaling_registers_update_required[index] = false; + driver_buffers[index].scaling_buffer_dirty = false; } } diff --git a/drivers/led/issi/is31fl3742a.c b/drivers/led/issi/is31fl3742a.c index 1fbec5fba6..4ff08cd75c 100644 --- a/drivers/led/issi/is31fl3742a.c +++ b/drivers/led/issi/is31fl3742a.c @@ -66,11 +66,19 @@ const uint8_t i2c_addresses[IS31FL3742A_DRIVER_COUNT] = { #endif }; -uint8_t g_pwm_buffer[IS31FL3742A_DRIVER_COUNT][IS31FL3742A_PWM_REGISTER_COUNT]; -bool g_pwm_buffer_update_required[IS31FL3742A_DRIVER_COUNT] = {false}; -bool g_scaling_registers_update_required[IS31FL3742A_DRIVER_COUNT] = {false}; +typedef struct is31fl3742a_driver_t { + uint8_t pwm_buffer[IS31FL3742A_PWM_REGISTER_COUNT]; + bool pwm_buffer_dirty; + uint8_t scaling_buffer[IS31FL3742A_SCALING_REGISTER_COUNT]; + bool scaling_buffer_dirty; +} PACKED is31fl3742a_driver_t; -uint8_t g_scaling_registers[IS31FL3742A_DRIVER_COUNT][IS31FL3742A_SCALING_REGISTER_COUNT]; +is31fl3742a_driver_t driver_buffers[IS31FL3742A_DRIVER_COUNT] = {{ + .pwm_buffer = {0}, + .pwm_buffer_dirty = false, + .scaling_buffer = {0}, + .scaling_buffer_dirty = false, +}}; void is31fl3742a_write_register(uint8_t index, uint8_t reg, uint8_t data) { #if IS31FL3742A_I2C_PERSISTENCE > 0 @@ -95,10 +103,10 @@ void is31fl3742a_write_pwm_buffer(uint8_t index) { for (uint8_t i = 0; i < IS31FL3742A_PWM_REGISTER_COUNT; i += 30) { #if IS31FL3742A_I2C_PERSISTENCE > 0 for (uint8_t j = 0; j < IS31FL3742A_I2C_PERSISTENCE; j++) { - if (i2c_write_register(i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 30, IS31FL3742A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; + if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 30, IS31FL3742A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; } #else - i2c_write_register(i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 30, IS31FL3742A_I2C_TIMEOUT); + i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 30, IS31FL3742A_I2C_TIMEOUT); #endif } } @@ -155,14 +163,14 @@ void is31fl3742a_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) if (index >= 0 && index < IS31FL3742A_LED_COUNT) { memcpy_P(&led, (&g_is31fl3742a_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) { + if (driver_buffers[led.driver].pwm_buffer[led.r] == red && driver_buffers[led.driver].pwm_buffer[led.g] == green && driver_buffers[led.driver].pwm_buffer[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; + driver_buffers[led.driver].pwm_buffer[led.r] = red; + driver_buffers[led.driver].pwm_buffer[led.g] = green; + driver_buffers[led.driver].pwm_buffer[led.b] = blue; + driver_buffers[led.driver].pwm_buffer_dirty = true; } } @@ -176,31 +184,31 @@ void is31fl3742a_set_scaling_register(uint8_t index, uint8_t red, uint8_t green, is31fl3742a_led_t led; memcpy_P(&led, (&g_is31fl3742a_leds[index]), sizeof(led)); - g_scaling_registers[led.driver][led.r] = red; - g_scaling_registers[led.driver][led.g] = green; - g_scaling_registers[led.driver][led.b] = blue; - g_scaling_registers_update_required[led.driver] = true; + driver_buffers[led.driver].scaling_buffer[led.r] = red; + driver_buffers[led.driver].scaling_buffer[led.g] = green; + driver_buffers[led.driver].scaling_buffer[led.b] = blue; + driver_buffers[led.driver].scaling_buffer_dirty = true; } void is31fl3742a_update_pwm_buffers(uint8_t index) { - if (g_pwm_buffer_update_required[index]) { + if (driver_buffers[index].pwm_buffer_dirty) { is31fl3742a_select_page(index, IS31FL3742A_COMMAND_PWM); is31fl3742a_write_pwm_buffer(index); - g_pwm_buffer_update_required[index] = false; + driver_buffers[index].pwm_buffer_dirty = false; } } void is31fl3742a_update_scaling_registers(uint8_t index) { - if (g_scaling_registers_update_required[index]) { + if (driver_buffers[index].scaling_buffer_dirty) { is31fl3742a_select_page(index, IS31FL3742A_COMMAND_SCALING); for (uint8_t i = 0; i < IS31FL3742A_SCALING_REGISTER_COUNT; i++) { - is31fl3742a_write_register(index, i, g_scaling_registers[index][i]); + is31fl3742a_write_register(index, i, driver_buffers[index].scaling_buffer[i]); } - g_scaling_registers_update_required[index] = false; + driver_buffers[index].scaling_buffer_dirty = false; } } diff --git a/drivers/led/issi/is31fl3743a-mono.c b/drivers/led/issi/is31fl3743a-mono.c index 49f5959ac8..3753fb605c 100644 --- a/drivers/led/issi/is31fl3743a-mono.c +++ b/drivers/led/issi/is31fl3743a-mono.c @@ -88,11 +88,19 @@ const uint8_t driver_sync[IS31FL3743A_DRIVER_COUNT] = { #endif }; -uint8_t g_pwm_buffer[IS31FL3743A_DRIVER_COUNT][IS31FL3743A_PWM_REGISTER_COUNT]; -bool g_pwm_buffer_update_required[IS31FL3743A_DRIVER_COUNT] = {false}; -bool g_scaling_registers_update_required[IS31FL3743A_DRIVER_COUNT] = {false}; +typedef struct is31fl3743a_driver_t { + uint8_t pwm_buffer[IS31FL3743A_PWM_REGISTER_COUNT]; + bool pwm_buffer_dirty; + uint8_t scaling_buffer[IS31FL3743A_SCALING_REGISTER_COUNT]; + bool scaling_buffer_dirty; +} PACKED is31fl3743a_driver_t; -uint8_t g_scaling_registers[IS31FL3743A_DRIVER_COUNT][IS31FL3743A_SCALING_REGISTER_COUNT]; +is31fl3743a_driver_t driver_buffers[IS31FL3743A_DRIVER_COUNT] = {{ + .pwm_buffer = {0}, + .pwm_buffer_dirty = false, + .scaling_buffer = {0}, + .scaling_buffer_dirty = false, +}}; void is31fl3743a_write_register(uint8_t index, uint8_t reg, uint8_t data) { #if IS31FL3743A_I2C_PERSISTENCE > 0 @@ -117,10 +125,10 @@ void is31fl3743a_write_pwm_buffer(uint8_t index) { for (uint8_t i = 0; i < IS31FL3743A_PWM_REGISTER_COUNT; i += 18) { #if IS31FL3743A_I2C_PERSISTENCE > 0 for (uint8_t j = 0; j < IS31FL3743A_I2C_PERSISTENCE; j++) { - if (i2c_write_register(i2c_addresses[index] << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3743A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; + if (i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3743A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; } #else - i2c_write_register(i2c_addresses[index] << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3743A_I2C_TIMEOUT); + i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3743A_I2C_TIMEOUT); #endif } } @@ -179,12 +187,12 @@ void is31fl3743a_set_value(int index, uint8_t value) { if (index >= 0 && index < IS31FL3743A_LED_COUNT) { memcpy_P(&led, (&g_is31fl3743a_leds[index]), sizeof(led)); - if (g_pwm_buffer[led.driver][led.v] == value) { + if (driver_buffers[led.driver].pwm_buffer[led.v] == value) { return; } - g_pwm_buffer_update_required[led.driver] = true; - g_pwm_buffer[led.driver][led.v] = value; + driver_buffers[led.driver].pwm_buffer[led.v] = value; + driver_buffers[led.driver].pwm_buffer_dirty = true; } } @@ -198,29 +206,29 @@ void is31fl3743a_set_scaling_register(uint8_t index, uint8_t value) { is31fl3743a_led_t led; memcpy_P(&led, (&g_is31fl3743a_leds[index]), sizeof(led)); - g_scaling_registers[led.driver][led.v] = value; - g_scaling_registers_update_required[led.driver] = true; + driver_buffers[led.driver].scaling_buffer[led.v] = value; + driver_buffers[led.driver].scaling_buffer_dirty = true; } void is31fl3743a_update_pwm_buffers(uint8_t index) { - if (g_pwm_buffer_update_required[index]) { + if (driver_buffers[index].pwm_buffer_dirty) { is31fl3743a_select_page(index, IS31FL3743A_COMMAND_PWM); is31fl3743a_write_pwm_buffer(index); - g_pwm_buffer_update_required[index] = false; + driver_buffers[index].pwm_buffer_dirty = false; } } void is31fl3743a_update_scaling_registers(uint8_t index) { - if (g_scaling_registers_update_required[index]) { + if (driver_buffers[index].scaling_buffer_dirty) { is31fl3743a_select_page(index, IS31FL3743A_COMMAND_SCALING); for (uint8_t i = 0; i < IS31FL3743A_SCALING_REGISTER_COUNT; i++) { - is31fl3743a_write_register(index, i + 1, g_scaling_registers[index][i]); + is31fl3743a_write_register(index, i + 1, driver_buffers[index].scaling_buffer[i]); } - g_scaling_registers_update_required[index] = false; + driver_buffers[index].scaling_buffer_dirty = false; } } diff --git a/drivers/led/issi/is31fl3743a.c b/drivers/led/issi/is31fl3743a.c index 99f74e8743..eca3dca6b2 100644 --- a/drivers/led/issi/is31fl3743a.c +++ b/drivers/led/issi/is31fl3743a.c @@ -88,11 +88,19 @@ const uint8_t driver_sync[IS31FL3743A_DRIVER_COUNT] = { #endif }; -uint8_t g_pwm_buffer[IS31FL3743A_DRIVER_COUNT][IS31FL3743A_PWM_REGISTER_COUNT]; -bool g_pwm_buffer_update_required[IS31FL3743A_DRIVER_COUNT] = {false}; -bool g_scaling_registers_update_required[IS31FL3743A_DRIVER_COUNT] = {false}; +typedef struct is31fl3743a_driver_t { + uint8_t pwm_buffer[IS31FL3743A_PWM_REGISTER_COUNT]; + bool pwm_buffer_dirty; + uint8_t scaling_buffer[IS31FL3743A_SCALING_REGISTER_COUNT]; + bool scaling_buffer_dirty; +} PACKED is31fl3743a_driver_t; -uint8_t g_scaling_registers[IS31FL3743A_DRIVER_COUNT][IS31FL3743A_SCALING_REGISTER_COUNT]; +is31fl3743a_driver_t driver_buffers[IS31FL3743A_DRIVER_COUNT] = {{ + .pwm_buffer = {0}, + .pwm_buffer_dirty = false, + .scaling_buffer = {0}, + .scaling_buffer_dirty = false, +}}; void is31fl3743a_write_register(uint8_t index, uint8_t reg, uint8_t data) { #if IS31FL3743A_I2C_PERSISTENCE > 0 @@ -117,10 +125,10 @@ void is31fl3743a_write_pwm_buffer(uint8_t index) { for (uint8_t i = 0; i < IS31FL3743A_PWM_REGISTER_COUNT; i += 18) { #if IS31FL3743A_I2C_PERSISTENCE > 0 for (uint8_t j = 0; j < IS31FL3743A_I2C_PERSISTENCE; j++) { - if (i2c_write_register(i2c_addresses[index] << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3743A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; + if (i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3743A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; } #else - i2c_write_register(i2c_addresses[index] << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3743A_I2C_TIMEOUT); + i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3743A_I2C_TIMEOUT); #endif } } @@ -179,14 +187,14 @@ void is31fl3743a_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) if (index >= 0 && index < IS31FL3743A_LED_COUNT) { memcpy_P(&led, (&g_is31fl3743a_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) { + if (driver_buffers[led.driver].pwm_buffer[led.r] == red && driver_buffers[led.driver].pwm_buffer[led.g] == green && driver_buffers[led.driver].pwm_buffer[led.b] == blue) { return; } - g_pwm_buffer_update_required[led.driver] = true; - g_pwm_buffer[led.driver][led.r] = red; - g_pwm_buffer[led.driver][led.g] = green; - g_pwm_buffer[led.driver][led.b] = blue; + driver_buffers[led.driver].pwm_buffer[led.r] = red; + driver_buffers[led.driver].pwm_buffer[led.g] = green; + driver_buffers[led.driver].pwm_buffer[led.b] = blue; + driver_buffers[led.driver].pwm_buffer_dirty = true; } } @@ -200,31 +208,31 @@ void is31fl3743a_set_scaling_register(uint8_t index, uint8_t red, uint8_t green, is31fl3743a_led_t led; memcpy_P(&led, (&g_is31fl3743a_leds[index]), sizeof(led)); - g_scaling_registers[led.driver][led.r] = red; - g_scaling_registers[led.driver][led.g] = green; - g_scaling_registers[led.driver][led.b] = blue; - g_scaling_registers_update_required[led.driver] = true; + driver_buffers[led.driver].scaling_buffer[led.r] = red; + driver_buffers[led.driver].scaling_buffer[led.g] = green; + driver_buffers[led.driver].scaling_buffer[led.b] = blue; + driver_buffers[led.driver].scaling_buffer_dirty = true; } void is31fl3743a_update_pwm_buffers(uint8_t index) { - if (g_pwm_buffer_update_required[index]) { + if (driver_buffers[index].pwm_buffer_dirty) { is31fl3743a_select_page(index, IS31FL3743A_COMMAND_PWM); is31fl3743a_write_pwm_buffer(index); - g_pwm_buffer_update_required[index] = false; + driver_buffers[index].pwm_buffer_dirty = false; } } void is31fl3743a_update_scaling_registers(uint8_t index) { - if (g_scaling_registers_update_required[index]) { + if (driver_buffers[index].scaling_buffer_dirty) { is31fl3743a_select_page(index, IS31FL3743A_COMMAND_SCALING); for (uint8_t i = 0; i < IS31FL3743A_SCALING_REGISTER_COUNT; i++) { - is31fl3743a_write_register(index, i + 1, g_scaling_registers[index][i]); + is31fl3743a_write_register(index, i + 1, driver_buffers[index].scaling_buffer[i]); } - g_scaling_registers_update_required[index] = false; + driver_buffers[index].scaling_buffer_dirty = false; } } diff --git a/drivers/led/issi/is31fl3745-mono.c b/drivers/led/issi/is31fl3745-mono.c index edb12b78bb..8e7a1c522c 100644 --- a/drivers/led/issi/is31fl3745-mono.c +++ b/drivers/led/issi/is31fl3745-mono.c @@ -88,11 +88,19 @@ const uint8_t driver_sync[IS31FL3745_DRIVER_COUNT] = { #endif }; -uint8_t g_pwm_buffer[IS31FL3745_DRIVER_COUNT][IS31FL3745_PWM_REGISTER_COUNT]; -bool g_pwm_buffer_update_required[IS31FL3745_DRIVER_COUNT] = {false}; -bool g_scaling_registers_update_required[IS31FL3745_DRIVER_COUNT] = {false}; +typedef struct is31fl3745_driver_t { + uint8_t pwm_buffer[IS31FL3745_PWM_REGISTER_COUNT]; + bool pwm_buffer_dirty; + uint8_t scaling_buffer[IS31FL3745_SCALING_REGISTER_COUNT]; + bool scaling_buffer_dirty; +} PACKED is31fl3745_driver_t; -uint8_t g_scaling_registers[IS31FL3745_DRIVER_COUNT][IS31FL3745_SCALING_REGISTER_COUNT]; +is31fl3745_driver_t driver_buffers[IS31FL3745_DRIVER_COUNT] = {{ + .pwm_buffer = {0}, + .pwm_buffer_dirty = false, + .scaling_buffer = {0}, + .scaling_buffer_dirty = false, +}}; void is31fl3745_write_register(uint8_t index, uint8_t reg, uint8_t data) { #if IS31FL3745_I2C_PERSISTENCE > 0 @@ -117,10 +125,10 @@ void is31fl3745_write_pwm_buffer(uint8_t index) { for (uint8_t i = 0; i < IS31FL3745_PWM_REGISTER_COUNT; i += 18) { #if IS31FL3745_I2C_PERSISTENCE > 0 for (uint8_t j = 0; j < IS31FL3745_I2C_PERSISTENCE; j++) { - if (i2c_write_register(i2c_addresses[index] << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3745_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; + if (i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3745_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; } #else - i2c_write_register(i2c_addresses[index] << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3745_I2C_TIMEOUT); + i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3745_I2C_TIMEOUT); #endif } } @@ -179,12 +187,12 @@ void is31fl3745_set_value(int index, uint8_t value) { if (index >= 0 && index < IS31FL3745_LED_COUNT) { memcpy_P(&led, (&g_is31fl3745_leds[index]), sizeof(led)); - if (g_pwm_buffer[led.driver][led.v] == value) { + if (driver_buffers[led.driver].pwm_buffer[led.v] == value) { return; } - g_pwm_buffer_update_required[led.driver] = true; - g_pwm_buffer[led.driver][led.v] = value; + driver_buffers[led.driver].pwm_buffer[led.v] = value; + driver_buffers[led.driver].pwm_buffer_dirty = true; } } @@ -198,29 +206,29 @@ void is31fl3745_set_scaling_register(uint8_t index, uint8_t value) { is31fl3745_led_t led; memcpy_P(&led, (&g_is31fl3745_leds[index]), sizeof(led)); - g_scaling_registers[led.driver][led.v] = value; - g_scaling_registers_update_required[led.driver] = true; + driver_buffers[led.driver].scaling_buffer[led.v] = value; + driver_buffers[led.driver].scaling_buffer_dirty = true; } void is31fl3745_update_pwm_buffers(uint8_t index) { - if (g_pwm_buffer_update_required[index]) { + if (driver_buffers[index].pwm_buffer_dirty) { is31fl3745_select_page(index, IS31FL3745_COMMAND_PWM); is31fl3745_write_pwm_buffer(index); - g_pwm_buffer_update_required[index] = false; + driver_buffers[index].pwm_buffer_dirty = false; } } void is31fl3745_update_scaling_registers(uint8_t index) { - if (g_scaling_registers_update_required[index]) { + if (driver_buffers[index].scaling_buffer_dirty) { is31fl3745_select_page(index, IS31FL3745_COMMAND_SCALING); for (uint8_t i = 0; i < IS31FL3745_SCALING_REGISTER_COUNT; i++) { - is31fl3745_write_register(index, i + 1, g_scaling_registers[index][i]); + is31fl3745_write_register(index, i + 1, driver_buffers[index].scaling_buffer[i]); } - g_scaling_registers_update_required[index] = false; + driver_buffers[index].scaling_buffer_dirty = false; } } diff --git a/drivers/led/issi/is31fl3745.c b/drivers/led/issi/is31fl3745.c index 3a67f6819a..5f06e340e0 100644 --- a/drivers/led/issi/is31fl3745.c +++ b/drivers/led/issi/is31fl3745.c @@ -88,11 +88,19 @@ const uint8_t driver_sync[IS31FL3745_DRIVER_COUNT] = { #endif }; -uint8_t g_pwm_buffer[IS31FL3745_DRIVER_COUNT][IS31FL3745_PWM_REGISTER_COUNT]; -bool g_pwm_buffer_update_required[IS31FL3745_DRIVER_COUNT] = {false}; -bool g_scaling_registers_update_required[IS31FL3745_DRIVER_COUNT] = {false}; +typedef struct is31fl3745_driver_t { + uint8_t pwm_buffer[IS31FL3745_PWM_REGISTER_COUNT]; + bool pwm_buffer_dirty; + uint8_t scaling_buffer[IS31FL3745_SCALING_REGISTER_COUNT]; + bool scaling_buffer_dirty; +} PACKED is31fl3745_driver_t; -uint8_t g_scaling_registers[IS31FL3745_DRIVER_COUNT][IS31FL3745_SCALING_REGISTER_COUNT]; +is31fl3745_driver_t driver_buffers[IS31FL3745_DRIVER_COUNT] = {{ + .pwm_buffer = {0}, + .pwm_buffer_dirty = false, + .scaling_buffer = {0}, + .scaling_buffer_dirty = false, +}}; void is31fl3745_write_register(uint8_t index, uint8_t reg, uint8_t data) { #if IS31FL3745_I2C_PERSISTENCE > 0 @@ -117,10 +125,10 @@ void is31fl3745_write_pwm_buffer(uint8_t index) { for (uint8_t i = 0; i < IS31FL3745_PWM_REGISTER_COUNT; i += 18) { #if IS31FL3745_I2C_PERSISTENCE > 0 for (uint8_t j = 0; j < IS31FL3745_I2C_PERSISTENCE; j++) { - if (i2c_write_register(i2c_addresses[index] << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3745_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; + if (i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3745_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; } #else - i2c_write_register(i2c_addresses[index] << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3745_I2C_TIMEOUT); + i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3745_I2C_TIMEOUT); #endif } } @@ -179,14 +187,14 @@ void is31fl3745_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { if (index >= 0 && index < IS31FL3745_LED_COUNT) { memcpy_P(&led, (&g_is31fl3745_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) { + if (driver_buffers[led.driver].pwm_buffer[led.r] == red && driver_buffers[led.driver].pwm_buffer[led.g] == green && driver_buffers[led.driver].pwm_buffer[led.b] == blue) { return; } - g_pwm_buffer_update_required[led.driver] = true; - g_pwm_buffer[led.driver][led.r] = red; - g_pwm_buffer[led.driver][led.g] = green; - g_pwm_buffer[led.driver][led.b] = blue; + driver_buffers[led.driver].pwm_buffer[led.r] = red; + driver_buffers[led.driver].pwm_buffer[led.g] = green; + driver_buffers[led.driver].pwm_buffer[led.b] = blue; + driver_buffers[led.driver].pwm_buffer_dirty = true; } } @@ -200,31 +208,31 @@ void is31fl3745_set_scaling_register(uint8_t index, uint8_t red, uint8_t green, is31fl3745_led_t led; memcpy_P(&led, (&g_is31fl3745_leds[index]), sizeof(led)); - g_scaling_registers[led.driver][led.r] = red; - g_scaling_registers[led.driver][led.g] = green; - g_scaling_registers[led.driver][led.b] = blue; - g_scaling_registers_update_required[led.driver] = true; + driver_buffers[led.driver].scaling_buffer[led.r] = red; + driver_buffers[led.driver].scaling_buffer[led.g] = green; + driver_buffers[led.driver].scaling_buffer[led.b] = blue; + driver_buffers[led.driver].scaling_buffer_dirty = true; } void is31fl3745_update_pwm_buffers(uint8_t index) { - if (g_pwm_buffer_update_required[index]) { + if (driver_buffers[index].pwm_buffer_dirty) { is31fl3745_select_page(index, IS31FL3745_COMMAND_PWM); is31fl3745_write_pwm_buffer(index); - g_pwm_buffer_update_required[index] = false; + driver_buffers[index].pwm_buffer_dirty = false; } } void is31fl3745_update_scaling_registers(uint8_t index) { - if (g_scaling_registers_update_required[index]) { + if (driver_buffers[index].scaling_buffer_dirty) { is31fl3745_select_page(index, IS31FL3745_COMMAND_SCALING); for (uint8_t i = 0; i < IS31FL3745_SCALING_REGISTER_COUNT; i++) { - is31fl3745_write_register(index, i + 1, g_scaling_registers[index][i]); + is31fl3745_write_register(index, i + 1, driver_buffers[index].scaling_buffer[i]); } - g_scaling_registers_update_required[index] = false; + driver_buffers[index].scaling_buffer_dirty = false; } } diff --git a/drivers/led/issi/is31fl3746a-mono.c b/drivers/led/issi/is31fl3746a-mono.c index 900482e75c..0c5b72ba2a 100644 --- a/drivers/led/issi/is31fl3746a-mono.c +++ b/drivers/led/issi/is31fl3746a-mono.c @@ -66,11 +66,19 @@ const uint8_t i2c_addresses[IS31FL3746A_DRIVER_COUNT] = { #endif }; -uint8_t g_pwm_buffer[IS31FL3746A_DRIVER_COUNT][IS31FL3746A_PWM_REGISTER_COUNT]; -bool g_pwm_buffer_update_required[IS31FL3746A_DRIVER_COUNT] = {false}; -bool g_scaling_registers_update_required[IS31FL3746A_DRIVER_COUNT] = {false}; +typedef struct is31fl3746a_driver_t { + uint8_t pwm_buffer[IS31FL3746A_PWM_REGISTER_COUNT]; + bool pwm_buffer_dirty; + uint8_t scaling_buffer[IS31FL3746A_SCALING_REGISTER_COUNT]; + bool scaling_buffer_dirty; +} PACKED is31fl3746a_driver_t; -uint8_t g_scaling_registers[IS31FL3746A_DRIVER_COUNT][IS31FL3746A_SCALING_REGISTER_COUNT]; +is31fl3746a_driver_t driver_buffers[IS31FL3746A_DRIVER_COUNT] = {{ + .pwm_buffer = {0}, + .pwm_buffer_dirty = false, + .scaling_buffer = {0}, + .scaling_buffer_dirty = false, +}}; void is31fl3746a_write_register(uint8_t index, uint8_t reg, uint8_t data) { #if IS31FL3746A_I2C_PERSISTENCE > 0 @@ -95,10 +103,10 @@ void is31fl3746a_write_pwm_buffer(uint8_t index) { for (uint8_t i = 0; i < IS31FL3746A_PWM_REGISTER_COUNT; i += 18) { #if IS31FL3746A_I2C_PERSISTENCE > 0 for (uint8_t j = 0; j < IS31FL3746A_I2C_PERSISTENCE; j++) { - if (i2c_write_register(i2c_addresses[index] << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3746A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; + if (i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3746A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; } #else - i2c_write_register(i2c_addresses[index] << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3746A_I2C_TIMEOUT); + i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3746A_I2C_TIMEOUT); #endif } } @@ -156,12 +164,12 @@ void is31fl3746a_set_value(int index, uint8_t value) { if (index >= 0 && index < IS31FL3746A_LED_COUNT) { memcpy_P(&led, (&g_is31fl3746a_leds[index]), sizeof(led)); - if (g_pwm_buffer[led.driver][led.v] == value) { + if (driver_buffers[led.driver].pwm_buffer[led.v] == value) { return; } - g_pwm_buffer_update_required[led.driver] = true; - g_pwm_buffer[led.driver][led.v] = value; + driver_buffers[led.driver].pwm_buffer[led.v] = value; + driver_buffers[led.driver].pwm_buffer_dirty = true; } } @@ -175,29 +183,29 @@ void is31fl3746a_set_scaling_register(uint8_t index, uint8_t value) { is31fl3746a_led_t led; memcpy_P(&led, (&g_is31fl3746a_leds[index]), sizeof(led)); - g_scaling_registers[led.driver][led.v] = value; - g_scaling_registers_update_required[led.driver] = true; + driver_buffers[led.driver].scaling_buffer[led.v] = value; + driver_buffers[led.driver].scaling_buffer_dirty = true; } void is31fl3746a_update_pwm_buffers(uint8_t index) { - if (g_pwm_buffer_update_required[index]) { + if (driver_buffers[index].pwm_buffer_dirty) { is31fl3746a_select_page(index, IS31FL3746A_COMMAND_PWM); is31fl3746a_write_pwm_buffer(index); - g_pwm_buffer_update_required[index] = false; + driver_buffers[index].pwm_buffer_dirty = false; } } void is31fl3746a_update_scaling_registers(uint8_t index) { - if (g_scaling_registers_update_required[index]) { + if (driver_buffers[index].scaling_buffer_dirty) { is31fl3746a_select_page(index, IS31FL3746A_COMMAND_SCALING); for (uint8_t i = 0; i < IS31FL3746A_SCALING_REGISTER_COUNT; i++) { - is31fl3746a_write_register(index, i + 1, g_scaling_registers[index][i]); + is31fl3746a_write_register(index, i + 1, driver_buffers[index].scaling_buffer[i]); } - g_scaling_registers_update_required[index] = false; + driver_buffers[index].scaling_buffer_dirty = false; } } diff --git a/drivers/led/issi/is31fl3746a.c b/drivers/led/issi/is31fl3746a.c index b4de9add6f..5cdb560594 100644 --- a/drivers/led/issi/is31fl3746a.c +++ b/drivers/led/issi/is31fl3746a.c @@ -66,11 +66,19 @@ const uint8_t i2c_addresses[IS31FL3746A_DRIVER_COUNT] = { #endif }; -uint8_t g_pwm_buffer[IS31FL3746A_DRIVER_COUNT][IS31FL3746A_PWM_REGISTER_COUNT]; -bool g_pwm_buffer_update_required[IS31FL3746A_DRIVER_COUNT] = {false}; -bool g_scaling_registers_update_required[IS31FL3746A_DRIVER_COUNT] = {false}; +typedef struct is31fl3746a_driver_t { + uint8_t pwm_buffer[IS31FL3746A_PWM_REGISTER_COUNT]; + bool pwm_buffer_dirty; + uint8_t scaling_buffer[IS31FL3746A_SCALING_REGISTER_COUNT]; + bool scaling_buffer_dirty; +} PACKED is31fl3746a_driver_t; -uint8_t g_scaling_registers[IS31FL3746A_DRIVER_COUNT][IS31FL3746A_SCALING_REGISTER_COUNT]; +is31fl3746a_driver_t driver_buffers[IS31FL3746A_DRIVER_COUNT] = {{ + .pwm_buffer = {0}, + .pwm_buffer_dirty = false, + .scaling_buffer = {0}, + .scaling_buffer_dirty = false, +}}; void is31fl3746a_write_register(uint8_t index, uint8_t reg, uint8_t data) { #if IS31FL3746A_I2C_PERSISTENCE > 0 @@ -95,10 +103,10 @@ void is31fl3746a_write_pwm_buffer(uint8_t index) { for (uint8_t i = 0; i < IS31FL3746A_PWM_REGISTER_COUNT; i += 18) { #if IS31FL3746A_I2C_PERSISTENCE > 0 for (uint8_t j = 0; j < IS31FL3746A_I2C_PERSISTENCE; j++) { - if (i2c_write_register(i2c_addresses[index] << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3746A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; + if (i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3746A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; } #else - i2c_write_register(i2c_addresses[index] << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3746A_I2C_TIMEOUT); + i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3746A_I2C_TIMEOUT); #endif } } @@ -156,14 +164,14 @@ void is31fl3746a_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) if (index >= 0 && index < IS31FL3746A_LED_COUNT) { memcpy_P(&led, (&g_is31fl3746a_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) { + if (driver_buffers[led.driver].pwm_buffer[led.r] == red && driver_buffers[led.driver].pwm_buffer[led.g] == green && driver_buffers[led.driver].pwm_buffer[led.b] == blue) { return; } - g_pwm_buffer_update_required[led.driver] = true; - g_pwm_buffer[led.driver][led.r] = red; - g_pwm_buffer[led.driver][led.g] = green; - g_pwm_buffer[led.driver][led.b] = blue; + driver_buffers[led.driver].pwm_buffer[led.r] = red; + driver_buffers[led.driver].pwm_buffer[led.g] = green; + driver_buffers[led.driver].pwm_buffer[led.b] = blue; + driver_buffers[led.driver].pwm_buffer_dirty = true; } } @@ -177,31 +185,31 @@ void is31fl3746a_set_scaling_register(uint8_t index, uint8_t red, uint8_t green, is31fl3746a_led_t led; memcpy_P(&led, (&g_is31fl3746a_leds[index]), sizeof(led)); - g_scaling_registers[led.driver][led.r] = red; - g_scaling_registers[led.driver][led.g] = green; - g_scaling_registers[led.driver][led.b] = blue; - g_scaling_registers_update_required[led.driver] = true; + driver_buffers[led.driver].scaling_buffer[led.r] = red; + driver_buffers[led.driver].scaling_buffer[led.g] = green; + driver_buffers[led.driver].scaling_buffer[led.b] = blue; + driver_buffers[led.driver].scaling_buffer_dirty = true; } void is31fl3746a_update_pwm_buffers(uint8_t index) { - if (g_pwm_buffer_update_required[index]) { + if (driver_buffers[index].pwm_buffer_dirty) { is31fl3746a_select_page(index, IS31FL3746A_COMMAND_PWM); is31fl3746a_write_pwm_buffer(index); - g_pwm_buffer_update_required[index] = false; + driver_buffers[index].pwm_buffer_dirty = false; } } void is31fl3746a_update_scaling_registers(uint8_t index) { - if (g_scaling_registers_update_required[index]) { + if (driver_buffers[index].scaling_buffer_dirty) { is31fl3746a_select_page(index, IS31FL3746A_COMMAND_SCALING); for (uint8_t i = 0; i < IS31FL3746A_SCALING_REGISTER_COUNT; i++) { - is31fl3746a_write_register(index, i + 1, g_scaling_registers[index][i]); + is31fl3746a_write_register(index, i + 1, driver_buffers[index].scaling_buffer[i]); } - g_scaling_registers_update_required[index] = false; + driver_buffers[index].scaling_buffer_dirty = false; } } diff --git a/drivers/led/snled27351-mono.c b/drivers/led/snled27351-mono.c index 8893e83537..0c047fa771 100644 --- a/drivers/led/snled27351-mono.c +++ b/drivers/led/snled27351-mono.c @@ -56,11 +56,19 @@ const uint8_t i2c_addresses[SNLED27351_DRIVER_COUNT] = { // We could optimize this and take out the unused registers from these // buffers and the transfers in snled27351_write_pwm_buffer() but it's // probably not worth the extra complexity. -uint8_t g_pwm_buffer[SNLED27351_DRIVER_COUNT][SNLED27351_PWM_REGISTER_COUNT]; -bool g_pwm_buffer_update_required[SNLED27351_DRIVER_COUNT] = {false}; +typedef struct snled27351_driver_t { + uint8_t pwm_buffer[SNLED27351_PWM_REGISTER_COUNT]; + bool pwm_buffer_dirty; + uint8_t led_control_buffer[SNLED27351_LED_CONTROL_REGISTER_COUNT]; + bool led_control_buffer_dirty; +} PACKED snled27351_driver_t; -uint8_t g_led_control_registers[SNLED27351_DRIVER_COUNT][SNLED27351_LED_CONTROL_REGISTER_COUNT] = {0}; -bool g_led_control_registers_update_required[SNLED27351_DRIVER_COUNT] = {false}; +snled27351_driver_t driver_buffers[SNLED27351_DRIVER_COUNT] = {{ + .pwm_buffer = {0}, + .pwm_buffer_dirty = false, + .led_control_buffer = {0}, + .led_control_buffer_dirty = false, +}}; void snled27351_write_register(uint8_t index, uint8_t reg, uint8_t data) { #if SNLED27351_I2C_PERSISTENCE > 0 @@ -84,10 +92,10 @@ void snled27351_write_pwm_buffer(uint8_t index) { for (uint8_t i = 0; i < SNLED27351_PWM_REGISTER_COUNT; i += 16) { #if SNLED27351_I2C_PERSISTENCE > 0 for (uint8_t j = 0; j < SNLED27351_I2C_PERSISTENCE; j++) { - if (i2c_write_register(i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 16, SNLED27351_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; + if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, SNLED27351_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; } #else - i2c_write_register(i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 16, SNLED27351_I2C_TIMEOUT); + i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, SNLED27351_I2C_TIMEOUT); #endif } } @@ -160,12 +168,12 @@ void snled27351_set_value(int index, uint8_t value) { if (index >= 0 && index < SNLED27351_LED_COUNT) { memcpy_P(&led, (&g_snled27351_leds[index]), sizeof(led)); - if (g_pwm_buffer[led.driver][led.v] == value) { + if (driver_buffers[led.driver].pwm_buffer[led.v] == value) { return; } - g_pwm_buffer[led.driver][led.v] = value; - g_pwm_buffer_update_required[led.driver] = true; + driver_buffers[led.driver].pwm_buffer[led.v] = value; + driver_buffers[led.driver].pwm_buffer_dirty = true; } } @@ -183,33 +191,33 @@ void snled27351_set_led_control_register(uint8_t index, bool value) { uint8_t bit_value = led.v % 8; if (value) { - g_led_control_registers[led.driver][control_register] |= (1 << bit_value); + driver_buffers[led.driver].led_control_buffer[control_register] |= (1 << bit_value); } else { - g_led_control_registers[led.driver][control_register] &= ~(1 << bit_value); + driver_buffers[led.driver].led_control_buffer[control_register] &= ~(1 << bit_value); } - g_led_control_registers_update_required[led.driver] = true; + driver_buffers[led.driver].led_control_buffer_dirty = true; } void snled27351_update_pwm_buffers(uint8_t index) { - if (g_pwm_buffer_update_required[index]) { + if (driver_buffers[index].pwm_buffer_dirty) { snled27351_select_page(index, SNLED27351_COMMAND_PWM); snled27351_write_pwm_buffer(index); - g_pwm_buffer_update_required[index] = false; + driver_buffers[index].pwm_buffer_dirty = false; } } void snled27351_update_led_control_registers(uint8_t index) { - if (g_led_control_registers_update_required[index]) { + if (driver_buffers[index].led_control_buffer_dirty) { snled27351_select_page(index, SNLED27351_COMMAND_LED_CONTROL); for (uint8_t i = 0; i < SNLED27351_LED_CONTROL_REGISTER_COUNT; i++) { - snled27351_write_register(index, i, g_led_control_registers[index][i]); + snled27351_write_register(index, i, driver_buffers[index].led_control_buffer[i]); } - g_led_control_registers_update_required[index] = false; + driver_buffers[index].led_control_buffer_dirty = false; } } diff --git a/drivers/led/snled27351.c b/drivers/led/snled27351.c index 31b69de388..14fb95e165 100644 --- a/drivers/led/snled27351.c +++ b/drivers/led/snled27351.c @@ -56,11 +56,19 @@ const uint8_t i2c_addresses[SNLED27351_DRIVER_COUNT] = { // We could optimize this and take out the unused registers from these // buffers and the transfers in snled27351_write_pwm_buffer() but it's // probably not worth the extra complexity. -uint8_t g_pwm_buffer[SNLED27351_DRIVER_COUNT][SNLED27351_PWM_REGISTER_COUNT]; -bool g_pwm_buffer_update_required[SNLED27351_DRIVER_COUNT] = {false}; +typedef struct snled27351_driver_t { + uint8_t pwm_buffer[SNLED27351_PWM_REGISTER_COUNT]; + bool pwm_buffer_dirty; + uint8_t led_control_buffer[SNLED27351_LED_CONTROL_REGISTER_COUNT]; + bool led_control_buffer_dirty; +} PACKED snled27351_driver_t; -uint8_t g_led_control_registers[SNLED27351_DRIVER_COUNT][SNLED27351_LED_CONTROL_REGISTER_COUNT] = {0}; -bool g_led_control_registers_update_required[SNLED27351_DRIVER_COUNT] = {false}; +snled27351_driver_t driver_buffers[SNLED27351_DRIVER_COUNT] = {{ + .pwm_buffer = {0}, + .pwm_buffer_dirty = false, + .led_control_buffer = {0}, + .led_control_buffer_dirty = false, +}}; void snled27351_write_register(uint8_t index, uint8_t reg, uint8_t data) { #if SNLED27351_I2C_PERSISTENCE > 0 @@ -84,10 +92,10 @@ void snled27351_write_pwm_buffer(uint8_t index) { for (uint8_t i = 0; i < SNLED27351_PWM_REGISTER_COUNT; i += 16) { #if SNLED27351_I2C_PERSISTENCE > 0 for (uint8_t j = 0; j < SNLED27351_I2C_PERSISTENCE; j++) { - if (i2c_write_register(i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 16, SNLED27351_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; + if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, SNLED27351_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; } #else - i2c_write_register(i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 16, SNLED27351_I2C_TIMEOUT); + i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, SNLED27351_I2C_TIMEOUT); #endif } } @@ -160,14 +168,14 @@ void snled27351_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { if (index >= 0 && index < SNLED27351_LED_COUNT) { memcpy_P(&led, (&g_snled27351_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) { + if (driver_buffers[led.driver].pwm_buffer[led.r] == red && driver_buffers[led.driver].pwm_buffer[led.g] == green && driver_buffers[led.driver].pwm_buffer[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; + driver_buffers[led.driver].pwm_buffer[led.r] = red; + driver_buffers[led.driver].pwm_buffer[led.g] = green; + driver_buffers[led.driver].pwm_buffer[led.b] = blue; + driver_buffers[led.driver].pwm_buffer_dirty = true; } } @@ -189,43 +197,43 @@ void snled27351_set_led_control_register(uint8_t index, bool red, bool green, bo uint8_t bit_b = led.b % 8; if (red) { - g_led_control_registers[led.driver][control_register_r] |= (1 << bit_r); + driver_buffers[led.driver].led_control_buffer[control_register_r] |= (1 << bit_r); } else { - g_led_control_registers[led.driver][control_register_r] &= ~(1 << bit_r); + driver_buffers[led.driver].led_control_buffer[control_register_r] &= ~(1 << bit_r); } if (green) { - g_led_control_registers[led.driver][control_register_g] |= (1 << bit_g); + driver_buffers[led.driver].led_control_buffer[control_register_g] |= (1 << bit_g); } else { - g_led_control_registers[led.driver][control_register_g] &= ~(1 << bit_g); + driver_buffers[led.driver].led_control_buffer[control_register_g] &= ~(1 << bit_g); } if (blue) { - g_led_control_registers[led.driver][control_register_b] |= (1 << bit_b); + driver_buffers[led.driver].led_control_buffer[control_register_b] |= (1 << bit_b); } else { - g_led_control_registers[led.driver][control_register_b] &= ~(1 << bit_b); + driver_buffers[led.driver].led_control_buffer[control_register_b] &= ~(1 << bit_b); } - g_led_control_registers_update_required[led.driver] = true; + driver_buffers[led.driver].led_control_buffer_dirty = true; } void snled27351_update_pwm_buffers(uint8_t index) { - if (g_pwm_buffer_update_required[index]) { + if (driver_buffers[index].pwm_buffer_dirty) { snled27351_select_page(index, SNLED27351_COMMAND_PWM); snled27351_write_pwm_buffer(index); - g_pwm_buffer_update_required[index] = false; + driver_buffers[index].pwm_buffer_dirty = false; } } void snled27351_update_led_control_registers(uint8_t index) { - if (g_led_control_registers_update_required[index]) { + if (driver_buffers[index].led_control_buffer_dirty) { snled27351_select_page(index, SNLED27351_COMMAND_LED_CONTROL); for (uint8_t i = 0; i < SNLED27351_LED_CONTROL_REGISTER_COUNT; i++) { - snled27351_write_register(index, i, g_led_control_registers[index][i]); + snled27351_write_register(index, i, driver_buffers[index].led_control_buffer[i]); } - g_led_control_registers_update_required[index] = false; + driver_buffers[index].led_control_buffer_dirty = false; } } diff --git a/keyboards/input_club/k_type/is31fl3733-dual.c b/keyboards/input_club/k_type/is31fl3733-dual.c index 60b251cc5a..f8f4398332 100644 --- a/keyboards/input_club/k_type/is31fl3733-dual.c +++ b/keyboards/input_club/k_type/is31fl3733-dual.c @@ -80,11 +80,19 @@ const uint8_t driver_sync[IS31FL3733_DRIVER_COUNT] = { // We could optimize this and take out the unused registers from these // buffers and the transfers in is31fl3733_write_pwm_buffer() but it's // probably not worth the extra complexity. -uint8_t g_pwm_buffer[IS31FL3733_DRIVER_COUNT][IS31FL3733_PWM_REGISTER_COUNT]; -bool g_pwm_buffer_update_required[IS31FL3733_DRIVER_COUNT] = {false}; +typedef struct is31fl3733_driver_t { + uint8_t pwm_buffer[IS31FL3733_PWM_REGISTER_COUNT]; + bool pwm_buffer_dirty; + uint8_t led_control_buffer[IS31FL3733_LED_CONTROL_REGISTER_COUNT]; + bool led_control_buffer_dirty; +} PACKED is31fl3733_driver_t; -uint8_t g_led_control_registers[IS31FL3733_DRIVER_COUNT][IS31FL3733_LED_CONTROL_REGISTER_COUNT] = {0}; -bool g_led_control_registers_update_required[IS31FL3733_DRIVER_COUNT] = {false}; +is31fl3733_driver_t driver_buffers[IS31FL3733_DRIVER_COUNT] = {{ + .pwm_buffer = {0}, + .pwm_buffer_dirty = false, + .led_control_buffer = {0}, + .led_control_buffer_dirty = false, +}}; void is31fl3733_write_register(uint8_t bus, uint8_t index, uint8_t reg, uint8_t data) { #if IS31FL3733_I2C_PERSISTENCE > 0 @@ -109,10 +117,10 @@ void is31fl3733_write_pwm_buffer(uint8_t bus, uint8_t index) { for (uint8_t i = 0; i < IS31FL3733_PWM_REGISTER_COUNT; i += 16) { #if IS31FL3733_I2C_PERSISTENCE > 0 for (uint8_t j = 0; j < IS31FL3733_I2C_PERSISTENCE; j++) { - if (i2c_write_register(bus, i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3733_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; + if (i2c_write_register(bus, i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3733_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break; } #else - i2c_write_register(bus, i2c_addresses[index] << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3733_I2C_TIMEOUT); + i2c_write_register(bus, i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3733_I2C_TIMEOUT); #endif } } @@ -180,14 +188,14 @@ void is31fl3733_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { if (index >= 0 && index < IS31FL3733_LED_COUNT) { memcpy_P(&led, (&g_is31fl3733_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) { + if (driver_buffers[led.driver].pwm_buffer[led.r] == red && driver_buffers[led.driver].pwm_buffer[led.g] == green && driver_buffers[led.driver].pwm_buffer[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; + driver_buffers[led.driver].pwm_buffer[led.r] = red; + driver_buffers[led.driver].pwm_buffer[led.g] = green; + driver_buffers[led.driver].pwm_buffer[led.b] = blue; + driver_buffers[led.driver].pwm_buffer_dirty = true; } } @@ -209,43 +217,43 @@ void is31fl3733_set_led_control_register(uint8_t index, bool red, bool green, bo uint8_t bit_b = led.b % 8; if (red) { - g_led_control_registers[led.driver][control_register_r] |= (1 << bit_r); + driver_buffers[led.driver].led_control_buffer[control_register_r] |= (1 << bit_r); } else { - g_led_control_registers[led.driver][control_register_r] &= ~(1 << bit_r); + driver_buffers[led.driver].led_control_buffer[control_register_r] &= ~(1 << bit_r); } if (green) { - g_led_control_registers[led.driver][control_register_g] |= (1 << bit_g); + driver_buffers[led.driver].led_control_buffer[control_register_g] |= (1 << bit_g); } else { - g_led_control_registers[led.driver][control_register_g] &= ~(1 << bit_g); + driver_buffers[led.driver].led_control_buffer[control_register_g] &= ~(1 << bit_g); } if (blue) { - g_led_control_registers[led.driver][control_register_b] |= (1 << bit_b); + driver_buffers[led.driver].led_control_buffer[control_register_b] |= (1 << bit_b); } else { - g_led_control_registers[led.driver][control_register_b] &= ~(1 << bit_b); + driver_buffers[led.driver].led_control_buffer[control_register_b] &= ~(1 << bit_b); } - g_led_control_registers_update_required[led.driver] = true; + driver_buffers[led.driver].led_control_buffer_dirty = true; } void is31fl3733_update_pwm_buffers(uint8_t bus, uint8_t index) { - if (g_pwm_buffer_update_required[index]) { + if (driver_buffers[index].pwm_buffer_dirty) { is31fl3733_select_page(bus, index, IS31FL3733_COMMAND_PWM); is31fl3733_write_pwm_buffer(bus, index); - g_pwm_buffer_update_required[index] = false; + driver_buffers[index].pwm_buffer_dirty = false; } } void is31fl3733_update_led_control_registers(uint8_t bus, uint8_t index) { - if (g_led_control_registers_update_required[index]) { + if (driver_buffers[index].led_control_buffer_dirty) { is31fl3733_select_page(bus, index, IS31FL3733_COMMAND_LED_CONTROL); for (int i = 0; i < IS31FL3733_LED_CONTROL_REGISTER_COUNT; i++) { - is31fl3733_write_register(bus, index, i, g_led_control_registers[index][i]); + is31fl3733_write_register(bus, index, i, driver_buffers[index].led_control_buffer[i]); } - g_led_control_registers_update_required[index] = false; + driver_buffers[index].led_control_buffer_dirty = false; } }