]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/cannonkeys/stm32f072/keyboard.c
Couple more minor Lily58 keymap tweaks (#7175)
[qmk_firmware.git] / keyboards / cannonkeys / stm32f072 / keyboard.c
1 #include "keyboard.h"
2 #include "ch.h"
3 #include "hal.h"
4 #include "led_custom.h"
5 #include "util.h"
6 #include "quantum.h"
7
8 #include "ws2812.h"
9
10 #include "raw_hid.h"
11 #include "dynamic_keymap.h"
12 #include "tmk_core/common/eeprom.h"
13
14 // HACK
15 #include "keyboards/wilba_tech/via_api.h" // Temporary hack
16 #include "keyboards/wilba_tech/via_keycodes.h" // Temporary hack
17
18
19 backlight_config_t kb_backlight_config = {
20   .enable = true,
21   .breathing = true,
22   .level = BACKLIGHT_LEVELS
23 };
24
25 bool eeprom_is_valid(void)
26 {
27         return (eeprom_read_word(((void*)EEPROM_MAGIC_ADDR)) == EEPROM_MAGIC &&
28                         eeprom_read_byte(((void*)EEPROM_VERSION_ADDR)) == EEPROM_VERSION);
29 }
30
31 void eeprom_set_valid(bool valid)
32 {
33         eeprom_update_word(((void*)EEPROM_MAGIC_ADDR), valid ? EEPROM_MAGIC : 0xFFFF);
34         eeprom_update_byte(((void*)EEPROM_VERSION_ADDR), valid ? EEPROM_VERSION : 0xFF);
35 }
36
37 void eeprom_reset(void)
38 {
39         eeprom_set_valid(false);
40         eeconfig_disable();
41 }
42
43 void save_backlight_config_to_eeprom(){
44   eeprom_update_byte((uint8_t*)EEPROM_CUSTOM_BACKLIGHT, kb_backlight_config.raw);
45 }
46
47 void load_custom_config(){
48   kb_backlight_config.raw = eeprom_read_byte((uint8_t*)EEPROM_CUSTOM_BACKLIGHT);
49 }
50
51 #ifdef DYNAMIC_KEYMAP_ENABLE
52 void dynamic_keymap_custom_reset(void){
53     void *p = (void*)(EEPROM_CUSTOM_BACKLIGHT);
54         void *end = (void*)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR);
55         while ( p != end ) {
56                 eeprom_update_byte(p, 0);
57                 ++p;
58         }
59 }
60 #endif
61
62 void eeprom_init_kb(void)
63 {
64         // If the EEPROM has the magic, the data is good.
65         // OK to load from EEPROM.
66         if (eeprom_is_valid()) {
67                 load_custom_config();
68         } else  {
69 #ifdef DYNAMIC_KEYMAP_ENABLE
70                 // This resets the keymaps in EEPROM to what is in flash.
71                 dynamic_keymap_reset();
72                 // This resets the macros in EEPROM to nothing.
73                 dynamic_keymap_macro_reset();
74         // Reset the custom stuff
75         dynamic_keymap_custom_reset();
76 #endif
77                 // Save the magic number last, in case saving was interrupted
78         save_backlight_config_to_eeprom();
79                 eeprom_set_valid(true);
80         }
81 }
82
83 __attribute__ ((weak))
84 void matrix_init_board(void);
85
86 void matrix_init_kb(void){
87         eeprom_init_kb();
88       /* MOSI pin*/
89 #ifdef RGBLIGHT_ENABLE
90     palSetPadMode(PORT_WS2812, PIN_WS2812, PAL_MODE_ALTERNATE(0));
91     wait_ms(500);
92     leds_init();
93 #endif
94     backlight_init_ports();
95
96     matrix_init_board();
97 }
98
99 void matrix_scan_kb(void)
100 {
101   #ifdef RGBLIGHT_ENABLE
102     rgblight_task();
103   #endif
104 }
105
106 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
107   switch (keycode) {
108     case BL_INC:
109       if (record->event.pressed) {
110         kb_backlight_config.level = kb_backlight_config.level + 1;
111         if(kb_backlight_config.level > BACKLIGHT_LEVELS){
112           kb_backlight_config.level = BACKLIGHT_LEVELS;
113         }
114         backlight_set(kb_backlight_config.level);
115         save_backlight_config_to_eeprom();
116       }
117       return false;
118     case BL_TOGG:
119       if (record->event.pressed) {
120         kb_backlight_config.enable = !kb_backlight_config.enable;
121         if(kb_backlight_config.enable){
122           backlight_set(kb_backlight_config.level);
123         } else {
124           backlight_set(0);
125         }
126         save_backlight_config_to_eeprom();
127       }
128       return false;
129
130     case BL_DEC:
131       if (record->event.pressed) {
132         if(kb_backlight_config.level <= 1){
133           kb_backlight_config.level = 0;
134         } else {
135           kb_backlight_config.level = kb_backlight_config.level - 1;
136         }
137         backlight_set(kb_backlight_config.level);
138         save_backlight_config_to_eeprom();
139       }
140       return false;
141     case BL_BRTG:
142       if (record->event.pressed) {
143         kb_backlight_config.breathing = !kb_backlight_config.breathing;
144         breathing_toggle();
145         save_backlight_config_to_eeprom();
146       }
147       return false;
148     default:
149         break;
150   }
151
152   #ifdef DYNAMIC_KEYMAP_ENABLE
153         // Handle macros
154         if (record->event.pressed) {
155                 if ( keycode >= MACRO00 && keycode <= MACRO15 )
156                 {
157                         uint8_t id = keycode - MACRO00;
158                         dynamic_keymap_macro_send(id);
159                         return false;
160                 }
161         }
162     #endif //DYNAMIC_KEYMAP_ENABLE
163
164   return process_record_user(keycode, record);;
165 }
166
167
168 // Start Dynamic Keymap code
169 #ifdef RAW_ENABLE
170
171 void raw_hid_receive( uint8_t *data, uint8_t length )
172 {
173         uint8_t *command_id = &(data[0]);
174         uint8_t *command_data = &(data[1]);
175         switch ( *command_id )
176         {
177                 case id_get_protocol_version:
178                 {
179                         command_data[0] = PROTOCOL_VERSION >> 8;
180                         command_data[1] = PROTOCOL_VERSION & 0xFF;
181                         break;
182                 }
183                 case id_get_keyboard_value:
184                 {
185             switch( command_data[0])
186             {
187                 case id_uptime:
188                 {
189                 uint32_t value = timer_read32();
190                 command_data[1] = (value >> 24 ) & 0xFF;
191                 command_data[2] = (value >> 16 ) & 0xFF;
192                 command_data[3] = (value >> 8 ) & 0xFF;
193                 command_data[4] = value & 0xFF;
194                 break;
195                 }
196                 default:
197                 {
198                 *command_id = id_unhandled;
199                 break;
200                 }
201             }
202             break;
203         }
204 #ifdef DYNAMIC_KEYMAP_ENABLE
205
206                 case id_dynamic_keymap_get_keycode:
207                 {
208                         uint16_t keycode = dynamic_keymap_get_keycode( command_data[0], command_data[1], command_data[2] );
209                         command_data[3] = keycode >> 8;
210                         command_data[4] = keycode & 0xFF;
211                         break;
212                 }
213                 case id_dynamic_keymap_set_keycode:
214                 {
215                         dynamic_keymap_set_keycode( command_data[0], command_data[1], command_data[2], ( command_data[3] << 8 ) | command_data[4] );
216                         break;
217                 }
218                 case id_dynamic_keymap_reset:
219                 {
220                         dynamic_keymap_reset();
221                         break;
222                 }
223                 case id_dynamic_keymap_macro_get_count:
224                 {
225                         command_data[0] = dynamic_keymap_macro_get_count();
226                         break;
227                 }
228                 case id_dynamic_keymap_macro_get_buffer_size:
229                 {
230                         uint16_t size = dynamic_keymap_macro_get_buffer_size();
231                         command_data[0] = size >> 8;
232                         command_data[1] = size & 0xFF;
233                         break;
234                 }
235                 case id_dynamic_keymap_macro_get_buffer:
236                 {
237                         uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
238                         uint16_t size = command_data[2]; // size <= 28
239                         dynamic_keymap_macro_get_buffer( offset, size, &command_data[3] );
240                         break;
241                 }
242                 case id_dynamic_keymap_macro_set_buffer:
243                 {
244                         uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
245                         uint16_t size = command_data[2]; // size <= 28
246                         dynamic_keymap_macro_set_buffer( offset, size, &command_data[3] );
247                         break;
248                 }
249                 case id_dynamic_keymap_macro_reset:
250                 {
251                         dynamic_keymap_macro_reset();
252                         break;
253                 }
254                 case id_dynamic_keymap_get_layer_count:
255                 {
256                         command_data[0] = dynamic_keymap_get_layer_count();
257                         break;
258                 }
259                 case id_dynamic_keymap_get_buffer:
260                 {
261                         uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
262                         uint16_t size = command_data[2]; // size <= 28
263                         dynamic_keymap_get_buffer( offset, size, &command_data[3] );
264                         break;
265                 }
266                 case id_dynamic_keymap_set_buffer:
267                 {
268                         uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
269                         uint16_t size = command_data[2]; // size <= 28
270                         dynamic_keymap_set_buffer( offset, size, &command_data[3] );
271                         break;
272                 }
273 #endif // DYNAMIC_KEYMAP_ENABLE
274                 case id_eeprom_reset:
275                 {
276                         eeprom_reset();
277                         break;
278                 }
279                 case id_bootloader_jump:
280                 {
281                         // Need to send data back before the jump
282                         // Informs host that the command is handled
283                         raw_hid_send( data, length );
284                         // Give host time to read it
285                         wait_ms(100);
286                         bootloader_jump();
287                         break;
288                 }
289                 default:
290                 {
291                         // Unhandled message.
292                         *command_id = id_unhandled;
293                         break;
294                 }
295         }
296
297         // Return same buffer with values changed
298         raw_hid_send( data, length );
299
300 }
301
302 #endif