aboutsummaryrefslogtreecommitdiff
path: root/quantum
diff options
context:
space:
mode:
Diffstat (limited to 'quantum')
-rw-r--r--quantum/rgblight.c46
-rw-r--r--quantum/rgblight.h1
2 files changed, 45 insertions, 2 deletions
diff --git a/quantum/rgblight.c b/quantum/rgblight.c
index 22dce963c..30f7d7528 100644
--- a/quantum/rgblight.c
+++ b/quantum/rgblight.c
@@ -58,6 +58,7 @@ const uint16_t RGBLED_GRADIENT_RANGES[] PROGMEM = {360, 240, 180, 120, 90};
58#endif 58#endif
59 59
60rgblight_config_t rgblight_config; 60rgblight_config_t rgblight_config;
61bool is_rgblight_initialized = false;
61 62
62LED_TYPE led[RGBLED_NUM]; 63LED_TYPE led[RGBLED_NUM];
63bool rgblight_timer_enabled = false; 64bool rgblight_timer_enabled = false;
@@ -123,6 +124,35 @@ void setrgb(uint8_t r, uint8_t g, uint8_t b, LED_TYPE *led1) {
123 (*led1).b = b; 124 (*led1).b = b;
124} 125}
125 126
127void rgblight_check_config(void) {
128 /* Add some out of bound checks for RGB light config */
129
130 if (rgblight_config.mode < RGBLIGHT_MODE_STATIC_LIGHT) {
131 rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
132 }
133 else if (rgblight_config.mode > RGBLIGHT_MODES) {
134 rgblight_config.mode = RGBLIGHT_MODES;
135 }
136
137 if (rgblight_config.hue < 0) {
138 rgblight_config.hue = 0;
139 } else if (rgblight_config.hue > 360) {
140 rgblight_config.hue %= 360;
141 }
142
143 if (rgblight_config.sat < 0) {
144 rgblight_config.sat = 0;
145 } else if (rgblight_config.sat > 255) {
146 rgblight_config.sat = 255;
147 }
148
149 if (rgblight_config.val < 0) {
150 rgblight_config.val = 0;
151 } else if (rgblight_config.val > RGBLIGHT_LIMIT_VAL) {
152 rgblight_config.val = RGBLIGHT_LIMIT_VAL;
153 }
154
155}
126 156
127uint32_t eeconfig_read_rgblight(void) { 157uint32_t eeconfig_read_rgblight(void) {
128 #if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE) 158 #if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
@@ -131,13 +161,14 @@ uint32_t eeconfig_read_rgblight(void) {
131 return 0; 161 return 0;
132 #endif 162 #endif
133} 163}
164
134void eeconfig_update_rgblight(uint32_t val) { 165void eeconfig_update_rgblight(uint32_t val) {
135 #if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE) 166 #if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
136 if (eeconfig_read_rgblight() != val) { 167 rgblight_check_config();
137 eeprom_update_dword(EECONFIG_RGBLIGHT, val); 168 eeprom_update_dword(EECONFIG_RGBLIGHT, val);
138 }
139 #endif 169 #endif
140} 170}
171
141void eeconfig_update_rgblight_default(void) { 172void eeconfig_update_rgblight_default(void) {
142 //dprintf("eeconfig_update_rgblight_default\n"); 173 //dprintf("eeconfig_update_rgblight_default\n");
143 rgblight_config.enable = 1; 174 rgblight_config.enable = 1;
@@ -148,6 +179,7 @@ void eeconfig_update_rgblight_default(void) {
148 rgblight_config.speed = 0; 179 rgblight_config.speed = 0;
149 eeconfig_update_rgblight(rgblight_config.raw); 180 eeconfig_update_rgblight(rgblight_config.raw);
150} 181}
182
151void eeconfig_debug_rgblight(void) { 183void eeconfig_debug_rgblight(void) {
152 dprintf("rgblight_config eprom\n"); 184 dprintf("rgblight_config eprom\n");
153 dprintf("rgblight_config.enable = %d\n", rgblight_config.enable); 185 dprintf("rgblight_config.enable = %d\n", rgblight_config.enable);
@@ -159,6 +191,11 @@ void eeconfig_debug_rgblight(void) {
159} 191}
160 192
161void rgblight_init(void) { 193void rgblight_init(void) {
194 /* if already initialized, don't do it again.
195 If you must do it again, extern this and set to false, first.
196 This is a dirty, dirty hack until proper hooks can be added for keyboard startup. */
197 if (is_rgblight_initialized) { return; }
198
162 debug_enable = 1; // Debug ON! 199 debug_enable = 1; // Debug ON!
163 dprintf("rgblight_init called.\n"); 200 dprintf("rgblight_init called.\n");
164 dprintf("rgblight_init start!\n"); 201 dprintf("rgblight_init start!\n");
@@ -173,6 +210,8 @@ void rgblight_init(void) {
173 eeconfig_update_rgblight_default(); 210 eeconfig_update_rgblight_default();
174 rgblight_config.raw = eeconfig_read_rgblight(); 211 rgblight_config.raw = eeconfig_read_rgblight();
175 } 212 }
213 rgblight_check_config();
214
176 eeconfig_debug_rgblight(); // display current eeprom values 215 eeconfig_debug_rgblight(); // display current eeprom values
177 216
178#ifdef RGBLIGHT_USE_TIMER 217#ifdef RGBLIGHT_USE_TIMER
@@ -182,6 +221,9 @@ void rgblight_init(void) {
182 if (rgblight_config.enable) { 221 if (rgblight_config.enable) {
183 rgblight_mode_noeeprom(rgblight_config.mode); 222 rgblight_mode_noeeprom(rgblight_config.mode);
184 } 223 }
224
225 is_rgblight_initialized = true;
226
185} 227}
186 228
187void rgblight_update_dword(uint32_t dword) { 229void rgblight_update_dword(uint32_t dword) {
diff --git a/quantum/rgblight.h b/quantum/rgblight.h
index 65dda3f52..03534bd31 100644
--- a/quantum/rgblight.h
+++ b/quantum/rgblight.h
@@ -146,6 +146,7 @@ extern const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[3] PROGMEM;
146extern const uint8_t RGBLED_SNAKE_INTERVALS[3] PROGMEM; 146extern const uint8_t RGBLED_SNAKE_INTERVALS[3] PROGMEM;
147extern const uint8_t RGBLED_KNIGHT_INTERVALS[3] PROGMEM; 147extern const uint8_t RGBLED_KNIGHT_INTERVALS[3] PROGMEM;
148extern const uint16_t RGBLED_RGBTEST_INTERVALS[1] PROGMEM; 148extern const uint16_t RGBLED_RGBTEST_INTERVALS[1] PROGMEM;
149extern bool is_rgblight_initialized;
149 150
150typedef union { 151typedef union {
151 uint32_t raw; 152 uint32_t raw;