visualizer_user.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. Copyright 2016 Fred Sundvik <fsundvik@gmail.com>
  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. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. // Currently we are assuming that both the backlight and LCD are enabled
  15. // But it's entirely possible to write a custom visualizer that use only
  16. // one of them
  17. #ifndef LCD_BACKLIGHT_ENABLE
  18. #error This visualizer needs that LCD backlight is enabled
  19. #endif
  20. #ifndef LCD_ENABLE
  21. #error This visualizer needs that LCD is enabled
  22. #endif
  23. #include "visualizer.h"
  24. #include "led_test.h"
  25. static const char* welcome_text[] = {"TMK", "Infinity Ergodox"};
  26. // Just an example how to write custom keyframe functions, we could have moved
  27. // all this into the init function
  28. bool display_welcome(keyframe_animation_t* animation, visualizer_state_t* state) {
  29. (void)animation;
  30. // Read the uGFX documentation for information how to use the displays
  31. // http://wiki.ugfx.org/index.php/Main_Page
  32. gdispClear(White);
  33. // You can use static variables for things that can't be found in the animation
  34. // or state structs
  35. gdispDrawString(0, 3, welcome_text[0], state->font_dejavusansbold12, Black);
  36. gdispDrawString(0, 15, welcome_text[1], state->font_dejavusansbold12, Black);
  37. // Always remember to flush the display
  38. gdispFlush();
  39. // you could set the backlight color as well, but we won't do it here, since
  40. // it's part of the following animation
  41. // lcd_backlight_color(hue, saturation, intensity);
  42. // We don't need constant updates, just drawing the screen once is enough
  43. return false;
  44. }
  45. // Feel free to modify the animations below, or even add new ones if needed
  46. // Don't worry, if the startup animation is long, you can use the keyboard like normal
  47. // during that time
  48. static keyframe_animation_t startup_animation = {
  49. .num_frames = 4,
  50. .loop = false,
  51. .frame_lengths = {0, gfxMillisecondsToTicks(1000), gfxMillisecondsToTicks(5000), 0},
  52. .frame_functions = {
  53. display_welcome,
  54. keyframe_animate_backlight_color,
  55. keyframe_no_operation,
  56. enable_visualization
  57. },
  58. };
  59. // The color animation animates the LCD color when you change layers
  60. static keyframe_animation_t color_animation = {
  61. .num_frames = 2,
  62. .loop = false,
  63. // Note that there's a 200 ms no-operation frame,
  64. // this prevents the color from changing when activating the layer
  65. // momentarily
  66. .frame_lengths = {gfxMillisecondsToTicks(200), gfxMillisecondsToTicks(500)},
  67. .frame_functions = {keyframe_no_operation, keyframe_animate_backlight_color},
  68. };
  69. // The LCD animation alternates between the layer name display and a
  70. // bitmap that displays all active layers
  71. static keyframe_animation_t lcd_animation = {
  72. .num_frames = 2,
  73. .loop = true,
  74. .frame_lengths = {gfxMillisecondsToTicks(2000), gfxMillisecondsToTicks(2000)},
  75. .frame_functions = {keyframe_display_layer_text, keyframe_display_layer_bitmap},
  76. };
  77. static keyframe_animation_t suspend_animation = {
  78. .num_frames = 3,
  79. .loop = false,
  80. .frame_lengths = {0, gfxMillisecondsToTicks(1000), 0},
  81. .frame_functions = {
  82. keyframe_display_layer_text,
  83. keyframe_animate_backlight_color,
  84. keyframe_disable_lcd_and_backlight,
  85. },
  86. };
  87. static keyframe_animation_t resume_animation = {
  88. .num_frames = 5,
  89. .loop = false,
  90. .frame_lengths = {0, 0, gfxMillisecondsToTicks(1000), gfxMillisecondsToTicks(5000), 0},
  91. .frame_functions = {
  92. keyframe_enable_lcd_and_backlight,
  93. display_welcome,
  94. keyframe_animate_backlight_color,
  95. keyframe_no_operation,
  96. enable_visualization,
  97. },
  98. };
  99. void initialize_user_visualizer(visualizer_state_t* state) {
  100. // The brightness will be dynamically adjustable in the future
  101. // But for now, change it here.
  102. lcd_backlight_brightness(0x50);
  103. state->current_lcd_color = LCD_COLOR(0x00, 0x00, 0xFF);
  104. state->target_lcd_color = LCD_COLOR(0x10, 0xFF, 0xFF);
  105. start_keyframe_animation(&startup_animation);
  106. start_keyframe_animation(&led_test_animation);
  107. }
  108. void update_user_visualizer_state(visualizer_state_t* state) {
  109. // Add more tests, change the colors and layer texts here
  110. // Usually you want to check the high bits (higher layers first)
  111. // because that's the order layers are processed for keypresses
  112. // You can for check for example:
  113. // state->status.layer
  114. // state->status.default_layer
  115. // state->status.leds (see led.h for available statuses)
  116. if (state->status.layer & 0x8) {
  117. state->target_lcd_color = LCD_COLOR(0xC0, 0xB0, 0xFF);
  118. state->layer_text = "Numpad";
  119. }
  120. else if (state->status.layer & 0x4) {
  121. state->target_lcd_color = LCD_COLOR(0, 0xB0, 0xFF);
  122. state->layer_text = "KBD functions";
  123. }
  124. else if (state->status.layer & 0x2) {
  125. state->target_lcd_color = LCD_COLOR(0x80, 0xB0, 0xFF);
  126. state->layer_text = "Function keys";
  127. }
  128. else {
  129. state->target_lcd_color = LCD_COLOR(0x40, 0xB0, 0xFF);
  130. state->layer_text = "Default";
  131. }
  132. // You can also stop existing animations, and start your custom ones here
  133. // remember that you should normally have only one animation for the LCD
  134. // and one for the background. But you can also combine them if you want.
  135. start_keyframe_animation(&lcd_animation);
  136. start_keyframe_animation(&color_animation);
  137. }
  138. void user_visualizer_suspend(visualizer_state_t* state) {
  139. state->layer_text = "Suspending...";
  140. uint8_t hue = LCD_HUE(state->current_lcd_color);
  141. uint8_t sat = LCD_SAT(state->current_lcd_color);
  142. state->target_lcd_color = LCD_COLOR(hue, sat, 0);
  143. start_keyframe_animation(&suspend_animation);
  144. }
  145. void user_visualizer_resume(visualizer_state_t* state) {
  146. state->current_lcd_color = LCD_COLOR(0x00, 0x00, 0x00);
  147. state->target_lcd_color = LCD_COLOR(0x10, 0xFF, 0xFF);
  148. start_keyframe_animation(&resume_animation);
  149. start_keyframe_animation(&led_test_animation);
  150. }