From 88882ab417ded08331d6a814ad039a0145046d2f Mon Sep 17 00:00:00 2001 From: Kuba Birecki Date: Sat, 4 Dec 2021 21:13:53 +0100 Subject: [PATCH] Add gamma settings --- app/drivers/led_strip/is31fl3741/is31fl3741.c | 41 +++++++++++-------- .../led_strip/is31fl3741/issi,is31fl3741.yaml | 6 --- .../bindings/led_strip/issi,is31fl3741.yaml | 33 +++++++++++++++ app/include/dt-bindings/zmk/issi_transform.h | 9 ++-- 4 files changed, 61 insertions(+), 28 deletions(-) delete mode 100644 app/drivers/led_strip/is31fl3741/issi,is31fl3741.yaml diff --git a/app/drivers/led_strip/is31fl3741/is31fl3741.c b/app/drivers/led_strip/is31fl3741/is31fl3741.c index 23b2afb6..81b34d84 100644 --- a/app/drivers/led_strip/is31fl3741/is31fl3741.c +++ b/app/drivers/led_strip/is31fl3741/is31fl3741.c @@ -44,6 +44,7 @@ struct is31fl3741_config { uint8_t gcc; uint8_t sws; uint16_t *rgb_map; + uint8_t (*gamma)[256]; }; struct is31fl3741_data { @@ -65,7 +66,7 @@ static int is31fl3741_reg_write(const struct device *dev, uint8_t addr, uint8_t } static int is31fl3741_reg_burst_write(const struct device *dev, uint8_t start_addr, - const uint8_t *buffer, size_t num_bytes) { + const uint8_t *buffer, size_t num_bytes) { const struct is31fl3741_data *data = dev->data; const struct is31fl3741_config *config = dev->config; @@ -101,7 +102,7 @@ static inline bool num_pixels_ok(const struct is31fl3741_config *config, size_t * Updates individual LED channels without an RGB interpretation. */ static int is31fl3741_strip_update_channels(const struct device *dev, uint8_t *channels, - size_t num_channels) { + size_t num_channels) { const struct is31fl3741_config *config = dev->config; if (config->px_buffer_size < num_channels) { @@ -112,11 +113,10 @@ static int is31fl3741_strip_update_channels(const struct device *dev, uint8_t *c int result; - result = is31fl3741_reg_burst_write( - dev, - 0x00, - channels, - (num_channels <= IS31FL3741_BUFFER_PAGE_BREAK) ? num_channels : IS31FL3741_BUFFER_PAGE_BREAK); + result = is31fl3741_reg_burst_write(dev, 0x00, channels, + (num_channels <= IS31FL3741_BUFFER_PAGE_BREAK) + ? num_channels + : IS31FL3741_BUFFER_PAGE_BREAK); if (result || num_channels <= IS31FL3741_BUFFER_PAGE_BREAK) { return result; @@ -131,7 +131,7 @@ static int is31fl3741_strip_update_channels(const struct device *dev, uint8_t *c * Updates the RGB LED matrix according to devicetree's map property. */ static int is31fl3741_strip_update_rgb(const struct device *dev, struct led_rgb *pixels, - size_t num_pixels) { + size_t num_pixels) { const struct is31fl3741_config *config = dev->config; uint8_t *px_buffer = config->px_buffer; @@ -145,9 +145,9 @@ static int is31fl3741_strip_update_rgb(const struct device *dev, struct led_rgb } while (i < num_pixels) { - px_buffer[rgb_map[j++]] = pixels[i].r; - px_buffer[rgb_map[j++]] = pixels[i].g; - px_buffer[rgb_map[j++]] = pixels[i].b; + px_buffer[rgb_map[j++]] = config->gamma[0][pixels[i].r]; + px_buffer[rgb_map[j++]] = config->gamma[1][pixels[i].g]; + px_buffer[rgb_map[j++]] = config->gamma[2][pixels[i].b]; ++i; } @@ -204,9 +204,9 @@ int static is31fl3741_init(const struct device *dev) { } // Configure LED driver operation mode - is31fl3741_reg_write( - dev, 0x00, (config->sws << 4) | (0x01 << 3) | 0x01); // SWS, H logic, Normal operation - is31fl3741_reg_write(dev, 0x01, config->gcc); // Set GCC + is31fl3741_reg_write(dev, 0x00, + (config->sws << 4) | (0x01 << 3) | 0x01); // SWS, H logic, Normal operation + is31fl3741_reg_write(dev, 0x01, config->gcc); // Set GCC // Set all scaling registers to 0xff, brightness is controlled using PWM uint8_t scaling_buffer[0xb4]; @@ -245,6 +245,12 @@ static const struct led_strip_driver_api is31fl3741_api = { \ static uint16_t is31fl3741_##idx##_rgb_map[IS31FL3741_BUFFER_SIZE] = DT_INST_PROP(idx, map); \ \ + static uint8_t is31fl3741_##idx##_gamma[3][256] = { \ + DT_INST_PROP(idx, red_gamma), \ + DT_INST_PROP(idx, green_gamma), \ + DT_INST_PROP(idx, blue_gamma), \ + }; \ + \ static const struct is31fl3741_config is31fl3741_##idx##_config = { \ .bus = DT_INST_BUS_LABEL(idx), \ .reg = DT_INST_REG_ADDR(idx), \ @@ -257,10 +263,11 @@ static const struct led_strip_driver_api is31fl3741_api = { .gcc = IS31FL3741_GCC(idx), \ .sws = DT_INST_PROP(idx, sw_setting), \ .rgb_map = is31fl3741_##idx##_rgb_map, \ + .gamma = is31fl3741_##idx##_gamma, \ }; \ \ - DEVICE_AND_API_INIT(is31fl3741_##idx, DT_INST_LABEL(idx), &is31fl3741_init, \ - &is31fl3741_##idx##_data, &is31fl3741_##idx##_config, POST_KERNEL, \ - CONFIG_LED_STRIP_INIT_PRIORITY, &is31fl3741_api); + DEVICE_DT_INST_DEFINE(idx, &is31fl3741_init, NULL, &is31fl3741_##idx##_data, \ + &is31fl3741_##idx##_config, POST_KERNEL, CONFIG_LED_STRIP_INIT_PRIORITY, \ + &is31fl3741_api); DT_INST_FOREACH_STATUS_OKAY(IS31FL3741_DEVICE); diff --git a/app/drivers/led_strip/is31fl3741/issi,is31fl3741.yaml b/app/drivers/led_strip/is31fl3741/issi,is31fl3741.yaml deleted file mode 100644 index 53d27936..00000000 --- a/app/drivers/led_strip/is31fl3741/issi,is31fl3741.yaml +++ /dev/null @@ -1,6 +0,0 @@ -description: | - Driver for the IS31FL3741 LED matrix driver - -compatible: "issi,issi_is31fl3741" - -include: i2c-device.yaml diff --git a/app/dts/bindings/led_strip/issi,is31fl3741.yaml b/app/dts/bindings/led_strip/issi,is31fl3741.yaml index 9b321ee8..31f8a552 100644 --- a/app/dts/bindings/led_strip/issi,is31fl3741.yaml +++ b/app/dts/bindings/led_strip/issi,is31fl3741.yaml @@ -50,3 +50,36 @@ properties: chain-length: type: int + required: true + description: | + How many RGB LEDs are driven by the IC. + + red-gamma: + type: array + required: false + default: [ 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 10, 10, 10, 10, 12, 12, 12, 12, 14, 14, 14, 14, 16, 16, 16, 16, 18, 18, 18, 18, 20, 20, 20, 20, 22, 22, 22, 22, 24, 24, 24, 24, 26, 26, 26, 26, 29, 29, 29, 29, 32, 32, 32, 32, 35, 35, 35, 35, 38, 38, 38, 38, 41, 41, 41, 41, 44, 44, 44, 44, 47, 47, 47, 47, 50, 50, 50, 50, 53, 53, 53, 53, 57, 57, 57, 57, 61, 61, 61, 61, 65, 65, 65, 65, 69, 69, 69, 69, 73, 73, 73, 73, 77, 77, 77, 77, 81, 81, 81, 81, 85, 85, 85, 85, 89, 89, 89, 89, 94, 94, 94, 94, 99, 99, 99, 99, 104, 104, 104, 104, 109, 109, 109, 109, 114, 114, 114, 114, 119, 119, 119, 119, 124, 124, 124, 124, 129, 129, 129, 129, 134, 134, 134, 134, 140, 140, 140, 140, 146, 146, 146, 146, 152, 152, 152, 152, 158, 158, 158, 158, 164, 164, 164, 164, 170, 170, 170, 170, 176, 176, 176, 176, 182, 182, 182, 182, 188, 188, 188, 188, 195, 195, 195, 195, 202, 202, 202, 202, 209, 209, 209, 209, 216, 216, 216, 216, 223, 223, 223, 223, 230, 230, 230, 230, 237, 237, 237, 237, 244, 244, 244, 244, 251, 251, 251, 251, 255, 255, 255, 255 ] + description: | + Gamma correction lookup values for the red channel. + The gamma values make the LED brightness seem more linear to human eyes. + Default values match the recommendation from the IC datasheet but note this may + or may not work for your particular LEDs. + + green-gamma: + type: array + required: false + default: [ 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 10, 10, 10, 10, 12, 12, 12, 12, 14, 14, 14, 14, 16, 16, 16, 16, 18, 18, 18, 18, 20, 20, 20, 20, 22, 22, 22, 22, 24, 24, 24, 24, 26, 26, 26, 26, 29, 29, 29, 29, 32, 32, 32, 32, 35, 35, 35, 35, 38, 38, 38, 38, 41, 41, 41, 41, 44, 44, 44, 44, 47, 47, 47, 47, 50, 50, 50, 50, 53, 53, 53, 53, 57, 57, 57, 57, 61, 61, 61, 61, 65, 65, 65, 65, 69, 69, 69, 69, 73, 73, 73, 73, 77, 77, 77, 77, 81, 81, 81, 81, 85, 85, 85, 85, 89, 89, 89, 89, 94, 94, 94, 94, 99, 99, 99, 99, 104, 104, 104, 104, 109, 109, 109, 109, 114, 114, 114, 114, 119, 119, 119, 119, 124, 124, 124, 124, 129, 129, 129, 129, 134, 134, 134, 134, 140, 140, 140, 140, 146, 146, 146, 146, 152, 152, 152, 152, 158, 158, 158, 158, 164, 164, 164, 164, 170, 170, 170, 170, 176, 176, 176, 176, 182, 182, 182, 182, 188, 188, 188, 188, 195, 195, 195, 195, 202, 202, 202, 202, 209, 209, 209, 209, 216, 216, 216, 216, 223, 223, 223, 223, 230, 230, 230, 230, 237, 237, 237, 237, 244, 244, 244, 244, 251, 251, 251, 251, 255, 255, 255, 255 ] + description: | + Gamma correction lookup values for the red channel. + The gamma values make the LED brightness seem more linear to human eyes. + Default values match the recommendation from the IC datasheet but note this may + or may not work for your particular LEDs. + + blue-gamma: + type: array + required: false + default: [ 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 10, 10, 10, 10, 12, 12, 12, 12, 14, 14, 14, 14, 16, 16, 16, 16, 18, 18, 18, 18, 20, 20, 20, 20, 22, 22, 22, 22, 24, 24, 24, 24, 26, 26, 26, 26, 29, 29, 29, 29, 32, 32, 32, 32, 35, 35, 35, 35, 38, 38, 38, 38, 41, 41, 41, 41, 44, 44, 44, 44, 47, 47, 47, 47, 50, 50, 50, 50, 53, 53, 53, 53, 57, 57, 57, 57, 61, 61, 61, 61, 65, 65, 65, 65, 69, 69, 69, 69, 73, 73, 73, 73, 77, 77, 77, 77, 81, 81, 81, 81, 85, 85, 85, 85, 89, 89, 89, 89, 94, 94, 94, 94, 99, 99, 99, 99, 104, 104, 104, 104, 109, 109, 109, 109, 114, 114, 114, 114, 119, 119, 119, 119, 124, 124, 124, 124, 129, 129, 129, 129, 134, 134, 134, 134, 140, 140, 140, 140, 146, 146, 146, 146, 152, 152, 152, 152, 158, 158, 158, 158, 164, 164, 164, 164, 170, 170, 170, 170, 176, 176, 176, 176, 182, 182, 182, 182, 188, 188, 188, 188, 195, 195, 195, 195, 202, 202, 202, 202, 209, 209, 209, 209, 216, 216, 216, 216, 223, 223, 223, 223, 230, 230, 230, 230, 237, 237, 237, 237, 244, 244, 244, 244, 251, 251, 251, 251, 255, 255, 255, 255 ] + description: | + Gamma correction lookup values for the red channel. + The gamma values make the LED brightness seem more linear to human eyes. + Default values match the recommendation from the IC datasheet but note this may + or may not work for your particular LEDs. diff --git a/app/include/dt-bindings/zmk/issi_transform.h b/app/include/dt-bindings/zmk/issi_transform.h index a5932315..8816fb8f 100644 --- a/app/include/dt-bindings/zmk/issi_transform.h +++ b/app/include/dt-bindings/zmk/issi_transform.h @@ -5,12 +5,11 @@ */ /** - * Maps a 39x9 matrix cell index to a 1D array matching IS31FL3741's PWM registers order. + * Maps a 39x9 matrix cell index to a 1D array matching IS31FL3741's PWN registers order. */ -#define PIXEL(n) ( \ - (((n) % 39) < 30) \ - ? ((((n) / 39) * 30) + ((n) % 39)) \ - : (270 + (((n) / 39) * 9) + ((n) % 39) - 30)) +#define PIXEL(n) \ + ((((n) % 39) < 30) ? ((((n) / 39) * 30) + ((n) % 39)) \ + : (270 + (((n) / 39) * 9) + ((n) % 39) - 30)) #define RGB(com, r, g, b) PIXEL(com + r) PIXEL(com + g) PIXEL(com + b)