You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
4.5 KiB

  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. #if RGBLED_NUM >= 2
  31. #define POWER_KEY_OFFSET (RGBLED_NUM / 2)
  32. #define SPACE_OFFSET_MAX (RGBLED_NUM / 2)
  33. #else
  34. #define POWER_KEY_OFFSET 1
  35. #define SPACE_OFFSET_MAX 1
  36. #endif
  37. uint16_t effect_start_timer = 0;
  38. uint8_t user_rgb_mode = 0;
  39. LED_TYPE shadowed_led[RGBLED_NUM] = {{0}};
  40. void start_firey_return(void) {
  41. user_rgb_mode = BREATH_FIRE;
  42. effect_start_timer = timer_read();
  43. for(uint8_t i = 0; i < RGBLED_NUM; i++) {
  44. shadowed_led[i] = led[i];
  45. }
  46. }
  47. /** 0---max
  48. * [___]
  49. * [__/]
  50. * [_/\]
  51. * [/\_]
  52. * [\__]
  53. * [___]
  54. **/
  55. void set_color_for_offsets(uint16_t time_offset, uint16_t space_offset, uint8_t idx) {
  56. float time_progress = (float)time_offset / BREATH_FIRE_TIME;
  57. float space_progress = (float)space_offset / SPACE_OFFSET_MAX;
  58. float progress = time_progress * 5.0 - space_progress;
  59. if(progress > 1.0) {
  60. progress -= 1.0;
  61. progress /= 4.0;
  62. progress = 1.0 - progress;
  63. }
  64. progress = fmax(0.0,progress);
  65. progress *= progress; // squared!
  66. float alpha = (time_progress + 0.1) * 7.0 - space_progress;
  67. alpha = fmin(1.0, alpha*alpha);
  68. LED_TYPE px[1] = {{0}};
  69. sethsv((uint16_t)(fmod(time_progress * 1.5 + space_progress,1.0)*360), 255, (uint8_t)(progress*255),&px[0]);
  70. led[idx].r = alpha * px[0].r + ( 1.0 - alpha) * shadowed_led[idx].r;
  71. led[idx].g = alpha * px[0].g + ( 1.0 - alpha) * shadowed_led[idx].g;
  72. led[idx].b = alpha * px[0].b + ( 1.0 - alpha) * shadowed_led[idx].b;
  73. }
  74. /**
  75. * It's actually a rainbow: a fire curve didn't really look right.
  76. * it's still cool, though!
  77. */
  78. void rgb_mode_breath_fire(void) {
  79. static uint16_t last_timer = 0;
  80. if(!last_timer) last_timer = timer_read();
  81. uint16_t this_timer = timer_read();
  82. // too soon. don't spam updates
  83. if(this_timer - last_timer < ANIMATION_STEP_INTERVAL) return;
  84. uint16_t elapsed = this_timer - effect_start_timer;
  85. last_timer = this_timer;
  86. if(elapsed >= BREATH_FIRE_TIME) {
  87. // complete
  88. user_rgb_mode = FADE_BACK;
  89. effect_start_timer = this_timer;
  90. } else {
  91. // linear fade
  92. for(uint16_t i = 0; i < RGBLED_NUM; i++) {
  93. uint16_t space_offset = ABSDIFF(i,POWER_KEY_OFFSET);
  94. if(space_offset > SPACE_OFFSET_MAX) space_offset = RGBLED_NUM - space_offset;
  95. set_color_for_offsets(elapsed, space_offset, i);
  96. }
  97. rgblight_set();
  98. }
  99. }
  100. void rgb_mode_fade_back(void) {
  101. static uint16_t last_timer = 0;
  102. if(!last_timer) last_timer = timer_read();
  103. uint16_t this_timer = timer_read();
  104. // too soon. don't spam updates
  105. if(this_timer - last_timer < ANIMATION_STEP_INTERVAL) return;
  106. uint16_t elapsed = this_timer - effect_start_timer;
  107. last_timer = this_timer;
  108. float progress = (float)elapsed / FADE_BACK_TIME;
  109. progress = fmin(1.0,progress);
  110. for(uint8_t i = 0; i < RGBLED_NUM; i++) {
  111. led[i].r = shadowed_led[i].r * progress;
  112. led[i].g = shadowed_led[i].g * progress;
  113. led[i].b = shadowed_led[i].b * progress;
  114. }
  115. rgblight_set();
  116. if(elapsed >= FADE_BACK_TIME) user_rgb_mode = 0;
  117. }
  118. /** called when layer state or vstate has changed */
  119. __attribute__ ((weak))
  120. void set_state_leds(void) {
  121. return;
  122. }
  123. void matrix_scan_user(void) {
  124. static uint32_t last_layer = 0;
  125. static uint32_t last_vstate = 0;
  126. if(last_layer != layer_state || last_vstate != vstate) set_state_leds();
  127. last_layer = layer_state;
  128. last_vstate = vstate;
  129. switch (user_rgb_mode) {
  130. case BREATH_FIRE:
  131. rgb_mode_breath_fire();
  132. break;
  133. case FADE_BACK:
  134. rgb_mode_fade_back();
  135. break;
  136. }
  137. matrix_scan_keymap();
  138. }
  139. #else
  140. void start_firey_return(void) {}
  141. #endif