satisfaction_encoder.c (6369B)
1 // Copyright 2023 Andrew Kannan 2 // SPDX-License-Identifier: GPL-2.0-or-later 3 4 #include "satisfaction_core.h" 5 #include "eeprom.h" 6 #ifdef BACKLIGHT_ENABLE 7 # include "backlight.h" 8 #endif 9 10 void pre_encoder_mode_change(void){ 11 if(encoder_mode == ENC_MODE_CLOCK_SET){ 12 RTCDateTime timespec; 13 timespec.year = year_config; 14 timespec.month = month_config; 15 timespec.day = day_config; 16 // timespec.dayofweek = last_timespec.dayofweek; 17 // timespec.dstflag = last_timespec.dstflag; 18 timespec.millisecond = (hour_config * 60 + minute_config) * 60 * 1000; 19 rtcSetTime(&RTCD1, ×pec); 20 } 21 } 22 23 void post_encoder_mode_change(void){ 24 if(encoder_mode == ENC_MODE_CLOCK_SET){ 25 hour_config = (last_minute / 60); 26 minute_config = last_minute % 60; 27 year_config = last_timespec.year; 28 month_config = last_timespec.month; 29 day_config = last_timespec.day; 30 time_config_idx = 0; 31 } 32 } 33 34 void change_encoder_mode(bool negative){ 35 pre_encoder_mode_change(); 36 if(enabled_encoder_modes == 0){ 37 enabled_encoder_modes = 0x1F; 38 } 39 do { 40 if(negative){ 41 if (encoder_mode == 0){ 42 encoder_mode = _NUM_ENCODER_MODES - 1; 43 } else{ 44 encoder_mode = encoder_mode - 1; 45 } 46 } else { 47 encoder_mode = (encoder_mode + 1) % _NUM_ENCODER_MODES; 48 } 49 } while(((1 << encoder_mode) & enabled_encoder_modes) == 0); 50 post_encoder_mode_change(); 51 } 52 53 void update_time_config(int8_t increment){ 54 uint8_t day_limit = 31; 55 uint16_t adjusted_year = 1980 + year_config; 56 switch(time_config_idx){ 57 case 0: // hour 58 default: 59 hour_config = (hour_config + increment) % 24; 60 if (hour_config < 0){ 61 hour_config += 24; 62 } 63 break; 64 case 1: // minute 65 minute_config = (minute_config + increment) % 60; 66 if (minute_config < 0){ 67 minute_config += 60; 68 } 69 break; 70 case 2: // year 71 year_config += increment; 72 break; 73 case 3: // month 74 month_config = (month_config % 12) + increment; 75 if (month_config <= 0){ 76 month_config += 12; 77 } 78 break; 79 case 4: //day 80 if (month_config == 9 || month_config == 4 || month_config == 6 || month_config == 11){ 81 day_limit = 30; 82 } else if(month_config == 2){ 83 day_limit = adjusted_year % 4 == 0 && !(adjusted_year % 100 == 0 && adjusted_year % 400 != 0) ? 29 : 28; 84 } 85 day_config = (day_config % day_limit) + increment; 86 if(day_config <= 0){ 87 day_config += day_limit; 88 } 89 break; 90 } 91 } 92 93 uint16_t handle_encoder_clockwise(void){ 94 uint16_t mapped_code = 0; 95 switch(encoder_mode){ 96 default: 97 case ENC_MODE_VOLUME: 98 mapped_code = KC_VOLU; 99 break; 100 case ENC_MODE_MEDIA: 101 mapped_code = KC_MEDIA_NEXT_TRACK; 102 break; 103 case ENC_MODE_SCROLL: 104 mapped_code = QK_MOUSE_WHEEL_DOWN; 105 break; 106 #ifdef BACKLIGHT_ENABLE 107 case ENC_MODE_BACKLIGHT: 108 backlight_increase(); 109 if(get_backlight_level() != 0){ 110 backlight_enable(); 111 } 112 break; 113 #endif 114 case ENC_MODE_BRIGHTNESS: 115 mapped_code = KC_BRIGHTNESS_UP; 116 break; 117 #ifdef DYNAMIC_KEYMAP_ENABLE 118 case ENC_MODE_CUSTOM0: 119 mapped_code = retrieve_custom_encoder_config(0, ENC_CUSTOM_CW); 120 break; 121 case ENC_MODE_CUSTOM1: 122 mapped_code = retrieve_custom_encoder_config(1, ENC_CUSTOM_CW); 123 break; 124 case ENC_MODE_CUSTOM2: 125 mapped_code = retrieve_custom_encoder_config(2, ENC_CUSTOM_CW); 126 break; 127 #endif 128 case ENC_MODE_CLOCK_SET: 129 update_time_config(1); 130 break; 131 } 132 return mapped_code; 133 } 134 135 uint16_t handle_encoder_ccw(void){ 136 uint16_t mapped_code = 0; 137 switch(encoder_mode){ 138 default: 139 case ENC_MODE_VOLUME: 140 mapped_code = KC_VOLD; 141 break; 142 case ENC_MODE_MEDIA: 143 mapped_code = KC_MEDIA_PREV_TRACK; 144 break; 145 case ENC_MODE_SCROLL: 146 mapped_code = QK_MOUSE_WHEEL_UP; 147 break; 148 #ifdef BACKLIGHT_ENABLE 149 case ENC_MODE_BACKLIGHT: 150 backlight_decrease(); 151 if(get_backlight_level() == 0){ 152 backlight_disable(); 153 } 154 break; 155 #endif 156 case ENC_MODE_BRIGHTNESS: 157 mapped_code = KC_BRIGHTNESS_DOWN; 158 break; 159 #ifdef DYNAMIC_KEYMAP_ENABLE 160 case ENC_MODE_CUSTOM0: 161 mapped_code = retrieve_custom_encoder_config(0, ENC_CUSTOM_CCW); 162 break; 163 case ENC_MODE_CUSTOM1: 164 mapped_code = retrieve_custom_encoder_config(1, ENC_CUSTOM_CCW); 165 break; 166 case ENC_MODE_CUSTOM2: 167 mapped_code = retrieve_custom_encoder_config(2, ENC_CUSTOM_CCW); 168 break; 169 #endif 170 171 case ENC_MODE_CLOCK_SET: 172 update_time_config(-1); 173 break; 174 } 175 return mapped_code; 176 } 177 178 uint16_t handle_encoder_press(void){ 179 uint16_t mapped_code = 0; 180 switch(encoder_mode){ 181 case ENC_MODE_VOLUME: 182 mapped_code = KC_MUTE; 183 break; 184 default: 185 case ENC_MODE_MEDIA: 186 mapped_code = KC_MEDIA_PLAY_PAUSE; 187 break; 188 case ENC_MODE_SCROLL: 189 mapped_code = QK_MOUSE_BUTTON_3; 190 break; 191 #ifdef BACKLIGHT_ENABLE 192 case ENC_MODE_BACKLIGHT: 193 breathing_toggle(); 194 break; 195 #endif 196 #ifdef DYNAMIC_KEYMAP_ENABLE 197 case ENC_MODE_CUSTOM0: 198 mapped_code = retrieve_custom_encoder_config(0, ENC_CUSTOM_PRESS); 199 break; 200 case ENC_MODE_CUSTOM1: 201 mapped_code = retrieve_custom_encoder_config(1, ENC_CUSTOM_PRESS); 202 break; 203 case ENC_MODE_CUSTOM2: 204 mapped_code = retrieve_custom_encoder_config(2, ENC_CUSTOM_PRESS); 205 break; 206 #endif 207 case ENC_MODE_CLOCK_SET: 208 time_config_idx = (time_config_idx + 1) % 5; 209 case ENC_MODE_BRIGHTNESS: 210 break; 211 } 212 return mapped_code; 213 } 214 215 216 uint16_t retrieve_custom_encoder_config(uint8_t encoder_idx, uint8_t behavior){ 217 #ifdef DYNAMIC_KEYMAP_ENABLE 218 uint32_t offset = EEPROM_CUSTOM_ENCODER_OFFSET + (encoder_idx * 6) + (behavior * 2); 219 //big endian 220 uint8_t hi, lo; 221 read_custom_config(&hi, offset+0, 1); 222 read_custom_config(&lo, offset+1, 1); 223 uint16_t keycode = hi << 8; 224 keycode |= lo; 225 return keycode; 226 #else 227 return 0; 228 #endif 229 } 230 231 void set_custom_encoder_config(uint8_t encoder_idx, uint8_t behavior, uint16_t new_code){ 232 #ifdef DYNAMIC_KEYMAP_ENABLE 233 uint32_t offset = EEPROM_CUSTOM_ENCODER_OFFSET + (encoder_idx * 6) + (behavior * 2); 234 uint8_t hi = new_code >> 8; 235 uint8_t lo = new_code & 0xFF; 236 write_custom_config(&hi, offset+0, 1); 237 write_custom_config(&lo, offset+1, 1); 238 #endif 239 }