fancylighting.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* Copyright 2015-2017 Christon DeWan
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifdef RGBLIGHT_ENABLE
  17. #include <math.h>
  18. #include "rgblight.h"
  19. #include "color.h"
  20. #include "fancylighting.h"
  21. __attribute__ ((weak))
  22. void matrix_scan_keymap(void) {
  23. // override me, if you want.
  24. return;
  25. }
  26. #define ABSDIFF(a,b) ((a)>(b)?(a)-(b):(b)-(a))
  27. #define FADE_BACK_TIME 500
  28. #define BREATH_FIRE_TIME 1000
  29. #define ANIMATION_STEP_INTERVAL 20
  30. #define POWER_KEY_OFFSET (RGBLED_NUM / 2)
  31. #define SPACE_OFFSET_MAX (RGBLED_NUM / 2)
  32. uint16_t effect_start_timer = 0;
  33. uint8_t user_rgb_mode = 0;
  34. LED_TYPE shadowed_led[RGBLED_NUM] = {0};
  35. void start_firey_return(void) {
  36. user_rgb_mode = BREATH_FIRE;
  37. effect_start_timer = timer_read();
  38. for(uint8_t i = 0; i < RGBLED_NUM; i++) {
  39. shadowed_led[i] = led[i];
  40. }
  41. }
  42. /** 0---max
  43. * [___]
  44. * [__/]
  45. * [_/\]
  46. * [/\_]
  47. * [\__]
  48. * [___]
  49. **/
  50. void set_color_for_offsets(uint16_t time_offset, uint16_t space_offset, uint8_t idx) {
  51. float time_progress = (float)time_offset / BREATH_FIRE_TIME;
  52. float space_progress = (float)space_offset / SPACE_OFFSET_MAX;
  53. float progress = time_progress * 5.0 - space_progress;
  54. if(progress > 1.0) {
  55. progress -= 1.0;
  56. progress /= 4.0;
  57. progress = 1.0 - progress;
  58. }
  59. progress = fmax(0.0,progress);
  60. progress *= progress; // squared!
  61. float alpha = (time_progress + 0.1) * 7.0 - space_progress;
  62. alpha = fmin(1.0, alpha*alpha);
  63. LED_TYPE px[1] = {0};
  64. sethsv((uint16_t)(fmod(time_progress * 1.5 + space_progress,1.0)*360), 255, (uint8_t)(progress*255),&px[0]);
  65. led[idx].r = alpha * px[0].r + ( 1.0 - alpha) * shadowed_led[idx].r;
  66. led[idx].g = alpha * px[0].g + ( 1.0 - alpha) * shadowed_led[idx].g;
  67. led[idx].b = alpha * px[0].b + ( 1.0 - alpha) * shadowed_led[idx].b;
  68. }
  69. /**
  70. * It's actually a rainbow: a fire curve didn't really look right.
  71. * it's still cool, though!
  72. */
  73. void rgb_mode_breath_fire(void) {
  74. static uint16_t last_timer = 0;
  75. if(!last_timer) last_timer = timer_read();
  76. uint16_t this_timer = timer_read();
  77. // too soon. don't spam updates
  78. if(this_timer - last_timer < ANIMATION_STEP_INTERVAL) return;
  79. uint16_t elapsed = this_timer - effect_start_timer;
  80. last_timer = this_timer;
  81. if(elapsed >= BREATH_FIRE_TIME) {
  82. // complete
  83. user_rgb_mode = FADE_BACK;
  84. effect_start_timer = this_timer;
  85. } else {
  86. // linear fade
  87. for(uint16_t i = 0; i < RGBLED_NUM; i++) {
  88. uint16_t space_offset = ABSDIFF(i,POWER_KEY_OFFSET);
  89. if(space_offset > SPACE_OFFSET_MAX) space_offset = RGBLED_NUM - space_offset;
  90. set_color_for_offsets(elapsed, space_offset, i);
  91. }
  92. rgblight_set();
  93. }
  94. }
  95. void rgb_mode_fade_back(void) {
  96. static uint16_t last_timer = 0;
  97. if(!last_timer) last_timer = timer_read();
  98. uint16_t this_timer = timer_read();
  99. // too soon. don't spam updates
  100. if(this_timer - last_timer < ANIMATION_STEP_INTERVAL) return;
  101. uint16_t elapsed = this_timer - effect_start_timer;
  102. last_timer = this_timer;
  103. float progress = (float)elapsed / FADE_BACK_TIME;
  104. progress = fmin(1.0,progress);
  105. for(uint8_t i = 0; i < RGBLED_NUM; i++) {
  106. led[i].r = shadowed_led[i].r * progress;
  107. led[i].g = shadowed_led[i].g * progress;
  108. led[i].b = shadowed_led[i].b * progress;
  109. }
  110. rgblight_set();
  111. if(elapsed >= FADE_BACK_TIME) user_rgb_mode = 0;
  112. }
  113. /** called when layer state or vstate has changed */
  114. __attribute__ ((weak))
  115. void set_state_leds(void) {
  116. return;
  117. }
  118. void matrix_scan_user(void) {
  119. static uint32_t last_layer = 0;
  120. static uint32_t last_vstate = 0;
  121. if(last_layer != layer_state || last_vstate != vstate) set_state_leds();
  122. last_layer = layer_state;
  123. last_vstate = vstate;
  124. switch (user_rgb_mode) {
  125. case BREATH_FIRE:
  126. rgb_mode_breath_fire();
  127. break;
  128. case FADE_BACK:
  129. rgb_mode_fade_back();
  130. break;
  131. }
  132. matrix_scan_keymap();
  133. }
  134. #else
  135. void start_firey_return(void) {}
  136. #endif