2 Copyright 2016 Fred Sundvik <fsundvik@gmail.com>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 // Currently we are assuming that both the backlight and LCD are enabled
19 // But it's entirely possible to write a custom visualizer that use only
21 #ifndef LCD_BACKLIGHT_ENABLE
22 #error This visualizer needs that LCD backlight is enabled
26 #error This visualizer needs that LCD is enabled
29 #include "visualizer.h"
30 #include "visualizer_keyframes.h"
31 #include "lcd_keyframes.h"
32 #include "lcd_backlight_keyframes.h"
33 #include "system/serial_link.h"
35 #include "resources/resources.h"
37 static const uint32_t logo_background_color = LCD_COLOR(0x00, 0x00, 0xFF);
38 static const uint32_t initial_color = LCD_COLOR(0, 0, 0);
40 static const uint32_t led_emulation_colors[4] = {
42 LCD_COLOR(255, 255, 255),
43 LCD_COLOR(84, 255, 255),
44 LCD_COLOR(168, 255, 255),
47 static uint32_t next_led_target_color = 0;
51 LCD_STATE_LAYER_BITMAP,
52 LCD_STATE_BITMAP_AND_LEDS,
55 static lcd_state_t lcd_state = LCD_STATE_INITIAL;
62 } visualizer_user_data_t;
64 // Don't access from visualization function, use the visualizer state instead
65 static visualizer_user_data_t user_data_keyboard = {
67 .led1 = LED_BRIGHTNESS_HI,
68 .led2 = LED_BRIGHTNESS_HI,
69 .led3 = LED_BRIGHTNESS_HI,
72 _Static_assert(sizeof(visualizer_user_data_t) <= VISUALIZER_USER_DATA_SIZE,
73 "Please increase the VISUALIZER_USER_DATA_SIZE");
75 // Feel free to modify the animations below, or even add new ones if needed
77 // Don't worry, if the startup animation is long, you can use the keyboard like normal
79 static keyframe_animation_t startup_animation = {
82 .frame_lengths = {0, 0, 0, gfxMillisecondsToTicks(10000), 0},
85 backlight_keyframe_enable,
86 lcd_keyframe_draw_logo,
87 backlight_keyframe_animate_color,
91 // The color animation animates the LCD color when you change layers
92 static keyframe_animation_t one_led_color = {
95 .frame_lengths = {gfxMillisecondsToTicks(0)},
96 .frame_functions = {backlight_keyframe_set_color},
99 bool swap_led_target_color(keyframe_animation_t* animation, visualizer_state_t* state) {
100 uint32_t temp = next_led_target_color;
101 next_led_target_color = state->target_lcd_color;
102 state->target_lcd_color = temp;
106 // The color animation animates the LCD color when you change layers
107 static keyframe_animation_t two_led_colors = {
110 .frame_lengths = {gfxMillisecondsToTicks(1000), gfxMillisecondsToTicks(0)},
111 .frame_functions = {backlight_keyframe_set_color, swap_led_target_color},
114 // The LCD animation alternates between the layer name display and a
115 // bitmap that displays all active layers
116 static keyframe_animation_t lcd_bitmap_animation = {
119 .frame_lengths = {gfxMillisecondsToTicks(0)},
120 .frame_functions = {lcd_keyframe_display_layer_bitmap},
123 static keyframe_animation_t lcd_bitmap_leds_animation = {
126 .frame_lengths = {gfxMillisecondsToTicks(2000), gfxMillisecondsToTicks(2000)},
127 .frame_functions = {lcd_keyframe_display_layer_bitmap, lcd_keyframe_display_led_states},
130 static keyframe_animation_t suspend_animation = {
133 .frame_lengths = {0, gfxMillisecondsToTicks(1000), 0, 0},
135 lcd_keyframe_display_layer_text,
136 backlight_keyframe_animate_color,
137 lcd_keyframe_disable,
138 backlight_keyframe_disable,
142 void initialize_user_visualizer(visualizer_state_t* state) {
143 // The brightness will be dynamically adjustable in the future
144 // But for now, change it here.
145 lcd_backlight_brightness(130);
146 state->current_lcd_color = initial_color;
147 state->target_lcd_color = logo_background_color;
148 lcd_state = LCD_STATE_INITIAL;
149 start_keyframe_animation(&startup_animation);
152 static const uint32_t red;
153 static const uint32_t green;
154 static const uint32_t blue;
156 inline bool is_led_on(visualizer_user_data_t* user_data, uint8_t num) {
157 return user_data->led_on & (1u << num);
160 static uint8_t get_led_index_master(visualizer_user_data_t* user_data) {
161 for (int i=0; i < 3; i++) {
162 if (is_led_on(user_data, i)) {
169 static uint8_t get_led_index_slave(visualizer_user_data_t* user_data) {
170 uint8_t master_index = get_led_index_master(user_data);
171 if (master_index!=0) {
172 for (int i=master_index; i < 3; i++) {
173 if (is_led_on(user_data, i)) {
182 static uint8_t get_secondary_led_index(visualizer_user_data_t* user_data) {
183 if (is_led_on(user_data, 0) &&
184 is_led_on(user_data, 1) &&
185 is_led_on(user_data, 2)) {
191 static uint8_t get_brightness(visualizer_user_data_t* user_data, uint8_t index) {
194 return user_data->led1;
196 return user_data->led2;
198 return user_data->led3;
203 static void update_emulated_leds(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
204 visualizer_user_data_t* user_data_new = (visualizer_user_data_t*)state->status.user_data;
205 visualizer_user_data_t* user_data_old = (visualizer_user_data_t*)prev_status->user_data;
210 if (is_serial_link_master()) {
211 new_index = get_led_index_master(user_data_new);
212 old_index = get_led_index_master(user_data_old);
215 new_index = get_led_index_slave(user_data_new);
216 old_index = get_led_index_slave(user_data_old);
218 uint8_t new_secondary_index = get_secondary_led_index(user_data_new);
219 uint8_t old_secondary_index = get_secondary_led_index(user_data_old);
221 uint8_t old_brightness = get_brightness(user_data_old, old_index);
222 uint8_t new_brightness = get_brightness(user_data_new, new_index);
224 uint8_t old_secondary_brightness = get_brightness(user_data_old, old_secondary_index);
225 uint8_t new_secondary_brightness = get_brightness(user_data_new, new_secondary_index);
227 if (lcd_state == LCD_STATE_INITIAL ||
228 new_index != old_index ||
229 new_secondary_index != old_secondary_index ||
230 new_brightness != old_brightness ||
231 new_secondary_brightness != old_secondary_brightness) {
233 if (new_secondary_index != 0) {
234 state->target_lcd_color = change_lcd_color_intensity(
235 led_emulation_colors[new_index], new_brightness);
236 next_led_target_color = change_lcd_color_intensity(
237 led_emulation_colors[new_secondary_index], new_secondary_brightness);
239 stop_keyframe_animation(&one_led_color);
240 start_keyframe_animation(&two_led_colors);
242 state->target_lcd_color = change_lcd_color_intensity(
243 led_emulation_colors[new_index], new_brightness);
244 stop_keyframe_animation(&two_led_colors);
245 start_keyframe_animation(&one_led_color);
250 static void update_lcd_text(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
251 if (state->status.leds) {
252 if (lcd_state != LCD_STATE_BITMAP_AND_LEDS ||
253 state->status.leds != prev_status->leds ||
254 state->status.layer != prev_status->layer ||
255 state->status.default_layer != prev_status->default_layer) {
257 // NOTE: that it doesn't matter if the animation isn't playing, stop will do nothing in that case
258 stop_keyframe_animation(&lcd_bitmap_animation);
260 lcd_state = LCD_STATE_BITMAP_AND_LEDS;
262 // The logic in this function makes sure that this doesn't happen, but if you call start on an
263 // animation that is already playing it will be restarted.
264 start_keyframe_animation(&lcd_bitmap_leds_animation);
267 if (lcd_state != LCD_STATE_LAYER_BITMAP ||
268 state->status.layer != prev_status->layer ||
269 state->status.default_layer != prev_status->default_layer) {
271 stop_keyframe_animation(&lcd_bitmap_leds_animation);
273 lcd_state = LCD_STATE_LAYER_BITMAP;
274 start_keyframe_animation(&lcd_bitmap_animation);
279 void update_user_visualizer_state(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
280 // Check the status here to start and stop animations
281 // You might have to save some state, like the current animation here so that you can start the right
282 // This function is called every time the status changes
284 // NOTE that this is called from the visualizer thread, so don't access anything else outside the status
285 // This is also important because the slave won't have access to the active layer for example outside the
288 update_emulated_leds(state, prev_status);
289 update_lcd_text(state, prev_status);
293 void user_visualizer_suspend(visualizer_state_t* state) {
294 state->layer_text = "Suspending...";
295 uint8_t hue = LCD_HUE(state->current_lcd_color);
296 uint8_t sat = LCD_SAT(state->current_lcd_color);
297 state->target_lcd_color = LCD_COLOR(hue, sat, 0);
298 start_keyframe_animation(&suspend_animation);
301 void user_visualizer_resume(visualizer_state_t* state) {
302 state->current_lcd_color = initial_color;
303 state->target_lcd_color = logo_background_color;
304 lcd_state = LCD_STATE_INITIAL;
305 start_keyframe_animation(&startup_animation);
308 void ergodox_board_led_on(void){
309 // No board led support
312 void ergodox_right_led_1_on(void){
313 user_data_keyboard.led_on |= (1u << 0);
314 visualizer_set_user_data(&user_data_keyboard);
317 void ergodox_right_led_2_on(void){
318 user_data_keyboard.led_on |= (1u << 1);
319 visualizer_set_user_data(&user_data_keyboard);
322 void ergodox_right_led_3_on(void){
323 user_data_keyboard.led_on |= (1u << 2);
324 visualizer_set_user_data(&user_data_keyboard);
327 void ergodox_board_led_off(void){
328 // No board led support
331 void ergodox_right_led_1_off(void){
332 user_data_keyboard.led_on &= ~(1u << 0);
333 visualizer_set_user_data(&user_data_keyboard);
336 void ergodox_right_led_2_off(void){
337 user_data_keyboard.led_on &= ~(1u << 1);
338 visualizer_set_user_data(&user_data_keyboard);
341 void ergodox_right_led_3_off(void){
342 user_data_keyboard.led_on &= ~(1u << 2);
343 visualizer_set_user_data(&user_data_keyboard);
346 void ergodox_right_led_1_set(uint8_t n) {
347 user_data_keyboard.led1 = n;
348 visualizer_set_user_data(&user_data_keyboard);
351 void ergodox_right_led_2_set(uint8_t n) {
352 user_data_keyboard.led2 = n;
353 visualizer_set_user_data(&user_data_keyboard);
356 void ergodox_right_led_3_set(uint8_t n) {
357 user_data_keyboard.led3 = n;
358 visualizer_set_user_data(&user_data_keyboard);