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/>.
19 * Currently we are assuming that both the backlight and LCD are enabled
20 * But it's entirely possible to write a custom visualizer that use only
23 #ifndef LCD_BACKLIGHT_ENABLE
24 #error This visualizer needs that LCD backlight is enabled
28 #error This visualizer needs that LCD is enabled
31 #include "visualizer.h"
32 #include "visualizer_keyframes.h"
33 #include "lcd_keyframes.h"
34 #include "lcd_backlight_keyframes.h"
35 #include "system/serial_link.h"
36 #include "default_animations.h"
38 static const uint32_t logo_background_color = LCD_COLOR(0, 0, 255);
39 static const uint32_t initial_color = LCD_COLOR(84, 255, 255);
41 static const uint32_t led_emulation_colors[4] = {
43 LCD_COLOR(141, 255, 255),
44 LCD_COLOR(18, 255, 255),
45 LCD_COLOR(194, 255, 255),
48 static uint32_t next_led_target_color = 0;
52 LCD_STATE_LAYER_BITMAP,
53 LCD_STATE_BITMAP_AND_LEDS,
56 static lcd_state_t lcd_state = LCD_STATE_INITIAL;
63 } visualizer_user_data_t;
66 * Don't access from visualization function, use the visualizer state instead
68 static visualizer_user_data_t user_data_keyboard = {
70 .led1 = LED_BRIGHTNESS_HI,
71 .led2 = LED_BRIGHTNESS_HI,
72 .led3 = LED_BRIGHTNESS_HI,
75 _Static_assert(sizeof(visualizer_user_data_t) <= VISUALIZER_USER_DATA_SIZE,
76 "Please increase the VISUALIZER_USER_DATA_SIZE");
78 // Feel free to modify the animations below, or even add new ones if needed
80 // The color animation animates the LCD color when you change layers
81 static keyframe_animation_t one_led_color = {
84 .frame_lengths = {gfxMillisecondsToTicks(0)},
85 .frame_functions = {lcd_backlight_keyframe_set_color},
88 bool swap_led_target_color(keyframe_animation_t* animation, visualizer_state_t* state) {
89 uint32_t temp = next_led_target_color;
90 next_led_target_color = state->target_lcd_color;
91 state->target_lcd_color = temp;
95 // The color animation animates the LCD color when you change layers
96 static keyframe_animation_t two_led_colors = {
99 .frame_lengths = {gfxMillisecondsToTicks(1000), gfxMillisecondsToTicks(0)},
100 .frame_functions = {lcd_backlight_keyframe_set_color, swap_led_target_color},
104 * The LCD animation alternates between the layer name display and a
105 * bitmap that displays all active layers
107 static keyframe_animation_t lcd_bitmap_animation = {
110 .frame_lengths = {gfxMillisecondsToTicks(0)},
111 .frame_functions = {lcd_keyframe_display_layer_bitmap},
114 static keyframe_animation_t lcd_bitmap_leds_animation = {
117 .frame_lengths = {gfxMillisecondsToTicks(2000), gfxMillisecondsToTicks(2000)},
118 .frame_functions = {lcd_keyframe_display_layer_bitmap, lcd_keyframe_display_led_states},
121 void initialize_user_visualizer(visualizer_state_t* state) {
123 * The brightness will be dynamically adjustable in the future
124 * But for now, change it here.
126 lcd_backlight_brightness(180);
127 state->current_lcd_color = initial_color;
128 state->target_lcd_color = logo_background_color;
129 lcd_state = LCD_STATE_INITIAL;
130 start_keyframe_animation(&default_startup_animation);
133 static inline bool is_led_on(visualizer_user_data_t* user_data, uint8_t num) {
134 return user_data->led_on & (1u << num);
137 static uint8_t get_led_index_master(visualizer_user_data_t* user_data) {
138 for (int i=0; i < 4; i++) {
139 if (is_led_on(user_data, i)) {
146 static uint8_t get_led_index_slave(visualizer_user_data_t* user_data) {
147 uint8_t master_index = get_led_index_master(user_data);
148 if (master_index!=0) {
149 for (int i=master_index; i < 4; i++) {
150 if (is_led_on(user_data, i)) {
159 static uint8_t get_secondary_led_index(visualizer_user_data_t* user_data) {
161 is_led_on(user_data, 0) &&
162 is_led_on(user_data, 1) &&
163 is_led_on(user_data, 2)
170 static uint8_t get_brightness(visualizer_user_data_t* user_data, uint8_t index) {
173 return user_data->led1;
175 return user_data->led2;
177 return user_data->led3;
182 static void update_emulated_leds(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
183 visualizer_user_data_t* user_data_new = (visualizer_user_data_t*)state->status.user_data;
184 visualizer_user_data_t* user_data_old = (visualizer_user_data_t*)prev_status->user_data;
189 if (true || is_serial_link_master()) {
190 new_index = get_led_index_master(user_data_new);
191 old_index = get_led_index_master(user_data_old);
193 new_index = get_led_index_slave(user_data_new);
194 old_index = get_led_index_slave(user_data_old);
197 uint8_t new_secondary_index = get_secondary_led_index(user_data_new);
198 uint8_t old_secondary_index = get_secondary_led_index(user_data_old);
200 uint8_t new_brightness = get_brightness(user_data_new, new_index);
201 uint8_t old_brightness = get_brightness(user_data_old, old_index);
203 uint8_t new_secondary_brightness = get_brightness(user_data_new, new_secondary_index);
204 uint8_t old_secondary_brightness = get_brightness(user_data_old, old_secondary_index);
207 lcd_state == LCD_STATE_INITIAL ||
208 new_index != old_index ||
209 new_secondary_index != old_secondary_index ||
210 new_brightness != old_brightness ||
211 new_secondary_brightness != old_secondary_brightness
213 if (new_secondary_index != 0) {
214 state->target_lcd_color = change_lcd_color_intensity(
215 led_emulation_colors[new_index], new_brightness);
216 next_led_target_color = change_lcd_color_intensity(
217 led_emulation_colors[new_secondary_index], new_secondary_brightness);
219 stop_keyframe_animation(&one_led_color);
220 start_keyframe_animation(&two_led_colors);
222 state->target_lcd_color = change_lcd_color_intensity(
223 led_emulation_colors[new_index], new_brightness);
225 stop_keyframe_animation(&two_led_colors);
226 start_keyframe_animation(&one_led_color);
231 static void update_lcd_text(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
232 if (state->status.leds) {
234 lcd_state != LCD_STATE_BITMAP_AND_LEDS ||
235 state->status.leds != prev_status->leds ||
236 state->status.layer != prev_status->layer ||
237 state->status.default_layer != prev_status->default_layer
239 // NOTE: that it doesn't matter if the animation isn't playing, stop will do nothing in that case
240 stop_keyframe_animation(&lcd_bitmap_animation);
242 lcd_state = LCD_STATE_BITMAP_AND_LEDS;
245 * The logic in this function makes sure that this doesn't happen, but if you call start on an
246 * animation that is already playing it will be restarted.
248 start_keyframe_animation(&lcd_bitmap_leds_animation);
252 lcd_state != LCD_STATE_LAYER_BITMAP ||
253 state->status.layer != prev_status->layer ||
254 state->status.default_layer != prev_status->default_layer
256 stop_keyframe_animation(&lcd_bitmap_leds_animation);
258 lcd_state = LCD_STATE_LAYER_BITMAP;
259 start_keyframe_animation(&lcd_bitmap_animation);
264 void update_user_visualizer_state(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
266 * Check the status here to start and stop animations
267 * You might have to save some state, like the current animation here so that you can start the right
268 * This function is called every time the status changes
270 * NOTE that this is called from the visualizer thread, so don't access anything else outside the status
271 * This is also important because the slave won't have access to the active layer for example outside the
275 update_emulated_leds(state, prev_status);
276 update_lcd_text(state, prev_status);
279 void user_visualizer_suspend(visualizer_state_t* state) {
280 state->layer_text = "Suspending...";
281 uint8_t hue = LCD_HUE(state->current_lcd_color);
282 uint8_t sat = LCD_SAT(state->current_lcd_color);
283 state->target_lcd_color = LCD_COLOR(hue, sat, 0);
284 start_keyframe_animation(&default_suspend_animation);
287 void user_visualizer_resume(visualizer_state_t* state) {
288 state->current_lcd_color = initial_color;
289 state->target_lcd_color = logo_background_color;
290 lcd_state = LCD_STATE_INITIAL;
291 start_keyframe_animation(&default_startup_animation);
294 void ergodox_board_led_on(void){
295 // No board led support
298 void ergodox_right_led_1_on(void){
299 user_data_keyboard.led_on |= (1u << 0);
300 visualizer_set_user_data(&user_data_keyboard);
303 void ergodox_right_led_2_on(void){
304 user_data_keyboard.led_on |= (1u << 1);
305 visualizer_set_user_data(&user_data_keyboard);
308 void ergodox_right_led_3_on(void){
309 user_data_keyboard.led_on |= (1u << 2);
310 visualizer_set_user_data(&user_data_keyboard);
313 void ergodox_board_led_off(void){
314 // No board led support
317 void ergodox_right_led_1_off(void){
318 user_data_keyboard.led_on &= ~(1u << 0);
319 visualizer_set_user_data(&user_data_keyboard);
322 void ergodox_right_led_2_off(void){
323 user_data_keyboard.led_on &= ~(1u << 1);
324 visualizer_set_user_data(&user_data_keyboard);
327 void ergodox_right_led_3_off(void){
328 user_data_keyboard.led_on &= ~(1u << 2);
329 visualizer_set_user_data(&user_data_keyboard);
332 void ergodox_right_led_1_set(uint8_t n) {
333 user_data_keyboard.led1 = n;
334 visualizer_set_user_data(&user_data_keyboard);
337 void ergodox_right_led_2_set(uint8_t n) {
338 user_data_keyboard.led2 = n;
339 visualizer_set_user_data(&user_data_keyboard);
342 void ergodox_right_led_3_set(uint8_t n) {
343 user_data_keyboard.led3 = n;
344 visualizer_set_user_data(&user_data_keyboard);