diff options
Diffstat (limited to 'keyboards/cannonkeys/satisfaction75/satisfaction_encoder.c')
-rw-r--r-- | keyboards/cannonkeys/satisfaction75/satisfaction_encoder.c | 238 |
1 files changed, 238 insertions, 0 deletions
diff --git a/keyboards/cannonkeys/satisfaction75/satisfaction_encoder.c b/keyboards/cannonkeys/satisfaction75/satisfaction_encoder.c new file mode 100644 index 000000000..0d2fdc359 --- /dev/null +++ b/keyboards/cannonkeys/satisfaction75/satisfaction_encoder.c | |||
@@ -0,0 +1,238 @@ | |||
1 | #include "satisfaction75.h" | ||
2 | #include "eeprom.h" | ||
3 | |||
4 | void pre_encoder_mode_change(){ | ||
5 | if(encoder_mode == ENC_MODE_CLOCK_SET){ | ||
6 | RTCDateTime timespec; | ||
7 | timespec.year = year_config; | ||
8 | timespec.month = month_config; | ||
9 | timespec.day = day_config; | ||
10 | // timespec.dayofweek = last_timespec.dayofweek; | ||
11 | // timespec.dstflag = last_timespec.dstflag; | ||
12 | timespec.millisecond = (hour_config * 60 + minute_config) * 60 * 1000; | ||
13 | rtcSetTime(&RTCD1, ×pec); | ||
14 | } else if (encoder_mode == ENC_MODE_BACKLIGHT){ | ||
15 | backlight_config_save(); | ||
16 | } | ||
17 | } | ||
18 | |||
19 | void post_encoder_mode_change(){ | ||
20 | if(encoder_mode == ENC_MODE_CLOCK_SET){ | ||
21 | hour_config = (last_minute / 60); | ||
22 | minute_config = last_minute % 60; | ||
23 | year_config = last_timespec.year; | ||
24 | month_config = last_timespec.month; | ||
25 | day_config = last_timespec.day; | ||
26 | time_config_idx = 0; | ||
27 | } | ||
28 | } | ||
29 | |||
30 | void change_encoder_mode(bool negative){ | ||
31 | pre_encoder_mode_change(); | ||
32 | if(enabled_encoder_modes == 0){ | ||
33 | enabled_encoder_modes = 0x1F; | ||
34 | } | ||
35 | do { | ||
36 | if(negative){ | ||
37 | if (encoder_mode == 0){ | ||
38 | encoder_mode = _NUM_ENCODER_MODES - 1; | ||
39 | } else{ | ||
40 | encoder_mode = encoder_mode - 1; | ||
41 | } | ||
42 | } else { | ||
43 | encoder_mode = (encoder_mode + 1) % _NUM_ENCODER_MODES; | ||
44 | } | ||
45 | } while(((1 << encoder_mode) & enabled_encoder_modes) == 0); | ||
46 | post_encoder_mode_change(); | ||
47 | } | ||
48 | |||
49 | void update_time_config(int8_t increment){ | ||
50 | uint8_t day_limit = 31; | ||
51 | uint16_t adjusted_year = 1980 + year_config; | ||
52 | switch(time_config_idx){ | ||
53 | case 0: // hour | ||
54 | default: | ||
55 | hour_config = (hour_config + increment) % 24; | ||
56 | if (hour_config < 0){ | ||
57 | hour_config += 24; | ||
58 | } | ||
59 | break; | ||
60 | case 1: // minute | ||
61 | minute_config = (minute_config + increment) % 60; | ||
62 | if (minute_config < 0){ | ||
63 | minute_config += 60; | ||
64 | } | ||
65 | break; | ||
66 | case 2: // year | ||
67 | year_config += increment; | ||
68 | break; | ||
69 | case 3: // month | ||
70 | month_config = (month_config % 12) + increment; | ||
71 | if (month_config <= 0){ | ||
72 | month_config += 12; | ||
73 | } | ||
74 | break; | ||
75 | case 4: //day | ||
76 | if (month_config == 9 || month_config == 4 || month_config == 6 || month_config == 11){ | ||
77 | day_limit = 30; | ||
78 | } else if(month_config == 2){ | ||
79 | day_limit = adjusted_year % 4 == 0 && !(adjusted_year % 100 == 0 && adjusted_year % 400 != 0) ? 29 : 28; | ||
80 | } | ||
81 | day_config = (day_config % day_limit) + increment; | ||
82 | if(day_config <= 0){ | ||
83 | day_config += day_limit; | ||
84 | } | ||
85 | break; | ||
86 | } | ||
87 | } | ||
88 | |||
89 | uint16_t handle_encoder_clockwise(){ | ||
90 | uint16_t mapped_code = 0; | ||
91 | switch(encoder_mode){ | ||
92 | default: | ||
93 | case ENC_MODE_VOLUME: | ||
94 | mapped_code = KC_VOLU; | ||
95 | break; | ||
96 | case ENC_MODE_MEDIA: | ||
97 | mapped_code = KC_MEDIA_NEXT_TRACK; | ||
98 | break; | ||
99 | case ENC_MODE_SCROLL: | ||
100 | mapped_code = KC_WH_D; | ||
101 | break; | ||
102 | case ENC_MODE_BACKLIGHT: | ||
103 | kb_backlight_config.level = kb_backlight_config.level + 1; | ||
104 | if(kb_backlight_config.level > BACKLIGHT_LEVELS){ | ||
105 | kb_backlight_config.level = BACKLIGHT_LEVELS; | ||
106 | } | ||
107 | backlight_set(kb_backlight_config.level); | ||
108 | if (kb_backlight_config.level != 0){ | ||
109 | kb_backlight_config.enable = true; | ||
110 | } | ||
111 | break; | ||
112 | case ENC_MODE_BRIGHTNESS: | ||
113 | mapped_code = KC_BRIGHTNESS_UP; | ||
114 | break; | ||
115 | #ifdef DYNAMIC_KEYMAP_ENABLE | ||
116 | case ENC_MODE_CUSTOM0: | ||
117 | mapped_code = retrieve_custom_encoder_config(0, ENC_CUSTOM_CW); | ||
118 | break; | ||
119 | case ENC_MODE_CUSTOM1: | ||
120 | mapped_code = retrieve_custom_encoder_config(1, ENC_CUSTOM_CW); | ||
121 | break; | ||
122 | case ENC_MODE_CUSTOM2: | ||
123 | mapped_code = retrieve_custom_encoder_config(2, ENC_CUSTOM_CW); | ||
124 | break; | ||
125 | #endif | ||
126 | case ENC_MODE_CLOCK_SET: | ||
127 | update_time_config(1); | ||
128 | break; | ||
129 | } | ||
130 | return mapped_code; | ||
131 | } | ||
132 | |||
133 | uint16_t handle_encoder_ccw(){ | ||
134 | uint16_t mapped_code = 0; | ||
135 | switch(encoder_mode){ | ||
136 | default: | ||
137 | case ENC_MODE_VOLUME: | ||
138 | mapped_code = KC_VOLD; | ||
139 | break; | ||
140 | case ENC_MODE_MEDIA: | ||
141 | mapped_code = KC_MEDIA_PREV_TRACK; | ||
142 | break; | ||
143 | case ENC_MODE_SCROLL: | ||
144 | mapped_code = KC_WH_U; | ||
145 | break; | ||
146 | case ENC_MODE_BACKLIGHT: | ||
147 | // mapped_code = BL_DEC; | ||
148 | if(kb_backlight_config.level != 0){ | ||
149 | kb_backlight_config.level = kb_backlight_config.level - 1; | ||
150 | } | ||
151 | backlight_set(kb_backlight_config.level); | ||
152 | if (kb_backlight_config.level == 0){ | ||
153 | kb_backlight_config.enable = false; | ||
154 | } | ||
155 | break; | ||
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(){ | ||
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 = KC_BTN3; | ||
190 | break; | ||
191 | case ENC_MODE_BACKLIGHT: | ||
192 | // mapped_code = BL_TOGG; | ||
193 | kb_backlight_config.breathing = !kb_backlight_config.breathing; | ||
194 | if(!kb_backlight_config.breathing){ | ||
195 | breathing_disable(); | ||
196 | } else{ | ||
197 | breathing_enable(); | ||
198 | } | ||
199 | break; | ||
200 | #ifdef DYNAMIC_KEYMAP_ENABLE | ||
201 | case ENC_MODE_CUSTOM0: | ||
202 | mapped_code = retrieve_custom_encoder_config(0, ENC_CUSTOM_PRESS); | ||
203 | break; | ||
204 | case ENC_MODE_CUSTOM1: | ||
205 | mapped_code = retrieve_custom_encoder_config(1, ENC_CUSTOM_PRESS); | ||
206 | break; | ||
207 | case ENC_MODE_CUSTOM2: | ||
208 | mapped_code = retrieve_custom_encoder_config(2, ENC_CUSTOM_PRESS); | ||
209 | break; | ||
210 | #endif | ||
211 | case ENC_MODE_CLOCK_SET: | ||
212 | time_config_idx = (time_config_idx + 1) % 5; | ||
213 | case ENC_MODE_BRIGHTNESS: | ||
214 | break; | ||
215 | } | ||
216 | return mapped_code; | ||
217 | } | ||
218 | |||
219 | |||
220 | uint16_t retrieve_custom_encoder_config(uint8_t encoder_idx, uint8_t behavior){ | ||
221 | #ifdef DYNAMIC_KEYMAP_ENABLE | ||
222 | void* addr = (void*)(EEPROM_CUSTOM_ENCODER + (encoder_idx * 6) + (behavior * 2)); | ||
223 | //big endian | ||
224 | uint16_t keycode = eeprom_read_byte(addr) << 8; | ||
225 | keycode |= eeprom_read_byte(addr + 1); | ||
226 | return keycode; | ||
227 | #else | ||
228 | return 0; | ||
229 | #endif | ||
230 | } | ||
231 | |||
232 | void set_custom_encoder_config(uint8_t encoder_idx, uint8_t behavior, uint16_t new_code){ | ||
233 | #ifdef DYNAMIC_KEYMAP_ENABLE | ||
234 | void* addr = (void*)(EEPROM_CUSTOM_ENCODER + (encoder_idx * 6) + (behavior * 2)); | ||
235 | eeprom_update_byte(addr, (uint8_t)(new_code >> 8)); | ||
236 | eeprom_update_byte(addr + 1, (uint8_t)(new_code & 0xFF)); | ||
237 | #endif | ||
238 | } | ||