aboutsummaryrefslogtreecommitdiff
path: root/keyboards/cannonkeys/stm32f072/keyboard.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/cannonkeys/stm32f072/keyboard.c')
-rw-r--r--keyboards/cannonkeys/stm32f072/keyboard.c266
1 files changed, 71 insertions, 195 deletions
diff --git a/keyboards/cannonkeys/stm32f072/keyboard.c b/keyboards/cannonkeys/stm32f072/keyboard.c
index 5a9ccafff..32e30f334 100644
--- a/keyboards/cannonkeys/stm32f072/keyboard.c
+++ b/keyboards/cannonkeys/stm32f072/keyboard.c
@@ -10,11 +10,10 @@
10#include "raw_hid.h" 10#include "raw_hid.h"
11#include "dynamic_keymap.h" 11#include "dynamic_keymap.h"
12#include "tmk_core/common/eeprom.h" 12#include "tmk_core/common/eeprom.h"
13#include "version.h" // for QMK_BUILDDATE used in EEPROM magic
13 14
14// HACK 15#include "via.h"
15#include "keyboards/wilba_tech/via_api.h" // Temporary hack 16#define EEPROM_CUSTOM_BACKLIGHT (VIA_EEPROM_CUSTOM_CONFIG_ADDR)
16#include "keyboards/wilba_tech/via_keycodes.h" // Temporary hack
17
18 17
19backlight_config_t kb_backlight_config = { 18backlight_config_t kb_backlight_config = {
20 .enable = true, 19 .enable = true,
@@ -22,61 +21,29 @@ backlight_config_t kb_backlight_config = {
22 .level = BACKLIGHT_LEVELS 21 .level = BACKLIGHT_LEVELS
23}; 22};
24 23
25bool eeprom_is_valid(void) 24void backlight_config_save(){
26{
27 return (eeprom_read_word(((void*)EEPROM_MAGIC_ADDR)) == EEPROM_MAGIC &&
28 eeprom_read_byte(((void*)EEPROM_VERSION_ADDR)) == EEPROM_VERSION);
29}
30
31void 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
37void eeprom_reset(void)
38{
39 eeprom_set_valid(false);
40 eeconfig_disable();
41}
42
43void save_backlight_config_to_eeprom(){
44 eeprom_update_byte((uint8_t*)EEPROM_CUSTOM_BACKLIGHT, kb_backlight_config.raw); 25 eeprom_update_byte((uint8_t*)EEPROM_CUSTOM_BACKLIGHT, kb_backlight_config.raw);
45} 26}
46 27
47void load_custom_config(){ 28void backlight_config_load(){
48 kb_backlight_config.raw = eeprom_read_byte((uint8_t*)EEPROM_CUSTOM_BACKLIGHT); 29 kb_backlight_config.raw = eeprom_read_byte((uint8_t*)EEPROM_CUSTOM_BACKLIGHT);
49} 30}
50 31
51#ifdef DYNAMIC_KEYMAP_ENABLE 32// Called from via_init() if VIA_ENABLE
52void dynamic_keymap_custom_reset(void){ 33// Called from matrix_init_kb() if not VIA_ENABLE
53 void *p = (void*)(EEPROM_CUSTOM_BACKLIGHT); 34void via_init_kb(void)
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
62void eeprom_init_kb(void)
63{ 35{
64 // If the EEPROM has the magic, the data is good. 36 // If the EEPROM has the magic, the data is good.
65 // OK to load from EEPROM. 37 // OK to load from EEPROM.
66 if (eeprom_is_valid()) { 38 if (via_eeprom_is_valid()) {
67 load_custom_config(); 39 backlight_config_load();
68 } else { 40 } else {
69#ifdef DYNAMIC_KEYMAP_ENABLE 41 // If the EEPROM has not been saved before, or is out of date,
70 // This resets the keymaps in EEPROM to what is in flash. 42 // save the default values to the EEPROM. Default values
71 dynamic_keymap_reset(); 43 // come from construction of the backlight_config instance.
72 // This resets the macros in EEPROM to nothing. 44 backlight_config_save();
73 dynamic_keymap_macro_reset(); 45
74 // Reset the custom stuff 46 // DO NOT set EEPROM valid here, let caller do this
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 } 47 }
81} 48}
82 49
@@ -84,7 +51,13 @@ __attribute__ ((weak))
84void matrix_init_board(void); 51void matrix_init_board(void);
85 52
86void matrix_init_kb(void){ 53void matrix_init_kb(void){
87 eeprom_init_kb(); 54 // If VIA is disabled, we still need to load backlight settings.
55 // Call via_init_kb() the same way as via_init(), with setting
56 // EEPROM valid afterwards.
57#ifndef VIA_ENABLE
58 via_init_kb();
59 via_eeprom_set_valid(true);
60#endif // VIA_ENABLE
88 /* MOSI pin*/ 61 /* MOSI pin*/
89#ifdef RGBLIGHT_ENABLE 62#ifdef RGBLIGHT_ENABLE
90 palSetPadMode(PORT_WS2812, PIN_WS2812, PAL_MODE_ALTERNATE(0)); 63 palSetPadMode(PORT_WS2812, PIN_WS2812, PAL_MODE_ALTERNATE(0));
@@ -112,7 +85,7 @@ bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
112 kb_backlight_config.level = BACKLIGHT_LEVELS; 85 kb_backlight_config.level = BACKLIGHT_LEVELS;
113 } 86 }
114 backlight_set(kb_backlight_config.level); 87 backlight_set(kb_backlight_config.level);
115 save_backlight_config_to_eeprom(); 88 backlight_config_save();
116 } 89 }
117 return false; 90 return false;
118 case BL_TOGG: 91 case BL_TOGG:
@@ -123,7 +96,7 @@ bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
123 } else { 96 } else {
124 backlight_set(0); 97 backlight_set(0);
125 } 98 }
126 save_backlight_config_to_eeprom(); 99 backlight_config_save();
127 } 100 }
128 return false; 101 return false;
129 102
@@ -135,168 +108,71 @@ bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
135 kb_backlight_config.level = kb_backlight_config.level - 1; 108 kb_backlight_config.level = kb_backlight_config.level - 1;
136 } 109 }
137 backlight_set(kb_backlight_config.level); 110 backlight_set(kb_backlight_config.level);
138 save_backlight_config_to_eeprom(); 111 backlight_config_save();
139 } 112 }
140 return false; 113 return false;
141 case BL_BRTG: 114 case BL_BRTG:
142 if (record->event.pressed) { 115 if (record->event.pressed) {
143 kb_backlight_config.breathing = !kb_backlight_config.breathing; 116 kb_backlight_config.breathing = !kb_backlight_config.breathing;
144 breathing_toggle(); 117 breathing_toggle();
145 save_backlight_config_to_eeprom(); 118 backlight_config_save();
146 } 119 }
147 return false; 120 return false;
148 default: 121 default:
149 break; 122 break;
150 } 123 }
151 124
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);; 125 return process_record_user(keycode, record);;
165} 126}
166 127
167 128
168// Start Dynamic Keymap code
169#ifdef RAW_ENABLE
170 129
171void 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 130
206 case id_dynamic_keymap_get_keycode: 131//
207 { 132// In the case of VIA being disabled, we still need to check if
208 uint16_t keycode = dynamic_keymap_get_keycode( command_data[0], command_data[1], command_data[2] ); 133// keyboard level EEPROM memory is valid before loading.
209 command_data[3] = keycode >> 8; 134// Thus these are copies of the same functions in VIA, since
210 command_data[4] = keycode & 0xFF; 135// the backlight settings reuse VIA's EEPROM magic/version,
211 break; 136// and the ones in via.c won't be compiled in.
212 } 137//
213 case id_dynamic_keymap_set_keycode: 138// Yes, this is sub-optimal, and is only here for completeness
214 { 139// (i.e. catering to the 1% of people that want wilba.tech LED bling
215 dynamic_keymap_set_keycode( command_data[0], command_data[1], command_data[2], ( command_data[3] << 8 ) | command_data[4] ); 140// AND want persistent settings BUT DON'T want to use dynamic keymaps/VIA).
216 break; 141//
217 } 142#ifndef VIA_ENABLE
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 143
297 // Return same buffer with values changed 144bool via_eeprom_is_valid(void)
298 raw_hid_send( data, length ); 145{
146 char *p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
147 uint8_t magic0 = ( ( p[2] & 0x0F ) << 4 ) | ( p[3] & 0x0F );
148 uint8_t magic1 = ( ( p[5] & 0x0F ) << 4 ) | ( p[6] & 0x0F );
149 uint8_t magic2 = ( ( p[8] & 0x0F ) << 4 ) | ( p[9] & 0x0F );
150
151 return (eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+0 ) == magic0 &&
152 eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+1 ) == magic1 &&
153 eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+2 ) == magic2 );
154}
299 155
156// Sets VIA/keyboard level usage of EEPROM to valid/invalid
157// Keyboard level code (eg. via_init_kb()) should not call this
158void via_eeprom_set_valid(bool valid)
159{
160 char *p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
161 uint8_t magic0 = ( ( p[2] & 0x0F ) << 4 ) | ( p[3] & 0x0F );
162 uint8_t magic1 = ( ( p[5] & 0x0F ) << 4 ) | ( p[6] & 0x0F );
163 uint8_t magic2 = ( ( p[8] & 0x0F ) << 4 ) | ( p[9] & 0x0F );
164
165 eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+0, valid ? magic0 : 0xFF);
166 eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+1, valid ? magic1 : 0xFF);
167 eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+2, valid ? magic2 : 0xFF);
300} 168}
301 169
302#endif 170void via_eeprom_reset(void)
171{
172 // Set the VIA specific EEPROM state as invalid.
173 via_eeprom_set_valid(false);
174 // Set the TMK/QMK EEPROM state as invalid.
175 eeconfig_disable();
176}
177
178#endif // VIA_ENABLE \ No newline at end of file