1 #include "satisfaction75.h"
8 // #ifdef QWIIC_MICRO_OLED_ENABLE
9 #include "micro_oled.h"
15 #include "dynamic_keymap.h"
16 #include "tmk_core/common/eeprom.h"
19 #include "keyboards/zeal60/zeal60_api.h" // Temporary hack
20 #include "keyboards/zeal60/zeal60_keycodes.h" // Temporary hack
23 /* Artificial delay added to get media keys to work in the encoder*/
24 #define MEDIA_KEY_DELAY 10
28 volatile uint8_t led_numlock = false;
29 volatile uint8_t led_capslock = false;
30 volatile uint8_t led_scrolllock = false;
34 bool queue_for_send = false;
35 bool clock_set_mode = false;
36 uint8_t oled_mode = OLED_DEFAULT;
37 bool oled_sleeping = false;
39 uint8_t encoder_value = 32;
40 uint8_t encoder_mode = ENC_MODE_VOLUME;
41 uint8_t enabled_encoder_modes = 0x1F;
43 RTCDateTime last_timespec;
44 uint16_t last_minute = 0;
46 uint8_t time_config_idx = 0;
47 int8_t hour_config = 0;
48 int16_t minute_config = 0;
49 int8_t year_config = 0;
50 int8_t month_config = 0;
51 int8_t day_config = 0;
52 uint8_t previous_encoder_mode = 0;
54 backlight_config_t kb_backlight_config = {
57 .level = BACKLIGHT_LEVELS
60 bool eeprom_is_valid(void)
62 return (eeprom_read_word(((void*)EEPROM_MAGIC_ADDR)) == EEPROM_MAGIC &&
63 eeprom_read_byte(((void*)EEPROM_VERSION_ADDR)) == EEPROM_VERSION);
66 void eeprom_set_valid(bool valid)
68 eeprom_update_word(((void*)EEPROM_MAGIC_ADDR), valid ? EEPROM_MAGIC : 0xFFFF);
69 eeprom_update_byte(((void*)EEPROM_VERSION_ADDR), valid ? EEPROM_VERSION : 0xFF);
72 void eeprom_reset(void)
74 // Set the Zeal60 specific EEPROM state as invalid.
75 eeprom_set_valid(false);
76 // Set the TMK/QMK EEPROM state as invalid.
82 void raw_hid_receive( uint8_t *data, uint8_t length )
84 uint8_t *command_id = &(data[0]);
85 uint8_t *command_data = &(data[1]);
86 switch ( *command_id )
88 case id_get_protocol_version:
90 command_data[0] = PROTOCOL_VERSION >> 8;
91 command_data[1] = PROTOCOL_VERSION & 0xFF;
94 case id_get_keyboard_value:
96 if ( command_data[0] == id_uptime )
98 uint32_t value = timer_read32();
99 command_data[1] = (value >> 24 ) & 0xFF;
100 command_data[2] = (value >> 16 ) & 0xFF;
101 command_data[3] = (value >> 8 ) & 0xFF;
102 command_data[4] = value & 0xFF;
106 *command_id = id_unhandled;
110 #ifdef DYNAMIC_KEYMAP_ENABLE
111 case id_dynamic_keymap_get_keycode:
113 uint16_t keycode = dynamic_keymap_get_keycode( command_data[0], command_data[1], command_data[2] );
114 command_data[3] = keycode >> 8;
115 command_data[4] = keycode & 0xFF;
118 case id_dynamic_keymap_set_keycode:
120 dynamic_keymap_set_keycode( command_data[0], command_data[1], command_data[2], ( command_data[3] << 8 ) | command_data[4] );
123 case id_dynamic_keymap_reset:
125 dynamic_keymap_reset();
128 case id_dynamic_keymap_macro_get_count:
130 command_data[0] = dynamic_keymap_macro_get_count();
133 case id_dynamic_keymap_macro_get_buffer_size:
135 uint16_t size = dynamic_keymap_macro_get_buffer_size();
136 command_data[0] = size >> 8;
137 command_data[1] = size & 0xFF;
140 case id_dynamic_keymap_macro_get_buffer:
142 uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
143 uint16_t size = command_data[2]; // size <= 28
144 dynamic_keymap_macro_get_buffer( offset, size, &command_data[3] );
147 case id_dynamic_keymap_macro_set_buffer:
149 uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
150 uint16_t size = command_data[2]; // size <= 28
151 dynamic_keymap_macro_set_buffer( offset, size, &command_data[3] );
154 case id_dynamic_keymap_macro_reset:
156 dynamic_keymap_macro_reset();
159 case id_dynamic_keymap_get_layer_count:
161 command_data[0] = dynamic_keymap_get_layer_count();
164 case id_dynamic_keymap_get_buffer:
166 uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
167 uint16_t size = command_data[2]; // size <= 28
168 dynamic_keymap_get_buffer( offset, size, &command_data[3] );
171 case id_dynamic_keymap_set_buffer:
173 uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
174 uint16_t size = command_data[2]; // size <= 28
175 dynamic_keymap_set_buffer( offset, size, &command_data[3] );
178 #endif // DYNAMIC_KEYMAP_ENABLE
179 case id_eeprom_reset:
184 case id_bootloader_jump:
186 // Need to send data back before the jump
187 // Informs host that the command is handled
188 raw_hid_send( data, length );
189 // Give host time to read it
196 // Unhandled message.
197 *command_id = id_unhandled;
202 // Return same buffer with values changed
203 raw_hid_send( data, length );
210 void read_host_led_state(void) {
211 uint8_t leds = host_keyboard_leds();
212 if (leds & (1 << USB_LED_NUM_LOCK)) {
213 if (led_numlock == false){
216 if (led_numlock == true){
217 led_numlock = false;}
219 if (leds & (1 << USB_LED_CAPS_LOCK)) {
220 if (led_capslock == false){
221 led_capslock = true;}
223 if (led_capslock == true){
224 led_capslock = false;}
226 if (leds & (1 << USB_LED_SCROLL_LOCK)) {
227 if (led_scrolllock == false){
228 led_scrolllock = true;}
230 if (led_scrolllock == true){
231 led_scrolllock = false;}
235 uint32_t layer_state_set_kb(uint32_t state) {
236 state = layer_state_set_user(state);
237 layer = biton32(state);
238 queue_for_send = true;
242 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
243 queue_for_send = true;
246 if (record->event.pressed) {
247 oled_mode = (oled_mode + 1) % _NUM_OLED_MODES;
252 if (record->event.pressed) {
254 pre_encoder_mode_change();
255 clock_set_mode = false;
256 encoder_mode = previous_encoder_mode;
257 post_encoder_mode_change();
260 previous_encoder_mode = encoder_mode;
261 pre_encoder_mode_change();
262 clock_set_mode = true;
263 encoder_mode = ENC_MODE_CLOCK_SET;
264 post_encoder_mode_change();
269 if (record->event.pressed) {
270 uint16_t mapped_code = handle_encoder_press();
271 uint16_t held_keycode_timer = timer_read();
272 if(mapped_code != 0){
273 register_code(mapped_code);
274 while (timer_elapsed(held_keycode_timer) < MEDIA_KEY_DELAY){ /* no-op */ }
275 unregister_code(mapped_code);
278 // Do something else when release
285 #ifdef DYNAMIC_KEYMAP_ENABLE
287 if (record->event.pressed) {
288 if ( keycode >= MACRO00 && keycode <= MACRO15 )
290 uint8_t id = keycode - MACRO00;
291 dynamic_keymap_macro_send(id);
295 #endif //DYNAMIC_KEYMAP_ENABLE
297 return process_record_user(keycode, record);
301 void encoder_update_kb(uint8_t index, bool clockwise) {
302 encoder_value = (encoder_value + (clockwise ? 1 : -1)) % 64;
303 queue_for_send = true;
306 uint16_t mapped_code = 0;
308 mapped_code = handle_encoder_clockwise();
310 mapped_code = handle_encoder_ccw();
312 uint16_t held_keycode_timer = timer_read();
313 if(mapped_code != 0){
314 register_code(mapped_code);
315 while (timer_elapsed(held_keycode_timer) < MEDIA_KEY_DELAY){ /* no-op */ }
316 unregister_code(mapped_code);
320 change_encoder_mode(false);
322 change_encoder_mode(true);
328 void eeprom_init_kb(void)
330 // If the EEPROM has the magic, the data is good.
331 // OK to load from EEPROM.
332 if (eeprom_is_valid()) {
333 //backlight_config_load();
335 // If the EEPROM has not been saved before, or is out of date,
336 // save the default values to the EEPROM. Default values
337 // come from construction of the zeal_backlight_config instance.
338 //backlight_config_save();
339 #ifdef DYNAMIC_KEYMAP_ENABLE
340 // This resets the keymaps in EEPROM to what is in flash.
341 dynamic_keymap_reset();
342 // This resets the macros in EEPROM to nothing.
343 dynamic_keymap_macro_reset();
345 // Save the magic number last, in case saving was interrupted
346 eeprom_set_valid(true);
350 void matrix_init_kb(void)
353 rtcGetTime(&RTCD1, &last_timespec);
354 queue_for_send = true;
355 backlight_init_ports();
360 void matrix_scan_kb(void) {
361 rtcGetTime(&RTCD1, &last_timespec);
362 uint16_t minutes_since_midnight = last_timespec.millisecond / 1000 / 60;
364 if (minutes_since_midnight != last_minute){
365 last_minute = minutes_since_midnight;
367 queue_for_send = true;
371 if (queue_for_send && oled_mode != OLED_OFF) {
372 oled_sleeping = false;
373 read_host_led_state();
375 queue_for_send = false;
377 if (timer_elapsed(last_flush) > ScreenOffInterval && !oled_sleeping) {
378 send_command(DISPLAYOFF); /* 0xAE */
379 oled_sleeping = true;