4 #include "led_custom.h"
11 #include "dynamic_keymap.h"
12 #include "tmk_core/common/eeprom.h"
15 #include "keyboards/wilba_tech/via_api.h" // Temporary hack
16 #include "keyboards/wilba_tech/via_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 #ifdef RGBLIGHT_ENABLE
90 palSetPadMode(PORT_WS2812, PIN_WS2812, PAL_MODE_ALTERNATE(0));
94 backlight_init_ports();
99 void matrix_scan_kb(void)
101 #ifdef RGBLIGHT_ENABLE
106 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
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;
114 backlight_set(kb_backlight_config.level);
115 save_backlight_config_to_eeprom();
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);
126 save_backlight_config_to_eeprom();
131 if (record->event.pressed) {
132 if(kb_backlight_config.level <= 1){
133 kb_backlight_config.level = 0;
135 kb_backlight_config.level = kb_backlight_config.level - 1;
137 backlight_set(kb_backlight_config.level);
138 save_backlight_config_to_eeprom();
142 if (record->event.pressed) {
143 kb_backlight_config.breathing = !kb_backlight_config.breathing;
145 save_backlight_config_to_eeprom();
152 #ifdef DYNAMIC_KEYMAP_ENABLE
154 if (record->event.pressed) {
155 if ( keycode >= MACRO00 && keycode <= MACRO15 )
157 uint8_t id = keycode - MACRO00;
158 dynamic_keymap_macro_send(id);
162 #endif //DYNAMIC_KEYMAP_ENABLE
164 return process_record_user(keycode, record);;
168 // Start Dynamic Keymap code
171 void raw_hid_receive( uint8_t *data, uint8_t length )
173 uint8_t *command_id = &(data[0]);
174 uint8_t *command_data = &(data[1]);
175 switch ( *command_id )
177 case id_get_protocol_version:
179 command_data[0] = PROTOCOL_VERSION >> 8;
180 command_data[1] = PROTOCOL_VERSION & 0xFF;
183 case id_get_keyboard_value:
185 switch( command_data[0])
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;
198 *command_id = id_unhandled;
204 #ifdef DYNAMIC_KEYMAP_ENABLE
206 case id_dynamic_keymap_get_keycode:
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;
213 case id_dynamic_keymap_set_keycode:
215 dynamic_keymap_set_keycode( command_data[0], command_data[1], command_data[2], ( command_data[3] << 8 ) | command_data[4] );
218 case id_dynamic_keymap_reset:
220 dynamic_keymap_reset();
223 case id_dynamic_keymap_macro_get_count:
225 command_data[0] = dynamic_keymap_macro_get_count();
228 case id_dynamic_keymap_macro_get_buffer_size:
230 uint16_t size = dynamic_keymap_macro_get_buffer_size();
231 command_data[0] = size >> 8;
232 command_data[1] = size & 0xFF;
235 case id_dynamic_keymap_macro_get_buffer:
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] );
242 case id_dynamic_keymap_macro_set_buffer:
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] );
249 case id_dynamic_keymap_macro_reset:
251 dynamic_keymap_macro_reset();
254 case id_dynamic_keymap_get_layer_count:
256 command_data[0] = dynamic_keymap_get_layer_count();
259 case id_dynamic_keymap_get_buffer:
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] );
266 case id_dynamic_keymap_set_buffer:
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] );
273 #endif // DYNAMIC_KEYMAP_ENABLE
274 case id_eeprom_reset:
279 case id_bootloader_jump:
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
291 // Unhandled message.
292 *command_id = id_unhandled;
297 // Return same buffer with values changed
298 raw_hid_send( data, length );