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
22 #include "dynamic_keymap.h"
24 #include "tmk_core/common/eeprom.h"
26 bool eeprom_is_valid(void)
28 return (eeprom_read_word(((void*)EEPROM_MAGIC_ADDR)) == EEPROM_MAGIC &&
29 eeprom_read_byte(((void*)EEPROM_VERSION_ADDR)) == EEPROM_VERSION);
32 void eeprom_set_valid(bool valid)
34 eeprom_update_word(((void*)EEPROM_MAGIC_ADDR), valid ? EEPROM_MAGIC : 0xFFFF);
35 eeprom_update_byte(((void*)EEPROM_VERSION_ADDR), valid ? EEPROM_VERSION : 0xFF);
38 void eeprom_reset(void)
40 // Set the Zeal60 specific EEPROM state as invalid.
41 eeprom_set_valid(false);
42 // Set the TMK/QMK EEPROM state as invalid.
48 void raw_hid_receive( uint8_t *data, uint8_t length )
50 uint8_t *command_id = &(data[0]);
51 uint8_t *command_data = &(data[1]);
52 switch ( *command_id )
54 case id_get_protocol_version:
56 command_data[0] = PROTOCOL_VERSION >> 8;
57 command_data[1] = PROTOCOL_VERSION & 0xFF;
60 case id_get_keyboard_value:
62 if ( command_data[0] == id_uptime )
64 uint32_t value = timer_read32();
65 command_data[1] = (value >> 24 ) & 0xFF;
66 command_data[2] = (value >> 16 ) & 0xFF;
67 command_data[3] = (value >> 8 ) & 0xFF;
68 command_data[4] = value & 0xFF;
72 *command_id = id_unhandled;
76 #ifdef DYNAMIC_KEYMAP_ENABLE
77 case id_dynamic_keymap_get_keycode:
79 uint16_t keycode = dynamic_keymap_get_keycode( command_data[0], command_data[1], command_data[2] );
80 command_data[3] = keycode >> 8;
81 command_data[4] = keycode & 0xFF;
84 case id_dynamic_keymap_set_keycode:
86 dynamic_keymap_set_keycode( command_data[0], command_data[1], command_data[2], ( command_data[3] << 8 ) | command_data[4] );
89 case id_dynamic_keymap_reset:
91 dynamic_keymap_reset();
94 #endif // DYNAMIC_KEYMAP_ENABLE
95 case id_backlight_config_set_value:
97 //backlight_config_set_value(command_data);
100 case id_backlight_config_get_value:
102 //backlight_config_get_value(command_data);
105 case id_backlight_config_save:
107 //backlight_config_save();
110 case id_eeprom_reset:
115 case id_bootloader_jump:
117 // Need to send data back before the jump
118 // Informs host that the command is handled
119 raw_hid_send( data, length );
120 // Give host time to read it
127 // Unhandled message.
128 *command_id = id_unhandled;
133 // Return same buffer with values changed
134 raw_hid_send( data, length );
142 // If the EEPROM has the magic, the data is good.
143 // OK to load from EEPROM.
144 if (eeprom_is_valid()) {
145 //backlight_config_load();
147 // If the EEPROM has not been saved before, or is out of date,
148 // save the default values to the EEPROM. Default values
149 // come from construction of the zeal_backlight_config instance.
150 //backlight_config_save();
151 #ifdef DYNAMIC_KEYMAP_ENABLE
152 // This resets the keymaps in EEPROM to what is in flash.
153 dynamic_keymap_reset();
155 // Save the magic number last, in case saving was interrupted
156 eeprom_set_valid(true);
159 // Initialize LED drivers for backlight.
160 backlight_init_drivers();
162 backlight_timer_init();
163 backlight_timer_enable();
166 void bootmagic_lite(void)
168 // The lite version of TMK's bootmagic.
169 // 100% less potential for accidentally making the
170 // keyboard do stupid things.
172 // We need multiple scans because debouncing can't be turned off.
174 wait_ms(DEBOUNCING_DELAY);
175 wait_ms(DEBOUNCING_DELAY);
178 // If the Esc (matrix 0,0) is held down on power up,
179 // reset the EEPROM valid state and jump to bootloader.
180 if ( matrix_get_row(0) & (1<<0) ) {
186 void matrix_init_kb(void)
193 void matrix_scan_kb(void)
195 // This only updates the LED driver buffers if something has changed.
196 backlight_update_pwm_buffers();