aboutsummaryrefslogtreecommitdiff
path: root/keyboards/wilba_tech/wt_main.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/wilba_tech/wt_main.c')
-rw-r--r--keyboards/wilba_tech/wt_main.c434
1 files changed, 125 insertions, 309 deletions
diff --git a/keyboards/wilba_tech/wt_main.c b/keyboards/wilba_tech/wt_main.c
index 32fc6f9e4..1ad12ee4a 100644
--- a/keyboards/wilba_tech/wt_main.c
+++ b/keyboards/wilba_tech/wt_main.c
@@ -23,361 +23,177 @@
23#if MONO_BACKLIGHT_ENABLED 23#if MONO_BACKLIGHT_ENABLED
24#include "keyboards/wilba_tech/wt_mono_backlight.h" 24#include "keyboards/wilba_tech/wt_mono_backlight.h"
25#endif // MONO_BACKLIGHT_ENABLED 25#endif // MONO_BACKLIGHT_ENABLED
26#include "keyboards/wilba_tech/via_api.h" // Temporary hack
27#include "keyboards/wilba_tech/via_keycodes.h" // Temporary hack
28 26
29#include "raw_hid.h" 27#include "via.h"
30#include "dynamic_keymap.h"
31#include "timer.h"
32#include "tmk_core/common/eeprom.h"
33
34bool eeprom_is_valid(void)
35{
36 return (eeprom_read_word(((void*)EEPROM_MAGIC_ADDR)) == EEPROM_MAGIC &&
37 eeprom_read_byte(((void*)EEPROM_VERSION_ADDR)) == EEPROM_VERSION);
38}
39
40void eeprom_set_valid(bool valid)
41{
42 eeprom_update_word(((void*)EEPROM_MAGIC_ADDR), valid ? EEPROM_MAGIC : 0xFFFF);
43 eeprom_update_byte(((void*)EEPROM_VERSION_ADDR), valid ? EEPROM_VERSION : 0xFF);
44}
45
46void eeprom_reset(void)
47{
48 // Set the Zeal60 specific EEPROM state as invalid.
49 eeprom_set_valid(false);
50 // Set the TMK/QMK EEPROM state as invalid.
51 eeconfig_disable();
52}
53
54#ifdef RAW_ENABLE
55
56void raw_hid_receive( uint8_t *data, uint8_t length )
57{
58 uint8_t *command_id = &(data[0]);
59 uint8_t *command_data = &(data[1]);
60 switch ( *command_id )
61 {
62 case id_get_protocol_version:
63 {
64 command_data[0] = PROTOCOL_VERSION >> 8;
65 command_data[1] = PROTOCOL_VERSION & 0xFF;
66 break;
67 }
68 case id_get_keyboard_value:
69 {
70 if ( command_data[0] == id_uptime )
71 {
72 uint32_t value = timer_read32();
73 command_data[1] = (value >> 24 ) & 0xFF;
74 command_data[2] = (value >> 16 ) & 0xFF;
75 command_data[3] = (value >> 8 ) & 0xFF;
76 command_data[4] = value & 0xFF;
77 }
78 else
79 {
80 *command_id = id_unhandled;
81 }
82 break;
83 }
84#ifdef DYNAMIC_KEYMAP_ENABLE
85 case id_dynamic_keymap_get_keycode:
86 {
87 uint16_t keycode = dynamic_keymap_get_keycode( command_data[0], command_data[1], command_data[2] );
88 command_data[3] = keycode >> 8;
89 command_data[4] = keycode & 0xFF;
90 break;
91 }
92 case id_dynamic_keymap_set_keycode:
93 {
94 dynamic_keymap_set_keycode( command_data[0], command_data[1], command_data[2], ( command_data[3] << 8 ) | command_data[4] );
95 break;
96 }
97 case id_dynamic_keymap_reset:
98 {
99 dynamic_keymap_reset();
100 break;
101 }
102 case id_dynamic_keymap_macro_get_count:
103 {
104 command_data[0] = dynamic_keymap_macro_get_count();
105 break;
106 }
107 case id_dynamic_keymap_macro_get_buffer_size:
108 {
109 uint16_t size = dynamic_keymap_macro_get_buffer_size();
110 command_data[0] = size >> 8;
111 command_data[1] = size & 0xFF;
112 break;
113 }
114 case id_dynamic_keymap_macro_get_buffer:
115 {
116 uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
117 uint16_t size = command_data[2]; // size <= 28
118 dynamic_keymap_macro_get_buffer( offset, size, &command_data[3] );
119 break;
120 }
121 case id_dynamic_keymap_macro_set_buffer:
122 {
123 uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
124 uint16_t size = command_data[2]; // size <= 28
125 dynamic_keymap_macro_set_buffer( offset, size, &command_data[3] );
126 break;
127 }
128 case id_dynamic_keymap_macro_reset:
129 {
130 dynamic_keymap_macro_reset();
131 break;
132 }
133 case id_dynamic_keymap_get_layer_count:
134 {
135 command_data[0] = dynamic_keymap_get_layer_count();
136 break;
137 }
138 case id_dynamic_keymap_get_buffer:
139 {
140 uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
141 uint16_t size = command_data[2]; // size <= 28
142 dynamic_keymap_get_buffer( offset, size, &command_data[3] );
143 break;
144 }
145 case id_dynamic_keymap_set_buffer:
146 {
147 uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
148 uint16_t size = command_data[2]; // size <= 28
149 dynamic_keymap_set_buffer( offset, size, &command_data[3] );
150 break;
151 }
152#endif // DYNAMIC_KEYMAP_ENABLE
153#if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
154 case id_backlight_config_set_value:
155 {
156 backlight_config_set_value(command_data);
157 break;
158 }
159 case id_backlight_config_get_value:
160 {
161 backlight_config_get_value(command_data);
162 break;
163 }
164 case id_backlight_config_save:
165 {
166 backlight_config_save();
167 break;
168 }
169#endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
170 case id_eeprom_reset:
171 {
172 eeprom_reset();
173 break;
174 }
175 case id_bootloader_jump:
176 {
177 // Need to send data back before the jump
178 // Informs host that the command is handled
179 raw_hid_send( data, length );
180 // Give host time to read it
181 wait_ms(100);
182 bootloader_jump();
183 break;
184 }
185 default:
186 {
187 // Unhandled message.
188 *command_id = id_unhandled;
189 break;
190 }
191 }
192
193 // Return same buffer with values changed
194 raw_hid_send( data, length );
195
196}
197 28
29#ifndef VIA_ENABLE
30#include "tmk_core/common/eeprom.h"
31#include "version.h" // for QMK_BUILDDATE used in EEPROM magic
198#endif 32#endif
199 33
200void main_init(void) 34// Called from via_init() if VIA_ENABLE
35// Called from matrix_init_kb() if not VIA_ENABLE
36void via_init_kb(void)
201{ 37{
202 // If the EEPROM has the magic, the data is good. 38 // If the EEPROM has the magic, the data is good.
203 // OK to load from EEPROM. 39 // OK to load from EEPROM
204 if (eeprom_is_valid()) { 40 if (via_eeprom_is_valid()) {
205#if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
206 backlight_config_load();
207#endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
208 } else {
209#if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED 41#if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
210 // If the EEPROM has not been saved before, or is out of date, 42 backlight_config_load();
211 // save the default values to the EEPROM. Default values
212 // come from construction of the backlight_config instance.
213 backlight_config_save();
214#endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED 43#endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
215#ifdef DYNAMIC_KEYMAP_ENABLE 44 } else {
216 // This resets the keymaps in EEPROM to what is in flash.
217 dynamic_keymap_reset();
218 // This resets the macros in EEPROM to nothing.
219 dynamic_keymap_macro_reset();
220#endif // DYNAMIC_KEYMAP_ENABLE
221 // Save the magic number last, in case saving was interrupted
222 eeprom_set_valid(true);
223 }
224
225#if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED 45#if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
226 // Initialize LED drivers for backlight. 46 // If the EEPROM has not been saved before, or is out of date,
227 backlight_init_drivers(); 47 // save the default values to the EEPROM. Default values
228 48 // come from construction of the backlight_config instance.
229 backlight_timer_init(); 49 backlight_config_save();
230 backlight_timer_enable();
231#endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED 50#endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
232}
233 51
234void bootmagic_lite(void) 52 // DO NOT set EEPROM valid here, let caller do this
235{ 53 }
236 // The lite version of TMK's bootmagic.
237 // 100% less potential for accidentally making the
238 // keyboard do stupid things.
239 54
240 // We need multiple scans because debouncing can't be turned off. 55#if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
241 matrix_scan(); 56 // Initialize LED drivers for backlight.
242 wait_ms(DEBOUNCE); 57 backlight_init_drivers();
243 wait_ms(DEBOUNCE);
244 matrix_scan();
245 58
246 // If the Esc (matrix 0,0) is held down on power up, 59 backlight_timer_init();
247 // reset the EEPROM valid state and jump to bootloader. 60 backlight_timer_enable();
248 if ( matrix_get_row(0) & (1<<0) ) { 61#endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
249 eeprom_reset();
250 bootloader_jump();
251 }
252} 62}
253 63
254void matrix_init_kb(void) 64void matrix_init_kb(void)
255{ 65{
256 bootmagic_lite(); 66 // If VIA is disabled, we still need to load backlight settings.
257 main_init(); 67 // Call via_init_kb() the same way as via_init(), with setting
258 matrix_init_user(); 68 // EEPROM valid afterwards.
69#ifndef VIA_ENABLE
70 via_init_kb();
71 via_eeprom_set_valid(true);
72#endif // VIA_ENABLE
73
74 matrix_init_user();
259} 75}
260 76
261void matrix_scan_kb(void) 77void matrix_scan_kb(void)
262{ 78{
263#if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED 79#if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
264 // This only updates the LED driver buffers if something has changed. 80 // This only updates the LED driver buffers if something has changed.
265 backlight_update_pwm_buffers(); 81 backlight_update_pwm_buffers();
266#endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED 82#endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
267 matrix_scan_user(); 83 matrix_scan_user();
268} 84}
269 85
270bool process_record_kb(uint16_t keycode, keyrecord_t *record) 86bool process_record_kb(uint16_t keycode, keyrecord_t *record)
271{ 87{
272#if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED 88#if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
273 process_record_backlight(keycode, record); 89 process_record_backlight(keycode, record);
274#endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED 90#endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
275 91
276 switch(keycode) { 92 return process_record_user(keycode, record);
277 case FN_MO13:
278 if (record->event.pressed) {
279 layer_on(1);
280 update_tri_layer(1, 2, 3);
281 } else {
282 layer_off(1);
283 update_tri_layer(1, 2, 3);
284 }
285 return false;
286 break;
287 case FN_MO23:
288 if (record->event.pressed) {
289 layer_on(2);
290 update_tri_layer(1, 2, 3);
291 } else {
292 layer_off(2);
293 update_tri_layer(1, 2, 3);
294 }
295 return false;
296 break;
297 }
298
299#ifdef DYNAMIC_KEYMAP_ENABLE
300 // Handle macros
301 if (record->event.pressed) {
302 if ( keycode >= MACRO00 && keycode <= MACRO15 )
303 {
304 uint8_t id = keycode - MACRO00;
305 dynamic_keymap_macro_send(id);
306 return false;
307 }
308 }
309#endif //DYNAMIC_KEYMAP_ENABLE
310
311 return process_record_user(keycode, record);
312}
313
314// This overrides the one in quantum/keymap_common.c
315uint16_t keymap_function_id_to_action( uint16_t function_id )
316{
317 // Zeal60 specific "action functions" are 0xF00 to 0xFFF
318 // i.e. F(0xF00) to F(0xFFF) are mapped to
319 // enum zeal60_action_functions by masking last 8 bits.
320 if ( function_id >= 0x0F00 && function_id <= 0x0FFF )
321 {
322 uint8_t id = function_id & 0xFF;
323 switch ( id ) {
324 case TRIPLE_TAP_1_3:
325 case TRIPLE_TAP_2_3:
326 {
327 return ACTION_FUNCTION_TAP(id);
328 break;
329 }
330 default:
331 break;
332 }
333 }
334
335 return pgm_read_word(&fn_actions[function_id]);
336}
337
338
339// Zeal60 specific "action functions"
340void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
341{
342 switch (id)
343 {
344 case TRIPLE_TAP_1_3:
345 case TRIPLE_TAP_2_3:
346 if (record->event.pressed) {
347 layer_on( id == TRIPLE_TAP_1_3 ? 1 : 2 );
348 if (record->tap.count && !record->tap.interrupted) {
349 if (record->tap.count >= 3) {
350 layer_invert(3);
351 }
352 } else {
353 record->tap.count = 0;
354 }
355 } else {
356 layer_off( id == TRIPLE_TAP_1_3 ? 1 : 2 );
357 }
358 break;
359 }
360} 93}
361 94
362void led_set_kb(uint8_t usb_led) 95void led_set_kb(uint8_t usb_led)
363{ 96{
364#if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED 97#if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
365 backlight_set_indicator_state(usb_led); 98 backlight_set_indicator_state(usb_led);
366#endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED 99#endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
367 led_set_user(usb_led); 100 led_set_user(usb_led);
368} 101}
369 102
370void suspend_power_down_kb(void) 103void suspend_power_down_kb(void)
371{ 104{
372#if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED 105#if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
373 backlight_set_suspend_state(true); 106 backlight_set_suspend_state(true);
374#endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED 107#endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
375} 108}
376 109
377void suspend_wakeup_init_kb(void) 110void suspend_wakeup_init_kb(void)
378{ 111{
379#if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED 112#if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
380 backlight_set_suspend_state(false); 113 backlight_set_suspend_state(false);
381#endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED 114#endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
382} 115}
383 116
117// Moving this to the bottom of this source file is a workaround
118// for an intermittent compiler error for Atmel compiler.
119#ifdef VIA_ENABLE
120void raw_hid_receive_kb(uint8_t *data, uint8_t length) {
121 uint8_t *command_id = &(data[0]);
122 uint8_t *command_data = &(data[1]);
123 switch ( *command_id )
124 {
125#if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
126 case id_backlight_config_set_value:
127 {
128 backlight_config_set_value(command_data);
129 break;
130 }
131 case id_backlight_config_get_value:
132 {
133 backlight_config_get_value(command_data);
134 break;
135 }
136 case id_backlight_config_save:
137 {
138 backlight_config_save();
139 break;
140 }
141#endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
142 default:
143 {
144 // Unhandled message.
145 *command_id = id_unhandled;
146 *command_data = *command_data; // force use of variable
147 break;
148 }
149 }
150 // DO NOT call raw_hid_send(data,length) here, let caller do this
151}
152#endif // VIA_ENABLE
153
154//
155// In the case of VIA being disabled, we still need to check if
156// keyboard level EEPROM memory is valid before loading.
157// Thus these are copies of the same functions in VIA, since
158// the backlight settings reuse VIA's EEPROM magic/version,
159// and the ones in via.c won't be compiled in.
160//
161// Yes, this is sub-optimal, and is only here for completeness
162// (i.e. catering to the 1% of people that want wilba.tech LED bling
163// AND want persistent settings BUT DON'T want to use dynamic keymaps/VIA).
164//
165#ifndef VIA_ENABLE
166
167bool via_eeprom_is_valid(void)
168{
169 char *p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
170 uint8_t magic0 = ( ( p[2] & 0x0F ) << 4 ) | ( p[3] & 0x0F );
171 uint8_t magic1 = ( ( p[5] & 0x0F ) << 4 ) | ( p[6] & 0x0F );
172 uint8_t magic2 = ( ( p[8] & 0x0F ) << 4 ) | ( p[9] & 0x0F );
173
174 return (eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+0 ) == magic0 &&
175 eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+1 ) == magic1 &&
176 eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+2 ) == magic2 );
177}
178
179void via_eeprom_set_valid(bool valid)
180{
181 char *p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
182 uint8_t magic0 = ( ( p[2] & 0x0F ) << 4 ) | ( p[3] & 0x0F );
183 uint8_t magic1 = ( ( p[5] & 0x0F ) << 4 ) | ( p[6] & 0x0F );
184 uint8_t magic2 = ( ( p[8] & 0x0F ) << 4 ) | ( p[9] & 0x0F );
185
186 eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+0, valid ? magic0 : 0xFF);
187 eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+1, valid ? magic1 : 0xFF);
188 eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+2, valid ? magic2 : 0xFF);
189}
190
191void via_eeprom_reset(void)
192{
193 // Set the VIA specific EEPROM state as invalid.
194 via_eeprom_set_valid(false);
195 // Set the TMK/QMK EEPROM state as invalid.
196 eeconfig_disable();
197}
198
199#endif // VIA_ENABLE