]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/wilba_tech/wt_main.c
f8056839a7b2a3adcafb74838cc579622c3eac88
[qmk_firmware.git] / keyboards / wilba_tech / wt_main.c
1 /* Copyright 2018 Jason Williams (Wilba)
2  *
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.
7  *
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.
12  *
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/>.
15  */
16
17 #include "quantum.h"
18 #ifdef WT_MONO_BACKLIGHT
19 #include "keyboards/wilba_tech/wt_mono_backlight.h"
20 #endif
21 #include "keyboards/zeal60/zeal60_api.h" // Temporary hack
22 #include "keyboards/zeal60/zeal60_keycodes.h" // Temporary hack
23
24 #include "raw_hid.h"
25 #include "dynamic_keymap.h"
26 #include "timer.h"
27 #include "tmk_core/common/eeprom.h"
28
29 bool eeprom_is_valid(void)
30 {
31         return (eeprom_read_word(((void*)EEPROM_MAGIC_ADDR)) == EEPROM_MAGIC &&
32                         eeprom_read_byte(((void*)EEPROM_VERSION_ADDR)) == EEPROM_VERSION);
33 }
34
35 void eeprom_set_valid(bool valid)
36 {
37         eeprom_update_word(((void*)EEPROM_MAGIC_ADDR), valid ? EEPROM_MAGIC : 0xFFFF);
38         eeprom_update_byte(((void*)EEPROM_VERSION_ADDR), valid ? EEPROM_VERSION : 0xFF);
39 }
40
41 void eeprom_reset(void)
42 {
43         // Set the Zeal60 specific EEPROM state as invalid.
44         eeprom_set_valid(false);
45         // Set the TMK/QMK EEPROM state as invalid.
46         eeconfig_disable();
47 }
48
49 #ifdef RAW_ENABLE
50
51 void raw_hid_receive( uint8_t *data, uint8_t length )
52 {
53         uint8_t *command_id = &(data[0]);
54         uint8_t *command_data = &(data[1]);
55         switch ( *command_id )
56         {
57                 case id_get_protocol_version:
58                 {
59                         command_data[0] = PROTOCOL_VERSION >> 8;
60                         command_data[1] = PROTOCOL_VERSION & 0xFF;
61                         break;
62                 }
63                 case id_get_keyboard_value:
64                 {
65                         if ( command_data[0] == id_uptime )
66                         {
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;
72                         }
73                         else
74                         {
75                                 *command_id = id_unhandled;
76                         }
77                         break;
78                 }
79 #ifdef DYNAMIC_KEYMAP_ENABLE
80                 case id_dynamic_keymap_get_keycode:
81                 {
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;
85                         break;
86                 }
87                 case id_dynamic_keymap_set_keycode:
88                 {
89                         dynamic_keymap_set_keycode( command_data[0], command_data[1], command_data[2], ( command_data[3] << 8 ) | command_data[4] );
90                         break;
91                 }
92                 case id_dynamic_keymap_reset:
93                 {
94                         dynamic_keymap_reset();
95                         break;
96                 }
97                 case id_dynamic_keymap_macro_get_count:
98                 {
99                         command_data[0] = dynamic_keymap_macro_get_count();
100                         break;
101                 }
102                 case id_dynamic_keymap_macro_get_buffer_size:
103                 {
104                         uint16_t size = dynamic_keymap_macro_get_buffer_size();
105                         command_data[0] = size >> 8;
106                         command_data[1] = size & 0xFF;
107                         break;
108                 }
109                 case id_dynamic_keymap_macro_get_buffer:
110                 {
111                         uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
112                         uint16_t size = command_data[2]; // size <= 28
113                         dynamic_keymap_macro_get_buffer( offset, size, &command_data[3] );
114                         break;
115                 }
116                 case id_dynamic_keymap_macro_set_buffer:
117                 {
118                         uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
119                         uint16_t size = command_data[2]; // size <= 28
120                         dynamic_keymap_macro_set_buffer( offset, size, &command_data[3] );
121                         break;
122                 }
123                 case id_dynamic_keymap_macro_reset:
124                 {
125                         dynamic_keymap_macro_reset();
126                         break;
127                 }
128                 case id_dynamic_keymap_get_layer_count:
129                 {
130                         command_data[0] = dynamic_keymap_get_layer_count();
131                         break;
132                 }
133                 case id_dynamic_keymap_get_buffer:
134                 {
135                         uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
136                         uint16_t size = command_data[2]; // size <= 28
137                         dynamic_keymap_get_buffer( offset, size, &command_data[3] );
138                         break;
139                 }
140                 case id_dynamic_keymap_set_buffer:
141                 {
142                         uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
143                         uint16_t size = command_data[2]; // size <= 28
144                         dynamic_keymap_set_buffer( offset, size, &command_data[3] );
145                         break;
146                 }
147 #endif // DYNAMIC_KEYMAP_ENABLE
148                 case id_eeprom_reset:
149                 {
150                         eeprom_reset();
151                         break;
152                 }
153                 case id_bootloader_jump:
154                 {
155                         // Need to send data back before the jump
156                         // Informs host that the command is handled
157                         raw_hid_send( data, length );
158                         // Give host time to read it
159                         wait_ms(100);
160                         bootloader_jump();
161                         break;
162                 }
163                 default:
164                 {
165                         // Unhandled message.
166                         *command_id = id_unhandled;
167                         break;
168                 }
169         }
170
171         // Return same buffer with values changed
172         raw_hid_send( data, length );
173
174 }
175
176 #endif
177
178 void main_init(void)
179 {
180         // If the EEPROM has the magic, the data is good.
181         // OK to load from EEPROM.
182         if (eeprom_is_valid()) {
183                 //backlight_config_load();
184         } else  {
185                 // If the EEPROM has not been saved before, or is out of date,
186                 // save the default values to the EEPROM. Default values
187                 // come from construction of the zeal_backlight_config instance.
188                 //backlight_config_save();
189 #ifdef DYNAMIC_KEYMAP_ENABLE
190                 // This resets the keymaps in EEPROM to what is in flash.
191                 dynamic_keymap_reset();
192                 // This resets the macros in EEPROM to nothing.
193                 dynamic_keymap_macro_reset();
194 #endif
195                 // Save the magic number last, in case saving was interrupted
196                 eeprom_set_valid(true);
197         }
198
199 #ifdef WT_MONO_BACKLIGHT
200         // Initialize LED drivers for backlight.
201         backlight_init_drivers();
202
203         backlight_timer_init();
204         backlight_timer_enable();
205 #endif
206 }
207
208 void bootmagic_lite(void)
209 {
210         // The lite version of TMK's bootmagic.
211         // 100% less potential for accidentally making the
212         // keyboard do stupid things.
213
214         // We need multiple scans because debouncing can't be turned off.
215         matrix_scan();
216         wait_ms(DEBOUNCE);
217         wait_ms(DEBOUNCE);
218         matrix_scan();
219
220         // If the Esc (matrix 0,0) is held down on power up,
221         // reset the EEPROM valid state and jump to bootloader.
222         if ( matrix_get_row(0) & (1<<0) ) {
223                 eeprom_reset();
224                 bootloader_jump();
225         }
226 }
227
228 void matrix_init_kb(void)
229 {
230         bootmagic_lite();
231         main_init();
232         matrix_init_user();
233 }
234
235 void matrix_scan_kb(void)
236 {
237 #ifdef WT_MONO_BACKLIGHT
238         // This only updates the LED driver buffers if something has changed.
239         backlight_update_pwm_buffers();
240 #endif
241         matrix_scan_user();
242 }
243
244 bool process_record_kb(uint16_t keycode, keyrecord_t *record)
245 {
246         switch(keycode) {
247                 case FN_MO13:
248                         if (record->event.pressed) {
249                                 layer_on(1);
250                                 update_tri_layer(1, 2, 3);
251                         } else {
252                                 layer_off(1);
253                                 update_tri_layer(1, 2, 3);
254                         }
255                         return false;
256                         break;
257                 case FN_MO23:
258                         if (record->event.pressed) {
259                                 layer_on(2);
260                                 update_tri_layer(1, 2, 3);
261                         } else {
262                                 layer_off(2);
263                                 update_tri_layer(1, 2, 3);
264                         }
265                         return false;
266                         break;
267         }
268
269 #ifdef DYNAMIC_KEYMAP_ENABLE
270         // Handle macros
271         if (record->event.pressed) {
272                 if ( keycode >= MACRO00 && keycode <= MACRO15 )
273                 {
274                         uint8_t id = keycode - MACRO00;
275                         dynamic_keymap_macro_send(id);
276                         return false;
277                 }
278         }
279 #endif //DYNAMIC_KEYMAP_ENABLE
280
281         return process_record_user(keycode, record);
282 }