From 66015f164507022e376a15b45f41b58c0fb5ee97 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 10 Feb 2024 18:48:13 +1100 Subject: LED drivers: create structs to hold PWM/scaling buffers (#22955) --- drivers/led/issi/is31fl3736.c | 56 ++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 24 deletions(-) (limited to 'drivers/led/issi/is31fl3736.c') 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}; - -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}; +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; + +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; } } -- cgit v1.2.3