From 951b6f33a0dc479d29831aaa5c73fc526faf0471 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 1 Apr 2017 21:28:27 +0300 Subject: [PATCH] CIE 1931 for the LCD backlight --- keyboards/ergodox/infinity/infinity.c | 29 ++++++++++++++++++++++--- keyboards/ergodox/infinity/visualizer.c | 6 ++--- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/keyboards/ergodox/infinity/infinity.c b/keyboards/ergodox/infinity/infinity.c index 02db67eaf2..ecc072abbe 100644 --- a/keyboards/ergodox/infinity/infinity.c +++ b/keyboards/ergodox/infinity/infinity.c @@ -70,10 +70,33 @@ void lcd_backlight_hal_init(void) { RGB_PORT->PCR[BLUE_PIN] = RGB_MODE; } +static uint16_t cie_lightness(uint16_t v) { + // The CIE 1931 formula for lightness + // Y = luminance (output) 0-1 + // L = lightness input 0 - 100 + + // Y = (L* / 902.3) if L* <= 8 + // Y = ((L* + 16) / 116)^3 if L* > 8 + + float l = 100.0f * (v / 65535.0f); + float y = 0.0f; + if (l <= 8.0f) { + y = l / 902.3; + } + else { + y = ((l + 16.0f) / 116.0f); + y = y * y * y; + if (y > 1.0f) { + y = 1.0f; + } + } + return y * 65535.0f; +} + void lcd_backlight_hal_color(uint16_t r, uint16_t g, uint16_t b) { - CHANNEL_RED.CnV = r; - CHANNEL_GREEN.CnV = g; - CHANNEL_BLUE.CnV = b; + CHANNEL_RED.CnV = cie_lightness(r); + CHANNEL_GREEN.CnV = cie_lightness(g); + CHANNEL_BLUE.CnV = cie_lightness(b); } __attribute__ ((weak)) diff --git a/keyboards/ergodox/infinity/visualizer.c b/keyboards/ergodox/infinity/visualizer.c index c8fc3d78ef..12336fdc57 100644 --- a/keyboards/ergodox/infinity/visualizer.c +++ b/keyboards/ergodox/infinity/visualizer.c @@ -70,8 +70,8 @@ static const uint8_t image_data_lcd_logo[512] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; -static const uint32_t logo_background_color = LCD_COLOR(0x00, 0x00, 0x80); -static const uint32_t initial_color = LCD_COLOR(0, 0, 0xFF); +static const uint32_t logo_background_color = LCD_COLOR(0x00, 0x00, 0xFF); +static const uint32_t initial_color = LCD_COLOR(0, 0, 0); bool display_logo(keyframe_animation_t* animation, visualizer_state_t* state) { (void)state; @@ -155,7 +155,7 @@ static keyframe_animation_t resume_animation = { void initialize_user_visualizer(visualizer_state_t* state) { // The brightness will be dynamically adjustable in the future // But for now, change it here. - lcd_backlight_brightness(0x50); + lcd_backlight_brightness(130); state->current_lcd_color = initial_color; state->target_lcd_color = logo_background_color; start_keyframe_animation(&startup_animation);