]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/zeal60/zeal60.c
e516c4dbfc6dcc1aff1b3dce6f402e192ff39e2e
[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 #ifdef RAW_ENABLE
42
43 void raw_hid_receive( uint8_t *data, uint8_t length )
44 {
45         uint8_t *command_id = &(data[0]);
46         uint8_t *command_data = &(data[1]);
47         switch ( *command_id )
48         {
49                 case id_get_protocol_version:
50                 {
51                         command_data[0] = PROTOCOL_VERSION >> 8;
52                         command_data[1] = PROTOCOL_VERSION & 0xFF;
53                         break;
54                 }
55                 case id_get_keyboard_value:
56                 {
57                         if ( command_data[0] == 0x01 )
58                         {
59                                 uint32_t value = timer_read32();
60                                 command_data[1] = (value >> 24 ) & 0xFF;
61                                 command_data[2] = (value >> 16 ) & 0xFF;
62                                 command_data[3] = (value >> 8 ) & 0xFF;
63                                 command_data[4] = value & 0xFF;
64                         }
65                         else
66                         {
67                                 *command_id = id_unhandled;
68                         }
69                         break;
70                 }
71 #ifdef DYNAMIC_KEYMAP_ENABLE
72                 case id_dynamic_keymap_get_keycode:
73                 {
74                         uint16_t keycode = dynamic_keymap_get_keycode( command_data[0], command_data[1], command_data[2] );
75                         command_data[3] = keycode >> 8;
76                         command_data[4] = keycode & 0xFF;
77                         break;
78                 }
79                 case id_dynamic_keymap_set_keycode:
80                 {
81                         dynamic_keymap_set_keycode( command_data[0], command_data[1], command_data[2], ( command_data[3] << 8 ) | command_data[4] );
82                         break;
83                 }
84                 case id_dynamic_keymap_clear_all:
85                 {
86                         dynamic_keymap_clear_all();
87                         break;
88                 }
89 #endif // DYNAMIC_KEYMAP_ENABLE
90 #if RGB_BACKLIGHT_ENABLED
91                 case id_backlight_config_set_value:
92                 {
93                         backlight_config_set_value(command_data);
94                         break;
95                 }
96                 case id_backlight_config_get_value:
97                 {
98                         backlight_config_get_value(command_data);
99                         break;
100                 }
101                 case id_backlight_config_save:
102                 {
103                         backlight_config_save();
104                         break;
105                 }
106 #endif // RGB_BACKLIGHT_ENABLED
107                 default:
108                 {
109                         // Unhandled message.
110                         *command_id = id_unhandled;
111                         break;
112                 }
113         }
114
115         // Return same buffer with values changed
116         raw_hid_send( data, length );
117
118 }
119
120 #endif
121
122 void bootmagic_lite(void)
123 {
124         // The lite version of TMK's bootmagic.
125         // 100% less potential for accidentally making the
126         // keyboard do stupid things.
127
128         // We need multiple scans because debouncing can't be turned off.
129         matrix_scan();
130         wait_ms(DEBOUNCING_DELAY);
131         wait_ms(DEBOUNCING_DELAY);
132         matrix_scan();
133
134         // If the Esc and space bar are held down on power up,
135         // reset the EEPROM valid state and jump to bootloader.
136         // Assumes Esc is at [0,0] and spacebar is at [4,7].
137         // This isn't very generalized, but we need something that doesn't
138         // rely on user's keymaps in firmware or EEPROM.
139         if ( ( matrix_get_row(0) & (1<<0) ) &&
140                 ( matrix_get_row(4) & (1<<7) ) )
141         {
142                 // Set the Zeal60 specific EEPROM state as invalid.
143                 eeprom_set_valid(false);
144                 // Set the TMK/QMK EEPROM state as invalid.
145                 eeconfig_disable();
146                 // Jump to bootloader.
147                 bootloader_jump();
148         }
149 }
150
151 void matrix_init_kb(void)
152 {
153         bootmagic_lite();
154
155         // If the EEPROM has the magic, the data is good.
156         // OK to load from EEPROM.
157         if (eeprom_is_valid())
158         {
159 #if RGB_BACKLIGHT_ENABLED
160                 backlight_config_load();
161 #endif // RGB_BACKLIGHT_ENABLED
162                 // TODO: do something to "turn on" keymaps in EEPROM?
163         }
164         else
165         {
166 #if RGB_BACKLIGHT_ENABLED
167                 // If the EEPROM has not been saved before, or is out of date,
168                 // save the default values to the EEPROM. Default values
169                 // come from construction of the zeal_backlight_config instance.
170                 backlight_config_save();
171 #endif // RGB_BACKLIGHT_ENABLED
172
173 #ifdef DYNAMIC_KEYMAP_ENABLE
174                 // This saves "empty" keymaps so it falls back to the keymaps
175                 // in the firmware (aka. progmem/flash)
176                 dynamic_keymap_clear_all();
177 #endif
178
179                 // Save the magic number last, in case saving was interrupted
180                 eeprom_set_valid(true);
181         }
182
183 #if RGB_BACKLIGHT_ENABLED
184         // Initialize LED drivers for backlight.
185         backlight_init_drivers();
186         
187         backlight_timer_init();
188         backlight_timer_enable();
189 #endif // RGB_BACKLIGHT_ENABLED
190
191         matrix_init_user();
192 }
193
194 void matrix_scan_kb(void)
195 {
196 #if RGB_BACKLIGHT_ENABLED
197         // This only updates the LED driver buffers if something has changed.
198         backlight_update_pwm_buffers();
199 #endif // BACKLIGHT_ENABLED
200         matrix_scan_user();
201 }
202
203 bool process_record_kb(uint16_t keycode, keyrecord_t *record)
204 {
205 #if RGB_BACKLIGHT_ENABLED
206         process_record_backlight(keycode, record);
207 #endif // BACKLIGHT_ENABLED
208
209         switch(keycode)
210         {
211                 case FN_MO13:
212                         if (record->event.pressed)
213                         {
214                                 layer_on(1);
215                                 update_tri_layer(1, 2, 3);
216                         }
217                         else
218                         {
219                                 layer_off(1);
220                                 update_tri_layer(1, 2, 3);
221                         }
222                         return false;
223                         break;
224                 case FN_MO23:
225                         if (record->event.pressed)
226                         {
227                                 layer_on(2);
228                                 update_tri_layer(1, 2, 3);
229                         }
230                         else
231                         {
232                                 layer_off(2);
233                                 update_tri_layer(1, 2, 3);
234                         }
235                         return false;
236                         break;
237         }
238         
239         return process_record_user(keycode, record);
240 }
241
242 // This overrides the one in quantum/keymap_common.c
243 uint16_t keymap_function_id_to_action( uint16_t function_id )
244 {
245         // Zeal60 specific "action functions" are 0xF00 to 0xFFF
246         // i.e. F(0xF00) to F(0xFFF) are mapped to
247         // enum zeal60_action_functions by masking last 8 bits.
248         if ( function_id >= 0x0F00 && function_id <= 0x0FFF )
249         {
250                 uint8_t id = function_id & 0xFF;
251                 switch ( id )
252                 {
253                         case TRIPLE_TAP_1_3:
254                         case TRIPLE_TAP_2_3:
255                         {
256                                 return ACTION_FUNCTION_TAP(id);
257                                 break;
258                         }
259                         default:
260                                 break;
261                 }
262         }
263
264 #if USE_KEYMAPS_IN_EEPROM
265
266 #if 0
267         // This is how to implement actions stored in EEPROM.
268         // Not yet implemented. Not sure if it's worth the trouble
269         // before we have a nice GUI for keymap editing.
270         if ( eeprom_is_valid() &&
271                  function_id < 32 ) // TODO: replace magic number
272         {
273                 uint16_t action = keymap_action_load(function_id);
274
275                 // If action is not "empty", return it, otherwise
276                 // drop down to return the one in flash
277                 if ( action != 0x0000 ) // TODO: replace magic number
278                 {
279                         return action;
280                 }
281         }
282 #endif
283
284 #endif // USE_KEYMAPS_IN_EEPROM
285
286         return pgm_read_word(&fn_actions[function_id]);
287 }
288
289
290 // Zeal60 specific "action functions"
291 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
292 {
293         switch (id)
294         {
295         case TRIPLE_TAP_1_3:
296         case TRIPLE_TAP_2_3:
297                 if (record->event.pressed)
298                 {
299                         layer_on( id == TRIPLE_TAP_1_3 ? 1 : 2 );
300
301                         if (record->tap.count && !record->tap.interrupted)
302                         {
303                                 if (record->tap.count >= 3)
304                                 {
305                                         layer_invert(3);
306                                 }
307                         }
308                         else
309                         {
310                                 record->tap.count = 0;
311                         }
312                 }
313                 else
314                 {
315                         layer_off( id == TRIPLE_TAP_1_3 ? 1 : 2 );
316                 }
317                 break;
318         }
319 }
320
321 void led_set_kb(uint8_t usb_led)
322 {
323 #if RGB_BACKLIGHT_ENABLED
324         backlight_set_indicator_state(usb_led);
325 #endif // RGB_BACKLIGHT_ENABLED
326 }
327
328 void suspend_power_down_kb(void)
329 {
330 #if RGB_BACKLIGHT_ENABLED
331         backlight_set_suspend_state(true);
332 #endif // RGB_BACKLIGHT_ENABLED
333 }
334
335 void suspend_wakeup_init_kb(void)
336 {
337 #if RGB_BACKLIGHT_ENABLED
338         backlight_set_suspend_state(false);
339 #endif // RGB_BACKLIGHT_ENABLED
340 }
341