]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/ergodox_infinity/keymaps/halfkeyboard/visualizer.c
a failed attempt at hot-plugging
[qmk_firmware.git] / keyboards / ergodox_infinity / keymaps / halfkeyboard / visualizer.c
1 /*
2 Copyright 2017 Fred Sundvik
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 #include "visualizer.h"
16 #include "gfx.h"
17 #include "math.h"
18 #include "default_animations.h"
19 #include "led_backlight_keyframes.h"
20
21 #define ONESIDESCAN 9
22 #define BOTHSIDESCAN 16
23 #define FULL_ON LUMA2COLOR(255)
24 #define THREE_QUARTER LUMA2COLOR(200)
25 #define HALF_ON LUMA2COLOR(150)
26 #define ONE_QUARTER LUMA2COLOR(50)
27 #define CROSSFADE_TIME 8000
28 bool KITT_scan_one_side_left_to_right(keyframe_animation_t* animation, visualizer_state_t* state);
29 bool KITT_scan_one_side_right_to_left(keyframe_animation_t* animation, visualizer_state_t* state);
30 keyframe_animation_t Fade_in_all_leds = {
31     .num_frames = 1,
32     .loop = false,
33     .frame_lengths = {
34         CROSSFADE_TIME,
35     },
36     .frame_functions = {
37         led_backlight_keyframe_fade_in_all,
38     },
39 };
40 /*
41  *  one set left to right.  then reverse to go back.
42  *  |    left side              |       right side          |       |
43     |---|---|---|---|---|---|---|:-:|---|---|---|---|---|---|-------|
44     | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | phase |
45     _________________________________________________________________
46     | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0     |
47     | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1     |
48     | 2 | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 2     |
49     | 1 | 2 | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 3     |
50     | 0 | 1 | 2 | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 4     |
51     | 0 | 0 | 1 | 2 | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 5     |
52     | 0 | 0 | 0 | 1 | 2 | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 6     |
53     | 0 | 0 | 0 | 0 | 1 | 2 | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 7     |
54     | 0 | 0 | 0 | 0 | 0 | 1 | 2 | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 8     |
55     | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 2 | 3 | 0 | 0 | 0 | 0 | 0 | 9     |
56     | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 2 | 3 | 0 | 0 | 0 | 0 | 10    |
57     | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 2 | 3 | 0 | 0 | 0 | 11    |
58     | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 2 | 3 | 0 | 0 | 12    |
59     | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 2 | 3 | 0 | 13    |
60     | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 2 | 3 | 14    |
61     | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 2 | 15    |
62     | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 16    |
63   */
64
65 #ifdef MASTER_IS_ON_RIGHT /*right side*/
66
67 keyframe_animation_t KITT_Scanner_animation = {
68     .num_frames = 2,
69     .loop = true,
70     .frame_lengths = {
71         CROSSFADE_TIME * BOTHSIDESCAN,
72         CROSSFADE_TIME * BOTHSIDESCAN,
73     },
74     .frame_functions = {
75         KITT_scan_one_side_left_to_right,
76         KITT_scan_one_side_right_to_left,
77     },
78 };
79
80 bool KITT_scan_one_side_left_to_right(keyframe_animation_t* animation, visualizer_state_t* state) {
81     (void)state;
82     float frame_length = animation->frame_lengths[animation->current_frame];
83     float current_pos = frame_length - animation->time_left_in_frame;
84     int phase = current_pos/(frame_length/BOTHSIDESCAN);
85     int row = 0;
86     gdispGClear(LED_DISPLAY, ONE_QUARTER);
87     gdispGDrawPixel(LED_DISPLAY, 14-phase, row, FULL_ON);
88     gdispGDrawPixel(LED_DISPLAY, 15-phase, row, THREE_QUARTER);
89     gdispGDrawPixel(LED_DISPLAY, 16-phase, row, HALF_ON);
90     gdispGDrawPixel(LED_DISPLAY, 6, row, ONE_QUARTER);
91     return true;
92 }
93
94 bool KITT_scan_one_side_right_to_left(keyframe_animation_t* animation, visualizer_state_t* state) {
95     (void)state;
96     float frame_length = animation->frame_lengths[animation->current_frame];
97     float current_pos = frame_length - animation->time_left_in_frame;
98     int phase = current_pos/(frame_length/BOTHSIDESCAN);
99     int row = 0;
100     gdispGClear(LED_DISPLAY, ONE_QUARTER);
101     gdispGDrawPixel(LED_DISPLAY, phase, row, FULL_ON);
102     gdispGDrawPixel(LED_DISPLAY, phase-1, row, THREE_QUARTER);
103     gdispGDrawPixel(LED_DISPLAY, phase-2, row, HALF_ON);
104     gdispGDrawPixel(LED_DISPLAY, 6, row, ONE_QUARTER);
105     return true;
106 }
107 #else /*left side*/
108 keyframe_animation_t KITT_Scanner_animation = {
109     .num_frames = 2,
110     .loop = true,
111     .frame_lengths = {
112         CROSSFADE_TIME * BOTHSIDESCAN,
113         CROSSFADE_TIME * BOTHSIDESCAN,
114     },
115     .frame_functions = {
116         KITT_scan_one_side_left_to_right,
117         KITT_scan_one_side_right_to_left,
118     },
119 };
120
121 bool KITT_scan_one_side_left_to_right(keyframe_animation_t* animation, visualizer_state_t* state) {
122     (void)state;
123     float frame_length = animation->frame_lengths[animation->current_frame];
124     float current_pos = frame_length - animation->time_left_in_frame;
125     int phase = current_pos/(frame_length/BOTHSIDESCAN);
126     int row = 0;
127     gdispGClear(LED_DISPLAY, ONE_QUARTER);
128     gdispGDrawPixel(LED_DISPLAY, phase, row, FULL_ON);
129     gdispGDrawPixel(LED_DISPLAY, phase-1, row, THREE_QUARTER);
130     gdispGDrawPixel(LED_DISPLAY, phase-2, row, HALF_ON);
131     gdispGDrawPixel(LED_DISPLAY, 6, row, ONE_QUARTER);
132     return true;
133 }
134
135 bool KITT_scan_one_side_right_to_left(keyframe_animation_t* animation, visualizer_state_t* state) {
136     (void)state;
137     float frame_length = animation->frame_lengths[animation->current_frame];
138     float current_pos = frame_length - animation->time_left_in_frame;
139     int phase = current_pos/(frame_length/BOTHSIDESCAN);
140     int row = 0;
141     gdispGClear(LED_DISPLAY, ONE_QUARTER);
142     gdispGDrawPixel(LED_DISPLAY, (14 - phase), row, FULL_ON);
143     gdispGDrawPixel(LED_DISPLAY, 14 - (phase-1), row, THREE_QUARTER);
144     gdispGDrawPixel(LED_DISPLAY, 14 - (phase-2), row, HALF_ON);
145     gdispGDrawPixel(LED_DISPLAY, 6, row, ONE_QUARTER);
146     return true;
147 }
148 #endif
149
150 #define RED 0
151 #define ORANGE 21
152 #define YELLOW 42
153 #define SPRING_GREEN 64
154 #define GREEN 85
155 #define TURQUOISE 107
156 #define CYAN 127
157 #define OCEAN 149
158 #define BLUE 170
159 #define VIOLET 192
160 #define MAGENTA 212
161 #define RASPBERRY 234
162
163 // This function should be implemented by the keymap visualizer
164 // Don't change anything else than state->target_lcd_color and state->layer_text as that's the only thing
165 // that the simple_visualizer assumes that you are updating
166 // Also make sure that the buffer passed to state->layer_text remains valid until the previous animation is
167 // stopped. This can be done by either double buffering it or by using constant strings
168 static void get_visualizer_layer_and_color(visualizer_state_t* state) {
169     uint8_t saturation = 255;
170     /* if (state->status.leds & (1u << USB_LED_CAPS_LOCK)) {
171         saturation = 255;
172     } */
173     if (state->status.layer & 0x200) {
174         state->target_lcd_color = LCD_COLOR(GREEN, saturation, 0xFF);
175         state->layer_text = "MOUSE";
176     }
177     else if (state->status.layer & 0x100) {
178         state->target_lcd_color = LCD_COLOR(MAGENTA, saturation, 0xFF);
179         state->layer_text = "Shortcuts Layer";
180     }
181     else    if (state->status.layer & 0x80) {
182         state->target_lcd_color = LCD_COLOR(VIOLET, saturation, 0xFF);
183         state->layer_text = "Plover";
184         start_keyframe_animation(&KITT_Scanner_animation);
185
186     }
187     else if (state->status.layer & 0x40) {
188         state->target_lcd_color = LCD_COLOR(RASPBERRY, saturation, 0xFF);
189         state->layer_text = "Mirrored Symbols";
190     }
191     else if (state->status.layer & 0x20) {
192         state->target_lcd_color = LCD_COLOR(RED, saturation, 0xFF);
193         state->layer_text = "Symbols";
194     }
195     else if (state->status.layer & 0x8) {
196         state->target_lcd_color = LCD_COLOR(OCEAN, saturation, 0xFF);
197         state->layer_text = "Mirrored Dvorak";
198     }
199     else if (state->status.layer & 0x4) {
200         state->target_lcd_color = LCD_COLOR(BLUE, saturation, 0xFF);
201         state->layer_text = "Dvorak";
202     }
203     else if (state->status.layer & 0x2) {
204         state->target_lcd_color = LCD_COLOR(ORANGE, saturation, 0xFF);
205         state->layer_text = "Mirrored Qwerty";
206     }
207     else {
208         state->target_lcd_color = LCD_COLOR(YELLOW, saturation, 0xFF);
209         state->layer_text = "Qwerty";
210         stop_keyframe_animation(&KITT_Scanner_animation);
211         start_keyframe_animation(&Fade_in_all_leds);
212     }
213 }