4 #include "led_custom.h"
11 #include "dynamic_keymap.h"
12 #include "tmk_core/common/eeprom.h"
15 #include "keyboards/zeal60/zeal60_api.h" // Temporary hack
16 #include "keyboards/zeal60/zeal60_keycodes.h" // Temporary hack
19 backlight_config_t kb_backlight_config = {
22 .level = BACKLIGHT_LEVELS
25 bool eeprom_is_valid(void)
27 return (eeprom_read_word(((void*)EEPROM_MAGIC_ADDR)) == EEPROM_MAGIC &&
28 eeprom_read_byte(((void*)EEPROM_VERSION_ADDR)) == EEPROM_VERSION);
31 void eeprom_set_valid(bool valid)
33 eeprom_update_word(((void*)EEPROM_MAGIC_ADDR), valid ? EEPROM_MAGIC : 0xFFFF);
34 eeprom_update_byte(((void*)EEPROM_VERSION_ADDR), valid ? EEPROM_VERSION : 0xFF);
37 void eeprom_reset(void)
39 eeprom_set_valid(false);
43 void save_backlight_config_to_eeprom(){
44 eeprom_update_byte((uint8_t*)EEPROM_CUSTOM_BACKLIGHT, kb_backlight_config.raw);
47 void load_custom_config(){
48 kb_backlight_config.raw = eeprom_read_byte((uint8_t*)EEPROM_CUSTOM_BACKLIGHT);
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);
56 eeprom_update_byte(p, 0);
62 void eeprom_init_kb(void)
64 // If the EEPROM has the magic, the data is good.
65 // OK to load from EEPROM.
66 if (eeprom_is_valid()) {
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();
77 // Save the magic number last, in case saving was interrupted
78 save_backlight_config_to_eeprom();
79 eeprom_set_valid(true);
83 __attribute__ ((weak))
84 void matrix_init_board(void);
86 void matrix_init_kb(void){
89 palSetPadMode(PORT_WS2812, PIN_WS2812, PAL_MODE_ALTERNATE(0));
92 #ifdef RGBLIGHT_ENABLE
95 backlight_init_ports();
100 void matrix_scan_kb(void)
102 #ifdef RGBLIGHT_ENABLE
107 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
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;
115 backlight_set(kb_backlight_config.level);
116 save_backlight_config_to_eeprom();
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);
127 save_backlight_config_to_eeprom();
132 if (record->event.pressed) {
133 if(kb_backlight_config.level <= 1){
134 kb_backlight_config.level = 0;
136 kb_backlight_config.level = kb_backlight_config.level - 1;
138 backlight_set(kb_backlight_config.level);
139 save_backlight_config_to_eeprom();
143 if (record->event.pressed) {
144 kb_backlight_config.breathing = !kb_backlight_config.breathing;
146 save_backlight_config_to_eeprom();
153 #ifdef DYNAMIC_KEYMAP_ENABLE
155 if (record->event.pressed) {
156 if ( keycode >= MACRO00 && keycode <= MACRO15 )
158 uint8_t id = keycode - MACRO00;
159 dynamic_keymap_macro_send(id);
163 #endif //DYNAMIC_KEYMAP_ENABLE
169 // Start Dynamic Keymap code
172 void raw_hid_receive( uint8_t *data, uint8_t length )
174 uint8_t *command_id = &(data[0]);
175 uint8_t *command_data = &(data[1]);
176 switch ( *command_id )
178 case id_get_protocol_version:
180 command_data[0] = PROTOCOL_VERSION >> 8;
181 command_data[1] = PROTOCOL_VERSION & 0xFF;
184 case id_get_keyboard_value:
186 switch( command_data[0])
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;
199 *command_id = id_unhandled;
205 #ifdef DYNAMIC_KEYMAP_ENABLE
207 case id_dynamic_keymap_get_keycode:
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;
214 case id_dynamic_keymap_set_keycode:
216 dynamic_keymap_set_keycode( command_data[0], command_data[1], command_data[2], ( command_data[3] << 8 ) | command_data[4] );
219 case id_dynamic_keymap_reset:
221 dynamic_keymap_reset();
224 case id_dynamic_keymap_macro_get_count:
226 command_data[0] = dynamic_keymap_macro_get_count();
229 case id_dynamic_keymap_macro_get_buffer_size:
231 uint16_t size = dynamic_keymap_macro_get_buffer_size();
232 command_data[0] = size >> 8;
233 command_data[1] = size & 0xFF;
236 case id_dynamic_keymap_macro_get_buffer:
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] );
243 case id_dynamic_keymap_macro_set_buffer:
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] );
250 case id_dynamic_keymap_macro_reset:
252 dynamic_keymap_macro_reset();
255 case id_dynamic_keymap_get_layer_count:
257 command_data[0] = dynamic_keymap_get_layer_count();
260 case id_dynamic_keymap_get_buffer:
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] );
267 case id_dynamic_keymap_set_buffer:
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] );
274 #endif // DYNAMIC_KEYMAP_ENABLE
275 case id_eeprom_reset:
280 case id_bootloader_jump:
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
292 // Unhandled message.
293 *command_id = id_unhandled;
298 // Return same buffer with values changed
299 raw_hid_send( data, length );