From 06ccd085ef01cb7f3114c00a336b17c0c80b1c54 Mon Sep 17 00:00:00 2001 From: Kai Ryu Date: Fri, 4 Nov 2016 12:28:06 +0900 Subject: [PATCH] staryu: Make number of RGB LEDs configurable --- keyboard/staryu/rgb.c | 28 ++++++++++++++++++++++------ keyboard/staryu/rgb.h | 3 ++- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/keyboard/staryu/rgb.c b/keyboard/staryu/rgb.c index 6a8caa18..5918ee9d 100644 --- a/keyboard/staryu/rgb.c +++ b/keyboard/staryu/rgb.c @@ -26,7 +26,8 @@ along with this program. If not, see . volatile static uint8_t rgb_fading_enable = 0; static rgb_config_t rgb_config; -static struct cRGB rgb_color[RGB_LED_COUNT]; +static struct cRGB rgb_color[RGB_LED_MAX_COUNT]; +static uint8_t rgb_count = 1; static uint16_t rgb_hue = 0; static uint8_t rgb_saturation = 255; static uint8_t rgb_brightness = 16; @@ -39,15 +40,28 @@ static void rgb_write_config(void); static void rgb_read_config(void); static void rgb_set_level(uint8_t level); static void rgb_refresh(void); +#if 0 static void hue_to_rgb(uint16_t hue, struct cRGB *rgb); +#endif static void hsb_to_rgb(uint16_t hue, uint8_t saturation, uint8_t brightness, struct cRGB *rgb); void rgb_init(void) { + bool write_config = false; rgb_read_config(); if (rgb_config.raw == RGB_UNCONFIGURED) { rgb_config.enable = 0; rgb_config.level = RGB_OFF; + write_config = true; + } + if (rgb_count == 0 || rgb_count == RGB_UNCONFIGURED) { + rgb_count = 1; + write_config = true; + } + else if (rgb_count > RGB_LED_MAX_COUNT) { + rgb_count = RGB_LED_MAX_COUNT; + } + if (write_config) { rgb_write_config(); } if (rgb_config.enable) { @@ -58,11 +72,13 @@ void rgb_init(void) void rgb_read_config(void) { rgb_config.raw = eeprom_read_byte(EECONFIG_RGB); + rgb_count = eeprom_read_byte(EECONFIG_RGB_COUNT); } void rgb_write_config(void) { - eeprom_write_byte(EECONFIG_RGB, rgb_config.raw); + eeprom_update_byte(EECONFIG_RGB, rgb_config.raw); + eeprom_update_byte(EECONFIG_RGB_COUNT, rgb_count); } void rgb_toggle(void) @@ -176,19 +192,19 @@ void rgb_refresh(void) uint16_t hue; uint8_t i; if (rgb_rainbow) { - for (i = 0; i < RGB_LED_COUNT; i++) { - hue = rgb_hue + (768 / RGB_LED_COUNT) * i; + for (i = 0; i < rgb_count; i++) { + hue = rgb_hue + (768 / rgb_count) * i; hsb_to_rgb(hue, rgb_saturation, rgb_brightness, &rgb); rgb_color[i] = rgb; } } else { hsb_to_rgb(rgb_hue, rgb_saturation, rgb_brightness, &rgb); - for (i = 0; i < RGB_LED_COUNT; i++) { + for (i = 0; i < rgb_count; i++) { rgb_color[i] = rgb; } } - ws2812_setleds(rgb_color, RGB_LED_COUNT); + ws2812_setleds(rgb_color, rgb_count); } #if 0 diff --git a/keyboard/staryu/rgb.h b/keyboard/staryu/rgb.h index ba77fb0f..586c3fd3 100644 --- a/keyboard/staryu/rgb.h +++ b/keyboard/staryu/rgb.h @@ -44,8 +44,9 @@ enum { }; #define EECONFIG_RGB (uint8_t *)7 +#define EECONFIG_RGB_COUNT (uint8_t *)8 #define RGB_UNCONFIGURED 0xFF -#define RGB_LED_COUNT 16 +#define RGB_LED_MAX_COUNT 16 void rgb_init(void); void rgb_toggle(void);