]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/wilba_tech/wt_main.c
de6d7b92c176e7fbf61da89145a0f5c3bf61d165
[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 #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
21
22 #include "raw_hid.h"
23 #include "dynamic_keymap.h"
24 #include "timer.h"
25 #include "tmk_core/common/eeprom.h"
26
27 bool eeprom_is_valid(void)
28 {
29         return (eeprom_read_word(((void*)EEPROM_MAGIC_ADDR)) == EEPROM_MAGIC &&
30                         eeprom_read_byte(((void*)EEPROM_VERSION_ADDR)) == EEPROM_VERSION);
31 }
32
33 void eeprom_set_valid(bool valid)
34 {
35         eeprom_update_word(((void*)EEPROM_MAGIC_ADDR), valid ? EEPROM_MAGIC : 0xFFFF);
36         eeprom_update_byte(((void*)EEPROM_VERSION_ADDR), valid ? EEPROM_VERSION : 0xFF);
37 }
38
39 void eeprom_reset(void)
40 {
41         // Set the Zeal60 specific EEPROM state as invalid.
42         eeprom_set_valid(false);
43         // Set the TMK/QMK EEPROM state as invalid.
44         eeconfig_disable();
45 }
46
47 #ifdef RAW_ENABLE
48
49 void raw_hid_receive( uint8_t *data, uint8_t length )
50 {
51         uint8_t *command_id = &(data[0]);
52         uint8_t *command_data = &(data[1]);
53         switch ( *command_id )
54         {
55                 case id_get_protocol_version:
56                 {
57                         command_data[0] = PROTOCOL_VERSION >> 8;
58                         command_data[1] = PROTOCOL_VERSION & 0xFF;
59                         break;
60                 }
61                 case id_get_keyboard_value:
62                 {
63                         if ( command_data[0] == id_uptime )
64                         {
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;
70                         }
71                         else
72                         {
73                                 *command_id = id_unhandled;
74                         }
75                         break;
76                 }
77 #ifdef DYNAMIC_KEYMAP_ENABLE
78                 case id_dynamic_keymap_get_keycode:
79                 {
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;
83                         break;
84                 }
85                 case id_dynamic_keymap_set_keycode:
86                 {
87                         dynamic_keymap_set_keycode( command_data[0], command_data[1], command_data[2], ( command_data[3] << 8 ) | command_data[4] );
88                         break;
89                 }
90                 case id_dynamic_keymap_reset:
91                 {
92                         dynamic_keymap_reset();
93                         break;
94                 }
95                 case id_dynamic_keymap_macro_get_count:
96                 {
97                         command_data[0] = dynamic_keymap_macro_get_count();
98                         break;
99                 }
100                 case id_dynamic_keymap_macro_get_buffer_size:
101                 {
102                         uint16_t size = dynamic_keymap_macro_get_buffer_size();
103                         command_data[0] = size >> 8;
104                         command_data[1] = size & 0xFF;
105                         break;
106                 }
107                 case id_dynamic_keymap_macro_get_buffer:
108                 {
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] );
112                         break;
113                 }
114                 case id_dynamic_keymap_macro_set_buffer:
115                 {
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] );
119                         break;
120                 }
121                 case id_dynamic_keymap_macro_reset:
122                 {
123                         dynamic_keymap_macro_reset();
124                         break;
125                 }
126                 case id_dynamic_keymap_get_layer_count:
127                 {
128                         command_data[0] = dynamic_keymap_get_layer_count();
129                         break;
130                 }
131                 case id_dynamic_keymap_get_buffer:
132                 {
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] );
136                         break;
137                 }
138                 case id_dynamic_keymap_set_buffer:
139                 {
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] );
143                         break;
144                 }
145 #endif // DYNAMIC_KEYMAP_ENABLE
146                 case id_eeprom_reset:
147                 {
148                         eeprom_reset();
149                         break;
150                 }
151                 case id_bootloader_jump:
152                 {
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
157                         wait_ms(100);
158                         bootloader_jump();
159                         break;
160                 }
161                 default:
162                 {
163                         // Unhandled message.
164                         *command_id = id_unhandled;
165                         break;
166                 }
167         }
168
169         // Return same buffer with values changed
170         raw_hid_send( data, length );
171
172 }
173
174 #endif
175
176 void main_init(void)
177 {
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();
182         } else  {
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();
192 #endif
193                 // Save the magic number last, in case saving was interrupted
194                 eeprom_set_valid(true);
195         }
196
197         // Initialize LED drivers for backlight.
198         backlight_init_drivers();
199
200         backlight_timer_init();
201         backlight_timer_enable();
202 }
203
204 void bootmagic_lite(void)
205 {
206         // The lite version of TMK's bootmagic.
207         // 100% less potential for accidentally making the
208         // keyboard do stupid things.
209
210         // We need multiple scans because debouncing can't be turned off.
211         matrix_scan();
212         wait_ms(DEBOUNCING_DELAY);
213         wait_ms(DEBOUNCING_DELAY);
214         matrix_scan();
215
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) ) {
219                 eeprom_reset();
220                 bootloader_jump();
221         }
222 }
223
224 void matrix_init_kb(void)
225 {
226         bootmagic_lite();
227         main_init();
228         matrix_init_user();
229 }
230
231 void matrix_scan_kb(void)
232 {
233         // This only updates the LED driver buffers if something has changed.
234         backlight_update_pwm_buffers();
235         matrix_scan_user();
236 }
237
238 bool process_record_kb(uint16_t keycode, keyrecord_t *record)
239 {
240         switch(keycode) {
241                 case FN_MO13:
242                         if (record->event.pressed) {
243                                 layer_on(1);
244                                 update_tri_layer(1, 2, 3);
245                         } else {
246                                 layer_off(1);
247                                 update_tri_layer(1, 2, 3);
248                         }
249                         return false;
250                         break;
251                 case FN_MO23:
252                         if (record->event.pressed) {
253                                 layer_on(2);
254                                 update_tri_layer(1, 2, 3);
255                         } else {
256                                 layer_off(2);
257                                 update_tri_layer(1, 2, 3);
258                         }
259                         return false;
260                         break;
261         }
262
263 #ifdef DYNAMIC_KEYMAP_ENABLE
264         // Handle macros
265         if (record->event.pressed) {
266                 if ( keycode >= MACRO00 && keycode <= MACRO15 )
267                 {
268                         uint8_t id = keycode - MACRO00;
269                         dynamic_keymap_macro_send(id);
270                         return false;
271                 }
272         }
273 #endif //DYNAMIC_KEYMAP_ENABLE
274
275         return process_record_user(keycode, record);
276 }