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 bool display_logo(keyframe_animation_t* animation, visualizer_state_t* state) {
79 // Read the uGFX documentation for information how to use the displays
80 // http://wiki.ugfx.org/index.php/Main_Page
83 // You can use static variables for things that can't be found in the animation
84 // or state structs, here we use the image
86 //gdispGBlitArea is a tricky function to use since it supports blitting part of the image
87 // if you have full screen image, then just use 128 and 32 for both source and target dimensions
88 gdispGBlitArea(GDISP, 0, 0, 128, 32, 0, 0, 128, (pixel_t*)resource_lcd_logo);
93 // Feel free to modify the animations below, or even add new ones if needed
95 // Don't worry, if the startup animation is long, you can use the keyboard like normal
97 static keyframe_animation_t startup_animation = {
100 .frame_lengths = {0, gfxMillisecondsToTicks(10000), 0},
103 backlight_keyframe_animate_color,
107 // The color animation animates the LCD color when you change layers
108 static keyframe_animation_t one_led_color = {
111 .frame_lengths = {gfxMillisecondsToTicks(0)},
112 .frame_functions = {backlight_keyframe_set_color},
115 bool swap_led_target_color(keyframe_animation_t* animation, visualizer_state_t* state) {
116 uint32_t temp = next_led_target_color;
117 next_led_target_color = state->target_lcd_color;
118 state->target_lcd_color = temp;
122 // The color animation animates the LCD color when you change layers
123 static keyframe_animation_t two_led_colors = {
126 .frame_lengths = {gfxMillisecondsToTicks(1000), gfxMillisecondsToTicks(0)},
127 .frame_functions = {backlight_keyframe_set_color, swap_led_target_color},
130 // The LCD animation alternates between the layer name display and a
131 // bitmap that displays all active layers
132 static keyframe_animation_t lcd_bitmap_animation = {
135 .frame_lengths = {gfxMillisecondsToTicks(0)},
136 .frame_functions = {lcd_keyframe_display_layer_bitmap},
139 static keyframe_animation_t lcd_bitmap_leds_animation = {
142 .frame_lengths = {gfxMillisecondsToTicks(2000), gfxMillisecondsToTicks(2000)},
143 .frame_functions = {lcd_keyframe_display_layer_bitmap, lcd_keyframe_display_led_states},
146 static keyframe_animation_t suspend_animation = {
149 .frame_lengths = {0, gfxMillisecondsToTicks(1000), 0, 0},
151 lcd_keyframe_display_layer_text,
152 backlight_keyframe_animate_color,
153 lcd_keyframe_disable,
154 lcd_keyframe_disable,
158 static keyframe_animation_t resume_animation = {
161 .frame_lengths = {0, 0, 0, gfxMillisecondsToTicks(10000), 0},
164 backlight_keyframe_enable,
166 backlight_keyframe_animate_color,
170 void initialize_user_visualizer(visualizer_state_t* state) {
171 // The brightness will be dynamically adjustable in the future
172 // But for now, change it here.
173 lcd_backlight_brightness(130);
174 state->current_lcd_color = initial_color;
175 state->target_lcd_color = logo_background_color;
176 lcd_state = LCD_STATE_INITIAL;
177 start_keyframe_animation(&startup_animation);
180 static const uint32_t red;
181 static const uint32_t green;
182 static const uint32_t blue;
184 inline bool is_led_on(visualizer_user_data_t* user_data, uint8_t num) {
185 return user_data->led_on & (1u << num);
188 static uint8_t get_led_index_master(visualizer_user_data_t* user_data) {
189 for (int i=0; i < 3; i++) {
190 if (is_led_on(user_data, i)) {
197 static uint8_t get_led_index_slave(visualizer_user_data_t* user_data) {
198 uint8_t master_index = get_led_index_master(user_data);
199 if (master_index!=0) {
200 for (int i=master_index; i < 3; i++) {
201 if (is_led_on(user_data, i)) {
210 static uint8_t get_secondary_led_index(visualizer_user_data_t* user_data) {
211 if (is_led_on(user_data, 0) &&
212 is_led_on(user_data, 1) &&
213 is_led_on(user_data, 2)) {
219 static uint8_t get_brightness(visualizer_user_data_t* user_data, uint8_t index) {
222 return user_data->led1;
224 return user_data->led2;
226 return user_data->led3;
231 static void update_emulated_leds(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
232 visualizer_user_data_t* user_data_new = (visualizer_user_data_t*)state->status.user_data;
233 visualizer_user_data_t* user_data_old = (visualizer_user_data_t*)prev_status->user_data;
238 if (is_serial_link_master()) {
239 new_index = get_led_index_master(user_data_new);
240 old_index = get_led_index_master(user_data_old);
243 new_index = get_led_index_slave(user_data_new);
244 old_index = get_led_index_slave(user_data_old);
246 uint8_t new_secondary_index = get_secondary_led_index(user_data_new);
247 uint8_t old_secondary_index = get_secondary_led_index(user_data_old);
249 uint8_t old_brightness = get_brightness(user_data_old, old_index);
250 uint8_t new_brightness = get_brightness(user_data_new, new_index);
252 uint8_t old_secondary_brightness = get_brightness(user_data_old, old_secondary_index);
253 uint8_t new_secondary_brightness = get_brightness(user_data_new, new_secondary_index);
255 if (lcd_state == LCD_STATE_INITIAL ||
256 new_index != old_index ||
257 new_secondary_index != old_secondary_index ||
258 new_brightness != old_brightness ||
259 new_secondary_brightness != old_secondary_brightness) {
261 if (new_secondary_index != 0) {
262 state->target_lcd_color = change_lcd_color_intensity(
263 led_emulation_colors[new_index], new_brightness);
264 next_led_target_color = change_lcd_color_intensity(
265 led_emulation_colors[new_secondary_index], new_secondary_brightness);
267 stop_keyframe_animation(&one_led_color);
268 start_keyframe_animation(&two_led_colors);
270 state->target_lcd_color = change_lcd_color_intensity(
271 led_emulation_colors[new_index], new_brightness);
272 stop_keyframe_animation(&two_led_colors);
273 start_keyframe_animation(&one_led_color);
278 static void update_lcd_text(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
279 if (state->status.leds) {
280 if (lcd_state != LCD_STATE_BITMAP_AND_LEDS ||
281 state->status.leds != prev_status->leds ||
282 state->status.layer != prev_status->layer ||
283 state->status.default_layer != prev_status->default_layer) {
285 // NOTE: that it doesn't matter if the animation isn't playing, stop will do nothing in that case
286 stop_keyframe_animation(&lcd_bitmap_animation);
288 lcd_state = LCD_STATE_BITMAP_AND_LEDS;
290 // The logic in this function makes sure that this doesn't happen, but if you call start on an
291 // animation that is already playing it will be restarted.
292 start_keyframe_animation(&lcd_bitmap_leds_animation);
295 if (lcd_state != LCD_STATE_LAYER_BITMAP ||
296 state->status.layer != prev_status->layer ||
297 state->status.default_layer != prev_status->default_layer) {
299 stop_keyframe_animation(&lcd_bitmap_leds_animation);
301 lcd_state = LCD_STATE_LAYER_BITMAP;
302 start_keyframe_animation(&lcd_bitmap_animation);
307 void update_user_visualizer_state(visualizer_state_t* state, visualizer_keyboard_status_t* prev_status) {
308 // Check the status here to start and stop animations
309 // You might have to save some state, like the current animation here so that you can start the right
310 // This function is called every time the status changes
312 // NOTE that this is called from the visualizer thread, so don't access anything else outside the status
313 // This is also important because the slave won't have access to the active layer for example outside the
316 update_emulated_leds(state, prev_status);
317 update_lcd_text(state, prev_status);
321 void user_visualizer_suspend(visualizer_state_t* state) {
322 state->layer_text = "Suspending...";
323 uint8_t hue = LCD_HUE(state->current_lcd_color);
324 uint8_t sat = LCD_SAT(state->current_lcd_color);
325 state->target_lcd_color = LCD_COLOR(hue, sat, 0);
326 start_keyframe_animation(&suspend_animation);
329 void user_visualizer_resume(visualizer_state_t* state) {
330 state->current_lcd_color = initial_color;
331 state->target_lcd_color = logo_background_color;
332 lcd_state = LCD_STATE_INITIAL;
333 start_keyframe_animation(&resume_animation);
336 void ergodox_board_led_on(void){
337 // No board led support
340 void ergodox_right_led_1_on(void){
341 user_data_keyboard.led_on |= (1u << 0);
342 visualizer_set_user_data(&user_data_keyboard);
345 void ergodox_right_led_2_on(void){
346 user_data_keyboard.led_on |= (1u << 1);
347 visualizer_set_user_data(&user_data_keyboard);
350 void ergodox_right_led_3_on(void){
351 user_data_keyboard.led_on |= (1u << 2);
352 visualizer_set_user_data(&user_data_keyboard);
355 void ergodox_board_led_off(void){
356 // No board led support
359 void ergodox_right_led_1_off(void){
360 user_data_keyboard.led_on &= ~(1u << 0);
361 visualizer_set_user_data(&user_data_keyboard);
364 void ergodox_right_led_2_off(void){
365 user_data_keyboard.led_on &= ~(1u << 1);
366 visualizer_set_user_data(&user_data_keyboard);
369 void ergodox_right_led_3_off(void){
370 user_data_keyboard.led_on &= ~(1u << 2);
371 visualizer_set_user_data(&user_data_keyboard);
374 void ergodox_right_led_1_set(uint8_t n) {
375 user_data_keyboard.led1 = n;
376 visualizer_set_user_data(&user_data_keyboard);
379 void ergodox_right_led_2_set(uint8_t n) {
380 user_data_keyboard.led2 = n;
381 visualizer_set_user_data(&user_data_keyboard);
384 void ergodox_right_led_3_set(uint8_t n) {
385 user_data_keyboard.led3 = n;
386 visualizer_set_user_data(&user_data_keyboard);