]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/zeal60/zeal60.c
Refactoring M6-A, M6-B, Zeal60, Zeal65, WT60-A, WT65-A, WT80-A (#4417)
[qmk_firmware.git] / keyboards / zeal60 / zeal60.c
1 /* Copyright 2017 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 #include "zeal60.h"
17 #include "zeal60_api.h"
18
19 // Check that no backlight functions are called
20 #if RGB_BACKLIGHT_ENABLED
21 #include "rgb_backlight.h"
22 #endif // BACKLIGHT_ENABLED
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 #endif // DYNAMIC_KEYMAP_ENABLE
98 #if RGB_BACKLIGHT_ENABLED
99                 case id_backlight_config_set_value:
100                 {
101                         backlight_config_set_value(command_data);
102                         break;
103                 }
104                 case id_backlight_config_get_value:
105                 {
106                         backlight_config_get_value(command_data);
107                         break;
108                 }
109                 case id_backlight_config_save:
110                 {
111                         backlight_config_save();
112                         break;
113                 }
114 #endif // RGB_BACKLIGHT_ENABLED
115                 case id_eeprom_reset:
116                 {
117                         eeprom_reset();
118                         break;
119                 }
120                 case id_bootloader_jump:
121                 {
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
126                         wait_ms(100);
127                         bootloader_jump();
128                         break;
129                 }
130                 default:
131                 {
132                         // Unhandled message.
133                         *command_id = id_unhandled;
134                         break;
135                 }
136         }
137
138         // Return same buffer with values changed
139         raw_hid_send( data, length );
140
141 }
142
143 #endif
144
145 void main_init(void)
146 {
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
153         } else  {
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();
163 #endif
164                 // Save the magic number last, in case saving was interrupted
165                 eeprom_set_valid(true);
166         }
167
168 #if RGB_BACKLIGHT_ENABLED
169         // Initialize LED drivers for backlight.
170         backlight_init_drivers();
171
172         backlight_timer_init();
173         backlight_timer_enable();
174 #endif // RGB_BACKLIGHT_ENABLED
175 }
176
177 void bootmagic_lite(void)
178 {
179         // The lite version of TMK's bootmagic.
180         // 100% less potential for accidentally making the
181         // keyboard do stupid things.
182
183         // We need multiple scans because debouncing can't be turned off.
184         matrix_scan();
185         wait_ms(DEBOUNCING_DELAY);
186         wait_ms(DEBOUNCING_DELAY);
187         matrix_scan();
188
189         // If the Esc (matrix 0,0) is held down on power up,
190         // reset the EEPROM valid state and jump to bootloader.
191         if ( matrix_get_row(0) & (1<<0) ) {
192                 eeprom_reset();
193                 bootloader_jump();
194         }
195 }
196
197 void matrix_init_kb(void)
198 {
199         bootmagic_lite();
200         main_init();
201         matrix_init_user();
202 }
203
204 void matrix_scan_kb(void)
205 {
206 #if RGB_BACKLIGHT_ENABLED
207         // This only updates the LED driver buffers if something has changed.
208         backlight_update_pwm_buffers();
209 #endif // BACKLIGHT_ENABLED
210         matrix_scan_user();
211 }
212
213 bool process_record_kb(uint16_t keycode, keyrecord_t *record)
214 {
215 #if RGB_BACKLIGHT_ENABLED
216         process_record_backlight(keycode, record);
217 #endif // BACKLIGHT_ENABLED
218
219         switch(keycode) {
220                 case FN_MO13:
221                         if (record->event.pressed) {
222                                 layer_on(1);
223                                 update_tri_layer(1, 2, 3);
224                         } else {
225                                 layer_off(1);
226                                 update_tri_layer(1, 2, 3);
227                         }
228                         return false;
229                         break;
230                 case FN_MO23:
231                         if (record->event.pressed) {
232                                 layer_on(2);
233                                 update_tri_layer(1, 2, 3);
234                         } else {
235                                 layer_off(2);
236                                 update_tri_layer(1, 2, 3);
237                         }
238                         return false;
239                         break;
240         }
241         
242         return process_record_user(keycode, record);
243 }
244
245 // This overrides the one in quantum/keymap_common.c
246 uint16_t keymap_function_id_to_action( uint16_t function_id )
247 {
248         // Zeal60 specific "action functions" are 0xF00 to 0xFFF
249         // i.e. F(0xF00) to F(0xFFF) are mapped to
250         // enum zeal60_action_functions by masking last 8 bits.
251         if ( function_id >= 0x0F00 && function_id <= 0x0FFF )
252         {
253                 uint8_t id = function_id & 0xFF;
254                 switch ( id ) {
255                         case TRIPLE_TAP_1_3:
256                         case TRIPLE_TAP_2_3:
257                         {
258                                 return ACTION_FUNCTION_TAP(id);
259                                 break;
260                         }
261                         default:
262                                 break;
263                 }
264         }
265
266 #if USE_KEYMAPS_IN_EEPROM
267
268 #if 0
269         // This is how to implement actions stored in EEPROM.
270         // Not yet implemented. Not sure if it's worth the trouble
271         // before we have a nice GUI for keymap editing.
272         if ( eeprom_is_valid() &&
273                  function_id < 32 ) // TODO: replace magic number
274         {
275                 uint16_t action = keymap_action_load(function_id);
276
277                 // If action is not "empty", return it, otherwise
278                 // drop down to return the one in flash
279                 if ( action != 0x0000 ) // TODO: replace magic number
280                 {
281                         return action;
282                 }
283         }
284 #endif
285
286 #endif // USE_KEYMAPS_IN_EEPROM
287
288         return pgm_read_word(&fn_actions[function_id]);
289 }
290
291
292 // Zeal60 specific "action functions"
293 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
294 {
295         switch (id)
296         {
297         case TRIPLE_TAP_1_3:
298         case TRIPLE_TAP_2_3:
299                 if (record->event.pressed) {
300                         layer_on( id == TRIPLE_TAP_1_3 ? 1 : 2 );
301                         if (record->tap.count && !record->tap.interrupted) {
302                                 if (record->tap.count >= 3) {
303                                         layer_invert(3);
304                                 }
305                         } else {
306                                 record->tap.count = 0;
307                         }
308                 } else {
309                         layer_off( id == TRIPLE_TAP_1_3 ? 1 : 2 );
310                 }
311                 break;
312         }
313 }
314
315 void led_set_kb(uint8_t usb_led)
316 {
317 #if RGB_BACKLIGHT_ENABLED
318         backlight_set_indicator_state(usb_led);
319 #endif // RGB_BACKLIGHT_ENABLED
320 }
321
322 void suspend_power_down_kb(void)
323 {
324 #if RGB_BACKLIGHT_ENABLED
325         backlight_set_suspend_state(true);
326 #endif // RGB_BACKLIGHT_ENABLED
327 }
328
329 void suspend_wakeup_init_kb(void)
330 {
331 #if RGB_BACKLIGHT_ENABLED
332         backlight_set_suspend_state(false);
333 #endif // RGB_BACKLIGHT_ENABLED
334 }
335