1 /* Copyright 2018 Jason Williams (Wilba)
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.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 // Check that no backlight functions are called
20 #if RGB_BACKLIGHT_ENABLED
21 #include "keyboards/wilba_tech/wt_rgb_backlight.h"
22 #endif // RGB_BACKLIGHT_ENABLED
23 #if MONO_BACKLIGHT_ENABLED
24 #include "keyboards/wilba_tech/wt_mono_backlight.h"
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
30 #include "dynamic_keymap.h"
32 #include "tmk_core/common/eeprom.h"
34 bool eeprom_is_valid(void)
36 return (eeprom_read_word(((void*)EEPROM_MAGIC_ADDR
)) == EEPROM_MAGIC
&&
37 eeprom_read_byte(((void*)EEPROM_VERSION_ADDR
)) == EEPROM_VERSION
);
40 void eeprom_set_valid(bool valid
)
42 eeprom_update_word(((void*)EEPROM_MAGIC_ADDR
), valid
? EEPROM_MAGIC
: 0xFFFF);
43 eeprom_update_byte(((void*)EEPROM_VERSION_ADDR
), valid
? EEPROM_VERSION
: 0xFF);
46 void eeprom_reset(void)
48 // Set the Zeal60 specific EEPROM state as invalid.
49 eeprom_set_valid(false);
50 // Set the TMK/QMK EEPROM state as invalid.
56 void raw_hid_receive( uint8_t *data
, uint8_t length
)
58 uint8_t *command_id
= &(data
[0]);
59 uint8_t *command_data
= &(data
[1]);
60 switch ( *command_id
)
62 case id_get_protocol_version
:
64 command_data
[0] = PROTOCOL_VERSION
>> 8;
65 command_data
[1] = PROTOCOL_VERSION
& 0xFF;
68 case id_get_keyboard_value
:
70 if ( command_data
[0] == id_uptime
)
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;
80 *command_id
= id_unhandled
;
84 #ifdef DYNAMIC_KEYMAP_ENABLE
85 case id_dynamic_keymap_get_keycode
:
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;
92 case id_dynamic_keymap_set_keycode
:
94 dynamic_keymap_set_keycode( command_data
[0], command_data
[1], command_data
[2], ( command_data
[3] << 8 ) | command_data
[4] );
97 case id_dynamic_keymap_reset
:
99 dynamic_keymap_reset();
102 case id_dynamic_keymap_macro_get_count
:
104 command_data
[0] = dynamic_keymap_macro_get_count();
107 case id_dynamic_keymap_macro_get_buffer_size
:
109 uint16_t size
= dynamic_keymap_macro_get_buffer_size();
110 command_data
[0] = size
>> 8;
111 command_data
[1] = size
& 0xFF;
114 case id_dynamic_keymap_macro_get_buffer
:
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] );
121 case id_dynamic_keymap_macro_set_buffer
:
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] );
128 case id_dynamic_keymap_macro_reset
:
130 dynamic_keymap_macro_reset();
133 case id_dynamic_keymap_get_layer_count
:
135 command_data
[0] = dynamic_keymap_get_layer_count();
138 case id_dynamic_keymap_get_buffer
:
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] );
145 case id_dynamic_keymap_set_buffer
:
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] );
152 #endif // DYNAMIC_KEYMAP_ENABLE
153 #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
154 case id_backlight_config_set_value
:
156 backlight_config_set_value(command_data
);
159 case id_backlight_config_get_value
:
161 backlight_config_get_value(command_data
);
164 case id_backlight_config_save
:
166 backlight_config_save();
169 #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
170 case id_eeprom_reset
:
175 case id_bootloader_jump
:
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
187 // Unhandled message.
188 *command_id
= id_unhandled
;
193 // Return same buffer with values changed
194 raw_hid_send( data
, length
);
202 // If the EEPROM has the magic, the data is good.
203 // OK to load from EEPROM.
204 if (eeprom_is_valid()) {
205 #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
206 backlight_config_load();
207 #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
209 #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
210 // If the EEPROM has not been saved before, or is out of date,
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
215 #ifdef DYNAMIC_KEYMAP_ENABLE
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);
225 #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
226 // Initialize LED drivers for backlight.
227 backlight_init_drivers();
229 backlight_timer_init();
230 backlight_timer_enable();
231 #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
234 void bootmagic_lite(void)
236 // The lite version of TMK's bootmagic.
237 // 100% less potential for accidentally making the
238 // keyboard do stupid things.
240 // We need multiple scans because debouncing can't be turned off.
246 // If the Esc (matrix 0,0) is held down on power up,
247 // reset the EEPROM valid state and jump to bootloader.
248 if ( matrix_get_row(0) & (1<<0) ) {
254 void matrix_init_kb(void)
261 void matrix_scan_kb(void)
263 #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
264 // This only updates the LED driver buffers if something has changed.
265 backlight_update_pwm_buffers();
266 #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
270 bool process_record_kb(uint16_t keycode
, keyrecord_t
*record
)
272 #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
273 process_record_backlight(keycode
, record
);
274 #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
278 if (record
->event
.pressed
) {
280 update_tri_layer(1, 2, 3);
283 update_tri_layer(1, 2, 3);
288 if (record
->event
.pressed
) {
290 update_tri_layer(1, 2, 3);
293 update_tri_layer(1, 2, 3);
299 #ifdef DYNAMIC_KEYMAP_ENABLE
301 if (record
->event
.pressed
) {
302 if ( keycode
>= MACRO00
&& keycode
<= MACRO15
)
304 uint8_t id
= keycode
- MACRO00
;
305 dynamic_keymap_macro_send(id
);
309 #endif //DYNAMIC_KEYMAP_ENABLE
311 return process_record_user(keycode
, record
);
314 // This overrides the one in quantum/keymap_common.c
315 uint16_t keymap_function_id_to_action( uint16_t function_id
)
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 )
322 uint8_t id
= function_id
& 0xFF;
327 return ACTION_FUNCTION_TAP(id
);
335 return pgm_read_word(&fn_actions
[function_id
]);
339 // Zeal60 specific "action functions"
340 void action_function(keyrecord_t
*record
, uint8_t id
, uint8_t opt
)
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) {
353 record
->tap
.count
= 0;
356 layer_off( id
== TRIPLE_TAP_1_3
? 1 : 2 );
362 void led_set_kb(uint8_t usb_led
)
364 #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
365 backlight_set_indicator_state(usb_led
);
366 #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
367 led_set_user(usb_led
);
370 void suspend_power_down_kb(void)
372 #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
373 backlight_set_suspend_state(true);
374 #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
377 void suspend_wakeup_init_kb(void)
379 #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
380 backlight_set_suspend_state(false);
381 #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED