summaryrefslogtreecommitdiff
path: root/users/bbaserdem/bb-encoder.c
blob: eea9751051aa33937fcb8774ec36990d7d85a2f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/* Copyright 2021 Batuhan Başerdem
 * <baserdem.batuhan@gmail.com> @bbaserdem
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "bb-encoder.h"

// Need this to call velocikey activation
#ifdef VELOCIKEY_ENABLE
#   include "velocikey.h"
#endif // VELOCIKEY_ENABLE
// Need memcpy and memcmp from string.h along with transfer stuff
#ifdef OLED_ENABLE
#   include <string.h>
#endif // OLED_ENABLE

/* ROTARY ENCODER
 * This contains my general rotary encoder code
 * Encoders each have a list of different modes they can be in.
 * Each mode also have an on click action as well.
 * Modes can be cycled using either shift-click or ctrl-click
 * Modes can be reset using OS click
 * Some modes are only accessible through some keymap layers
 */

// Default state for the encoders
void reset_encoder_state(void) {
    userspace_config.e0base = 0;
    userspace_config.e0point = 0;
    userspace_config.e0rgb = 0;
    userspace_config.e1base = 1;
    userspace_config.e1point = 1;
    userspace_config.e1rgb = 1;
}

// Encoder scroll functionality
bool encoder_update_user(uint8_t index, bool clockwise) {
    uint8_t this_number;
    // Differentiate layer roles
    switch (get_highest_layer(layer_state)) {
#       ifdef RGB_MATRIX_ENABLE
        case _MEDI:
            // Get correct index
            if (index == 0) {
                this_number = userspace_config.e0rgb;
            } else if (index == 1) {
                this_number = userspace_config.e1rgb;
            } else {
                this_number = 128;
            }
            switch(this_number) {
                case 0: // Effect the RGB mode
                    if (clockwise) {
                        rgb_matrix_step_noeeprom();
                    } else {
                        rgb_matrix_step_reverse_noeeprom();
                    }
                    break;
                case 1: // Effect the RGB hue
                    if (clockwise) {
                        rgb_matrix_increase_hue_noeeprom();
                    } else {
                        rgb_matrix_decrease_hue_noeeprom();
                    }
                    break;
                case 2: // Effect the RGB saturation
                    if (clockwise) {
                        rgb_matrix_increase_sat_noeeprom();
                    } else {
                        rgb_matrix_decrease_sat_noeeprom();
                    }
                    break;
                case 3: // Effect the RGB brightness
                    if (clockwise) {
                        rgb_matrix_increase_val_noeeprom();
                    } else {
                        rgb_matrix_decrease_val_noeeprom();
                    }
                    break;
                case 4: // Effect the RGB effect speed
                    if (clockwise) {
                        rgb_matrix_increase_speed_noeeprom();
                    } else {
                        rgb_matrix_decrease_speed_noeeprom();
                    }
                    break;
            }
            break;
#       endif // RGB_MATRIX_ENABLE
#       ifdef MOUSEKEY_ENABLE
        case _MOUS:
            // Get correct index
            if (index == 0) {
                this_number = userspace_config.e0point;
            } else if (index == 1) {
                this_number = userspace_config.e1point;
            } else {
                this_number = 128;
            }
            switch(this_number) {
                case 0: // Move mouse on horizontal axis
                    if (clockwise) {
                        tap_code(KC_MS_R);
                    } else {
                        tap_code(KC_MS_L);
                    }
                    break;
                case 1: // Move mouse on vertical axis
                    if (clockwise) {
                        tap_code(KC_MS_D);
                    } else {
                        tap_code(KC_MS_U);
                    }
                    break;
                case 2: // Move mouse wheel on vertical axis
                    if (clockwise) {
                        tap_code(KC_WH_D);
                    } else {
                        tap_code(KC_WH_U);
                    }
                    break;
                case 3: // Move mouse on horizontal axis
                    if (clockwise) {
                        tap_code(KC_WH_R);
                    } else {
                        tap_code(KC_WH_L);
                    }
                    break;
            }
            break;
#       endif // MOUSEKEY_ENABLE
        default:
            // Get correct index
            if (index == 0) {
                this_number = userspace_config.e0base;
            } else if (index == 1) {
                this_number = userspace_config.e1base;
            } else {
                this_number = 128;
            }
            switch(this_number) {
                case 0: // Volume
                    if (clockwise) {
                        tap_code16(KC_VOLU);
                    } else {
                        tap_code16(KC_VOLD);
                    }
                    break;
                case 1: // Song change
                    if (clockwise) {
                        tap_code16(KC_MNXT);
                    } else {
                        tap_code16(KC_MPRV);
                    }
                    break;
                case 2: // Move to audio sink
                    if (clockwise) {
                        tap_code16(KC_F13);
                    } else {
                        tap_code16(S(KC_F13));
                    }
                    break;
                case 3: // Volume of source
                    if (clockwise) {
                        tap_code16(S(KC_VOLU));
                    } else {
                        tap_code16(C(KC_VOLD));
                    }
                    break;
                case 4: // Move to audio source
                    if (clockwise) {
                        tap_code16(C(KC_F13));
                    } else {
                        tap_code16(C(S(KC_F13)));
                    }
                    break;
                case 5: // Left-right
                    if (clockwise) {
                        tap_code16(KC_RGHT);
                    } else {
                        tap_code16(KC_LEFT);
                    }
                    break;
                case 6: // Up-down
                    if (clockwise) {
                        tap_code16(KC_DOWN);
                    } else {
                        tap_code16(KC_UP);
                    }
                    break;
                case 7: // Page Up-down
                    if (clockwise) {
                        tap_code16(KC_PGDN);
                    } else {
                        tap_code16(KC_PGUP);
                    }
                    break;
                case 8: // Delete 
                    if (clockwise) {
                        tap_code16(KC_DEL);
                    } else {
                        tap_code16(KC_BSPC);
                    }
                    break;
            }
            break;
    }
    return false;
}

void encoder_click_action(uint8_t index) {
    uint8_t this_number;
    // Differentiate layer roles
    switch (get_highest_layer(layer_state)) {
#       ifdef RGB_MATRIX_ENABLE
        case _MEDI:
            // Get correct index
            if (index == 0) {
                this_number = userspace_config.e0rgb;
            } else if (index == 1) {
                this_number = userspace_config.e1rgb;
            } else {
                this_number = 128;
            }
            switch(this_number) {
                case 0: // Return to no animation
                    rgb_matrix_mode_noeeprom(RGB_MATRIX_SOLID_COLOR);
                    break;
                case 1:
                case 2:
                case 3: // Toggle
                    rgb_matrix_increase_val_noeeprom();
                    break;
                case 4: // Toggle velocikey
#                   ifdef VELOCIKEY_ENABLE
                    velocikey_toggle();
#                   endif // VELOCIKEY_ENABLE
                    break;
            }
            break;
#       endif // RGB_MATRIX_ENABLE
#       ifdef MOUSEKEY_ENABLE
        case _MOUS:
            // Get correct index
            if (index == 0) {
                this_number = userspace_config.e0point;
            } else if (index == 1) {
                this_number = userspace_config.e1point;
            } else {
                this_number = 128;
            }
            switch(this_number) {
                case 0: // Left click
                    tap_code16(KC_BTN1);
                    break;
                case 1: // Right click
                    tap_code16(KC_BTN2);
                    break;
                case 2:
                case 3: // Middle click
                    tap_code16(KC_BTN2);
                    break;
            }
            break;
#       endif // MOUSEKEY_ENABLE
        default:
            // Get correct index
            if (index == 0) {
                this_number = userspace_config.e0base;
            } else if (index == 1) {
                this_number = userspace_config.e1base;
            } else {
                this_number = 128;
            }
            switch(this_number) {
                case 0: // Toggle mute
                case 2:
                    tap_code16(KC_MUTE);
                    break;
                case 1: // Pause
                    tap_code16(KC_MPLY);
                    break;
                case 3: // Mute source
                case 4:
                    tap_code16(A(KC_MUTE));
                    break;
                case 5: // Insert
                    tap_code16(KC_INS);
                    break;
                case 6: // Capslock
                    tap_code16(KC_CAPS);
                    break;
                case 7: // Redo
                    tap_code16(BB_REDO);
                    break;
                case 8: // Undo
                    tap_code16(BB_UNDO);
                    break;
            }
            break;
    }
}

bool process_record_encoder(uint16_t keycode, keyrecord_t *record) {
    // Check if and which encoder
    int encoder_index = -1;
    
    // Get the pressed encoder
    switch (keycode) {
        case BB_ENC0:
            encoder_index = 0;
            break;
        case BB_ENC1:
            encoder_index = 1;
            break;
    }

    // Activate encoder function of button
    if ((encoder_index >= 0) & (!record->event.pressed)) {
        // If shifted, move mode one point forward
        if (get_mods() & MOD_MASK_SHIFT) {
            switch (get_highest_layer(layer_state)) {
#               ifdef RGB_MATRIX_ENABLE
                case _MEDI:
                    if (encoder_index == 0) {
                        userspace_config.e0rgb = (userspace_config.e0rgb + 1) % 5;
                    } else {
                        userspace_config.e1rgb = (userspace_config.e1rgb + 1) % 5;
                    }
                    break;
#               endif // RGB_MATRIX_ENABLE
#               ifdef MOUSEKEY_ENABLE
                case _MOUS:
                    if (encoder_index == 0) {
                        userspace_config.e0point = (userspace_config.e0point + 1) % 4;
                    } else {
                        userspace_config.e1point = (userspace_config.e1point + 1) % 4;
                    }
                    break;
#               endif // MOUSEKEY_ENABLE
                default:
                    if (encoder_index == 0) {
                        userspace_config.e0base = (userspace_config.e0base + 1) % 9;
                    } else {
                        userspace_config.e1base = (userspace_config.e1base + 1) % 9;
                    }
                    break;
            }
        // If ctrl is active, move mode one point backwards
        } else if (get_mods() & MOD_MASK_CTRL) {
            switch (get_highest_layer(layer_state)) {
#               ifdef RGB_MATRIX_ENABLE
                case _MEDI:
                    if (encoder_index == 0) {
                        userspace_config.e0rgb = (userspace_config.e0rgb + 5 - 1) % 5;
                    } else {
                        userspace_config.e1rgb = (userspace_config.e1rgb + 5 - 1) % 5;
                    }
                    break;
#               endif // RGB_MATRIX_ENABLE
#               ifdef MOUSEKEY_ENABLE
                case _MOUS:
                    if (encoder_index == 0) {
                        userspace_config.e0point = (userspace_config.e0point + 4 - 1) % 4;
                    } else {
                        userspace_config.e1point = (userspace_config.e1point + 4 - 1) % 4;
                    }
                    break;
#               endif // MOUSEKEY_ENABLE
                default:
                    if (encoder_index == 0) {
                        userspace_config.e0base = (userspace_config.e0base + 9 - 1) % 9;
                    } else {
                        userspace_config.e1base = (userspace_config.e1base + 9 - 1) % 9;
                    }
                    break;
            }
        // If meta is active, reset the encoder states
        } else if (get_mods() & MOD_MASK_GUI) {
            reset_encoder_state();
            eeconfig_update_user(userspace_config.raw);
        // If nothing else; just perform the click action
        } else {
            encoder_click_action(encoder_index);
        }
    }
    return true;
}

// For printing status to OLED
#ifdef OLED_ENABLE
void encoder_state_string(uint8_t index, uint8_t layer, char* buffer) {
    uint8_t this_number;
    // Get the layer straight from the main function
    switch (layer) {
        // If RGB control mode is enabled
#       ifdef RGB_MATRIX_ENABLE
        case _MEDI:
            // Get correct index
            if (index == 0) {
                this_number = userspace_config.e0rgb;
            } else if (index == 1) {
                this_number = userspace_config.e1rgb;
            } else {
                this_number = 128;
            }
            switch (this_number) {
                case 0:
                    strcpy(buffer, "ani mode");
                    break;
                case 1:
                    strcpy(buffer, "hue     ");
                    break;
                case 2:
                    strcpy(buffer, "saturat.");
                    break;
                case 3:
                    strcpy(buffer, "bright. ");
                    break;
                case 4:
                    strcpy(buffer, "ani. spd");
                    break;
                default:
                    strcpy(buffer, " -N/A-  ");
                    break;
            }
            break;
#       endif // RGB_MATRIX_ENABLE
        // If pointer control is enabled
#       ifdef MOUSEKEY_ENABLE
        case _MOUS:
            // Get correct index
            if (index == 0) {
                this_number = userspace_config.e0point;
            } else if (index == 1) {
                this_number = userspace_config.e1point;
            } else {
                this_number = 128;
            }
            switch (this_number) {
                case 0:
                    strcpy(buffer, "Lateral ");
                    break;
                case 1:
                    strcpy(buffer, "Vertical");
                    break;
                case 2:
                    strcpy(buffer, "Scr. Ver");
                    break;
                case 3:
                    strcpy(buffer, "Scr. Lat");
                    break;
                default:
                    strcpy(buffer, " -N/A-  ");
                    break;
            }
            break;
#       endif // MOUSEKEY_ENABLE
        default:
            // Get correct index
            if (index == 0) {
                this_number = userspace_config.e0base;
            } else if (index == 1) {
                this_number = userspace_config.e1base;
            } else {
                this_number = 128;
            }
            switch (this_number) {
                case 0:
                    strcpy(buffer, "Volume  ");
                    break;
                case 1:
                    strcpy(buffer, "Song    ");
                    break;
                case 2:
                    strcpy(buffer, "Sink    ");
                    break;
                case 3:
                    strcpy(buffer, "Src. Vol");
                    break;
                case 4:
                    strcpy(buffer, "Source  ");
                    break;
                case 5:
                    strcpy(buffer, "Arrow LR");
                    break;
                case 6:
                    strcpy(buffer, "Arrow UD");
                    break;
                case 7:
                    strcpy(buffer, "Page U/D");
                    break;
                case 8:
                    strcpy(buffer, "Erase   ");
                    break;
                default:
                    strcpy(buffer, " -N/A-  ");
                    break;
            }
            break;
    }
}
#endif // OLED_ENABLE