diff options
| author | Takeshi ISHII <2170248+mtei@users.noreply.github.com> | 2019-05-16 13:11:28 +0900 |
|---|---|---|
| committer | MechMerlin <30334081+mechmerlin@users.noreply.github.com> | 2019-05-15 21:11:28 -0700 |
| commit | 670a9b7f83dacf2b0e9fb42756935a77598c6677 (patch) | |
| tree | e265e0b8f4dbc2df2e0eff45c474a3ce9423b122 /quantum/rgblight.c | |
| parent | acd3e79add2d47ab664b831936e5b6e71d3b8e15 (diff) | |
| download | qmk_firmware-670a9b7f83dacf2b0e9fb42756935a77598c6677.tar.gz qmk_firmware-670a9b7f83dacf2b0e9fb42756935a77598c6677.zip | |
Add effect range to rgblight.c (#5856)
* add rgblight_set_effect_range()
* implement effect range
* Arrange the order of function list in rgblight.h .
* update docs/feature_rgblight.md
* fix RGBLIGHT_RAINBOW_SWIRL_RANGE default value
* add example code about Utility Functions
* add example code about direct operation functions
* When RGBLIGHT_SPLIT is defined, the following function has no meaning and is invalidated.
* rgblight_setrgb_master(r, g, b)
* rgblight_setrgb_slave(r, g, b)
* rgblight_sethsv_master(h, s, v)
* rgblight_sethsv_slave(h, s, v)
* add temporary test code for rgblight_set_effect_range
* fix rgblight_effect_knight() bug
* Test End. Revert "add temporary test code for rgblight_set_effect_range"
This reverts commit 5680cddd012d68b2db75a532862a7fef250f8973.
Diffstat (limited to 'quantum/rgblight.c')
| -rw-r--r-- | quantum/rgblight.c | 117 |
1 files changed, 67 insertions, 50 deletions
diff --git a/quantum/rgblight.c b/quantum/rgblight.c index 77772e292..75e4ef0d8 100644 --- a/quantum/rgblight.c +++ b/quantum/rgblight.c | |||
| @@ -101,19 +101,35 @@ LED_TYPE led[RGBLED_NUM]; | |||
| 101 | 101 | ||
| 102 | static uint8_t clipping_start_pos = 0; | 102 | static uint8_t clipping_start_pos = 0; |
| 103 | static uint8_t clipping_num_leds = RGBLED_NUM; | 103 | static uint8_t clipping_num_leds = RGBLED_NUM; |
| 104 | static uint8_t effect_start_pos = 0; | ||
| 105 | static uint8_t effect_end_pos = RGBLED_NUM; | ||
| 106 | static uint8_t effect_num_leds = RGBLED_NUM; | ||
| 104 | 107 | ||
| 105 | void rgblight_set_clipping_range(uint8_t start_pos, uint8_t num_leds) { | 108 | void rgblight_set_clipping_range(uint8_t start_pos, uint8_t num_leds) { |
| 106 | clipping_start_pos = start_pos; | 109 | clipping_start_pos = start_pos; |
| 107 | clipping_num_leds = num_leds; | 110 | clipping_num_leds = num_leds; |
| 108 | } | 111 | } |
| 109 | 112 | ||
| 113 | void rgblight_set_effect_range(uint8_t start_pos, uint8_t num_leds) { | ||
| 114 | if (start_pos >= RGBLED_NUM) return; | ||
| 115 | if (start_pos + num_leds > RGBLED_NUM) return; | ||
| 116 | effect_start_pos = start_pos; | ||
| 117 | effect_end_pos = start_pos + num_leds; | ||
| 118 | effect_num_leds = num_leds; | ||
| 119 | } | ||
| 110 | 120 | ||
| 111 | void sethsv(uint8_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1) { | 121 | void sethsv_raw(uint8_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1) { |
| 112 | HSV hsv = { hue, sat, val > RGBLIGHT_LIMIT_VAL ? RGBLIGHT_LIMIT_VAL : val }; | 122 | HSV hsv = { hue, sat, val }; |
| 113 | RGB rgb = hsv_to_rgb(hsv); | 123 | RGB rgb = hsv_to_rgb(hsv); |
| 114 | setrgb(rgb.r, rgb.g, rgb.b, led1); | 124 | setrgb(rgb.r, rgb.g, rgb.b, led1); |
| 115 | } | 125 | } |
| 116 | 126 | ||
| 127 | void sethsv(uint8_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1) { | ||
| 128 | sethsv_raw( hue, sat, | ||
| 129 | val > RGBLIGHT_LIMIT_VAL ? RGBLIGHT_LIMIT_VAL : val, | ||
| 130 | led1); | ||
| 131 | } | ||
| 132 | |||
| 117 | void setrgb(uint8_t r, uint8_t g, uint8_t b, LED_TYPE *led1) { | 133 | void setrgb(uint8_t r, uint8_t g, uint8_t b, LED_TYPE *led1) { |
| 118 | (*led1).r = r; | 134 | (*led1).r = r; |
| 119 | (*led1).g = g; | 135 | (*led1).g = g; |
| @@ -501,15 +517,15 @@ void rgblight_sethsv_eeprom_helper(uint8_t hue, uint8_t sat, uint8_t val, bool w | |||
| 501 | #else | 517 | #else |
| 502 | uint8_t range = RGBLED_GRADIENT_RANGES[delta / 2]; | 518 | uint8_t range = RGBLED_GRADIENT_RANGES[delta / 2]; |
| 503 | #endif | 519 | #endif |
| 504 | for (uint8_t i = 0; i < RGBLED_NUM; i++) { | 520 | for (uint8_t i = 0; i < effect_num_leds; i++) { |
| 505 | uint8_t _hue = ((uint16_t)i * (uint16_t)range) / RGBLED_NUM; | 521 | uint8_t _hue = ((uint16_t)i * (uint16_t)range) / effect_num_leds; |
| 506 | if (direction) { | 522 | if (direction) { |
| 507 | _hue = hue + _hue; | 523 | _hue = hue + _hue; |
| 508 | } else { | 524 | } else { |
| 509 | _hue = hue - _hue; | 525 | _hue = hue - _hue; |
| 510 | } | 526 | } |
| 511 | dprintf("rgblight rainbow set hsv: %d,%d,%d,%u\n", i, _hue, direction, range); | 527 | dprintf("rgblight rainbow set hsv: %d,%d,%d,%u\n", i, _hue, direction, range); |
| 512 | sethsv(_hue, sat, val, (LED_TYPE *)&led[i]); | 528 | sethsv(_hue, sat, val, (LED_TYPE *)&led[i + effect_start_pos]); |
| 513 | } | 529 | } |
| 514 | rgblight_set(); | 530 | rgblight_set(); |
| 515 | } | 531 | } |
| @@ -557,7 +573,7 @@ uint8_t rgblight_get_val(void) { | |||
| 557 | void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) { | 573 | void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) { |
| 558 | if (!rgblight_config.enable) { return; } | 574 | if (!rgblight_config.enable) { return; } |
| 559 | 575 | ||
| 560 | for (uint8_t i = 0; i < RGBLED_NUM; i++) { | 576 | for (uint8_t i = effect_start_pos; i < effect_end_pos; i++) { |
| 561 | led[i].r = r; | 577 | led[i].r = r; |
| 562 | led[i].g = g; | 578 | led[i].g = g; |
| 563 | led[i].b = b; | 579 | led[i].b = b; |
| @@ -615,6 +631,7 @@ void rgblight_sethsv_range(uint8_t hue, uint8_t sat, uint8_t val, uint8_t start, | |||
| 615 | rgblight_setrgb_range(tmp_led.r, tmp_led.g, tmp_led.b, start, end); | 631 | rgblight_setrgb_range(tmp_led.r, tmp_led.g, tmp_led.b, start, end); |
| 616 | } | 632 | } |
| 617 | 633 | ||
| 634 | #ifndef RGBLIGHT_SPLIT | ||
| 618 | void rgblight_setrgb_master(uint8_t r, uint8_t g, uint8_t b) { | 635 | void rgblight_setrgb_master(uint8_t r, uint8_t g, uint8_t b) { |
| 619 | rgblight_setrgb_range(r, g, b, 0 , (uint8_t) RGBLED_NUM/2); | 636 | rgblight_setrgb_range(r, g, b, 0 , (uint8_t) RGBLED_NUM/2); |
| 620 | } | 637 | } |
| @@ -630,36 +647,34 @@ void rgblight_sethsv_master(uint8_t hue, uint8_t sat, uint8_t val) { | |||
| 630 | void rgblight_sethsv_slave(uint8_t hue, uint8_t sat, uint8_t val) { | 647 | void rgblight_sethsv_slave(uint8_t hue, uint8_t sat, uint8_t val) { |
| 631 | rgblight_sethsv_range(hue, sat, val, (uint8_t) RGBLED_NUM/2, (uint8_t) RGBLED_NUM); | 648 | rgblight_sethsv_range(hue, sat, val, (uint8_t) RGBLED_NUM/2, (uint8_t) RGBLED_NUM); |
| 632 | } | 649 | } |
| 650 | #endif // ifndef RGBLIGHT_SPLIT | ||
| 633 | 651 | ||
| 634 | #ifndef RGBLIGHT_CUSTOM_DRIVER | 652 | #ifndef RGBLIGHT_CUSTOM_DRIVER |
| 635 | void rgblight_set(void) { | 653 | void rgblight_set(void) { |
| 636 | LED_TYPE *start_led = led + clipping_start_pos; | 654 | LED_TYPE *start_led; |
| 637 | uint16_t num_leds = clipping_num_leds; | 655 | uint16_t num_leds = clipping_num_leds; |
| 638 | if (rgblight_config.enable) { | 656 | |
| 639 | #ifdef RGBLIGHT_LED_MAP | 657 | if (!rgblight_config.enable) { |
| 640 | LED_TYPE led0[RGBLED_NUM]; | 658 | for (uint8_t i = effect_start_pos; i < effect_end_pos; i++) { |
| 641 | for(uint8_t i = 0; i < RGBLED_NUM; i++) { | ||
| 642 | led0[i] = led[pgm_read_byte(&led_map[i])]; | ||
| 643 | } | ||
| 644 | start_led = led0 + clipping_start_pos; | ||
| 645 | #endif | ||
| 646 | #ifdef RGBW | ||
| 647 | ws2812_setleds_rgbw(start_led, num_leds); | ||
| 648 | #else | ||
| 649 | ws2812_setleds(start_led, num_leds); | ||
| 650 | #endif | ||
| 651 | } else { | ||
| 652 | for (uint8_t i = 0; i < RGBLED_NUM; i++) { | ||
| 653 | led[i].r = 0; | 659 | led[i].r = 0; |
| 654 | led[i].g = 0; | 660 | led[i].g = 0; |
| 655 | led[i].b = 0; | 661 | led[i].b = 0; |
| 656 | } | 662 | } |
| 657 | #ifdef RGBW | ||
| 658 | ws2812_setleds_rgbw(start_led, num_leds); | ||
| 659 | #else | ||
| 660 | ws2812_setleds(start_led, num_leds); | ||
| 661 | #endif | ||
| 662 | } | 663 | } |
| 664 | #ifdef RGBLIGHT_LED_MAP | ||
| 665 | LED_TYPE led0[RGBLED_NUM]; | ||
| 666 | for(uint8_t i = 0; i < RGBLED_NUM; i++) { | ||
| 667 | led0[i] = led[pgm_read_byte(&led_map[i])]; | ||
| 668 | } | ||
| 669 | start_led = led0 + clipping_start_pos; | ||
| 670 | #else | ||
| 671 | start_led = led + clipping_start_pos; | ||
| 672 | #endif | ||
| 673 | #ifdef RGBW | ||
| 674 | ws2812_setleds_rgbw(start_led, num_leds); | ||
| 675 | #else | ||
| 676 | ws2812_setleds(start_led, num_leds); | ||
| 677 | #endif | ||
| 663 | } | 678 | } |
| 664 | #endif | 679 | #endif |
| 665 | 680 | ||
| @@ -926,9 +941,9 @@ void rgblight_effect_rainbow_swirl(animation_status_t *anim) { | |||
| 926 | uint8_t hue; | 941 | uint8_t hue; |
| 927 | uint8_t i; | 942 | uint8_t i; |
| 928 | 943 | ||
| 929 | for (i = 0; i < RGBLED_NUM; i++) { | 944 | for (i = 0; i < effect_num_leds; i++) { |
| 930 | hue = (RGBLIGHT_RAINBOW_SWIRL_RANGE / RGBLED_NUM * i + anim->current_hue); | 945 | hue = (RGBLIGHT_RAINBOW_SWIRL_RANGE / effect_num_leds * i + anim->current_hue); |
| 931 | sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]); | 946 | sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i + effect_start_pos]); |
| 932 | } | 947 | } |
| 933 | rgblight_set(); | 948 | rgblight_set(); |
| 934 | 949 | ||
| @@ -957,7 +972,7 @@ void rgblight_effect_snake(animation_status_t *anim) { | |||
| 957 | #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC) | 972 | #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC) |
| 958 | if (anim->pos == 0) { // restart signal | 973 | if (anim->pos == 0) { // restart signal |
| 959 | if (increment == 1) { | 974 | if (increment == 1) { |
| 960 | pos = RGBLED_NUM - 1; | 975 | pos = effect_num_leds - 1; |
| 961 | } else { | 976 | } else { |
| 962 | pos = 0; | 977 | pos = 0; |
| 963 | } | 978 | } |
| @@ -965,26 +980,27 @@ void rgblight_effect_snake(animation_status_t *anim) { | |||
| 965 | } | 980 | } |
| 966 | #endif | 981 | #endif |
| 967 | 982 | ||
| 968 | for (i = 0; i < RGBLED_NUM; i++) { | 983 | for (i = 0; i < effect_num_leds; i++) { |
| 969 | led[i].r = 0; | 984 | LED_TYPE *ledp = led + i + effect_start_pos; |
| 970 | led[i].g = 0; | 985 | ledp->r = 0; |
| 971 | led[i].b = 0; | 986 | ledp->g = 0; |
| 987 | ledp->b = 0; | ||
| 972 | for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) { | 988 | for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) { |
| 973 | k = pos + j * increment; | 989 | k = pos + j * increment; |
| 974 | if (k < 0) { | 990 | if (k < 0) { |
| 975 | k = k + RGBLED_NUM; | 991 | k = k + effect_num_leds; |
| 976 | } | 992 | } |
| 977 | if (i == k) { | 993 | if (i == k) { |
| 978 | sethsv(rgblight_config.hue, rgblight_config.sat, | 994 | sethsv(rgblight_config.hue, rgblight_config.sat, |
| 979 | (uint8_t)(rgblight_config.val*(RGBLIGHT_EFFECT_SNAKE_LENGTH-j)/RGBLIGHT_EFFECT_SNAKE_LENGTH), | 995 | (uint8_t)(rgblight_config.val*(RGBLIGHT_EFFECT_SNAKE_LENGTH-j)/RGBLIGHT_EFFECT_SNAKE_LENGTH), |
| 980 | (LED_TYPE *)&led[i]); | 996 | ledp); |
| 981 | } | 997 | } |
| 982 | } | 998 | } |
| 983 | } | 999 | } |
| 984 | rgblight_set(); | 1000 | rgblight_set(); |
| 985 | if (increment == 1) { | 1001 | if (increment == 1) { |
| 986 | if (pos - 1 < 0) { | 1002 | if (pos - 1 < 0) { |
| 987 | pos = RGBLED_NUM - 1; | 1003 | pos = effect_num_leds - 1; |
| 988 | #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC) | 1004 | #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC) |
| 989 | anim->pos = 0; | 1005 | anim->pos = 0; |
| 990 | #endif | 1006 | #endif |
| @@ -995,7 +1011,7 @@ void rgblight_effect_snake(animation_status_t *anim) { | |||
| 995 | #endif | 1011 | #endif |
| 996 | } | 1012 | } |
| 997 | } else { | 1013 | } else { |
| 998 | pos = (pos + 1) % RGBLED_NUM; | 1014 | pos = (pos + 1) % effect_num_leds; |
| 999 | #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC) | 1015 | #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC) |
| 1000 | anim->pos = pos; | 1016 | anim->pos = pos; |
| 1001 | #endif | 1017 | #endif |
| @@ -1023,14 +1039,14 @@ void rgblight_effect_knight(animation_status_t *anim) { | |||
| 1023 | } | 1039 | } |
| 1024 | #endif | 1040 | #endif |
| 1025 | // Set all the LEDs to 0 | 1041 | // Set all the LEDs to 0 |
| 1026 | for (i = 0; i < RGBLED_NUM; i++) { | 1042 | for (i = effect_start_pos; i < effect_end_pos; i++) { |
| 1027 | led[i].r = 0; | 1043 | led[i].r = 0; |
| 1028 | led[i].g = 0; | 1044 | led[i].g = 0; |
| 1029 | led[i].b = 0; | 1045 | led[i].b = 0; |
| 1030 | } | 1046 | } |
| 1031 | // Determine which LEDs should be lit up | 1047 | // Determine which LEDs should be lit up |
| 1032 | for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) { | 1048 | for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) { |
| 1033 | cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM; | 1049 | cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % effect_num_leds + effect_start_pos; |
| 1034 | 1050 | ||
| 1035 | if (i >= low_bound && i <= high_bound) { | 1051 | if (i >= low_bound && i <= high_bound) { |
| 1036 | sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]); | 1052 | sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]); |
| @@ -1064,9 +1080,9 @@ void rgblight_effect_christmas(animation_status_t *anim) { | |||
| 1064 | uint8_t i; | 1080 | uint8_t i; |
| 1065 | 1081 | ||
| 1066 | anim->current_offset = (anim->current_offset + 1) % 2; | 1082 | anim->current_offset = (anim->current_offset + 1) % 2; |
| 1067 | for (i = 0; i < RGBLED_NUM; i++) { | 1083 | for (i = 0; i < effect_num_leds; i++) { |
| 1068 | hue = 0 + ((i/RGBLIGHT_EFFECT_CHRISTMAS_STEP + anim->current_offset) % 2) * 85; | 1084 | hue = 0 + ((i/RGBLIGHT_EFFECT_CHRISTMAS_STEP + anim->current_offset) % 2) * 85; |
| 1069 | sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]); | 1085 | sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i + effect_start_pos]); |
| 1070 | } | 1086 | } |
| 1071 | rgblight_set(); | 1087 | rgblight_set(); |
| 1072 | } | 1088 | } |
| @@ -1099,13 +1115,14 @@ void rgblight_effect_rgbtest(animation_status_t *anim) { | |||
| 1099 | #ifdef RGBLIGHT_EFFECT_ALTERNATING | 1115 | #ifdef RGBLIGHT_EFFECT_ALTERNATING |
| 1100 | void rgblight_effect_alternating(animation_status_t *anim) { | 1116 | void rgblight_effect_alternating(animation_status_t *anim) { |
| 1101 | 1117 | ||
| 1102 | for(int i = 0; i<RGBLED_NUM; i++){ | 1118 | for (int i = 0; i < effect_num_leds; i++) { |
| 1103 | if(i<RGBLED_NUM/2 && anim->pos){ | 1119 | LED_TYPE *ledp = led + i + effect_start_pos; |
| 1104 | sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]); | 1120 | if (i<effect_num_leds/2 && anim->pos) { |
| 1105 | }else if (i>=RGBLED_NUM/2 && !anim->pos){ | 1121 | sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, ledp); |
| 1106 | sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]); | 1122 | } else if (i>=effect_num_leds/2 && !anim->pos) { |
| 1107 | }else{ | 1123 | sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, ledp); |
| 1108 | sethsv(rgblight_config.hue, rgblight_config.sat, 0, (LED_TYPE *)&led[i]); | 1124 | } else { |
| 1125 | sethsv(rgblight_config.hue, rgblight_config.sat, 0, ledp); | ||
| 1109 | } | 1126 | } |
| 1110 | } | 1127 | } |
| 1111 | rgblight_set(); | 1128 | rgblight_set(); |
