visualizer.c 10 KB

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