aboutsummaryrefslogtreecommitdiff
path: root/quantum/encoder.c
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/encoder.c')
-rw-r--r--quantum/encoder.c146
1 files changed, 55 insertions, 91 deletions
diff --git a/quantum/encoder.c b/quantum/encoder.c
index 7d4e97898..8fb87281c 100644
--- a/quantum/encoder.c
+++ b/quantum/encoder.c
@@ -16,17 +16,8 @@
16 */ 16 */
17 17
18#include "encoder.h" 18#include "encoder.h"
19 19#ifdef SPLIT_KEYBOARD
20// this is for unit testing 20# include "split_util.h"
21#if defined(ENCODER_MOCK_SINGLE)
22# include "encoder/tests/mock.h"
23#elif defined(ENCODER_MOCK_SPLIT)
24# include "encoder/tests/mock_split.h"
25#else
26# include <gpio.h>
27# ifdef SPLIT_KEYBOARD
28# include "split_util.h"
29# endif
30#endif 21#endif
31 22
32// for memcpy 23// for memcpy
@@ -36,41 +27,17 @@
36# define ENCODER_RESOLUTION 4 27# define ENCODER_RESOLUTION 4
37#endif 28#endif
38 29
39#if (!defined(ENCODERS_PAD_A) || !defined(ENCODERS_PAD_B)) && (!defined(ENCODERS_PAD_A) || !defined(ENCODERS_PAD_B)) 30#if !defined(ENCODERS_PAD_A) || !defined(ENCODERS_PAD_B)
40# error "No encoder pads defined by ENCODERS_PAD_A and ENCODERS_PAD_B or ENCODERS_PAD_A_RIGHT and ENCODERS_PAD_B_RIGHT" 31# error "No encoder pads defined by ENCODERS_PAD_A and ENCODERS_PAD_B"
41#endif 32#endif
42 33
43// on split keyboards, these are the pads and resolutions for the left half 34#define NUMBER_OF_ENCODERS (sizeof(encoders_pad_a) / sizeof(pin_t))
44static pin_t encoders_pad_a[] = ENCODERS_PAD_A; 35static pin_t encoders_pad_a[] = ENCODERS_PAD_A;
45static pin_t encoders_pad_b[] = ENCODERS_PAD_B; 36static pin_t encoders_pad_b[] = ENCODERS_PAD_B;
46#ifdef ENCODER_RESOLUTIONS 37#ifdef ENCODER_RESOLUTIONS
47static uint8_t encoder_resolutions[] = ENCODER_RESOLUTIONS; 38static uint8_t encoder_resolutions[] = ENCODER_RESOLUTIONS;
48#endif 39#endif
49 40
50#ifndef SPLIT_KEYBOARD
51# define NUMBER_OF_ENCODERS (sizeof(encoders_pad_a) / sizeof(pin_t))
52#else
53// if no pads for right half are defined, we assume the keyboard is symmetric (i.e. same pads)
54# ifndef ENCODERS_PAD_A_RIGHT
55# define ENCODERS_PAD_A_RIGHT ENCODERS_PAD_A
56# endif
57# ifndef ENCODERS_PAD_B_RIGHT
58# define ENCODERS_PAD_B_RIGHT ENCODERS_PAD_B
59# endif
60# if defined(ENCODER_RESOLUTIONS) && !defined(ENCODER_RESOLUTIONS_RIGHT)
61# define ENCODER_RESOLUTIONS_RIGHT ENCODER_RESOLUTIONS
62# endif
63
64# define NUMBER_OF_ENCODERS ((sizeof(encoders_pad_a) + sizeof(encoders_pad_a_right)) / sizeof(pin_t))
65# define NUMBER_OF_ENCODERS_LEFT (sizeof(encoders_pad_a) / sizeof(pin_t))
66# define NUMBER_OF_ENCODERS_RIGHT (sizeof(encoders_pad_a_right) / sizeof(pin_t))
67static pin_t encoders_pad_a_right[] = ENCODERS_PAD_A_RIGHT;
68static pin_t encoders_pad_b_right[] = ENCODERS_PAD_B_RIGHT;
69# ifdef ENCODER_RESOLUTIONS_RIGHT
70static uint8_t encoder_resolutions_right[] = ENCODER_RESOLUTIONS_RIGHT;
71# endif
72#endif
73
74#ifndef ENCODER_DIRECTION_FLIP 41#ifndef ENCODER_DIRECTION_FLIP
75# define ENCODER_CLOCKWISE true 42# define ENCODER_CLOCKWISE true
76# define ENCODER_COUNTER_CLOCKWISE false 43# define ENCODER_COUNTER_CLOCKWISE false
@@ -83,81 +50,78 @@ static int8_t encoder_LUT[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1,
83static uint8_t encoder_state[NUMBER_OF_ENCODERS] = {0}; 50static uint8_t encoder_state[NUMBER_OF_ENCODERS] = {0};
84static int8_t encoder_pulses[NUMBER_OF_ENCODERS] = {0}; 51static int8_t encoder_pulses[NUMBER_OF_ENCODERS] = {0};
85 52
53#ifdef SPLIT_KEYBOARD
54// right half encoders come over as second set of encoders
55static uint8_t encoder_value[NUMBER_OF_ENCODERS * 2] = {0};
56// row offsets for each hand
57static uint8_t thisHand, thatHand;
58#else
86static uint8_t encoder_value[NUMBER_OF_ENCODERS] = {0}; 59static uint8_t encoder_value[NUMBER_OF_ENCODERS] = {0};
60#endif
87 61
88__attribute__((weak)) bool encoder_update_user(uint8_t index, bool clockwise) { return true; } 62__attribute__((weak)) bool encoder_update_user(uint8_t index, bool clockwise) { return true; }
89 63
90__attribute__((weak)) bool encoder_update_kb(uint8_t index, bool clockwise) { return encoder_update_user(index, clockwise); } 64__attribute__((weak)) bool encoder_update_kb(uint8_t index, bool clockwise) { return encoder_update_user(index, clockwise); }
91 65
92// number of encoders connected to this controller
93static uint8_t numEncodersHere;
94// index of the first encoder connected to this controller (only for right halves, this will be nonzero)
95static uint8_t firstEncoderHere;
96#ifdef SPLIT_KEYBOARD
97// index of the first encoder connected to the other half
98static uint8_t firstEncoderThere;
99#endif
100// the pads for this controller
101static pin_t* pad_a;
102static pin_t* pad_b;
103
104void encoder_init(void) { 66void encoder_init(void) {
105#ifndef SPLIT_KEYBOARD 67#if defined(SPLIT_KEYBOARD) && defined(ENCODERS_PAD_A_RIGHT) && defined(ENCODERS_PAD_B_RIGHT)
106 numEncodersHere = NUMBER_OF_ENCODERS; 68 if (!isLeftHand) {
107 pad_a = encoders_pad_a; 69 const pin_t encoders_pad_a_right[] = ENCODERS_PAD_A_RIGHT;
108 pad_b = encoders_pad_b; 70 const pin_t encoders_pad_b_right[] = ENCODERS_PAD_B_RIGHT;
109 firstEncoderHere = 0; 71# if defined(ENCODER_RESOLUTIONS_RIGHT)
110#else 72 const uint8_t encoder_resolutions_right[] = ENCODER_RESOLUTIONS_RIGHT;
111 if (isLeftHand) { 73# endif
112 numEncodersHere = NUMBER_OF_ENCODERS_LEFT; 74 for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) {
113 pad_a = encoders_pad_a; 75 encoders_pad_a[i] = encoders_pad_a_right[i];
114 pad_b = encoders_pad_b; 76 encoders_pad_b[i] = encoders_pad_b_right[i];
115 firstEncoderHere = 0; 77# if defined(ENCODER_RESOLUTIONS_RIGHT)
116 firstEncoderThere = NUMBER_OF_ENCODERS_LEFT; 78 encoder_resolutions[i] = encoder_resolutions_right[i];
117 } else { 79# endif
118 numEncodersHere = NUMBER_OF_ENCODERS_RIGHT; 80 }
119 pad_a = encoders_pad_a_right;
120 pad_b = encoders_pad_b_right;
121 firstEncoderHere = NUMBER_OF_ENCODERS_LEFT;
122 firstEncoderThere = 0;
123 } 81 }
124#endif 82#endif
125 83
126 for (int i = 0; i < numEncodersHere; i++) { 84 for (int i = 0; i < NUMBER_OF_ENCODERS; i++) {
127 setPinInputHigh(pad_a[i]); 85 setPinInputHigh(encoders_pad_a[i]);
128 setPinInputHigh(pad_b[i]); 86 setPinInputHigh(encoders_pad_b[i]);
129 87
130 encoder_state[firstEncoderHere + i] = (readPin(pad_a[i]) << 0) | (readPin(pad_b[i]) << 1); 88 encoder_state[i] = (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1);
131 } 89 }
90
91#ifdef SPLIT_KEYBOARD
92 thisHand = isLeftHand ? 0 : NUMBER_OF_ENCODERS;
93 thatHand = NUMBER_OF_ENCODERS - thisHand;
94#endif
132} 95}
133 96
134static bool encoder_update(int8_t index, uint8_t state) { 97static bool encoder_update(uint8_t index, uint8_t state) {
135 bool changed = false; 98 bool changed = false;
99 uint8_t i = index;
136 100
137#ifdef ENCODER_RESOLUTIONS 101#ifdef ENCODER_RESOLUTIONS
138# ifndef SPLIT_KEYBOARD 102 uint8_t resolution = encoder_resolutions[i];
139 int8_t resolution = encoder_resolutions[index];
140# else
141 int8_t resolution = isLeftHand ? encoder_resolutions[index] : encoder_resolutions_right[index - NUMBER_OF_ENCODERS_LEFT];
142# endif
143#else 103#else
144 uint8_t resolution = ENCODER_RESOLUTION; 104 uint8_t resolution = ENCODER_RESOLUTION;
145#endif 105#endif
146 encoder_pulses[index] += encoder_LUT[state & 0xF]; 106
147 if (encoder_pulses[index] >= resolution) { 107#ifdef SPLIT_KEYBOARD
108 index += thisHand;
109#endif
110 encoder_pulses[i] += encoder_LUT[state & 0xF];
111 if (encoder_pulses[i] >= resolution) {
148 encoder_value[index]++; 112 encoder_value[index]++;
149 changed = true; 113 changed = true;
150 encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE); 114 encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
151 } 115 }
152 if (encoder_pulses[index] <= -resolution) { // direction is arbitrary here, but this clockwise 116 if (encoder_pulses[i] <= -resolution) { // direction is arbitrary here, but this clockwise
153 encoder_value[index]--; 117 encoder_value[index]--;
154 changed = true; 118 changed = true;
155 encoder_update_kb(index, ENCODER_CLOCKWISE); 119 encoder_update_kb(index, ENCODER_CLOCKWISE);
156 } 120 }
157 encoder_pulses[index] %= resolution; 121 encoder_pulses[i] %= resolution;
158#ifdef ENCODER_DEFAULT_POS 122#ifdef ENCODER_DEFAULT_POS
159 if ((state & 0x3) == ENCODER_DEFAULT_POS) { 123 if ((state & 0x3) == ENCODER_DEFAULT_POS) {
160 encoder_pulses[index] = 0; 124 encoder_pulses[i] = 0;
161 } 125 }
162#endif 126#endif
163 return changed; 127 return changed;
@@ -165,10 +129,10 @@ static bool encoder_update(int8_t index, uint8_t state) {
165 129
166bool encoder_read(void) { 130bool encoder_read(void) {
167 bool changed = false; 131 bool changed = false;
168 for (uint8_t i = 0; i < numEncodersHere; i++) { 132 for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) {
169 encoder_state[firstEncoderHere + i] <<= 2; 133 encoder_state[i] <<= 2;
170 encoder_state[firstEncoderHere + i] |= (readPin(pad_a[i]) << 0) | (readPin(pad_b[i]) << 1); 134 encoder_state[i] |= (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1);
171 changed |= encoder_update(firstEncoderHere + i, encoder_state[firstEncoderHere + i]); 135 changed |= encoder_update(i, encoder_state[i]);
172 } 136 }
173 return changed; 137 return changed;
174} 138}
@@ -176,12 +140,12 @@ bool encoder_read(void) {
176#ifdef SPLIT_KEYBOARD 140#ifdef SPLIT_KEYBOARD
177void last_encoder_activity_trigger(void); 141void last_encoder_activity_trigger(void);
178 142
179void encoder_state_raw(uint8_t* slave_state) { memcpy(slave_state, &encoder_value[firstEncoderHere], sizeof(uint8_t) * numEncodersHere); } 143void encoder_state_raw(uint8_t* slave_state) { memcpy(slave_state, &encoder_value[thisHand], sizeof(uint8_t) * NUMBER_OF_ENCODERS); }
180 144
181void encoder_update_raw(uint8_t* slave_state) { 145void encoder_update_raw(uint8_t* slave_state) {
182 bool changed = false; 146 bool changed = false;
183 for (uint8_t i = 0; i < NUMBER_OF_ENCODERS - numEncodersHere; i++) { 147 for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) {
184 uint8_t index = firstEncoderThere + i; 148 uint8_t index = i + thatHand;
185 int8_t delta = slave_state[i] - encoder_value[index]; 149 int8_t delta = slave_state[i] - encoder_value[index];
186 while (delta > 0) { 150 while (delta > 0) {
187 delta--; 151 delta--;