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 "rgb_backlight.h"
22 #endif // RGB_BACKLIGHT_ENABLED
25 #include "dynamic_keymap.h"
27 #include "tmk_core/common/eeprom.h"
29 bool eeprom_is_valid(void)
31 return (eeprom_read_word(((void*)EEPROM_MAGIC_ADDR)) == EEPROM_MAGIC &&
32 eeprom_read_byte(((void*)EEPROM_VERSION_ADDR)) == EEPROM_VERSION);
35 void eeprom_set_valid(bool valid)
37 eeprom_update_word(((void*)EEPROM_MAGIC_ADDR), valid ? EEPROM_MAGIC : 0xFFFF);
38 eeprom_update_byte(((void*)EEPROM_VERSION_ADDR), valid ? EEPROM_VERSION : 0xFF);
41 void eeprom_reset(void)
43 // Set the keyboard-specific EEPROM state as invalid.
44 eeprom_set_valid(false);
45 // Set the TMK/QMK EEPROM state as invalid.
51 void raw_hid_receive( uint8_t *data, uint8_t length )
53 uint8_t *command_id = &(data[0]);
54 uint8_t *command_data = &(data[1]);
55 switch ( *command_id )
57 case id_get_protocol_version:
59 command_data[0] = PROTOCOL_VERSION >> 8;
60 command_data[1] = PROTOCOL_VERSION & 0xFF;
63 case id_get_keyboard_value:
65 if ( command_data[0] == id_uptime )
67 uint32_t value = timer_read32();
68 command_data[1] = (value >> 24 ) & 0xFF;
69 command_data[2] = (value >> 16 ) & 0xFF;
70 command_data[3] = (value >> 8 ) & 0xFF;
71 command_data[4] = value & 0xFF;
75 *command_id = id_unhandled;
79 #ifdef DYNAMIC_KEYMAP_ENABLE
80 case id_dynamic_keymap_get_keycode:
82 uint16_t keycode = dynamic_keymap_get_keycode( command_data[0], command_data[1], command_data[2] );
83 command_data[3] = keycode >> 8;
84 command_data[4] = keycode & 0xFF;
87 case id_dynamic_keymap_set_keycode:
89 dynamic_keymap_set_keycode( command_data[0], command_data[1], command_data[2], ( command_data[3] << 8 ) | command_data[4] );
92 case id_dynamic_keymap_reset:
94 dynamic_keymap_reset();
97 #endif // DYNAMIC_KEYMAP_ENABLE
98 #if RGB_BACKLIGHT_ENABLED
99 case id_backlight_config_set_value:
101 //backlight_config_set_value(command_data);
104 case id_backlight_config_get_value:
106 //backlight_config_get_value(command_data);
109 case id_backlight_config_save:
111 //backlight_config_save();
114 #endif // RGB_BACKLIGHT_ENABLED
115 case id_eeprom_reset:
120 case id_bootloader_jump:
122 // Need to send data back before the jump
123 // Informs host that the command is handled
124 raw_hid_send( data, length );
125 // Give host time to read it
132 // Unhandled message.
133 *command_id = id_unhandled;
138 // Return same buffer with values changed
139 raw_hid_send( data, length );
147 // If the EEPROM has the magic, the data is good.
148 // OK to load from EEPROM.
149 if (eeprom_is_valid()) {
150 #if RGB_BACKLIGHT_ENABLED
151 //backlight_config_load();
152 #endif // RGB_BACKLIGHT_ENABLED
154 #if RGB_BACKLIGHT_ENABLED
155 // If the EEPROM has not been saved before, or is out of date,
156 // save the default values to the EEPROM. Default values
157 // come from construction of the zeal_backlight_config instance.
158 //backlight_config_save();
159 #endif // RGB_BACKLIGHT_ENABLED
160 #ifdef DYNAMIC_KEYMAP_ENABLE
161 // This resets the keymaps in EEPROM to what is in flash.
162 dynamic_keymap_reset();
164 // Save the magic number last, in case saving was interrupted
165 eeprom_set_valid(true);
167 #if RGB_BACKLIGHT_ENABLED
168 // Initialize LED drivers for backlight.
169 backlight_init_drivers();
171 backlight_timer_init();
172 backlight_timer_enable();
173 #endif // RGB_BACKLIGHT_ENABLED
176 void bootmagic_lite(void)
178 // The lite version of TMK's bootmagic.
179 // 100% less potential for accidentally making the
180 // keyboard do stupid things.
182 // We need multiple scans because debouncing can't be turned off.
184 wait_ms(DEBOUNCING_DELAY);
185 wait_ms(DEBOUNCING_DELAY);
188 // If the Esc (matrix 0,0) is held down on power up,
189 // reset the EEPROM valid state and jump to bootloader.
190 if ( matrix_get_row(0) & (1<<0) ) {
196 void matrix_init_kb(void) {
202 void matrix_scan_kb(void) {
203 #if RGB_BACKLIGHT_ENABLED
204 // This only updates the LED driver buffers if something has changed.
205 backlight_update_pwm_buffers();
206 #endif // BACKLIGHT_ENABLED
210 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
211 return process_record_user(keycode, record);
214 void led_set_kb(uint8_t usb_led) {
215 led_set_user(usb_led);
218 void suspend_power_down_kb(void)
220 #if RGB_BACKLIGHT_ENABLED
221 //backlight_set_suspend_state(true);
222 #endif // BACKLIGHT_ENABLED
225 void suspend_wakeup_init_kb(void)
227 #if RGB_BACKLIGHT_ENABLED
228 //backlight_set_suspend_state(false);
229 #endif // BACKLIGHT_ENABLED