]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/cannonkeys/stm32f072/keyboard.c
c0c3a74fb9bb41a8c4d18c2b89178d410e11e135
[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/zeal60/zeal60_api.h" // Temporary hack
16 #include "keyboards/zeal60/zeal60_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     palSetPadMode(PORT_WS2812, PIN_WS2812, PAL_MODE_ALTERNATE(0));
90     wait_ms(500);
91
92 #ifdef RGBLIGHT_ENABLE
93     leds_init();
94 #endif
95     backlight_init_ports();
96
97     matrix_init_board();
98 }
99
100 void matrix_scan_kb(void)
101 {
102   #ifdef RGBLIGHT_ENABLE
103     rgblight_task();
104   #endif
105 }
106
107 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
108   switch (keycode) {
109     case BL_INC:
110       if (record->event.pressed) {
111         kb_backlight_config.level = kb_backlight_config.level + 1;
112         if(kb_backlight_config.level > BACKLIGHT_LEVELS){
113           kb_backlight_config.level = BACKLIGHT_LEVELS;
114         }
115         backlight_set(kb_backlight_config.level);
116         save_backlight_config_to_eeprom();
117       }
118       return false;
119     case BL_TOGG:
120       if (record->event.pressed) {
121         kb_backlight_config.enable = !kb_backlight_config.enable;
122         if(kb_backlight_config.enable){
123           backlight_set(kb_backlight_config.level);
124         } else {
125           backlight_set(0);
126         }
127         save_backlight_config_to_eeprom();
128       }
129       return false;
130
131     case BL_DEC:
132       if (record->event.pressed) {
133         if(kb_backlight_config.level <= 1){
134           kb_backlight_config.level = 0;
135         } else {
136           kb_backlight_config.level = kb_backlight_config.level - 1;
137         }
138         backlight_set(kb_backlight_config.level);
139         save_backlight_config_to_eeprom();
140       }
141       return false;
142     case BL_BRTG:
143       if (record->event.pressed) {
144         kb_backlight_config.breathing = !kb_backlight_config.breathing;
145         breathing_toggle();
146         save_backlight_config_to_eeprom();
147       }
148       return false;
149     default:
150         break;
151   }
152
153   #ifdef DYNAMIC_KEYMAP_ENABLE
154         // Handle macros
155         if (record->event.pressed) {
156                 if ( keycode >= MACRO00 && keycode <= MACRO15 )
157                 {
158                         uint8_t id = keycode - MACRO00;
159                         dynamic_keymap_macro_send(id);
160                         return false;
161                 }
162         }
163     #endif //DYNAMIC_KEYMAP_ENABLE
164
165   return process_record_user(keycode, record);;
166 }
167
168
169 // Start Dynamic Keymap code
170 #ifdef RAW_ENABLE
171
172 void raw_hid_receive( uint8_t *data, uint8_t length )
173 {
174         uint8_t *command_id = &(data[0]);
175         uint8_t *command_data = &(data[1]);
176         switch ( *command_id )
177         {
178                 case id_get_protocol_version:
179                 {
180                         command_data[0] = PROTOCOL_VERSION >> 8;
181                         command_data[1] = PROTOCOL_VERSION & 0xFF;
182                         break;
183                 }
184                 case id_get_keyboard_value:
185                 {
186             switch( command_data[0])
187             {
188                 case id_uptime:
189                 {
190                 uint32_t value = timer_read32();
191                 command_data[1] = (value >> 24 ) & 0xFF;
192                 command_data[2] = (value >> 16 ) & 0xFF;
193                 command_data[3] = (value >> 8 ) & 0xFF;
194                 command_data[4] = value & 0xFF;
195                 break;
196                 }
197                 default:
198                 {
199                 *command_id = id_unhandled;
200                 break;
201                 }
202             }
203             break;
204         }
205 #ifdef DYNAMIC_KEYMAP_ENABLE
206
207                 case id_dynamic_keymap_get_keycode:
208                 {
209                         uint16_t keycode = dynamic_keymap_get_keycode( command_data[0], command_data[1], command_data[2] );
210                         command_data[3] = keycode >> 8;
211                         command_data[4] = keycode & 0xFF;
212                         break;
213                 }
214                 case id_dynamic_keymap_set_keycode:
215                 {
216                         dynamic_keymap_set_keycode( command_data[0], command_data[1], command_data[2], ( command_data[3] << 8 ) | command_data[4] );
217                         break;
218                 }
219                 case id_dynamic_keymap_reset:
220                 {
221                         dynamic_keymap_reset();
222                         break;
223                 }
224                 case id_dynamic_keymap_macro_get_count:
225                 {
226                         command_data[0] = dynamic_keymap_macro_get_count();
227                         break;
228                 }
229                 case id_dynamic_keymap_macro_get_buffer_size:
230                 {
231                         uint16_t size = dynamic_keymap_macro_get_buffer_size();
232                         command_data[0] = size >> 8;
233                         command_data[1] = size & 0xFF;
234                         break;
235                 }
236                 case id_dynamic_keymap_macro_get_buffer:
237                 {
238                         uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
239                         uint16_t size = command_data[2]; // size <= 28
240                         dynamic_keymap_macro_get_buffer( offset, size, &command_data[3] );
241                         break;
242                 }
243                 case id_dynamic_keymap_macro_set_buffer:
244                 {
245                         uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
246                         uint16_t size = command_data[2]; // size <= 28
247                         dynamic_keymap_macro_set_buffer( offset, size, &command_data[3] );
248                         break;
249                 }
250                 case id_dynamic_keymap_macro_reset:
251                 {
252                         dynamic_keymap_macro_reset();
253                         break;
254                 }
255                 case id_dynamic_keymap_get_layer_count:
256                 {
257                         command_data[0] = dynamic_keymap_get_layer_count();
258                         break;
259                 }
260                 case id_dynamic_keymap_get_buffer:
261                 {
262                         uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
263                         uint16_t size = command_data[2]; // size <= 28
264                         dynamic_keymap_get_buffer( offset, size, &command_data[3] );
265                         break;
266                 }
267                 case id_dynamic_keymap_set_buffer:
268                 {
269                         uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
270                         uint16_t size = command_data[2]; // size <= 28
271                         dynamic_keymap_set_buffer( offset, size, &command_data[3] );
272                         break;
273                 }
274 #endif // DYNAMIC_KEYMAP_ENABLE
275                 case id_eeprom_reset:
276                 {
277                         eeprom_reset();
278                         break;
279                 }
280                 case id_bootloader_jump:
281                 {
282                         // Need to send data back before the jump
283                         // Informs host that the command is handled
284                         raw_hid_send( data, length );
285                         // Give host time to read it
286                         wait_ms(100);
287                         bootloader_jump();
288                         break;
289                 }
290                 default:
291                 {
292                         // Unhandled message.
293                         *command_id = id_unhandled;
294                         break;
295                 }
296         }
297
298         // Return same buffer with values changed
299         raw_hid_send( data, length );
300
301 }
302
303 #endif