]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/encoder.c
Remove the need to specify NUM_OF_ENCODERS for the Encoder feature (#6328)
[qmk_firmware.git] / quantum / encoder.c
1 /*
2  * Copyright 2018 Jack Humbert <jack.humb@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "encoder.h"
19
20 // for memcpy
21 #include <string.h>
22
23
24 #ifndef ENCODER_RESOLUTION
25   #define ENCODER_RESOLUTION 4
26 #endif
27
28 #if !defined(ENCODERS_PAD_A) || !defined(ENCODERS_PAD_B)
29   #error "No encoder pads defined by ENCODERS_PAD_A and ENCODERS_PAD_B"
30 #endif
31
32
33 #define NUMBER_OF_ENCODERS (sizeof(encoders_pad_a)/sizeof(pin_t))
34 static pin_t encoders_pad_a[] = ENCODERS_PAD_A;
35 static pin_t encoders_pad_b[] = ENCODERS_PAD_B;
36
37 static int8_t encoder_LUT[] = { 0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0 };
38
39 static uint8_t encoder_state[NUMBER_OF_ENCODERS] = {0};
40
41 #ifdef SPLIT_KEYBOARD
42 // slave half encoders come over as second set of encoders
43 static int8_t encoder_value[NUMBER_OF_ENCODERS * 2] = {0};
44 #else
45 static int8_t encoder_value[NUMBER_OF_ENCODERS] = {0};
46 #endif
47
48 __attribute__ ((weak))
49 void encoder_update_user(int8_t index, bool clockwise) { }
50
51 __attribute__ ((weak))
52 void encoder_update_kb(int8_t index, bool clockwise) {
53   encoder_update_user(index, clockwise);
54 }
55
56 void encoder_init(void) {
57   for (int i = 0; i < NUMBER_OF_ENCODERS; i++) {
58     setPinInputHigh(encoders_pad_a[i]);
59     setPinInputHigh(encoders_pad_b[i]);
60
61     encoder_state[i] = (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1);
62   }
63 }
64
65 void encoder_read(void) {
66   for (int i = 0; i < NUMBER_OF_ENCODERS; i++) {
67     encoder_state[i] <<= 2;
68     encoder_state[i] |= (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1);
69     encoder_value[i] += encoder_LUT[encoder_state[i] & 0xF];
70     if (encoder_value[i] >= ENCODER_RESOLUTION) {
71         encoder_update_kb(i, false);
72     }
73     if (encoder_value[i] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise
74         encoder_update_kb(i, true);
75     }
76     encoder_value[i] %= ENCODER_RESOLUTION;
77   }
78 }
79
80 #ifdef SPLIT_KEYBOARD
81 void encoder_state_raw(uint8_t* slave_state) {
82   memcpy(slave_state, encoder_state, sizeof(encoder_state));
83 }
84
85 void encoder_update_raw(uint8_t* slave_state) {
86   for (int i = 0; i < NUMBER_OF_ENCODERS; i++) {
87     encoder_value[NUMBER_OF_ENCODERS + i] += encoder_LUT[slave_state[i] & 0xF];
88     if (encoder_value[NUMBER_OF_ENCODERS + i] >= ENCODER_RESOLUTION) {
89         encoder_update_kb(NUMBER_OF_ENCODERS + i, false);
90     }
91     if (encoder_value[NUMBER_OF_ENCODERS + i] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise
92         encoder_update_kb(NUMBER_OF_ENCODERS + i, true);
93     }
94     encoder_value[NUMBER_OF_ENCODERS + i] %= ENCODER_RESOLUTION;
95   }
96 }
97 #endif