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/>.
18 #include "keyboards/wilba_tech/wt_mono_backlight.h"
19 #include "keyboards/zeal60/zeal60_api.h" // Temporary hack
20 #include "keyboards/zeal60/zeal60_keycodes.h" // Temporary hack
23 #include "dynamic_keymap.h"
25 #include "tmk_core/common/eeprom.h"
27 bool eeprom_is_valid(void)
29 return (eeprom_read_word(((void*)EEPROM_MAGIC_ADDR)) == EEPROM_MAGIC &&
30 eeprom_read_byte(((void*)EEPROM_VERSION_ADDR)) == EEPROM_VERSION);
33 void eeprom_set_valid(bool valid)
35 eeprom_update_word(((void*)EEPROM_MAGIC_ADDR), valid ? EEPROM_MAGIC : 0xFFFF);
36 eeprom_update_byte(((void*)EEPROM_VERSION_ADDR), valid ? EEPROM_VERSION : 0xFF);
39 void eeprom_reset(void)
41 // Set the Zeal60 specific EEPROM state as invalid.
42 eeprom_set_valid(false);
43 // Set the TMK/QMK EEPROM state as invalid.
49 void raw_hid_receive( uint8_t *data, uint8_t length )
51 uint8_t *command_id = &(data[0]);
52 uint8_t *command_data = &(data[1]);
53 switch ( *command_id )
55 case id_get_protocol_version:
57 command_data[0] = PROTOCOL_VERSION >> 8;
58 command_data[1] = PROTOCOL_VERSION & 0xFF;
61 case id_get_keyboard_value:
63 if ( command_data[0] == id_uptime )
65 uint32_t value = timer_read32();
66 command_data[1] = (value >> 24 ) & 0xFF;
67 command_data[2] = (value >> 16 ) & 0xFF;
68 command_data[3] = (value >> 8 ) & 0xFF;
69 command_data[4] = value & 0xFF;
73 *command_id = id_unhandled;
77 #ifdef DYNAMIC_KEYMAP_ENABLE
78 case id_dynamic_keymap_get_keycode:
80 uint16_t keycode = dynamic_keymap_get_keycode( command_data[0], command_data[1], command_data[2] );
81 command_data[3] = keycode >> 8;
82 command_data[4] = keycode & 0xFF;
85 case id_dynamic_keymap_set_keycode:
87 dynamic_keymap_set_keycode( command_data[0], command_data[1], command_data[2], ( command_data[3] << 8 ) | command_data[4] );
90 case id_dynamic_keymap_reset:
92 dynamic_keymap_reset();
95 case id_dynamic_keymap_macro_get_count:
97 command_data[0] = dynamic_keymap_macro_get_count();
100 case id_dynamic_keymap_macro_get_buffer_size:
102 uint16_t size = dynamic_keymap_macro_get_buffer_size();
103 command_data[0] = size >> 8;
104 command_data[1] = size & 0xFF;
107 case id_dynamic_keymap_macro_get_buffer:
109 uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
110 uint16_t size = command_data[2]; // size <= 28
111 dynamic_keymap_macro_get_buffer( offset, size, &command_data[3] );
114 case id_dynamic_keymap_macro_set_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_set_buffer( offset, size, &command_data[3] );
121 case id_dynamic_keymap_macro_reset:
123 dynamic_keymap_macro_reset();
126 case id_dynamic_keymap_get_layer_count:
128 command_data[0] = dynamic_keymap_get_layer_count();
131 case id_dynamic_keymap_get_buffer:
133 uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
134 uint16_t size = command_data[2]; // size <= 28
135 dynamic_keymap_get_buffer( offset, size, &command_data[3] );
138 case id_dynamic_keymap_set_buffer:
140 uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
141 uint16_t size = command_data[2]; // size <= 28
142 dynamic_keymap_set_buffer( offset, size, &command_data[3] );
145 #endif // DYNAMIC_KEYMAP_ENABLE
146 case id_eeprom_reset:
151 case id_bootloader_jump:
153 // Need to send data back before the jump
154 // Informs host that the command is handled
155 raw_hid_send( data, length );
156 // Give host time to read it
163 // Unhandled message.
164 *command_id = id_unhandled;
169 // Return same buffer with values changed
170 raw_hid_send( data, length );
178 // If the EEPROM has the magic, the data is good.
179 // OK to load from EEPROM.
180 if (eeprom_is_valid()) {
181 //backlight_config_load();
183 // If the EEPROM has not been saved before, or is out of date,
184 // save the default values to the EEPROM. Default values
185 // come from construction of the zeal_backlight_config instance.
186 //backlight_config_save();
187 #ifdef DYNAMIC_KEYMAP_ENABLE
188 // This resets the keymaps in EEPROM to what is in flash.
189 dynamic_keymap_reset();
190 // This resets the macros in EEPROM to nothing.
191 dynamic_keymap_macro_reset();
193 // Save the magic number last, in case saving was interrupted
194 eeprom_set_valid(true);
197 // Initialize LED drivers for backlight.
198 backlight_init_drivers();
200 backlight_timer_init();
201 backlight_timer_enable();
204 void bootmagic_lite(void)
206 // The lite version of TMK's bootmagic.
207 // 100% less potential for accidentally making the
208 // keyboard do stupid things.
210 // We need multiple scans because debouncing can't be turned off.
212 wait_ms(DEBOUNCING_DELAY);
213 wait_ms(DEBOUNCING_DELAY);
216 // If the Esc (matrix 0,0) is held down on power up,
217 // reset the EEPROM valid state and jump to bootloader.
218 if ( matrix_get_row(0) & (1<<0) ) {
224 void matrix_init_kb(void)
231 void matrix_scan_kb(void)
233 // This only updates the LED driver buffers if something has changed.
234 backlight_update_pwm_buffers();
238 bool process_record_kb(uint16_t keycode, keyrecord_t *record)
242 if (record->event.pressed) {
244 update_tri_layer(1, 2, 3);
247 update_tri_layer(1, 2, 3);
252 if (record->event.pressed) {
254 update_tri_layer(1, 2, 3);
257 update_tri_layer(1, 2, 3);
263 #ifdef DYNAMIC_KEYMAP_ENABLE
265 if (record->event.pressed) {
266 if ( keycode >= MACRO00 && keycode <= MACRO15 )
268 uint8_t id = keycode - MACRO00;
269 dynamic_keymap_macro_send(id);
273 #endif //DYNAMIC_KEYMAP_ENABLE
275 return process_record_user(keycode, record);