]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/wilba_tech/wt_main.c
23f07d7eb144198f7141586f0df87e4de2c5375b
[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
21 #include "raw_hid.h"
22 #include "dynamic_keymap.h"
23 #include "timer.h"
24 #include "tmk_core/common/eeprom.h"
25
26 bool eeprom_is_valid(void)
27 {
28         return (eeprom_read_word(((void*)EEPROM_MAGIC_ADDR)) == EEPROM_MAGIC &&
29                         eeprom_read_byte(((void*)EEPROM_VERSION_ADDR)) == EEPROM_VERSION);
30 }
31
32 void eeprom_set_valid(bool valid)
33 {
34         eeprom_update_word(((void*)EEPROM_MAGIC_ADDR), valid ? EEPROM_MAGIC : 0xFFFF);
35         eeprom_update_byte(((void*)EEPROM_VERSION_ADDR), valid ? EEPROM_VERSION : 0xFF);
36 }
37
38 void eeprom_reset(void)
39 {
40         // Set the Zeal60 specific EEPROM state as invalid.
41         eeprom_set_valid(false);
42         // Set the TMK/QMK EEPROM state as invalid.
43         eeconfig_disable();
44 }
45
46 #ifdef RAW_ENABLE
47
48 void raw_hid_receive( uint8_t *data, uint8_t length )
49 {
50         uint8_t *command_id = &(data[0]);
51         uint8_t *command_data = &(data[1]);
52         switch ( *command_id )
53         {
54                 case id_get_protocol_version:
55                 {
56                         command_data[0] = PROTOCOL_VERSION >> 8;
57                         command_data[1] = PROTOCOL_VERSION & 0xFF;
58                         break;
59                 }
60                 case id_get_keyboard_value:
61                 {
62                         if ( command_data[0] == id_uptime )
63                         {
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;
69                         }
70                         else
71                         {
72                                 *command_id = id_unhandled;
73                         }
74                         break;
75                 }
76 #ifdef DYNAMIC_KEYMAP_ENABLE
77                 case id_dynamic_keymap_get_keycode:
78                 {
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;
82                         break;
83                 }
84                 case id_dynamic_keymap_set_keycode:
85                 {
86                         dynamic_keymap_set_keycode( command_data[0], command_data[1], command_data[2], ( command_data[3] << 8 ) | command_data[4] );
87                         break;
88                 }
89                 case id_dynamic_keymap_reset:
90                 {
91                         dynamic_keymap_reset();
92                         break;
93                 }
94 #endif // DYNAMIC_KEYMAP_ENABLE
95                 case id_backlight_config_set_value:
96                 {
97                         //backlight_config_set_value(command_data);
98                         break;
99                 }
100                 case id_backlight_config_get_value:
101                 {
102                         //backlight_config_get_value(command_data);
103                         break;
104                 }
105                 case id_backlight_config_save:
106                 {
107                         //backlight_config_save();
108                         break;
109                 }
110                 case id_eeprom_reset:
111                 {
112                         eeprom_reset();
113                         break;
114                 }
115                 case id_bootloader_jump:
116                 {
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
121                         wait_ms(100);
122                         bootloader_jump();
123                         break;
124                 }
125                 default:
126                 {
127                         // Unhandled message.
128                         *command_id = id_unhandled;
129                         break;
130                 }
131         }
132
133         // Return same buffer with values changed
134         raw_hid_send( data, length );
135
136 }
137
138 #endif
139
140 void main_init(void)
141 {
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();
146         } else  {
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();
154 #endif
155                 // Save the magic number last, in case saving was interrupted
156                 eeprom_set_valid(true);
157         }
158
159         // Initialize LED drivers for backlight.
160         backlight_init_drivers();
161
162         backlight_timer_init();
163         backlight_timer_enable();
164 }
165
166 void bootmagic_lite(void)
167 {
168         // The lite version of TMK's bootmagic.
169         // 100% less potential for accidentally making the
170         // keyboard do stupid things.
171
172         // We need multiple scans because debouncing can't be turned off.
173         matrix_scan();
174         wait_ms(DEBOUNCING_DELAY);
175         wait_ms(DEBOUNCING_DELAY);
176         matrix_scan();
177
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) ) {
181                 eeprom_reset();
182                 bootloader_jump();
183         }
184 }
185
186 void matrix_init_kb(void)
187 {
188         bootmagic_lite();
189         main_init();
190         matrix_init_user();
191 }
192
193 void matrix_scan_kb(void)
194 {
195         // This only updates the LED driver buffers if something has changed.
196         backlight_update_pwm_buffers();
197         matrix_scan_user();
198 }