Skip to content

Commit 9d9cdaa

Browse files
authored
Add encoder abstraction. (qmk#21548)
1 parent 2eb9ff8 commit 9d9cdaa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+852
-642
lines changed

builddefs/common_features.mk

+15
Original file line numberDiff line numberDiff line change
@@ -886,9 +886,24 @@ ifeq ($(strip $(BLUETOOTH_ENABLE)), yes)
886886
endif
887887
endif
888888

889+
ENCODER_ENABLE ?= no
890+
ENCODER_DRIVER ?= quadrature
891+
VALID_ENCODER_DRIVER_TYPES := quadrature custom
889892
ifeq ($(strip $(ENCODER_ENABLE)), yes)
893+
ifeq ($(filter $(ENCODER_DRIVER),$(VALID_ENCODER_DRIVER_TYPES)),)
894+
$(call CATASTROPHIC_ERROR,Invalid ENCODER_DRIVER,ENCODER_DRIVER="$(ENCODER_DRIVER)" is not a valid encoder driver)
895+
endif
890896
SRC += $(QUANTUM_DIR)/encoder.c
891897
OPT_DEFS += -DENCODER_ENABLE
898+
OPT_DEFS += -DENCODER_DRIVER_$(strip $(shell echo $(ENCODER_DRIVER) | tr '[:lower:]' '[:upper:]'))
899+
900+
COMMON_VPATH += $(PLATFORM_PATH)/$(PLATFORM_KEY)/$(DRIVER_DIR)/encoder
901+
COMMON_VPATH += $(DRIVER_PATH)/encoder
902+
903+
ifneq ($(strip $(ENCODER_DRIVER)), custom)
904+
SRC += encoder_$(strip $(ENCODER_DRIVER)).c
905+
endif
906+
892907
ifeq ($(strip $(ENCODER_MAP_ENABLE)), yes)
893908
OPT_DEFS += -DENCODER_MAP_ENABLE
894909
endif

data/mappings/info_rules.hjson

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"DEBOUNCE_TYPE": {"info_key": "build.debounce_type"},
2222
"EEPROM_DRIVER": {"info_key": "eeprom.driver"},
2323
"ENCODER_ENABLE": {"info_key": "encoder.enabled", "value_type": "bool"},
24+
"ENCODER_DRIVER": {"info_key": "encoder.driver"},
2425
"FIRMWARE_FORMAT": {"info_key": "build.firmware_format"},
2526
"KEYBOARD_SHARED_EP": {"info_key": "usb.shared_endpoint.keyboard", "value_type": "bool"},
2627
"LAYOUTS": {"info_key": "community_layouts", "value_type": "list"},

data/schemas/keyboard.jsonschema

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
"encoder_config": {
77
"type": "object",
88
"properties": {
9+
"driver": {
10+
"type": "string",
11+
"enum": ["quadrature", "custom"]
12+
},
913
"rotary": {
1014
"type": "array",
1115
"items": {

drivers/encoder/encoder_quadrature.c

+213
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
// Copyright 2018 Jack Humbert <[email protected]>
2+
// Copyright 2018-2023 Nick Brassel (@tzarc)
3+
// SPDX-License-Identifier: GPL-2.0-or-later
4+
5+
#include <stdint.h>
6+
#include "encoder.h"
7+
#include "gpio.h"
8+
#include "keyboard.h"
9+
#include "action.h"
10+
#include "keycodes.h"
11+
#include "wait.h"
12+
13+
#ifdef SPLIT_KEYBOARD
14+
# include "split_util.h"
15+
#endif
16+
17+
// for memcpy
18+
#include <string.h>
19+
20+
#if !defined(ENCODER_RESOLUTIONS) && !defined(ENCODER_RESOLUTION)
21+
# define ENCODER_RESOLUTION 4
22+
#endif
23+
24+
#undef ENCODER_DEFAULT_PIN_API_IMPL
25+
#if defined(ENCODERS_PAD_A) && defined(ENCODERS_PAD_B)
26+
// Inform the quadrature driver that it needs to implement pin init/read functions
27+
# define ENCODER_DEFAULT_PIN_API_IMPL
28+
#endif
29+
30+
extern volatile bool isLeftHand;
31+
32+
__attribute__((weak)) void encoder_quadrature_init_pin(uint8_t index, bool pad_b);
33+
__attribute__((weak)) uint8_t encoder_quadrature_read_pin(uint8_t index, bool pad_b);
34+
35+
#ifdef ENCODER_DEFAULT_PIN_API_IMPL
36+
37+
static pin_t encoders_pad_a[NUM_ENCODERS_MAX_PER_SIDE] = ENCODERS_PAD_A;
38+
static pin_t encoders_pad_b[NUM_ENCODERS_MAX_PER_SIDE] = ENCODERS_PAD_B;
39+
40+
__attribute__((weak)) void encoder_wait_pullup_charge(void) {
41+
wait_us(100);
42+
}
43+
44+
__attribute__((weak)) void encoder_quadrature_init_pin(uint8_t index, bool pad_b) {
45+
pin_t pin = pad_b ? encoders_pad_b[index] : encoders_pad_a[index];
46+
if (pin != NO_PIN) {
47+
gpio_set_pin_input_high(pin);
48+
}
49+
}
50+
51+
__attribute__((weak)) uint8_t encoder_quadrature_read_pin(uint8_t index, bool pad_b) {
52+
pin_t pin = pad_b ? encoders_pad_b[index] : encoders_pad_a[index];
53+
if (pin != NO_PIN) {
54+
return gpio_read_pin(pin) ? 1 : 0;
55+
}
56+
return 0;
57+
}
58+
59+
#endif // ENCODER_DEFAULT_PIN_API_IMPL
60+
61+
#ifdef ENCODER_RESOLUTIONS
62+
static uint8_t encoder_resolutions[NUM_ENCODERS] = ENCODER_RESOLUTIONS;
63+
#endif
64+
65+
#ifndef ENCODER_DIRECTION_FLIP
66+
# define ENCODER_CLOCKWISE true
67+
# define ENCODER_COUNTER_CLOCKWISE false
68+
#else
69+
# define ENCODER_CLOCKWISE false
70+
# define ENCODER_COUNTER_CLOCKWISE true
71+
#endif
72+
static int8_t encoder_LUT[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
73+
74+
static uint8_t encoder_state[NUM_ENCODERS] = {0};
75+
static int8_t encoder_pulses[NUM_ENCODERS] = {0};
76+
77+
// encoder counts
78+
static uint8_t thisCount;
79+
#ifdef SPLIT_KEYBOARD
80+
// encoder offsets for each hand
81+
static uint8_t thisHand, thatHand;
82+
// encoder counts for each hand
83+
static uint8_t thatCount;
84+
#endif
85+
86+
__attribute__((weak)) void encoder_quadrature_post_init_kb(void) {
87+
extern void encoder_quadrature_handle_read(uint8_t index, uint8_t pin_a_state, uint8_t pin_b_state);
88+
// Unused normally, but can be used for things like setting up pin-change interrupts in keyboard code.
89+
// During the interrupt, read the pins then call `encoder_handle_read()` with the pin states and it'll queue up an encoder event if needed.
90+
}
91+
92+
void encoder_quadrature_post_init(void) {
93+
#ifdef ENCODER_DEFAULT_PIN_API_IMPL
94+
for (uint8_t i = 0; i < thisCount; i++) {
95+
encoder_quadrature_init_pin(i, false);
96+
encoder_quadrature_init_pin(i, true);
97+
}
98+
encoder_wait_pullup_charge();
99+
for (uint8_t i = 0; i < thisCount; i++) {
100+
encoder_state[i] = (encoder_quadrature_read_pin(i, false) << 0) | (encoder_quadrature_read_pin(i, true) << 1);
101+
}
102+
#else
103+
memset(encoder_state, 0, sizeof(encoder_state));
104+
#endif
105+
106+
encoder_quadrature_post_init_kb();
107+
}
108+
109+
void encoder_driver_init(void) {
110+
#ifdef SPLIT_KEYBOARD
111+
thisHand = isLeftHand ? 0 : NUM_ENCODERS_LEFT;
112+
thatHand = NUM_ENCODERS_LEFT - thisHand;
113+
thisCount = isLeftHand ? NUM_ENCODERS_LEFT : NUM_ENCODERS_RIGHT;
114+
thatCount = isLeftHand ? NUM_ENCODERS_RIGHT : NUM_ENCODERS_LEFT;
115+
#else // SPLIT_KEYBOARD
116+
thisCount = NUM_ENCODERS;
117+
#endif
118+
119+
#ifdef ENCODER_TESTS
120+
// Annoying that we have to clear out values during initialisation here, but
121+
// because all the arrays are static locals, rerunning tests in the same
122+
// executable doesn't reset any of these. Kinda crappy having test-only code
123+
// here, but it's the simplest solution.
124+
memset(encoder_state, 0, sizeof(encoder_state));
125+
memset(encoder_pulses, 0, sizeof(encoder_pulses));
126+
const pin_t encoders_pad_a_left[] = ENCODERS_PAD_A;
127+
const pin_t encoders_pad_b_left[] = ENCODERS_PAD_B;
128+
for (uint8_t i = 0; i < thisCount; i++) {
129+
encoders_pad_a[i] = encoders_pad_a_left[i];
130+
encoders_pad_b[i] = encoders_pad_b_left[i];
131+
}
132+
#endif
133+
134+
#if defined(SPLIT_KEYBOARD) && defined(ENCODERS_PAD_A_RIGHT) && defined(ENCODERS_PAD_B_RIGHT)
135+
// Re-initialise the pads if it's the right-hand side
136+
if (!isLeftHand) {
137+
const pin_t encoders_pad_a_right[] = ENCODERS_PAD_A_RIGHT;
138+
const pin_t encoders_pad_b_right[] = ENCODERS_PAD_B_RIGHT;
139+
for (uint8_t i = 0; i < thisCount; i++) {
140+
encoders_pad_a[i] = encoders_pad_a_right[i];
141+
encoders_pad_b[i] = encoders_pad_b_right[i];
142+
}
143+
}
144+
#endif // defined(SPLIT_KEYBOARD) && defined(ENCODERS_PAD_A_RIGHT) && defined(ENCODERS_PAD_B_RIGHT)
145+
146+
// Encoder resolutions is defined differently in config.h, so concatenate
147+
#if defined(SPLIT_KEYBOARD) && defined(ENCODER_RESOLUTIONS)
148+
# if defined(ENCODER_RESOLUTIONS_RIGHT)
149+
static const uint8_t encoder_resolutions_right[NUM_ENCODERS_RIGHT] = ENCODER_RESOLUTIONS_RIGHT;
150+
# else // defined(ENCODER_RESOLUTIONS_RIGHT)
151+
static const uint8_t encoder_resolutions_right[NUM_ENCODERS_RIGHT] = ENCODER_RESOLUTIONS;
152+
# endif // defined(ENCODER_RESOLUTIONS_RIGHT)
153+
for (uint8_t i = 0; i < NUM_ENCODERS_RIGHT; i++) {
154+
encoder_resolutions[NUM_ENCODERS_LEFT + i] = encoder_resolutions_right[i];
155+
}
156+
#endif // defined(SPLIT_KEYBOARD) && defined(ENCODER_RESOLUTIONS)
157+
158+
encoder_quadrature_post_init();
159+
}
160+
161+
static void encoder_handle_state_change(uint8_t index, uint8_t state) {
162+
uint8_t i = index;
163+
164+
#ifdef SPLIT_KEYBOARD
165+
index += thisHand;
166+
#endif
167+
168+
#ifdef ENCODER_RESOLUTIONS
169+
const uint8_t resolution = encoder_resolutions[index];
170+
#else
171+
const uint8_t resolution = ENCODER_RESOLUTION;
172+
#endif
173+
174+
encoder_pulses[i] += encoder_LUT[state & 0xF];
175+
176+
#ifdef ENCODER_DEFAULT_POS
177+
if ((encoder_pulses[i] >= resolution) || (encoder_pulses[i] <= -resolution) || ((state & 0x3) == ENCODER_DEFAULT_POS)) {
178+
if (encoder_pulses[i] >= 1) {
179+
#else
180+
if (encoder_pulses[i] >= resolution) {
181+
#endif
182+
183+
encoder_queue_event(index, ENCODER_COUNTER_CLOCKWISE);
184+
}
185+
186+
#ifdef ENCODER_DEFAULT_POS
187+
if (encoder_pulses[i] <= -1) {
188+
#else
189+
if (encoder_pulses[i] <= -resolution) { // direction is arbitrary here, but this clockwise
190+
#endif
191+
encoder_queue_event(index, ENCODER_CLOCKWISE);
192+
}
193+
encoder_pulses[i] %= resolution;
194+
#ifdef ENCODER_DEFAULT_POS
195+
encoder_pulses[i] = 0;
196+
}
197+
#endif
198+
}
199+
200+
void encoder_quadrature_handle_read(uint8_t index, uint8_t pin_a_state, uint8_t pin_b_state) {
201+
uint8_t state = pin_a_state | (pin_b_state << 1);
202+
if ((encoder_state[index] & 0x3) != state) {
203+
encoder_state[index] <<= 2;
204+
encoder_state[index] |= state;
205+
encoder_handle_state_change(index, encoder_state[index]);
206+
}
207+
}
208+
209+
__attribute__((weak)) void encoder_driver_task(void) {
210+
for (uint8_t i = 0; i < thisCount; i++) {
211+
encoder_quadrature_handle_read(i, encoder_quadrature_read_pin(i, false), encoder_quadrature_read_pin(i, true));
212+
}
213+
}

keyboards/mechwild/sugarglider/matrix.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static void select_row(uint8_t row) {
5050
//wait_us(100);
5151
return;
5252
}
53-
53+
5454
if (row > 1) {
5555
mcp23018_errors += !mcp23018_set_config(I2C_ADDR, mcp23018_PORTB, ALL_INPUT);
5656
mcp23018_errors += !mcp23018_set_config(I2C_ADDR, mcp23018_PORTA, ~(row_pos[row]));
@@ -87,8 +87,10 @@ bool matrix_scan_custom(matrix_row_t current_matrix[]) {
8787
bool changed = false;
8888
for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
8989
changed |= read_cols_on_row(current_matrix, current_row);
90+
9091
#ifdef ENCODER_ENABLE
91-
encoder_read();
92+
// Need to frequently read the encoder pins while scanning because the I/O expander takes a long time in comparison.
93+
encoder_driver_task();
9294
#endif
9395
}
9496
return changed;

keyboards/pica40/rev2/post_rules.mk

-8
This file was deleted.

0 commit comments

Comments
 (0)