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.

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