led_test.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2016 Fred Sundvik
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. #include "led_test.h"
  21. #include "gfx.h"
  22. #include "math.h"
  23. keyframe_animation_t led_test_animation = {
  24. .num_frames = 14,
  25. .loop = true,
  26. .frame_lengths = {
  27. MS2ST(1000), // fade in
  28. MS2ST(1000), // no op (leds on)
  29. MS2ST(1000), // fade out
  30. MS2ST(1000), // crossfade
  31. MS2ST(3000), // left to rigt (outside in)
  32. MS2ST(1000), // crossfade
  33. MS2ST(3000), // top_to_bottom
  34. 0, // mirror leds
  35. MS2ST(1000), // crossfade
  36. MS2ST(3000), // left_to_right (mirrored, so inside out)
  37. MS2ST(1000), // crossfade
  38. MS2ST(3000), // top_to_bottom
  39. 0, // normal leds
  40. MS2ST(1000), // crossfade
  41. },
  42. .frame_functions = {
  43. keyframe_fade_in_all_leds,
  44. keyframe_no_operation,
  45. keyframe_fade_out_all_leds,
  46. keyframe_led_crossfade,
  47. keyframe_led_left_to_right_gradient,
  48. keyframe_led_crossfade,
  49. keyframe_led_top_to_bottom_gradient,
  50. keyframe_mirror_led_orientation,
  51. keyframe_led_crossfade,
  52. keyframe_led_left_to_right_gradient,
  53. keyframe_led_crossfade,
  54. keyframe_led_top_to_bottom_gradient,
  55. keyframe_normal_led_orientation,
  56. keyframe_led_crossfade,
  57. },
  58. };
  59. static uint8_t fade_led_color(keyframe_animation_t* animation, int from, int to) {
  60. int frame_length = animation->frame_lengths[animation->current_frame];
  61. int current_pos = frame_length - animation->time_left_in_frame;
  62. int delta = to - from;
  63. int luma = (delta * current_pos) / frame_length;
  64. luma += from;
  65. return luma;
  66. }
  67. static void keyframe_fade_all_leds_from_to(keyframe_animation_t* animation, uint8_t from, uint8_t to) {
  68. uint8_t luma = fade_led_color(animation, from, to);
  69. color_t color = LUMA2COLOR(luma);
  70. gdispGClear(LED_DISPLAY, color);
  71. }
  72. // TODO: Should be customizable per keyboard
  73. #define NUM_ROWS 7
  74. #define NUM_COLS 7
  75. static uint8_t crossfade_start_frame[NUM_ROWS][NUM_COLS];
  76. static uint8_t crossfade_end_frame[NUM_ROWS][NUM_COLS];
  77. static uint8_t compute_gradient_color(float t, float index, float num) {
  78. float d = fabs(index - t);
  79. if (d > num / 2.0f) {
  80. d = num - d;
  81. }
  82. return (uint8_t)(255.0f * d);
  83. }
  84. bool keyframe_fade_in_all_leds(keyframe_animation_t* animation, visualizer_state_t* state) {
  85. (void)state;
  86. keyframe_fade_all_leds_from_to(animation, 0, 255);
  87. return true;
  88. }
  89. bool keyframe_fade_out_all_leds(keyframe_animation_t* animation, visualizer_state_t* state) {
  90. (void)state;
  91. keyframe_fade_all_leds_from_to(animation, 255, 0);
  92. return true;
  93. }
  94. bool keyframe_led_left_to_right_gradient(keyframe_animation_t* animation, visualizer_state_t* state) {
  95. (void)state;
  96. float frame_length = animation->frame_lengths[animation->current_frame];
  97. float current_pos = frame_length - animation->time_left_in_frame;
  98. float t = current_pos / frame_length;
  99. for (int i=0; i< NUM_COLS; i++) {
  100. uint8_t color = compute_gradient_color(t, i, NUM_COLS);
  101. gdispGDrawLine(LED_DISPLAY, i, 0, i, NUM_ROWS - 1, LUMA2COLOR(color));
  102. }
  103. return true;
  104. }
  105. bool keyframe_led_top_to_bottom_gradient(keyframe_animation_t* animation, visualizer_state_t* state) {
  106. (void)state;
  107. float frame_length = animation->frame_lengths[animation->current_frame];
  108. float current_pos = frame_length - animation->time_left_in_frame;
  109. float t = current_pos / frame_length;
  110. for (int i=0; i< NUM_ROWS; i++) {
  111. uint8_t color = compute_gradient_color(t, i, NUM_ROWS);
  112. gdispGDrawLine(LED_DISPLAY, 0, i, NUM_COLS - 1, i, LUMA2COLOR(color));
  113. }
  114. return true;
  115. }
  116. static void copy_current_led_state(uint8_t* dest) {
  117. for (int i=0;i<NUM_ROWS;i++) {
  118. for (int j=0;j<NUM_COLS;j++) {
  119. dest[i*NUM_COLS + j] = gdispGGetPixelColor(LED_DISPLAY, j, i);
  120. }
  121. }
  122. }
  123. bool keyframe_led_crossfade(keyframe_animation_t* animation, visualizer_state_t* state) {
  124. (void)state;
  125. if (animation->first_update_of_frame) {
  126. copy_current_led_state(&crossfade_start_frame[0][0]);
  127. run_next_keyframe(animation, state);
  128. copy_current_led_state(&crossfade_end_frame[0][0]);
  129. }
  130. for (int i=0;i<NUM_ROWS;i++) {
  131. for (int j=0;j<NUM_COLS;j++) {
  132. color_t color = LUMA2COLOR(fade_led_color(animation, crossfade_start_frame[i][j], crossfade_end_frame[i][j]));
  133. gdispGDrawPixel(LED_DISPLAY, j, i, color);
  134. }
  135. }
  136. return true;
  137. }
  138. bool keyframe_mirror_led_orientation(keyframe_animation_t* animation, visualizer_state_t* state) {
  139. (void)state;
  140. (void)animation;
  141. gdispGSetOrientation(LED_DISPLAY, GDISP_ROTATE_180);
  142. return false;
  143. }
  144. bool keyframe_normal_led_orientation(keyframe_animation_t* animation, visualizer_state_t* state) {
  145. (void)state;
  146. (void)animation;
  147. gdispGSetOrientation(LED_DISPLAY, GDISP_ROTATE_0);
  148. return false;
  149. }