diff options
Diffstat (limited to 'quantum/rgblight.c')
| -rw-r--r-- | quantum/rgblight.c | 343 |
1 files changed, 235 insertions, 108 deletions
diff --git a/quantum/rgblight.c b/quantum/rgblight.c index 4919ae4ab..23420ddd8 100644 --- a/quantum/rgblight.c +++ b/quantum/rgblight.c | |||
| @@ -14,6 +14,7 @@ | |||
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ | 15 | */ |
| 16 | #include <math.h> | 16 | #include <math.h> |
| 17 | #include <string.h> | ||
| 17 | #ifdef __AVR__ | 18 | #ifdef __AVR__ |
| 18 | #include <avr/eeprom.h> | 19 | #include <avr/eeprom.h> |
| 19 | #include <avr/interrupt.h> | 20 | #include <avr/interrupt.h> |
| @@ -29,23 +30,27 @@ | |||
| 29 | #define RGBLIGHT_LIMIT_VAL 255 | 30 | #define RGBLIGHT_LIMIT_VAL 255 |
| 30 | #endif | 31 | #endif |
| 31 | 32 | ||
| 33 | #define _RGBM_SINGLE_STATIC(sym) RGBLIGHT_MODE_ ## sym, | ||
| 34 | #define _RGBM_SINGLE_DYNAMIC(sym) | ||
| 35 | #define _RGBM_MULTI_STATIC(sym) RGBLIGHT_MODE_ ## sym, | ||
| 36 | #define _RGBM_MULTI_DYNAMIC(sym) | ||
| 37 | #define _RGBM_TMP_STATIC(sym) RGBLIGHT_MODE_ ## sym, | ||
| 38 | #define _RGBM_TMP_DYNAMIC(sym) | ||
| 39 | static uint8_t static_effect_table [] = { | ||
| 40 | #include "rgblight.h" | ||
| 41 | }; | ||
| 42 | |||
| 43 | static inline int is_static_effect(uint8_t mode) { | ||
| 44 | return memchr(static_effect_table, mode, sizeof(static_effect_table)) != NULL; | ||
| 45 | } | ||
| 46 | |||
| 32 | #define MIN(a,b) (((a)<(b))?(a):(b)) | 47 | #define MIN(a,b) (((a)<(b))?(a):(b)) |
| 33 | #define MAX(a,b) (((a)>(b))?(a):(b)) | 48 | #define MAX(a,b) (((a)>(b))?(a):(b)) |
| 34 | 49 | ||
| 35 | __attribute__ ((weak)) | 50 | #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT |
| 36 | const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5}; | ||
| 37 | __attribute__ ((weak)) | ||
| 38 | const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30}; | ||
| 39 | __attribute__ ((weak)) | ||
| 40 | const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20}; | ||
| 41 | __attribute__ ((weak)) | ||
| 42 | const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20}; | ||
| 43 | __attribute__ ((weak)) | ||
| 44 | const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {127, 63, 31}; | ||
| 45 | __attribute__ ((weak)) | 51 | __attribute__ ((weak)) |
| 46 | const uint16_t RGBLED_GRADIENT_RANGES[] PROGMEM = {360, 240, 180, 120, 90}; | 52 | const uint16_t RGBLED_GRADIENT_RANGES[] PROGMEM = {360, 240, 180, 120, 90}; |
| 47 | __attribute__ ((weak)) | 53 | #endif |
| 48 | const uint16_t RGBLED_RGBTEST_INTERVALS[] PROGMEM = {1024}; | ||
| 49 | 54 | ||
| 50 | rgblight_config_t rgblight_config; | 55 | rgblight_config_t rgblight_config; |
| 51 | 56 | ||
| @@ -129,7 +134,7 @@ void eeconfig_update_rgblight(uint32_t val) { | |||
| 129 | void eeconfig_update_rgblight_default(void) { | 134 | void eeconfig_update_rgblight_default(void) { |
| 130 | //dprintf("eeconfig_update_rgblight_default\n"); | 135 | //dprintf("eeconfig_update_rgblight_default\n"); |
| 131 | rgblight_config.enable = 1; | 136 | rgblight_config.enable = 1; |
| 132 | rgblight_config.mode = 1; | 137 | rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT; |
| 133 | rgblight_config.hue = 0; | 138 | rgblight_config.hue = 0; |
| 134 | rgblight_config.sat = 255; | 139 | rgblight_config.sat = 255; |
| 135 | rgblight_config.val = RGBLIGHT_LIMIT_VAL; | 140 | rgblight_config.val = RGBLIGHT_LIMIT_VAL; |
| @@ -163,9 +168,9 @@ void rgblight_init(void) { | |||
| 163 | } | 168 | } |
| 164 | eeconfig_debug_rgblight(); // display current eeprom values | 169 | eeconfig_debug_rgblight(); // display current eeprom values |
| 165 | 170 | ||
| 166 | #ifdef RGBLIGHT_ANIMATIONS | 171 | #ifdef RGBLIGHT_USE_TIMER |
| 167 | rgblight_timer_init(); // setup the timer | 172 | rgblight_timer_init(); // setup the timer |
| 168 | #endif | 173 | #endif |
| 169 | 174 | ||
| 170 | if (rgblight_config.enable) { | 175 | if (rgblight_config.enable) { |
| 171 | rgblight_mode_noeeprom(rgblight_config.mode); | 176 | rgblight_mode_noeeprom(rgblight_config.mode); |
| @@ -178,9 +183,9 @@ void rgblight_update_dword(uint32_t dword) { | |||
| 178 | if (rgblight_config.enable) | 183 | if (rgblight_config.enable) |
| 179 | rgblight_mode(rgblight_config.mode); | 184 | rgblight_mode(rgblight_config.mode); |
| 180 | else { | 185 | else { |
| 181 | #ifdef RGBLIGHT_ANIMATIONS | 186 | #ifdef RGBLIGHT_USE_TIMER |
| 182 | rgblight_timer_disable(); | 187 | rgblight_timer_disable(); |
| 183 | #endif | 188 | #endif |
| 184 | rgblight_set(); | 189 | rgblight_set(); |
| 185 | } | 190 | } |
| 186 | } | 191 | } |
| @@ -195,29 +200,41 @@ void rgblight_increase(void) { | |||
| 195 | void rgblight_decrease(void) { | 200 | void rgblight_decrease(void) { |
| 196 | uint8_t mode = 0; | 201 | uint8_t mode = 0; |
| 197 | // Mode will never be < 1. If it ever is, eeprom needs to be initialized. | 202 | // Mode will never be < 1. If it ever is, eeprom needs to be initialized. |
| 198 | if (rgblight_config.mode > 1) { | 203 | if (rgblight_config.mode > RGBLIGHT_MODE_STATIC_LIGHT) { |
| 199 | mode = rgblight_config.mode - 1; | 204 | mode = rgblight_config.mode - 1; |
| 200 | } | 205 | } |
| 201 | rgblight_mode(mode); | 206 | rgblight_mode(mode); |
| 202 | } | 207 | } |
| 203 | void rgblight_step(void) { | 208 | void rgblight_step_helper(bool write_to_eeprom) { |
| 204 | uint8_t mode = 0; | 209 | uint8_t mode = 0; |
| 205 | mode = rgblight_config.mode + 1; | 210 | mode = rgblight_config.mode + 1; |
| 206 | if (mode > RGBLIGHT_MODES) { | 211 | if (mode > RGBLIGHT_MODES) { |
| 207 | mode = 1; | 212 | mode = 1; |
| 208 | } | 213 | } |
| 209 | rgblight_mode(mode); | 214 | rgblight_mode_eeprom_helper(mode, write_to_eeprom); |
| 210 | } | 215 | } |
| 211 | void rgblight_step_reverse(void) { | 216 | void rgblight_step_noeeprom(void) { |
| 217 | rgblight_step_helper(false); | ||
| 218 | } | ||
| 219 | void rgblight_step(void) { | ||
| 220 | rgblight_step_helper(true); | ||
| 221 | } | ||
| 222 | void rgblight_step_reverse_helper(bool write_to_eeprom) { | ||
| 212 | uint8_t mode = 0; | 223 | uint8_t mode = 0; |
| 213 | mode = rgblight_config.mode - 1; | 224 | mode = rgblight_config.mode - 1; |
| 214 | if (mode < 1) { | 225 | if (mode < 1) { |
| 215 | mode = RGBLIGHT_MODES; | 226 | mode = RGBLIGHT_MODES; |
| 216 | } | 227 | } |
| 217 | rgblight_mode(mode); | 228 | rgblight_mode_eeprom_helper(mode, write_to_eeprom); |
| 229 | } | ||
| 230 | void rgblight_step_reverse_noeeprom(void) { | ||
| 231 | rgblight_step_reverse_helper(false); | ||
| 232 | } | ||
| 233 | void rgblight_step_reverse(void) { | ||
| 234 | rgblight_step_reverse_helper(true); | ||
| 218 | } | 235 | } |
| 219 | 236 | ||
| 220 | uint32_t rgblight_get_mode(void) { | 237 | uint8_t rgblight_get_mode(void) { |
| 221 | if (!rgblight_config.enable) { | 238 | if (!rgblight_config.enable) { |
| 222 | return false; | 239 | return false; |
| 223 | } | 240 | } |
| @@ -229,8 +246,8 @@ void rgblight_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) { | |||
| 229 | if (!rgblight_config.enable) { | 246 | if (!rgblight_config.enable) { |
| 230 | return; | 247 | return; |
| 231 | } | 248 | } |
| 232 | if (mode < 1) { | 249 | if (mode < RGBLIGHT_MODE_STATIC_LIGHT) { |
| 233 | rgblight_config.mode = 1; | 250 | rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT; |
| 234 | } else if (mode > RGBLIGHT_MODES) { | 251 | } else if (mode > RGBLIGHT_MODES) { |
| 235 | rgblight_config.mode = RGBLIGHT_MODES; | 252 | rgblight_config.mode = RGBLIGHT_MODES; |
| 236 | } else { | 253 | } else { |
| @@ -242,30 +259,14 @@ void rgblight_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) { | |||
| 242 | } else { | 259 | } else { |
| 243 | xprintf("rgblight mode [NOEEPROM]: %u\n", rgblight_config.mode); | 260 | xprintf("rgblight mode [NOEEPROM]: %u\n", rgblight_config.mode); |
| 244 | } | 261 | } |
| 245 | if (rgblight_config.mode == 1) { | 262 | if( is_static_effect(rgblight_config.mode) ) { |
| 246 | #ifdef RGBLIGHT_ANIMATIONS | 263 | #ifdef RGBLIGHT_USE_TIMER |
| 247 | rgblight_timer_disable(); | 264 | rgblight_timer_disable(); |
| 248 | #endif | 265 | #endif |
| 249 | } else if ((rgblight_config.mode >= 2 && rgblight_config.mode <= 24) || | 266 | } else { |
| 250 | rgblight_config.mode == 35 || rgblight_config.mode == 36) { | 267 | #ifdef RGBLIGHT_USE_TIMER |
| 251 | // MODE 2-5, breathing | ||
| 252 | // MODE 6-8, rainbow mood | ||
| 253 | // MODE 9-14, rainbow swirl | ||
| 254 | // MODE 15-20, snake | ||
| 255 | // MODE 21-23, knight | ||
| 256 | // MODE 24, xmas | ||
| 257 | // MODE 35 RGB test | ||
| 258 | // MODE 36, alterating | ||
| 259 | |||
| 260 | #ifdef RGBLIGHT_ANIMATIONS | ||
| 261 | rgblight_timer_enable(); | 268 | rgblight_timer_enable(); |
| 262 | #endif | 269 | #endif |
| 263 | } else if (rgblight_config.mode >= 25 && rgblight_config.mode <= 34) { | ||
| 264 | // MODE 25-34, static gradient | ||
| 265 | |||
| 266 | #ifdef RGBLIGHT_ANIMATIONS | ||
| 267 | rgblight_timer_disable(); | ||
| 268 | #endif | ||
| 269 | } | 270 | } |
| 270 | rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, rgblight_config.val); | 271 | rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, rgblight_config.val); |
| 271 | } | 272 | } |
| @@ -317,9 +318,9 @@ void rgblight_disable(void) { | |||
| 317 | rgblight_config.enable = 0; | 318 | rgblight_config.enable = 0; |
| 318 | eeconfig_update_rgblight(rgblight_config.raw); | 319 | eeconfig_update_rgblight(rgblight_config.raw); |
| 319 | xprintf("rgblight disable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable); | 320 | xprintf("rgblight disable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable); |
| 320 | #ifdef RGBLIGHT_ANIMATIONS | 321 | #ifdef RGBLIGHT_USE_TIMER |
| 321 | rgblight_timer_disable(); | 322 | rgblight_timer_disable(); |
| 322 | #endif | 323 | #endif |
| 323 | wait_ms(50); | 324 | wait_ms(50); |
| 324 | rgblight_set(); | 325 | rgblight_set(); |
| 325 | } | 326 | } |
| @@ -327,76 +328,112 @@ void rgblight_disable(void) { | |||
| 327 | void rgblight_disable_noeeprom(void) { | 328 | void rgblight_disable_noeeprom(void) { |
| 328 | rgblight_config.enable = 0; | 329 | rgblight_config.enable = 0; |
| 329 | xprintf("rgblight disable [noEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable); | 330 | xprintf("rgblight disable [noEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable); |
| 330 | #ifdef RGBLIGHT_ANIMATIONS | 331 | #ifdef RGBLIGHT_USE_TIMER |
| 331 | rgblight_timer_disable(); | 332 | rgblight_timer_disable(); |
| 332 | #endif | 333 | #endif |
| 333 | _delay_ms(50); | 334 | _delay_ms(50); |
| 334 | rgblight_set(); | 335 | rgblight_set(); |
| 335 | } | 336 | } |
| 336 | 337 | ||
| 337 | 338 | ||
| 338 | // Deals with the messy details of incrementing an integer | 339 | // Deals with the messy details of incrementing an integer |
| 339 | uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) { | 340 | static uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) { |
| 340 | int16_t new_value = value; | 341 | int16_t new_value = value; |
| 341 | new_value += step; | 342 | new_value += step; |
| 342 | return MIN( MAX( new_value, min ), max ); | 343 | return MIN( MAX( new_value, min ), max ); |
| 343 | } | 344 | } |
| 344 | 345 | ||
| 345 | uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) { | 346 | static uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) { |
| 346 | int16_t new_value = value; | 347 | int16_t new_value = value; |
| 347 | new_value -= step; | 348 | new_value -= step; |
| 348 | return MIN( MAX( new_value, min ), max ); | 349 | return MIN( MAX( new_value, min ), max ); |
| 349 | } | 350 | } |
| 350 | 351 | ||
| 351 | void rgblight_increase_hue(void) { | 352 | void rgblight_increase_hue_helper(bool write_to_eeprom) { |
| 352 | uint16_t hue; | 353 | uint16_t hue; |
| 353 | hue = (rgblight_config.hue+RGBLIGHT_HUE_STEP) % 360; | 354 | hue = (rgblight_config.hue+RGBLIGHT_HUE_STEP) % 360; |
| 354 | rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val); | 355 | rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom); |
| 355 | } | 356 | } |
| 356 | void rgblight_decrease_hue(void) { | 357 | void rgblight_increase_hue_noeeprom(void) { |
| 358 | rgblight_increase_hue_helper(false); | ||
| 359 | } | ||
| 360 | void rgblight_increase_hue(void) { | ||
| 361 | rgblight_increase_hue_helper(true); | ||
| 362 | } | ||
| 363 | void rgblight_decrease_hue_helper(bool write_to_eeprom) { | ||
| 357 | uint16_t hue; | 364 | uint16_t hue; |
| 358 | if (rgblight_config.hue-RGBLIGHT_HUE_STEP < 0) { | 365 | if (rgblight_config.hue-RGBLIGHT_HUE_STEP < 0) { |
| 359 | hue = (rgblight_config.hue + 360 - RGBLIGHT_HUE_STEP) % 360; | 366 | hue = (rgblight_config.hue + 360 - RGBLIGHT_HUE_STEP) % 360; |
| 360 | } else { | 367 | } else { |
| 361 | hue = (rgblight_config.hue - RGBLIGHT_HUE_STEP) % 360; | 368 | hue = (rgblight_config.hue - RGBLIGHT_HUE_STEP) % 360; |
| 362 | } | 369 | } |
| 363 | rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val); | 370 | rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom); |
| 364 | } | 371 | } |
| 365 | void rgblight_increase_sat(void) { | 372 | void rgblight_decrease_hue_noeeprom(void) { |
| 373 | rgblight_decrease_hue_helper(false); | ||
| 374 | } | ||
| 375 | void rgblight_decrease_hue(void) { | ||
| 376 | rgblight_decrease_hue_helper(true); | ||
| 377 | } | ||
| 378 | void rgblight_increase_sat_helper(bool write_to_eeprom) { | ||
| 366 | uint8_t sat; | 379 | uint8_t sat; |
| 367 | if (rgblight_config.sat + RGBLIGHT_SAT_STEP > 255) { | 380 | if (rgblight_config.sat + RGBLIGHT_SAT_STEP > 255) { |
| 368 | sat = 255; | 381 | sat = 255; |
| 369 | } else { | 382 | } else { |
| 370 | sat = rgblight_config.sat + RGBLIGHT_SAT_STEP; | 383 | sat = rgblight_config.sat + RGBLIGHT_SAT_STEP; |
| 371 | } | 384 | } |
| 372 | rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val); | 385 | rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom); |
| 373 | } | 386 | } |
| 374 | void rgblight_decrease_sat(void) { | 387 | void rgblight_increase_sat_noeeprom(void) { |
| 388 | rgblight_increase_sat_helper(false); | ||
| 389 | } | ||
| 390 | void rgblight_increase_sat(void) { | ||
| 391 | rgblight_increase_sat_helper(true); | ||
| 392 | } | ||
| 393 | void rgblight_decrease_sat_helper(bool write_to_eeprom) { | ||
| 375 | uint8_t sat; | 394 | uint8_t sat; |
| 376 | if (rgblight_config.sat - RGBLIGHT_SAT_STEP < 0) { | 395 | if (rgblight_config.sat - RGBLIGHT_SAT_STEP < 0) { |
| 377 | sat = 0; | 396 | sat = 0; |
| 378 | } else { | 397 | } else { |
| 379 | sat = rgblight_config.sat - RGBLIGHT_SAT_STEP; | 398 | sat = rgblight_config.sat - RGBLIGHT_SAT_STEP; |
| 380 | } | 399 | } |
| 381 | rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val); | 400 | rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom); |
| 382 | } | 401 | } |
| 383 | void rgblight_increase_val(void) { | 402 | void rgblight_decrease_sat_noeeprom(void) { |
| 403 | rgblight_decrease_sat_helper(false); | ||
| 404 | } | ||
| 405 | void rgblight_decrease_sat(void) { | ||
| 406 | rgblight_decrease_sat_helper(true); | ||
| 407 | } | ||
| 408 | void rgblight_increase_val_helper(bool write_to_eeprom) { | ||
| 384 | uint8_t val; | 409 | uint8_t val; |
| 385 | if (rgblight_config.val + RGBLIGHT_VAL_STEP > RGBLIGHT_LIMIT_VAL) { | 410 | if (rgblight_config.val + RGBLIGHT_VAL_STEP > RGBLIGHT_LIMIT_VAL) { |
| 386 | val = RGBLIGHT_LIMIT_VAL; | 411 | val = RGBLIGHT_LIMIT_VAL; |
| 387 | } else { | 412 | } else { |
| 388 | val = rgblight_config.val + RGBLIGHT_VAL_STEP; | 413 | val = rgblight_config.val + RGBLIGHT_VAL_STEP; |
| 389 | } | 414 | } |
| 390 | rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val); | 415 | rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom); |
| 391 | } | 416 | } |
| 392 | void rgblight_decrease_val(void) { | 417 | void rgblight_increase_val_noeeprom(void) { |
| 418 | rgblight_increase_val_helper(false); | ||
| 419 | } | ||
| 420 | void rgblight_increase_val(void) { | ||
| 421 | rgblight_increase_val_helper(true); | ||
| 422 | } | ||
| 423 | void rgblight_decrease_val_helper(bool write_to_eeprom) { | ||
| 393 | uint8_t val; | 424 | uint8_t val; |
| 394 | if (rgblight_config.val - RGBLIGHT_VAL_STEP < 0) { | 425 | if (rgblight_config.val - RGBLIGHT_VAL_STEP < 0) { |
| 395 | val = 0; | 426 | val = 0; |
| 396 | } else { | 427 | } else { |
| 397 | val = rgblight_config.val - RGBLIGHT_VAL_STEP; | 428 | val = rgblight_config.val - RGBLIGHT_VAL_STEP; |
| 398 | } | 429 | } |
| 399 | rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val); | 430 | rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom); |
| 431 | } | ||
| 432 | void rgblight_decrease_val_noeeprom(void) { | ||
| 433 | rgblight_decrease_val_helper(false); | ||
| 434 | } | ||
| 435 | void rgblight_decrease_val(void) { | ||
| 436 | rgblight_decrease_val_helper(true); | ||
| 400 | } | 437 | } |
| 401 | void rgblight_increase_speed(void) { | 438 | void rgblight_increase_speed(void) { |
| 402 | rgblight_config.speed = increment( rgblight_config.speed, 1, 0, 3 ); | 439 | rgblight_config.speed = increment( rgblight_config.speed, 1, 0, 3 ); |
| @@ -419,24 +456,43 @@ void rgblight_sethsv_noeeprom_old(uint16_t hue, uint8_t sat, uint8_t val) { | |||
| 419 | 456 | ||
| 420 | void rgblight_sethsv_eeprom_helper(uint16_t hue, uint8_t sat, uint8_t val, bool write_to_eeprom) { | 457 | void rgblight_sethsv_eeprom_helper(uint16_t hue, uint8_t sat, uint8_t val, bool write_to_eeprom) { |
| 421 | if (rgblight_config.enable) { | 458 | if (rgblight_config.enable) { |
| 422 | if (rgblight_config.mode == 1) { | 459 | if (rgblight_config.mode == RGBLIGHT_MODE_STATIC_LIGHT) { |
| 423 | // same static color | 460 | // same static color |
| 424 | LED_TYPE tmp_led; | 461 | LED_TYPE tmp_led; |
| 425 | sethsv(hue, sat, val, &tmp_led); | 462 | sethsv(hue, sat, val, &tmp_led); |
| 426 | rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b); | 463 | rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b); |
| 427 | } else { | 464 | } else { |
| 428 | // all LEDs in same color | 465 | // all LEDs in same color |
| 429 | if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) { | 466 | if ( 1 == 0 ) { //dummy |
| 467 | } | ||
| 468 | #ifdef RGBLIGHT_EFFECT_BREATHING | ||
| 469 | else if (rgblight_config.mode >= RGBLIGHT_MODE_BREATHING && | ||
| 470 | rgblight_config.mode <= RGBLIGHT_MODE_BREATHING_end) { | ||
| 430 | // breathing mode, ignore the change of val, use in memory value instead | 471 | // breathing mode, ignore the change of val, use in memory value instead |
| 431 | val = rgblight_config.val; | 472 | val = rgblight_config.val; |
| 432 | } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 14) { | 473 | } |
| 433 | // rainbow mood and rainbow swirl, ignore the change of hue | 474 | #endif |
| 475 | #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD | ||
| 476 | else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_MOOD && | ||
| 477 | rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_MOOD_end) { | ||
| 478 | // rainbow mood, ignore the change of hue | ||
| 434 | hue = rgblight_config.hue; | 479 | hue = rgblight_config.hue; |
| 435 | } else if (rgblight_config.mode >= 25 && rgblight_config.mode <= 34) { | 480 | } |
| 481 | #endif | ||
| 482 | #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL | ||
| 483 | else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_SWIRL && | ||
| 484 | rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_SWIRL_end) { | ||
| 485 | // rainbow swirl, ignore the change of hue | ||
| 486 | hue = rgblight_config.hue; | ||
| 487 | } | ||
| 488 | #endif | ||
| 489 | #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT | ||
| 490 | else if (rgblight_config.mode >= RGBLIGHT_MODE_STATIC_GRADIENT && | ||
| 491 | rgblight_config.mode <= RGBLIGHT_MODE_STATIC_GRADIENT_end) { | ||
| 436 | // static gradient | 492 | // static gradient |
| 437 | uint16_t _hue; | 493 | uint16_t _hue; |
| 438 | int8_t direction = ((rgblight_config.mode - 25) % 2) ? -1 : 1; | 494 | int8_t direction = ((rgblight_config.mode - RGBLIGHT_MODE_STATIC_GRADIENT) % 2) ? -1 : 1; |
| 439 | uint16_t range = pgm_read_word(&RGBLED_GRADIENT_RANGES[(rgblight_config.mode - 25) / 2]); | 495 | uint16_t range = pgm_read_word(&RGBLED_GRADIENT_RANGES[(rgblight_config.mode - RGBLIGHT_MODE_STATIC_GRADIENT) / 2]); |
| 440 | for (uint8_t i = 0; i < RGBLED_NUM; i++) { | 496 | for (uint8_t i = 0; i < RGBLED_NUM; i++) { |
| 441 | _hue = (range / RGBLED_NUM * i * direction + hue + 360) % 360; | 497 | _hue = (range / RGBLED_NUM * i * direction + hue + 360) % 360; |
| 442 | dprintf("rgblight rainbow set hsv: %u,%u,%d,%u\n", i, _hue, direction, range); | 498 | dprintf("rgblight rainbow set hsv: %u,%u,%d,%u\n", i, _hue, direction, range); |
| @@ -444,6 +500,7 @@ void rgblight_sethsv_eeprom_helper(uint16_t hue, uint8_t sat, uint8_t val, bool | |||
| 444 | } | 500 | } |
| 445 | rgblight_set(); | 501 | rgblight_set(); |
| 446 | } | 502 | } |
| 503 | #endif | ||
| 447 | } | 504 | } |
| 448 | rgblight_config.hue = hue; | 505 | rgblight_config.hue = hue; |
| 449 | rgblight_config.sat = sat; | 506 | rgblight_config.sat = sat; |
| @@ -528,7 +585,7 @@ void rgblight_set(void) { | |||
| 528 | } | 585 | } |
| 529 | #endif | 586 | #endif |
| 530 | 587 | ||
| 531 | #ifdef RGBLIGHT_ANIMATIONS | 588 | #ifdef RGBLIGHT_USE_TIMER |
| 532 | 589 | ||
| 533 | // Animation timer -- AVR Timer3 | 590 | // Animation timer -- AVR Timer3 |
| 534 | void rgblight_timer_init(void) { | 591 | void rgblight_timer_init(void) { |
| @@ -564,41 +621,77 @@ void rgblight_timer_toggle(void) { | |||
| 564 | 621 | ||
| 565 | void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) { | 622 | void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) { |
| 566 | rgblight_enable(); | 623 | rgblight_enable(); |
| 567 | rgblight_mode(1); | 624 | rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT); |
| 568 | rgblight_setrgb(r, g, b); | 625 | rgblight_setrgb(r, g, b); |
| 569 | } | 626 | } |
| 570 | 627 | ||
| 571 | void rgblight_task(void) { | 628 | void rgblight_task(void) { |
| 572 | if (rgblight_timer_enabled) { | 629 | if (rgblight_timer_enabled) { |
| 573 | // mode = 1, static light, do nothing here | 630 | // static light mode, do nothing here |
| 574 | if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) { | 631 | if ( 1 == 0 ) { //dummy |
| 575 | // mode = 2 to 5, breathing mode | 632 | } |
| 576 | rgblight_effect_breathing(rgblight_config.mode - 2); | 633 | #ifdef RGBLIGHT_EFFECT_BREATHING |
| 577 | } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 8) { | 634 | else if (rgblight_config.mode >= RGBLIGHT_MODE_BREATHING && |
| 578 | // mode = 6 to 8, rainbow mood mod | 635 | rgblight_config.mode <= RGBLIGHT_MODE_BREATHING_end) { |
| 579 | rgblight_effect_rainbow_mood(rgblight_config.mode - 6); | 636 | // breathing mode |
| 580 | } else if (rgblight_config.mode >= 9 && rgblight_config.mode <= 14) { | 637 | rgblight_effect_breathing(rgblight_config.mode - RGBLIGHT_MODE_BREATHING ); |
| 581 | // mode = 9 to 14, rainbow swirl mode | 638 | } |
| 582 | rgblight_effect_rainbow_swirl(rgblight_config.mode - 9); | 639 | #endif |
| 583 | } else if (rgblight_config.mode >= 15 && rgblight_config.mode <= 20) { | 640 | #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD |
| 584 | // mode = 15 to 20, snake mode | 641 | else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_MOOD && |
| 585 | rgblight_effect_snake(rgblight_config.mode - 15); | 642 | rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_MOOD_end) { |
| 586 | } else if (rgblight_config.mode >= 21 && rgblight_config.mode <= 23) { | 643 | // rainbow mood mode |
| 587 | // mode = 21 to 23, knight mode | 644 | rgblight_effect_rainbow_mood(rgblight_config.mode - RGBLIGHT_MODE_RAINBOW_MOOD); |
| 588 | rgblight_effect_knight(rgblight_config.mode - 21); | 645 | } |
| 589 | } else if (rgblight_config.mode == 24) { | 646 | #endif |
| 590 | // mode = 24, christmas mode | 647 | #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL |
| 648 | else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_SWIRL && | ||
| 649 | rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_SWIRL_end) { | ||
| 650 | // rainbow swirl mode | ||
| 651 | rgblight_effect_rainbow_swirl(rgblight_config.mode - RGBLIGHT_MODE_RAINBOW_SWIRL); | ||
| 652 | } | ||
| 653 | #endif | ||
| 654 | #ifdef RGBLIGHT_EFFECT_SNAKE | ||
| 655 | else if (rgblight_config.mode >= RGBLIGHT_MODE_SNAKE && | ||
| 656 | rgblight_config.mode <= RGBLIGHT_MODE_SNAKE_end) { | ||
| 657 | // snake mode | ||
| 658 | rgblight_effect_snake(rgblight_config.mode - RGBLIGHT_MODE_SNAKE); | ||
| 659 | } | ||
| 660 | #endif | ||
| 661 | #ifdef RGBLIGHT_EFFECT_KNIGHT | ||
| 662 | else if (rgblight_config.mode >= RGBLIGHT_MODE_KNIGHT && | ||
| 663 | rgblight_config.mode <= RGBLIGHT_MODE_KNIGHT_end) { | ||
| 664 | // knight mode | ||
| 665 | rgblight_effect_knight(rgblight_config.mode - RGBLIGHT_MODE_KNIGHT); | ||
| 666 | } | ||
| 667 | #endif | ||
| 668 | #ifdef RGBLIGHT_EFFECT_CHRISTMAS | ||
| 669 | else if (rgblight_config.mode == RGBLIGHT_MODE_CHRISTMAS) { | ||
| 670 | // christmas mode | ||
| 591 | rgblight_effect_christmas(); | 671 | rgblight_effect_christmas(); |
| 592 | } else if (rgblight_config.mode == 35) { | 672 | } |
| 593 | // mode = 35, RGB test | 673 | #endif |
| 674 | #ifdef RGBLIGHT_EFFECT_RGB_TEST | ||
| 675 | else if (rgblight_config.mode == RGBLIGHT_MODE_RGB_TEST) { | ||
| 676 | // RGB test mode | ||
| 594 | rgblight_effect_rgbtest(); | 677 | rgblight_effect_rgbtest(); |
| 595 | } else if (rgblight_config.mode == 36){ | 678 | } |
| 679 | #endif | ||
| 680 | #ifdef RGBLIGHT_EFFECT_ALTERNATING | ||
| 681 | else if (rgblight_config.mode == RGBLIGHT_MODE_ALTERNATING){ | ||
| 596 | rgblight_effect_alternating(); | 682 | rgblight_effect_alternating(); |
| 597 | } | 683 | } |
| 684 | #endif | ||
| 598 | } | 685 | } |
| 599 | } | 686 | } |
| 600 | 687 | ||
| 688 | #endif /* RGBLIGHT_USE_TIMER */ | ||
| 689 | |||
| 601 | // Effects | 690 | // Effects |
| 691 | #ifdef RGBLIGHT_EFFECT_BREATHING | ||
| 692 | __attribute__ ((weak)) | ||
| 693 | const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5}; | ||
| 694 | |||
| 602 | void rgblight_effect_breathing(uint8_t interval) { | 695 | void rgblight_effect_breathing(uint8_t interval) { |
| 603 | static uint8_t pos = 0; | 696 | static uint8_t pos = 0; |
| 604 | static uint16_t last_timer = 0; | 697 | static uint16_t last_timer = 0; |
| @@ -609,12 +702,17 @@ void rgblight_effect_breathing(uint8_t interval) { | |||
| 609 | } | 702 | } |
| 610 | last_timer = timer_read(); | 703 | last_timer = timer_read(); |
| 611 | 704 | ||
| 612 | |||
| 613 | // http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/ | 705 | // http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/ |
| 614 | val = (exp(sin((pos/255.0)*M_PI)) - RGBLIGHT_EFFECT_BREATHE_CENTER/M_E)*(RGBLIGHT_EFFECT_BREATHE_MAX/(M_E-1/M_E)); | 706 | val = (exp(sin((pos/255.0)*M_PI)) - RGBLIGHT_EFFECT_BREATHE_CENTER/M_E)*(RGBLIGHT_EFFECT_BREATHE_MAX/(M_E-1/M_E)); |
| 615 | rgblight_sethsv_noeeprom_old(rgblight_config.hue, rgblight_config.sat, val); | 707 | rgblight_sethsv_noeeprom_old(rgblight_config.hue, rgblight_config.sat, val); |
| 616 | pos = (pos + 1) % 256; | 708 | pos = (pos + 1) % 256; |
| 617 | } | 709 | } |
| 710 | #endif | ||
| 711 | |||
| 712 | #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD | ||
| 713 | __attribute__ ((weak)) | ||
| 714 | const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30}; | ||
| 715 | |||
| 618 | void rgblight_effect_rainbow_mood(uint8_t interval) { | 716 | void rgblight_effect_rainbow_mood(uint8_t interval) { |
| 619 | static uint16_t current_hue = 0; | 717 | static uint16_t current_hue = 0; |
| 620 | static uint16_t last_timer = 0; | 718 | static uint16_t last_timer = 0; |
| @@ -626,6 +724,16 @@ void rgblight_effect_rainbow_mood(uint8_t interval) { | |||
| 626 | rgblight_sethsv_noeeprom_old(current_hue, rgblight_config.sat, rgblight_config.val); | 724 | rgblight_sethsv_noeeprom_old(current_hue, rgblight_config.sat, rgblight_config.val); |
| 627 | current_hue = (current_hue + 1) % 360; | 725 | current_hue = (current_hue + 1) % 360; |
| 628 | } | 726 | } |
| 727 | #endif | ||
| 728 | |||
| 729 | #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL | ||
| 730 | #ifndef RGBLIGHT_RAINBOW_SWIRL_RANGE | ||
| 731 | #define RGBLIGHT_RAINBOW_SWIRL_RANGE 360 | ||
| 732 | #endif | ||
| 733 | |||
| 734 | __attribute__ ((weak)) | ||
| 735 | const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20}; | ||
| 736 | |||
| 629 | void rgblight_effect_rainbow_swirl(uint8_t interval) { | 737 | void rgblight_effect_rainbow_swirl(uint8_t interval) { |
| 630 | static uint16_t current_hue = 0; | 738 | static uint16_t current_hue = 0; |
| 631 | static uint16_t last_timer = 0; | 739 | static uint16_t last_timer = 0; |
| @@ -636,7 +744,7 @@ void rgblight_effect_rainbow_swirl(uint8_t interval) { | |||
| 636 | } | 744 | } |
| 637 | last_timer = timer_read(); | 745 | last_timer = timer_read(); |
| 638 | for (i = 0; i < RGBLED_NUM; i++) { | 746 | for (i = 0; i < RGBLED_NUM; i++) { |
| 639 | hue = (360 / RGBLED_NUM * i + current_hue) % 360; | 747 | hue = (RGBLIGHT_RAINBOW_SWIRL_RANGE / RGBLED_NUM * i + current_hue) % 360; |
| 640 | sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]); | 748 | sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]); |
| 641 | } | 749 | } |
| 642 | rgblight_set(); | 750 | rgblight_set(); |
| @@ -651,6 +759,12 @@ void rgblight_effect_rainbow_swirl(uint8_t interval) { | |||
| 651 | } | 759 | } |
| 652 | } | 760 | } |
| 653 | } | 761 | } |
| 762 | #endif | ||
| 763 | |||
| 764 | #ifdef RGBLIGHT_EFFECT_SNAKE | ||
| 765 | __attribute__ ((weak)) | ||
| 766 | const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20}; | ||
| 767 | |||
| 654 | void rgblight_effect_snake(uint8_t interval) { | 768 | void rgblight_effect_snake(uint8_t interval) { |
| 655 | static uint8_t pos = 0; | 769 | static uint8_t pos = 0; |
| 656 | static uint16_t last_timer = 0; | 770 | static uint16_t last_timer = 0; |
| @@ -689,6 +803,12 @@ void rgblight_effect_snake(uint8_t interval) { | |||
| 689 | pos = (pos + 1) % RGBLED_NUM; | 803 | pos = (pos + 1) % RGBLED_NUM; |
| 690 | } | 804 | } |
| 691 | } | 805 | } |
| 806 | #endif | ||
| 807 | |||
| 808 | #ifdef RGBLIGHT_EFFECT_KNIGHT | ||
| 809 | __attribute__ ((weak)) | ||
| 810 | const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {127, 63, 31}; | ||
| 811 | |||
| 692 | void rgblight_effect_knight(uint8_t interval) { | 812 | void rgblight_effect_knight(uint8_t interval) { |
| 693 | static uint16_t last_timer = 0; | 813 | static uint16_t last_timer = 0; |
| 694 | if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) { | 814 | if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) { |
| @@ -730,8 +850,9 @@ void rgblight_effect_knight(uint8_t interval) { | |||
| 730 | increment = -increment; | 850 | increment = -increment; |
| 731 | } | 851 | } |
| 732 | } | 852 | } |
| 853 | #endif | ||
| 733 | 854 | ||
| 734 | 855 | #ifdef RGBLIGHT_EFFECT_CHRISTMAS | |
| 735 | void rgblight_effect_christmas(void) { | 856 | void rgblight_effect_christmas(void) { |
| 736 | static uint16_t current_offset = 0; | 857 | static uint16_t current_offset = 0; |
| 737 | static uint16_t last_timer = 0; | 858 | static uint16_t last_timer = 0; |
| @@ -748,6 +869,11 @@ void rgblight_effect_christmas(void) { | |||
| 748 | } | 869 | } |
| 749 | rgblight_set(); | 870 | rgblight_set(); |
| 750 | } | 871 | } |
| 872 | #endif | ||
| 873 | |||
| 874 | #ifdef RGBLIGHT_EFFECT_RGB_TEST | ||
| 875 | __attribute__ ((weak)) | ||
| 876 | const uint16_t RGBLED_RGBTEST_INTERVALS[] PROGMEM = {1024}; | ||
| 751 | 877 | ||
| 752 | void rgblight_effect_rgbtest(void) { | 878 | void rgblight_effect_rgbtest(void) { |
| 753 | static uint8_t pos = 0; | 879 | static uint8_t pos = 0; |
| @@ -774,7 +900,9 @@ void rgblight_effect_rgbtest(void) { | |||
| 774 | rgblight_setrgb(r, g, b); | 900 | rgblight_setrgb(r, g, b); |
| 775 | pos = (pos + 1) % 3; | 901 | pos = (pos + 1) % 3; |
| 776 | } | 902 | } |
| 903 | #endif | ||
| 777 | 904 | ||
| 905 | #ifdef RGBLIGHT_EFFECT_ALTERNATING | ||
| 778 | void rgblight_effect_alternating(void){ | 906 | void rgblight_effect_alternating(void){ |
| 779 | static uint16_t last_timer = 0; | 907 | static uint16_t last_timer = 0; |
| 780 | static uint16_t pos = 0; | 908 | static uint16_t pos = 0; |
| @@ -784,16 +912,15 @@ void rgblight_effect_alternating(void){ | |||
| 784 | last_timer = timer_read(); | 912 | last_timer = timer_read(); |
| 785 | 913 | ||
| 786 | for(int i = 0; i<RGBLED_NUM; i++){ | 914 | for(int i = 0; i<RGBLED_NUM; i++){ |
| 787 | if(i<RGBLED_NUM/2 && pos){ | 915 | if(i<RGBLED_NUM/2 && pos){ |
| 788 | rgblight_sethsv_at(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, i); | 916 | sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]); |
| 789 | }else if (i>=RGBLED_NUM/2 && !pos){ | 917 | }else if (i>=RGBLED_NUM/2 && !pos){ |
| 790 | rgblight_sethsv_at(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, i); | 918 | sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]); |
| 791 | }else{ | 919 | }else{ |
| 792 | rgblight_sethsv_at(rgblight_config.hue, rgblight_config.sat, 0, i); | 920 | sethsv(rgblight_config.hue, rgblight_config.sat, 0, (LED_TYPE *)&led[i]); |
| 793 | } | 921 | } |
| 794 | } | 922 | } |
| 795 | rgblight_set(); | 923 | rgblight_set(); |
| 796 | pos = (pos + 1) % 2; | 924 | pos = (pos + 1) % 2; |
| 797 | } | 925 | } |
| 798 | 926 | #endif | |
| 799 | #endif /* RGBLIGHT_ANIMATIONS */ | ||
