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.

328 lines
11 KiB

  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 "visualizer_keyframes.h"
  25. #include "lcd_keyframes.h"
  26. #include "lcd_backlight_keyframes.h"
  27. #include "default_animations.h"
  28. static const uint32_t logo_background_color = LCD_COLOR(0x00, 0x00, 0xFF);
  29. static const uint32_t initial_color = LCD_COLOR(0, 0, 0);
  30. static const uint32_t led_emulation_colors[4] = {
  31. LCD_COLOR(0, 0, 0),
  32. LCD_COLOR(255, 255, 255),
  33. LCD_COLOR(84, 255, 255),
  34. LCD_COLOR(168, 255, 255),
  35. };
  36. static uint32_t next_led_target_color = 0;
  37. typedef enum {
  38. LCD_STATE_INITIAL,
  39. LCD_STATE_LAYER_BITMAP,
  40. LCD_STATE_BITMAP_AND_LEDS,
  41. } lcd_state_t;
  42. static lcd_state_t lcd_state = LCD_STATE_INITIAL;
  43. typedef struct {
  44. uint8_t led_on;
  45. uint8_t led1;
  46. uint8_t led2;
  47. uint8_t led3;
  48. } visualizer_user_data_t;
  49. // Don't access from visualization function, use the visualizer state instead
  50. static visualizer_user_data_t user_data_keyboard = {
  51. .led_on = 0,
  52. .led1 = LED_BRIGHTNESS_HI,
  53. .led2 = LED_BRIGHTNESS_HI,
  54. .led3 = LED_BRIGHTNESS_HI,
  55. };
  56. _Static_assert(sizeof(visualizer_user_data_t) <= VISUALIZER_USER_DATA_SIZE,
  57. "Please increase the VISUALIZER_USER_DATA_SIZE");
  58. // Feel free to modify the animations below, or even add new ones if needed
  59. // The color animation animates the LCD color when you change layers
  60. static keyframe_animation_t one_led_color = {
  61. .num_frames = 1,
  62. .loop = false,
  63. .frame_lengths = {gfxMillisecondsToTicks(0)},
  64. .frame_functions = {lcd_backlight_keyframe_set_color},
  65. };
  66. bool swap_led_target_color(keyframe_animation_t* animation, visualizer_state_t* state) {
  67. uint32_t temp = next_led_target_color;
  68. next_led_target_color = state->target_lcd_color;
  69. state->target_lcd_color = temp;
  70. return false;
  71. }
  72. // The color animation animates the LCD color when you change layers
  73. static keyframe_animation_t two_led_colors = {
  74. .num_frames = 2,
  75. .loop = true,
  76. .frame_lengths = {gfxMillisecondsToTicks(1000), gfxMillisecondsToTicks(0)},
  77. .frame_functions = {lcd_backlight_keyframe_set_color, swap_led_target_color},
  78. };
  79. // The LCD animation alternates between the layer name display and a
  80. // bitmap that displays all active layers
  81. static keyframe_animation_t lcd_bitmap_animation = {
  82. .num_frames = 1,
  83. .loop = false,
  84. .frame_lengths = {gfxMillisecondsToTicks(0)},
  85. .frame_functions = {lcd_keyframe_display_layer_bitmap},
  86. };
  87. static keyframe_animation_t lcd_bitmap_leds_animation = {
  88. .num_frames = 2,
  89. .loop = true,
  90. .frame_lengths = {gfxMillisecondsToTicks(2000), gfxMillisecondsToTicks(2000)},
  91. .frame_functions = {lcd_keyframe_display_layer_bitmap, lcd_keyframe_display_led_states},
  92. };
  93. void initialize_user_visualizer(visualizer_state_t* state) {
  94. // The brightness will be dynamically adjustable in the future
  95. // But for now, change it here.
  96. lcd_backlight_brightness(130);
  97. state->current_lcd_color = initial_color;
  98. state->target_lcd_color = logo_background_color;
  99. lcd_state = LCD_STATE_INITIAL;
  100. start_keyframe_animation(&default_startup_animation);
  101. }
  102. static inline bool is_led_on(visualizer_user_data_t* user_data, uint8_t num) {
  103. return user_data->led_on & (1u << num);
  104. }
  105. static uint8_t get_led_index_master(visualizer_user_data_t* user_data) {
  106. for (int i=0; i < 3; i++) {
  107. if (is_led_on(user_data, i)) {
  108. return i + 1;
  109. }
  110. }
  111. return 0;
  112. }
  113. static uint8_t get_led_index_slave(visualizer_user_data_t* user_data) {
  114. uint8_t master_index = get_led_index_master(user_data);
  115. if (master_index!=0) {
  116. for (int i=master_index; i < 3; i++) {
  117. if (is_led_on(user_data, i)) {
  118. return i + 1;
  119. }
  120. }
  121. }
  122. return 0;
  123. }
  124. static uint8_t get_secondary_led_index(visualizer_user_data_t* user_data) {
  125. if (is_led_on(user_data, 0) &&
  126. is_led_on(user_data, 1) &&
  127. is_led_on(user_data, 2)) {
  128. return 3;
  129. }
  130. return 0;
  131. }
  132. static uint8_t get_brightness(visualizer_user_data_t* user_data, uint8_t index) {
  133. switch (index) {
  134. case 1:
  135. return user_data->led1;
  136. case 2:
  137. return user_data->led2;
  138. case 3:
  139. return user_data->led3;
  140. }
  141. return 0;
  142. }
  143. static void update_emulated_leds(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
  144. visualizer_user_data_t* user_data_new = (visualizer_user_data_t*)state->status.user_data;
  145. visualizer_user_data_t* user_data_old = (visualizer_user_data_t*)prev_status->user_data;
  146. uint8_t new_index;
  147. uint8_t old_index;
  148. if (is_keyboard_master()) {
  149. new_index = get_led_index_master(user_data_new);
  150. old_index = get_led_index_master(user_data_old);
  151. }
  152. else {
  153. new_index = get_led_index_slave(user_data_new);
  154. old_index = get_led_index_slave(user_data_old);
  155. }
  156. uint8_t new_secondary_index = get_secondary_led_index(user_data_new);
  157. uint8_t old_secondary_index = get_secondary_led_index(user_data_old);
  158. uint8_t old_brightness = get_brightness(user_data_old, old_index);
  159. uint8_t new_brightness = get_brightness(user_data_new, new_index);
  160. uint8_t old_secondary_brightness = get_brightness(user_data_old, old_secondary_index);
  161. uint8_t new_secondary_brightness = get_brightness(user_data_new, new_secondary_index);
  162. if (lcd_state == LCD_STATE_INITIAL ||
  163. new_index != old_index ||
  164. new_secondary_index != old_secondary_index ||
  165. new_brightness != old_brightness ||
  166. new_secondary_brightness != old_secondary_brightness) {
  167. if (new_secondary_index != 0) {
  168. state->target_lcd_color = change_lcd_color_intensity(
  169. led_emulation_colors[new_index], new_brightness);
  170. next_led_target_color = change_lcd_color_intensity(
  171. led_emulation_colors[new_secondary_index], new_secondary_brightness);
  172. stop_keyframe_animation(&one_led_color);
  173. start_keyframe_animation(&two_led_colors);
  174. } else {
  175. state->target_lcd_color = change_lcd_color_intensity(
  176. led_emulation_colors[new_index], new_brightness);
  177. stop_keyframe_animation(&two_led_colors);
  178. start_keyframe_animation(&one_led_color);
  179. }
  180. }
  181. }
  182. static void update_lcd_text(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
  183. if (state->status.leds) {
  184. if (lcd_state != LCD_STATE_BITMAP_AND_LEDS ||
  185. state->status.leds != prev_status->leds ||
  186. state->status.layer != prev_status->layer ||
  187. state->status.default_layer != prev_status->default_layer) {
  188. // NOTE: that it doesn't matter if the animation isn't playing, stop will do nothing in that case
  189. stop_keyframe_animation(&lcd_bitmap_animation);
  190. lcd_state = LCD_STATE_BITMAP_AND_LEDS;
  191. // For information:
  192. // The logic in this function makes sure that this doesn't happen, but if you call start on an
  193. // animation that is already playing it will be restarted.
  194. start_keyframe_animation(&lcd_bitmap_leds_animation);
  195. }
  196. } else {
  197. if (lcd_state != LCD_STATE_LAYER_BITMAP ||
  198. state->status.layer != prev_status->layer ||
  199. state->status.default_layer != prev_status->default_layer) {
  200. stop_keyframe_animation(&lcd_bitmap_leds_animation);
  201. lcd_state = LCD_STATE_LAYER_BITMAP;
  202. start_keyframe_animation(&lcd_bitmap_animation);
  203. }
  204. }
  205. }
  206. void update_user_visualizer_state(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
  207. // Check the status here to start and stop animations
  208. // You might have to save some state, like the current animation here so that you can start the right
  209. // This function is called every time the status changes
  210. // NOTE that this is called from the visualizer thread, so don't access anything else outside the status
  211. // This is also important because the slave won't have access to the active layer for example outside the
  212. // status.
  213. update_emulated_leds(state, prev_status);
  214. update_lcd_text(state, prev_status);
  215. }
  216. void user_visualizer_suspend(visualizer_state_t* state) {
  217. state->layer_text = "Suspending...";
  218. uint8_t hue = LCD_HUE(state->current_lcd_color);
  219. uint8_t sat = LCD_SAT(state->current_lcd_color);
  220. state->target_lcd_color = LCD_COLOR(hue, sat, 0);
  221. start_keyframe_animation(&default_suspend_animation);
  222. }
  223. void user_visualizer_resume(visualizer_state_t* state) {
  224. state->current_lcd_color = initial_color;
  225. state->target_lcd_color = logo_background_color;
  226. lcd_state = LCD_STATE_INITIAL;
  227. start_keyframe_animation(&default_startup_animation);
  228. }
  229. void ergodox_board_led_on(void){
  230. // No board led support
  231. }
  232. void ergodox_right_led_1_on(void){
  233. user_data_keyboard.led_on |= (1u << 0);
  234. visualizer_set_user_data(&user_data_keyboard);
  235. }
  236. void ergodox_right_led_2_on(void){
  237. user_data_keyboard.led_on |= (1u << 1);
  238. visualizer_set_user_data(&user_data_keyboard);
  239. }
  240. void ergodox_right_led_3_on(void){
  241. user_data_keyboard.led_on |= (1u << 2);
  242. visualizer_set_user_data(&user_data_keyboard);
  243. }
  244. void ergodox_board_led_off(void){
  245. // No board led support
  246. }
  247. void ergodox_right_led_1_off(void){
  248. user_data_keyboard.led_on &= ~(1u << 0);
  249. visualizer_set_user_data(&user_data_keyboard);
  250. }
  251. void ergodox_right_led_2_off(void){
  252. user_data_keyboard.led_on &= ~(1u << 1);
  253. visualizer_set_user_data(&user_data_keyboard);
  254. }
  255. void ergodox_right_led_3_off(void){
  256. user_data_keyboard.led_on &= ~(1u << 2);
  257. visualizer_set_user_data(&user_data_keyboard);
  258. }
  259. void ergodox_right_led_1_set(uint8_t n) {
  260. user_data_keyboard.led1 = n;
  261. visualizer_set_user_data(&user_data_keyboard);
  262. }
  263. void ergodox_right_led_2_set(uint8_t n) {
  264. user_data_keyboard.led2 = n;
  265. visualizer_set_user_data(&user_data_keyboard);
  266. }
  267. void ergodox_right_led_3_set(uint8_t n) {
  268. user_data_keyboard.led3 = n;
  269. visualizer_set_user_data(&user_data_keyboard);
  270. }