Move encoder config to data driven (#19923)

Co-authored-by: Nick Brassel <nick@tzarc.org>
This commit is contained in:
Ryan 2023-02-26 09:45:12 +11:00 committed by GitHub
parent 77ae66cd06
commit b440e0c050
10 changed files with 34 additions and 65 deletions

View file

@ -24,9 +24,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
// when turn knob clockwise or counterclockwise. // when turn knob clockwise or counterclockwise.
// This is handy to assign key codes by VIA. // This is handy to assign key codes by VIA.
// Number of endoder
#define ENCODERS 2
// Mappings of encoder rotation to key position in key matrix. // Mappings of encoder rotation to key position in key matrix.
// First encoder, Left side : k85, k86 // First encoder, Left side : k85, k86
// Second encoder, Right side : k95, k96 // Second encoder, Right side : k95, k96

View file

@ -168,12 +168,12 @@ bool led_update_user(led_t led_state) {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Rotary Encoder // Rotary Encoder
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
static uint8_t encoder_state[ENCODERS] = {0}; static uint8_t encoder_state[NUM_ENCODERS] = {0};
static keypos_t encoder_cw[ENCODERS] = ENCODERS_CW_KEY; static keypos_t encoder_cw[NUM_ENCODERS] = ENCODERS_CW_KEY;
static keypos_t encoder_ccw[ENCODERS] = ENCODERS_CCW_KEY; static keypos_t encoder_ccw[NUM_ENCODERS] = ENCODERS_CCW_KEY;
void encoder_action_unregister(void) { void encoder_action_unregister(void) {
for (int index = 0; index < ENCODERS; ++index) { for (int index = 0; index < NUM_ENCODERS; ++index) {
if (encoder_state[index]) { if (encoder_state[index]) {
keyevent_t encoder_event = (keyevent_t) { keyevent_t encoder_event = (keyevent_t) {
.key = encoder_state[index] >> 1 ? encoder_cw[index] : encoder_ccw[index], .key = encoder_state[index] >> 1 ? encoder_cw[index] : encoder_ccw[index],

View file

@ -16,14 +16,13 @@
#include QMK_KEYBOARD_H #include QMK_KEYBOARD_H
#ifdef VIA_ENABLE static uint8_t encoder_state[NUM_ENCODERS] = {0};
static uint8_t encoder_state[ENCODERS] = {0}; static keypos_t encoder_cw[NUM_ENCODERS] = ENCODERS_CW_KEY;
static keypos_t encoder_cw[ENCODERS] = ENCODERS_CW_KEY; static keypos_t encoder_ccw[NUM_ENCODERS] = ENCODERS_CCW_KEY;
static keypos_t encoder_ccw[ENCODERS] = ENCODERS_CCW_KEY;
void encoder_action_unregister(void) void encoder_action_unregister(void)
{ {
for (int index = 0; index < ENCODERS; ++index) for (int index = 0; index < NUM_ENCODERS; ++index)
{ {
if (encoder_state[index]) if (encoder_state[index])
{ {
@ -56,8 +55,7 @@ bool encoder_update_kb(uint8_t index, bool clockwise)
{ {
encoder_action_register(index, clockwise); encoder_action_register(index, clockwise);
return true; return true;
}; }
#endif
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
[0] = LAYOUT_all( [0] = LAYOUT_all(

View file

@ -159,14 +159,12 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) {
} }
#ifdef ENCODER_ENABLE #ifdef ENCODER_ENABLE
static uint8_t encoder_state[NUM_ENCODERS] = {0};
#define ENCODERS 1 static keypos_t encoder_cw[NUM_ENCODERS] = {{3, 5}};
static uint8_t encoder_state[ENCODERS] = {0}; static keypos_t encoder_ccw[NUM_ENCODERS] = {{4, 5}};
static keypos_t encoder_cw[ENCODERS] = {{3, 5}};
static keypos_t encoder_ccw[ENCODERS] = {{4, 5}};
void encoder_action_unregister(void) { void encoder_action_unregister(void) {
for (int index = 0; index < ENCODERS; ++index) { for (int index = 0; index < NUM_ENCODERS; ++index) {
if (encoder_state[index]) { if (encoder_state[index]) {
keyevent_t encoder_event = (keyevent_t) { keyevent_t encoder_event = (keyevent_t) {
.key = encoder_state[index] >> 1 ? encoder_cw[index] : encoder_ccw[index], .key = encoder_state[index] >> 1 ? encoder_cw[index] : encoder_ccw[index],

View file

@ -53,15 +53,12 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
#ifdef ENCODER_ENABLE #ifdef ENCODER_ENABLE
#define ENCODERS 1 static uint8_t encoder_state[NUM_ENCODERS] = {0};
static uint8_t encoder_state[ENCODERS] = {0}; static keypos_t encoder_cw[NUM_ENCODERS] = {{6, 4}};
static keypos_t encoder_cw[ENCODERS] = {{6, 4}}; static keypos_t encoder_ccw[NUM_ENCODERS] = {{7, 4}};
static keypos_t encoder_ccw[ENCODERS] = {{7, 4}};
void encoder_action_unregister(void) { void encoder_action_unregister(void) {
#ifdef ENCODERS for (uint8_t index = 0; index < NUM_ENCODERS; ++index) {
for (uint8_t index = 0; index < ENCODERS; ++index) {
if (encoder_state[index]) { if (encoder_state[index]) {
keyevent_t encoder_event = (keyevent_t) { keyevent_t encoder_event = (keyevent_t) {
.key = encoder_state[index] >> 1 ? encoder_cw[index] : encoder_ccw[index], .key = encoder_state[index] >> 1 ? encoder_cw[index] : encoder_ccw[index],
@ -72,7 +69,6 @@ void encoder_action_unregister(void) {
action_exec(encoder_event); action_exec(encoder_event);
} }
} }
#endif
} }
void encoder_action_register(uint8_t index, bool clockwise) { void encoder_action_register(uint8_t index, bool clockwise) {

View file

@ -105,15 +105,12 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) {
} }
#ifdef ENCODER_ENABLE #ifdef ENCODER_ENABLE
#define ENCODERS 1 static uint8_t encoder_state[NUM_ENCODERS] = {0};
static uint8_t encoder_state[ENCODERS] = {0}; static keypos_t encoder_cw[NUM_ENCODERS] = {{3, 5}};
static keypos_t encoder_cw[ENCODERS] = {{3, 5}}; static keypos_t encoder_ccw[NUM_ENCODERS] = {{4, 5}};
static keypos_t encoder_ccw[ENCODERS] = {{4, 5}};
void encoder_action_unregister(void) { void encoder_action_unregister(void) {
#ifdef ENCODERS for (uint8_t index = 0; index < NUM_ENCODERS; ++index) {
for (uint8_t index = 0; index < ENCODERS; ++index) {
if (encoder_state[index]) { if (encoder_state[index]) {
keyevent_t encoder_event = (keyevent_t) { keyevent_t encoder_event = (keyevent_t) {
.key = encoder_state[index] >> 1 ? encoder_cw[index] : encoder_ccw[index], .key = encoder_state[index] >> 1 ? encoder_cw[index] : encoder_ccw[index],
@ -124,7 +121,6 @@ void encoder_action_unregister(void) {
action_exec(encoder_event); action_exec(encoder_event);
} }
} }
#endif
} }
void encoder_action_register(uint8_t index, bool clockwise) { void encoder_action_register(uint8_t index, bool clockwise) {

View file

@ -27,16 +27,12 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
}; };
#if defined(VIA_ENABLE) && defined(ENCODER_ENABLE) #if defined(VIA_ENABLE) && defined(ENCODER_ENABLE)
static uint8_t encoder_state[NUM_ENCODERS] = {0};
#ifdef ENCODERS static keypos_t encoder_cw[NUM_ENCODERS] = ENCODERS_CW_KEY;
static uint8_t encoder_state[] = {0}; static keypos_t encoder_ccw[NUM_ENCODERS] = ENCODERS_CCW_KEY;
static keypos_t encoder_cw[] = ENCODERS_CW_KEY;
static keypos_t encoder_ccw[] = ENCODERS_CCW_KEY;
#endif
void encoder_action_unregister(void) { void encoder_action_unregister(void) {
#ifdef ENCODERS for (int index = 0; index < NUM_ENCODERS; ++index) {
for (int index = 0; index < ENCODERS; ++index) {
if (encoder_state[index]) { if (encoder_state[index]) {
keyevent_t encoder_event = (keyevent_t) { keyevent_t encoder_event = (keyevent_t) {
.key = encoder_state[index] >> 1 ? encoder_cw[index] : encoder_ccw[index], .key = encoder_state[index] >> 1 ? encoder_cw[index] : encoder_ccw[index],
@ -47,11 +43,9 @@ void encoder_action_unregister(void) {
action_exec(encoder_event); action_exec(encoder_event);
} }
} }
#endif
} }
void encoder_action_register(uint8_t index, bool clockwise) { void encoder_action_register(uint8_t index, bool clockwise) {
#ifdef ENCODERS
keyevent_t encoder_event = (keyevent_t) { keyevent_t encoder_event = (keyevent_t) {
.key = clockwise ? encoder_cw[index] : encoder_ccw[index], .key = clockwise ? encoder_cw[index] : encoder_ccw[index],
.pressed = true, .pressed = true,
@ -59,7 +53,6 @@ void encoder_action_register(uint8_t index, bool clockwise) {
}; };
encoder_state[index] = (clockwise ^ 1) | (clockwise << 1); encoder_state[index] = (clockwise ^ 1) | (clockwise << 1);
action_exec(encoder_event); action_exec(encoder_event);
#endif
} }
void matrix_scan_user(void) { void matrix_scan_user(void) {

View file

@ -12,9 +12,6 @@
// when turn knob clockwise or counterclockwise. // when turn knob clockwise or counterclockwise.
// This is handy to assign key codes by VIA. // This is handy to assign key codes by VIA.
// Number of endoder
#define ENCODERS 1
// Mappings of encoder rotation to key position in key matrix. // Mappings of encoder rotation to key position in key matrix.
#define ENCODERS_CW_KEY { { 0, 4 }} #define ENCODERS_CW_KEY { { 0, 4 }}
#define ENCODERS_CCW_KEY { { 6, 0 }} #define ENCODERS_CCW_KEY { { 6, 0 }}

View file

@ -88,12 +88,12 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Rotary Encoder // Rotary Encoder
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
static uint8_t encoder_state[ENCODERS] = {0}; static uint8_t encoder_state[NUM_ENCODERS] = {0};
static keypos_t encoder_cw[ENCODERS] = ENCODERS_CW_KEY; static keypos_t encoder_cw[NUM_ENCODERS] = ENCODERS_CW_KEY;
static keypos_t encoder_ccw[ENCODERS] = ENCODERS_CCW_KEY; static keypos_t encoder_ccw[NUM_ENCODERS] = ENCODERS_CCW_KEY;
void encoder_action_unregister(void) { void encoder_action_unregister(void) {
for (int index = 0; index < ENCODERS; ++index) { for (int index = 0; index < NUM_ENCODERS; ++index) {
if (encoder_state[index]) { if (encoder_state[index]) {
keyevent_t encoder_event = (keyevent_t) { keyevent_t encoder_event = (keyevent_t) {
.key = encoder_state[index] >> 1 ? encoder_cw[index] : encoder_ccw[index], .key = encoder_state[index] >> 1 ? encoder_cw[index] : encoder_ccw[index],

View file

@ -27,11 +27,10 @@
# define ENCODER_STATE_CW 0x01 # define ENCODER_STATE_CW 0x01
# define ENCODER_STATE_CCW 0x02 # define ENCODER_STATE_CCW 0x02
# ifdef ENCODERS static uint8_t encoder_state[NUM_ENCODERS] = {0};
static uint8_t encoder_state[ENCODERS] = {0}; static uint16_t encoder_timer[NUM_ENCODERS] = {0};
static uint16_t encoder_timer[ENCODERS] = {0}; static keypos_t encoder_cw[NUM_ENCODERS] = ENCODERS_CW_KEY;
static keypos_t encoder_cw[ENCODERS] = ENCODERS_CW_KEY; static keypos_t encoder_ccw[NUM_ENCODERS] = ENCODERS_CCW_KEY;
static keypos_t encoder_ccw[ENCODERS] = ENCODERS_CCW_KEY;
static void exec_encoder_action(uint8_t index, bool clockwise, bool pressed) { static void exec_encoder_action(uint8_t index, bool clockwise, bool pressed) {
// clang-format off // clang-format off
@ -43,22 +42,18 @@ static void exec_encoder_action(uint8_t index, bool clockwise, bool pressed) {
// clang-format on // clang-format on
action_exec(encoder_event); action_exec(encoder_event);
} }
# endif
void encoder_action_unregister(void) { void encoder_action_unregister(void) {
# ifdef ENCODERS for (int index = 0; index < NUM_ENCODERS; ++index) {
for (int index = 0; index < ENCODERS; ++index) {
if (encoder_state[index] && (timer_elapsed(encoder_timer[index]) >= ENCODER_TAP_DURATION_MS)) { if (encoder_state[index] && (timer_elapsed(encoder_timer[index]) >= ENCODER_TAP_DURATION_MS)) {
bool clockwise = !!(encoder_state[index] & ENCODER_STATE_CW); bool clockwise = !!(encoder_state[index] & ENCODER_STATE_CW);
encoder_state[index] = 0; encoder_state[index] = 0;
exec_encoder_action(index, clockwise, false); exec_encoder_action(index, clockwise, false);
} }
} }
# endif
} }
void encoder_action_register(uint8_t index, bool clockwise) { void encoder_action_register(uint8_t index, bool clockwise) {
# ifdef ENCODERS
if (encoder_state[index]) { if (encoder_state[index]) {
bool was_clockwise = !!(encoder_state[index] & ENCODER_STATE_CW); bool was_clockwise = !!(encoder_state[index] & ENCODER_STATE_CW);
encoder_state[index] = 0; encoder_state[index] = 0;
@ -67,7 +62,6 @@ void encoder_action_register(uint8_t index, bool clockwise) {
encoder_state[index] = clockwise ? ENCODER_STATE_CW : ENCODER_STATE_CCW; encoder_state[index] = clockwise ? ENCODER_STATE_CW : ENCODER_STATE_CCW;
encoder_timer[index] = timer_read(); encoder_timer[index] = timer_read();
exec_encoder_action(index, clockwise, true); exec_encoder_action(index, clockwise, true);
# endif
} }
void matrix_scan_kb(void) { void matrix_scan_kb(void) {