Add encoder abstraction. (#21548)
This commit is contained in:
parent
2eb9ff8efd
commit
9d9cdaaa2d
50 changed files with 863 additions and 653 deletions
|
@ -1,82 +1,111 @@
|
|||
/*
|
||||
* Copyright 2018 Jack Humbert <jack.humb@gmail.com>
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
// Copyright 2022-2023 Nick Brassel (@tzarc)
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include "encoder.h"
|
||||
#include "keyboard.h"
|
||||
#include <string.h>
|
||||
#include "action.h"
|
||||
#include "keycodes.h"
|
||||
#include "encoder.h"
|
||||
#include "wait.h"
|
||||
|
||||
#ifdef SPLIT_KEYBOARD
|
||||
# include "split_util.h"
|
||||
#endif
|
||||
|
||||
// for memcpy
|
||||
#include <string.h>
|
||||
|
||||
#ifndef ENCODER_MAP_KEY_DELAY
|
||||
# include "action.h"
|
||||
# define ENCODER_MAP_KEY_DELAY TAP_CODE_DELAY
|
||||
#endif
|
||||
|
||||
#if !defined(ENCODER_RESOLUTIONS) && !defined(ENCODER_RESOLUTION)
|
||||
# define ENCODER_RESOLUTION 4
|
||||
#endif
|
||||
|
||||
#if !defined(ENCODERS_PAD_A) || !defined(ENCODERS_PAD_B)
|
||||
# error "No encoder pads defined by ENCODERS_PAD_A and ENCODERS_PAD_B"
|
||||
#endif
|
||||
|
||||
extern volatile bool isLeftHand;
|
||||
|
||||
static pin_t encoders_pad_a[NUM_ENCODERS_MAX_PER_SIDE] = ENCODERS_PAD_A;
|
||||
static pin_t encoders_pad_b[NUM_ENCODERS_MAX_PER_SIDE] = ENCODERS_PAD_B;
|
||||
|
||||
#ifdef ENCODER_RESOLUTIONS
|
||||
static uint8_t encoder_resolutions[NUM_ENCODERS] = ENCODER_RESOLUTIONS;
|
||||
#endif
|
||||
|
||||
#ifndef ENCODER_DIRECTION_FLIP
|
||||
# define ENCODER_CLOCKWISE true
|
||||
# define ENCODER_COUNTER_CLOCKWISE false
|
||||
#else
|
||||
# define ENCODER_CLOCKWISE false
|
||||
# define ENCODER_COUNTER_CLOCKWISE true
|
||||
#endif
|
||||
static int8_t encoder_LUT[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
|
||||
|
||||
static uint8_t encoder_state[NUM_ENCODERS] = {0};
|
||||
static int8_t encoder_pulses[NUM_ENCODERS] = {0};
|
||||
|
||||
// encoder counts
|
||||
static uint8_t thisCount;
|
||||
#ifdef SPLIT_KEYBOARD
|
||||
// encoder offsets for each hand
|
||||
static uint8_t thisHand, thatHand;
|
||||
// encoder counts for each hand
|
||||
static uint8_t thatCount;
|
||||
#endif
|
||||
|
||||
static uint8_t encoder_value[NUM_ENCODERS] = {0};
|
||||
|
||||
__attribute__((weak)) void encoder_wait_pullup_charge(void) {
|
||||
wait_us(100);
|
||||
__attribute__((weak)) bool should_process_encoder(void) {
|
||||
return is_keyboard_master();
|
||||
}
|
||||
|
||||
static encoder_events_t encoder_events;
|
||||
|
||||
void encoder_init(void) {
|
||||
memset(&encoder_events, 0, sizeof(encoder_events));
|
||||
encoder_driver_init();
|
||||
}
|
||||
|
||||
static bool encoder_handle_queue(void) {
|
||||
bool changed = false;
|
||||
while (encoder_events.tail != encoder_events.head) {
|
||||
encoder_event_t event = encoder_events.queue[encoder_events.tail];
|
||||
encoder_events.tail = (encoder_events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
|
||||
#ifdef ENCODER_MAP_ENABLE
|
||||
|
||||
// The delays below cater for Windows and its wonderful requirements.
|
||||
action_exec(event.clockwise ? MAKE_ENCODER_CW_EVENT(event.index, true) : MAKE_ENCODER_CCW_EVENT(event.index, true));
|
||||
# if ENCODER_MAP_KEY_DELAY > 0
|
||||
wait_ms(ENCODER_MAP_KEY_DELAY);
|
||||
# endif // ENCODER_MAP_KEY_DELAY > 0
|
||||
|
||||
action_exec(event.clockwise ? MAKE_ENCODER_CW_EVENT(event.index, false) : MAKE_ENCODER_CCW_EVENT(event.index, false));
|
||||
# if ENCODER_MAP_KEY_DELAY > 0
|
||||
wait_ms(ENCODER_MAP_KEY_DELAY);
|
||||
# endif // ENCODER_MAP_KEY_DELAY > 0
|
||||
|
||||
#else // ENCODER_MAP_ENABLE
|
||||
|
||||
encoder_update_kb(event.index, event.clockwise ? true : false);
|
||||
|
||||
#endif // ENCODER_MAP_ENABLE
|
||||
|
||||
changed = true;
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
bool encoder_task(void) {
|
||||
bool changed = false;
|
||||
|
||||
#ifdef SPLIT_KEYBOARD
|
||||
// Attempt to process existing encoder events in case split handling has already enqueued events
|
||||
if (should_process_encoder()) {
|
||||
changed |= encoder_handle_queue();
|
||||
}
|
||||
#endif // SPLIT_KEYBOARD
|
||||
|
||||
// Let the encoder driver produce events
|
||||
encoder_driver_task();
|
||||
|
||||
// Process any events that were enqueued
|
||||
if (should_process_encoder()) {
|
||||
changed |= encoder_handle_queue();
|
||||
}
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
bool encoder_queue_event(uint8_t index, bool clockwise) {
|
||||
// Drop out if we're full
|
||||
if ((encoder_events.head + 1) % MAX_QUEUED_ENCODER_EVENTS == encoder_events.tail) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Append the event
|
||||
encoder_event_t new_event = {.index = index, .clockwise = clockwise ? 1 : 0};
|
||||
encoder_events.queue[encoder_events.head] = new_event;
|
||||
|
||||
// Increment the head index
|
||||
encoder_events.head = (encoder_events.head + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void encoder_retrieve_events(encoder_events_t *events) {
|
||||
memcpy(events, &encoder_events, sizeof(encoder_events));
|
||||
}
|
||||
|
||||
#ifdef SPLIT_KEYBOARD
|
||||
void encoder_set_tail_index(uint8_t tail_index) {
|
||||
encoder_events.tail = tail_index;
|
||||
}
|
||||
|
||||
void encoder_handle_slave_events(encoder_events_t *events) {
|
||||
while (events->tail != events->head) {
|
||||
encoder_event_t event = events->queue[events->tail];
|
||||
events->tail = (events->tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
encoder_queue_event(event.index, event.clockwise ? true : false);
|
||||
}
|
||||
}
|
||||
#endif // SPLIT_KEYBOARD
|
||||
|
||||
__attribute__((weak)) bool encoder_update_user(uint8_t index, bool clockwise) {
|
||||
return true;
|
||||
}
|
||||
|
@ -106,192 +135,3 @@ __attribute__((weak)) bool encoder_update_kb(uint8_t index, bool clockwise) {
|
|||
#endif // ENCODER_TESTS
|
||||
return res;
|
||||
}
|
||||
|
||||
__attribute__((weak)) bool should_process_encoder(void) {
|
||||
return is_keyboard_master();
|
||||
}
|
||||
|
||||
void encoder_init(void) {
|
||||
#ifdef SPLIT_KEYBOARD
|
||||
thisHand = isLeftHand ? 0 : NUM_ENCODERS_LEFT;
|
||||
thatHand = NUM_ENCODERS_LEFT - thisHand;
|
||||
thisCount = isLeftHand ? NUM_ENCODERS_LEFT : NUM_ENCODERS_RIGHT;
|
||||
thatCount = isLeftHand ? NUM_ENCODERS_RIGHT : NUM_ENCODERS_LEFT;
|
||||
#else // SPLIT_KEYBOARD
|
||||
thisCount = NUM_ENCODERS;
|
||||
#endif
|
||||
|
||||
#ifdef ENCODER_TESTS
|
||||
// Annoying that we have to clear out values during initialisation here, but
|
||||
// because all the arrays are static locals, rerunning tests in the same
|
||||
// executable doesn't reset any of these. Kinda crappy having test-only code
|
||||
// here, but it's the simplest solution.
|
||||
memset(encoder_value, 0, sizeof(encoder_value));
|
||||
memset(encoder_state, 0, sizeof(encoder_state));
|
||||
memset(encoder_pulses, 0, sizeof(encoder_pulses));
|
||||
static const pin_t encoders_pad_a_left[] = ENCODERS_PAD_A;
|
||||
static const pin_t encoders_pad_b_left[] = ENCODERS_PAD_B;
|
||||
for (uint8_t i = 0; i < thisCount; i++) {
|
||||
encoders_pad_a[i] = encoders_pad_a_left[i];
|
||||
encoders_pad_b[i] = encoders_pad_b_left[i];
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(SPLIT_KEYBOARD) && defined(ENCODERS_PAD_A_RIGHT) && defined(ENCODERS_PAD_B_RIGHT)
|
||||
// Re-initialise the pads if it's the right-hand side
|
||||
if (!isLeftHand) {
|
||||
static const pin_t encoders_pad_a_right[] = ENCODERS_PAD_A_RIGHT;
|
||||
static const pin_t encoders_pad_b_right[] = ENCODERS_PAD_B_RIGHT;
|
||||
for (uint8_t i = 0; i < thisCount; i++) {
|
||||
encoders_pad_a[i] = encoders_pad_a_right[i];
|
||||
encoders_pad_b[i] = encoders_pad_b_right[i];
|
||||
}
|
||||
}
|
||||
#endif // defined(SPLIT_KEYBOARD) && defined(ENCODERS_PAD_A_RIGHT) && defined(ENCODERS_PAD_B_RIGHT)
|
||||
|
||||
// Encoder resolutions is handled purely master-side, so concatenate the two arrays
|
||||
#if defined(SPLIT_KEYBOARD) && defined(ENCODER_RESOLUTIONS)
|
||||
# if defined(ENCODER_RESOLUTIONS_RIGHT)
|
||||
static const uint8_t encoder_resolutions_right[NUM_ENCODERS_RIGHT] = ENCODER_RESOLUTIONS_RIGHT;
|
||||
# else // defined(ENCODER_RESOLUTIONS_RIGHT)
|
||||
static const uint8_t encoder_resolutions_right[NUM_ENCODERS_RIGHT] = ENCODER_RESOLUTIONS;
|
||||
# endif // defined(ENCODER_RESOLUTIONS_RIGHT)
|
||||
for (uint8_t i = 0; i < NUM_ENCODERS_RIGHT; i++) {
|
||||
encoder_resolutions[NUM_ENCODERS_LEFT + i] = encoder_resolutions_right[i];
|
||||
}
|
||||
#endif // defined(SPLIT_KEYBOARD) && defined(ENCODER_RESOLUTIONS)
|
||||
|
||||
for (uint8_t i = 0; i < thisCount; i++) {
|
||||
gpio_set_pin_input_high(encoders_pad_a[i]);
|
||||
gpio_set_pin_input_high(encoders_pad_b[i]);
|
||||
}
|
||||
encoder_wait_pullup_charge();
|
||||
for (uint8_t i = 0; i < thisCount; i++) {
|
||||
encoder_state[i] = (gpio_read_pin(encoders_pad_a[i]) << 0) | (gpio_read_pin(encoders_pad_b[i]) << 1);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef ENCODER_MAP_ENABLE
|
||||
static void encoder_exec_mapping(uint8_t index, bool clockwise) {
|
||||
// The delays below cater for Windows and its wonderful requirements.
|
||||
action_exec(clockwise ? MAKE_ENCODER_CW_EVENT(index, true) : MAKE_ENCODER_CCW_EVENT(index, true));
|
||||
# if ENCODER_MAP_KEY_DELAY > 0
|
||||
wait_ms(ENCODER_MAP_KEY_DELAY);
|
||||
# endif // ENCODER_MAP_KEY_DELAY > 0
|
||||
|
||||
action_exec(clockwise ? MAKE_ENCODER_CW_EVENT(index, false) : MAKE_ENCODER_CCW_EVENT(index, false));
|
||||
# if ENCODER_MAP_KEY_DELAY > 0
|
||||
wait_ms(ENCODER_MAP_KEY_DELAY);
|
||||
# endif // ENCODER_MAP_KEY_DELAY > 0
|
||||
}
|
||||
#endif // ENCODER_MAP_ENABLE
|
||||
|
||||
static bool encoder_update(uint8_t index, uint8_t state) {
|
||||
bool changed = false;
|
||||
uint8_t i = index;
|
||||
|
||||
#ifdef ENCODER_RESOLUTIONS
|
||||
const uint8_t resolution = encoder_resolutions[i];
|
||||
#else
|
||||
const uint8_t resolution = ENCODER_RESOLUTION;
|
||||
#endif
|
||||
|
||||
#ifdef SPLIT_KEYBOARD
|
||||
index += thisHand;
|
||||
#endif
|
||||
encoder_pulses[i] += encoder_LUT[state & 0xF];
|
||||
|
||||
#ifdef ENCODER_DEFAULT_POS
|
||||
if ((encoder_pulses[i] >= resolution) || (encoder_pulses[i] <= -resolution) || ((state & 0x3) == ENCODER_DEFAULT_POS)) {
|
||||
if (encoder_pulses[i] >= 1) {
|
||||
#else
|
||||
if (encoder_pulses[i] >= resolution) {
|
||||
#endif
|
||||
|
||||
encoder_value[index]++;
|
||||
changed = true;
|
||||
#ifdef SPLIT_KEYBOARD
|
||||
if (should_process_encoder())
|
||||
#endif // SPLIT_KEYBOARD
|
||||
#ifdef ENCODER_MAP_ENABLE
|
||||
encoder_exec_mapping(index, ENCODER_COUNTER_CLOCKWISE);
|
||||
#else // ENCODER_MAP_ENABLE
|
||||
encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
|
||||
#endif // ENCODER_MAP_ENABLE
|
||||
}
|
||||
|
||||
#ifdef ENCODER_DEFAULT_POS
|
||||
if (encoder_pulses[i] <= -1) {
|
||||
#else
|
||||
if (encoder_pulses[i] <= -resolution) { // direction is arbitrary here, but this clockwise
|
||||
#endif
|
||||
encoder_value[index]--;
|
||||
changed = true;
|
||||
#ifdef SPLIT_KEYBOARD
|
||||
if (should_process_encoder())
|
||||
#endif // SPLIT_KEYBOARD
|
||||
#ifdef ENCODER_MAP_ENABLE
|
||||
encoder_exec_mapping(index, ENCODER_CLOCKWISE);
|
||||
#else // ENCODER_MAP_ENABLE
|
||||
encoder_update_kb(index, ENCODER_CLOCKWISE);
|
||||
#endif // ENCODER_MAP_ENABLE
|
||||
}
|
||||
encoder_pulses[i] %= resolution;
|
||||
#ifdef ENCODER_DEFAULT_POS
|
||||
encoder_pulses[i] = 0;
|
||||
}
|
||||
#endif
|
||||
return changed;
|
||||
}
|
||||
|
||||
bool encoder_read(void) {
|
||||
bool changed = false;
|
||||
for (uint8_t i = 0; i < thisCount; i++) {
|
||||
uint8_t new_status = (gpio_read_pin(encoders_pad_a[i]) << 0) | (gpio_read_pin(encoders_pad_b[i]) << 1);
|
||||
if ((encoder_state[i] & 0x3) != new_status) {
|
||||
encoder_state[i] <<= 2;
|
||||
encoder_state[i] |= new_status;
|
||||
changed |= encoder_update(i, encoder_state[i]);
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
#ifdef SPLIT_KEYBOARD
|
||||
void last_encoder_activity_trigger(void);
|
||||
|
||||
void encoder_state_raw(uint8_t *slave_state) {
|
||||
memcpy(slave_state, &encoder_value[thisHand], sizeof(uint8_t) * thisCount);
|
||||
}
|
||||
|
||||
void encoder_update_raw(uint8_t *slave_state) {
|
||||
bool changed = false;
|
||||
for (uint8_t i = 0; i < thatCount; i++) { // Note inverted logic -- we want the opposite side
|
||||
const uint8_t index = i + thatHand;
|
||||
int8_t delta = slave_state[i] - encoder_value[index];
|
||||
while (delta > 0) {
|
||||
delta--;
|
||||
encoder_value[index]++;
|
||||
changed = true;
|
||||
# ifdef ENCODER_MAP_ENABLE
|
||||
encoder_exec_mapping(index, ENCODER_COUNTER_CLOCKWISE);
|
||||
# else // ENCODER_MAP_ENABLE
|
||||
encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
|
||||
# endif // ENCODER_MAP_ENABLE
|
||||
}
|
||||
while (delta < 0) {
|
||||
delta++;
|
||||
encoder_value[index]--;
|
||||
changed = true;
|
||||
# ifdef ENCODER_MAP_ENABLE
|
||||
encoder_exec_mapping(index, ENCODER_CLOCKWISE);
|
||||
# else // ENCODER_MAP_ENABLE
|
||||
encoder_update_kb(index, ENCODER_CLOCKWISE);
|
||||
# endif // ENCODER_MAP_ENABLE
|
||||
}
|
||||
}
|
||||
|
||||
// Update the last encoder input time -- handled external to encoder_read() when we're running a split
|
||||
if (changed) last_encoder_activity_trigger();
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -22,45 +22,88 @@
|
|||
#include "gpio.h"
|
||||
#include "util.h"
|
||||
|
||||
#ifdef ENCODER_ENABLE
|
||||
|
||||
__attribute__((weak)) bool should_process_encoder(void);
|
||||
|
||||
void encoder_init(void);
|
||||
bool encoder_read(void);
|
||||
bool encoder_task(void);
|
||||
bool encoder_queue_event(uint8_t index, bool clockwise);
|
||||
|
||||
bool encoder_update_kb(uint8_t index, bool clockwise);
|
||||
bool encoder_update_user(uint8_t index, bool clockwise);
|
||||
|
||||
#ifdef SPLIT_KEYBOARD
|
||||
# ifdef SPLIT_KEYBOARD
|
||||
|
||||
void encoder_state_raw(uint8_t* slave_state);
|
||||
void encoder_update_raw(uint8_t* slave_state);
|
||||
# if defined(ENCODERS_PAD_A_RIGHT)
|
||||
# ifndef NUM_ENCODERS_LEFT
|
||||
# define NUM_ENCODERS_LEFT ARRAY_SIZE(((pin_t[])ENCODERS_PAD_A))
|
||||
# endif
|
||||
# ifndef NUM_ENCODERS_RIGHT
|
||||
# define NUM_ENCODERS_RIGHT ARRAY_SIZE(((pin_t[])ENCODERS_PAD_A_RIGHT))
|
||||
# endif
|
||||
# else
|
||||
# ifndef NUM_ENCODERS_LEFT
|
||||
# define NUM_ENCODERS_LEFT ARRAY_SIZE(((pin_t[])ENCODERS_PAD_A))
|
||||
# endif
|
||||
# ifndef NUM_ENCODERS_RIGHT
|
||||
# define NUM_ENCODERS_RIGHT NUM_ENCODERS_LEFT
|
||||
# endif
|
||||
# endif
|
||||
# ifndef NUM_ENCODERS
|
||||
# define NUM_ENCODERS (NUM_ENCODERS_LEFT + NUM_ENCODERS_RIGHT)
|
||||
# endif
|
||||
|
||||
# if defined(ENCODERS_PAD_A_RIGHT)
|
||||
# define NUM_ENCODERS_LEFT ARRAY_SIZE(((pin_t[])ENCODERS_PAD_A))
|
||||
# define NUM_ENCODERS_RIGHT ARRAY_SIZE(((pin_t[])ENCODERS_PAD_A_RIGHT))
|
||||
# else
|
||||
# define NUM_ENCODERS_LEFT ARRAY_SIZE(((pin_t[])ENCODERS_PAD_A))
|
||||
# define NUM_ENCODERS_RIGHT NUM_ENCODERS_LEFT
|
||||
# endif
|
||||
# define NUM_ENCODERS (NUM_ENCODERS_LEFT + NUM_ENCODERS_RIGHT)
|
||||
# else // SPLIT_KEYBOARD
|
||||
|
||||
#else // SPLIT_KEYBOARD
|
||||
# ifndef NUM_ENCODERS
|
||||
# define NUM_ENCODERS ARRAY_SIZE(((pin_t[])ENCODERS_PAD_A))
|
||||
# endif
|
||||
# define NUM_ENCODERS_LEFT NUM_ENCODERS
|
||||
# define NUM_ENCODERS_RIGHT 0
|
||||
|
||||
# define NUM_ENCODERS ARRAY_SIZE(((pin_t[])ENCODERS_PAD_A))
|
||||
# define NUM_ENCODERS_LEFT NUM_ENCODERS
|
||||
# define NUM_ENCODERS_RIGHT 0
|
||||
# endif // SPLIT_KEYBOARD
|
||||
|
||||
#endif // SPLIT_KEYBOARD
|
||||
# ifndef NUM_ENCODERS
|
||||
# define NUM_ENCODERS 0
|
||||
# define NUM_ENCODERS_LEFT 0
|
||||
# define NUM_ENCODERS_RIGHT 0
|
||||
# endif // NUM_ENCODERS
|
||||
|
||||
#ifndef NUM_ENCODERS
|
||||
# define NUM_ENCODERS 0
|
||||
# define NUM_ENCODERS_LEFT 0
|
||||
# define NUM_ENCODERS_RIGHT 0
|
||||
#endif // NUM_ENCODERS
|
||||
# define NUM_ENCODERS_MAX_PER_SIDE MAX(NUM_ENCODERS_LEFT, NUM_ENCODERS_RIGHT)
|
||||
|
||||
#define NUM_ENCODERS_MAX_PER_SIDE MAX(NUM_ENCODERS_LEFT, NUM_ENCODERS_RIGHT)
|
||||
# ifndef MAX_QUEUED_ENCODER_EVENTS
|
||||
# define MAX_QUEUED_ENCODER_EVENTS MAX(4, ((NUM_ENCODERS_MAX_PER_SIDE) + 1))
|
||||
# endif // MAX_QUEUED_ENCODER_EVENTS
|
||||
|
||||
#ifdef ENCODER_MAP_ENABLE
|
||||
# define NUM_DIRECTIONS 2
|
||||
# define ENCODER_CCW_CW(ccw, cw) \
|
||||
{ (cw), (ccw) }
|
||||
typedef struct encoder_event_t {
|
||||
uint8_t index : 7;
|
||||
uint8_t clockwise : 1;
|
||||
} encoder_event_t;
|
||||
|
||||
typedef struct encoder_events_t {
|
||||
uint8_t head;
|
||||
uint8_t tail;
|
||||
encoder_event_t queue[MAX_QUEUED_ENCODER_EVENTS];
|
||||
} encoder_events_t;
|
||||
|
||||
// Get the current queued events
|
||||
void encoder_retrieve_events(encoder_events_t *events);
|
||||
|
||||
# ifdef SPLIT_KEYBOARD
|
||||
void encoder_set_tail_index(uint8_t tail_index);
|
||||
void encoder_handle_slave_events(encoder_events_t *events);
|
||||
# endif // SPLIT_KEYBOARD
|
||||
|
||||
# ifdef ENCODER_MAP_ENABLE
|
||||
# define NUM_DIRECTIONS 2
|
||||
# define ENCODER_CCW_CW(ccw, cw) \
|
||||
{ (cw), (ccw) }
|
||||
extern const uint16_t encoder_map[][NUM_ENCODERS][NUM_DIRECTIONS];
|
||||
#endif // ENCODER_MAP_ENABLE
|
||||
# endif // ENCODER_MAP_ENABLE
|
||||
|
||||
// "Custom encoder lite" support
|
||||
void encoder_driver_init(void);
|
||||
void encoder_driver_task(void);
|
||||
|
||||
#endif // ENCODER_ENABLE
|
||||
|
|
6
quantum/encoder/tests/config_encoder_common.h
Normal file
6
quantum/encoder/tests/config_encoder_common.h
Normal file
|
@ -0,0 +1,6 @@
|
|||
// Copyright 2023 Nick Brassel (@tzarc)
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#pragma once
|
||||
|
||||
// Override the one in quantum/util because it doesn't like working on x64 builds.
|
||||
#define ARRAY_SIZE(array) (sizeof((array)) / sizeof((array)[0]))
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright 2022 Nick Brassel (@tzarc)
|
||||
// Copyright 2022-2023 Nick Brassel (@tzarc)
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#pragma once
|
||||
#include "config_encoder_common.h"
|
||||
|
||||
#define MATRIX_ROWS 1
|
||||
#define MATRIX_COLS 1
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright 2022 Nick Brassel (@tzarc)
|
||||
// Copyright 2022-2023 Nick Brassel (@tzarc)
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#pragma once
|
||||
#include "config_encoder_common.h"
|
||||
|
||||
#define MATRIX_ROWS 1
|
||||
#define MATRIX_COLS 1
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright 2022 Nick Brassel (@tzarc)
|
||||
// Copyright 2022-2023 Nick Brassel (@tzarc)
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#pragma once
|
||||
#include "config_encoder_common.h"
|
||||
|
||||
#define MATRIX_ROWS 1
|
||||
#define MATRIX_COLS 1
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright 2022 Nick Brassel (@tzarc)
|
||||
// Copyright 2022-2023 Nick Brassel (@tzarc)
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#pragma once
|
||||
#include "config_encoder_common.h"
|
||||
|
||||
#define MATRIX_ROWS 1
|
||||
#define MATRIX_COLS 1
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright 2022 Nick Brassel (@tzarc)
|
||||
// Copyright 2022-2023 Nick Brassel (@tzarc)
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#pragma once
|
||||
#include "config_encoder_common.h"
|
||||
|
||||
#define MATRIX_ROWS 1
|
||||
#define MATRIX_COLS 1
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright 2022 Nick Brassel (@tzarc)
|
||||
// Copyright 2022-2023 Nick Brassel (@tzarc)
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#pragma once
|
||||
#include "config_encoder_common.h"
|
||||
|
||||
#define MATRIX_ROWS 1
|
||||
#define MATRIX_COLS 1
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright 2022 Nick Brassel (@tzarc)
|
||||
// Copyright 2022-2023 Nick Brassel (@tzarc)
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#pragma once
|
||||
#include "config_encoder_common.h"
|
||||
|
||||
#define MATRIX_ROWS 1
|
||||
#define MATRIX_COLS 1
|
||||
|
|
|
@ -41,7 +41,7 @@ bool encoder_update_kb(uint8_t index, bool clockwise) {
|
|||
|
||||
bool setAndRead(pin_t pin, bool val) {
|
||||
setPin(pin, val);
|
||||
return encoder_read();
|
||||
return encoder_task();
|
||||
}
|
||||
|
||||
class EncoderTest : public ::testing::Test {};
|
||||
|
|
|
@ -33,22 +33,29 @@ struct update {
|
|||
uint8_t updates_array_idx = 0;
|
||||
update updates[32];
|
||||
|
||||
bool isMaster;
|
||||
bool isLeftHand;
|
||||
|
||||
extern "C" {
|
||||
bool is_keyboard_master(void) {
|
||||
return isMaster;
|
||||
}
|
||||
|
||||
bool encoder_update_kb(uint8_t index, bool clockwise) {
|
||||
if (!isLeftHand) {
|
||||
if (!is_keyboard_master()) {
|
||||
// this method has no effect on slave half
|
||||
printf("ignoring update on right hand (%d,%s)\n", index, clockwise ? "CW" : "CC");
|
||||
printf("ignoring update on slave (%d,%s)\n", index, clockwise ? "CW" : "CC");
|
||||
return true;
|
||||
}
|
||||
updates[updates_array_idx % 32] = {index, clockwise};
|
||||
updates_array_idx++;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
bool setAndRead(pin_t pin, bool val) {
|
||||
setPin(pin, val);
|
||||
return encoder_read();
|
||||
return encoder_task();
|
||||
}
|
||||
|
||||
class EncoderSplitTestLeftEqRight : public ::testing::Test {
|
||||
|
@ -63,6 +70,7 @@ class EncoderSplitTestLeftEqRight : public ::testing::Test {
|
|||
};
|
||||
|
||||
TEST_F(EncoderSplitTestLeftEqRight, TestInitLeft) {
|
||||
isMaster = true;
|
||||
isLeftHand = true;
|
||||
encoder_init();
|
||||
EXPECT_EQ(pinIsInputHigh[0], true);
|
||||
|
@ -77,6 +85,7 @@ TEST_F(EncoderSplitTestLeftEqRight, TestInitLeft) {
|
|||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftEqRight, TestInitRight) {
|
||||
isMaster = true;
|
||||
isLeftHand = false;
|
||||
encoder_init();
|
||||
EXPECT_EQ(pinIsInputHigh[0], false);
|
||||
|
@ -90,7 +99,8 @@ TEST_F(EncoderSplitTestLeftEqRight, TestInitRight) {
|
|||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseLeft) {
|
||||
TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseLeftMaster) {
|
||||
isMaster = true;
|
||||
isLeftHand = true;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
|
@ -102,9 +112,19 @@ TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseLeft) {
|
|||
EXPECT_EQ(updates_array_idx, 1); // one update received
|
||||
EXPECT_EQ(updates[0].index, 0);
|
||||
EXPECT_EQ(updates[0].clockwise, true);
|
||||
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 0); // No events should be queued on master
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseRightSent) {
|
||||
TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseRightMaster) {
|
||||
isMaster = true;
|
||||
isLeftHand = false;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
|
@ -113,23 +133,60 @@ TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseRightSent) {
|
|||
setAndRead(6, true);
|
||||
setAndRead(7, true);
|
||||
|
||||
uint8_t slave_state[32] = {0};
|
||||
encoder_state_raw(slave_state);
|
||||
EXPECT_EQ(updates_array_idx, 1); // one update received
|
||||
EXPECT_EQ(updates[0].index, 3);
|
||||
EXPECT_EQ(updates[0].clockwise, true);
|
||||
|
||||
EXPECT_EQ(slave_state[0], 0);
|
||||
EXPECT_EQ(slave_state[1], 0xFF);
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 0); // No events should be queued on master
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftEqRight, TestMultipleEncodersRightReceived) {
|
||||
TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseLeftSlave) {
|
||||
isMaster = false;
|
||||
isLeftHand = true;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
setAndRead(0, false);
|
||||
setAndRead(1, false);
|
||||
setAndRead(0, true);
|
||||
setAndRead(1, true);
|
||||
|
||||
uint8_t slave_state[32] = {1, 0xFF}; // First right encoder is CCW, Second right encoder CW
|
||||
encoder_update_raw(slave_state);
|
||||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
|
||||
EXPECT_EQ(updates_array_idx, 2); // two updates received, one for each changed item on the right side
|
||||
EXPECT_EQ(updates[0].index, 2);
|
||||
EXPECT_EQ(updates[0].clockwise, false);
|
||||
EXPECT_EQ(updates[1].index, 3);
|
||||
EXPECT_EQ(updates[1].clockwise, true);
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 1); // One event should be queued on slave
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftEqRight, TestOneClockwiseRightSlave) {
|
||||
isMaster = false;
|
||||
isLeftHand = false;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
setAndRead(6, false);
|
||||
setAndRead(7, false);
|
||||
setAndRead(6, true);
|
||||
setAndRead(7, true);
|
||||
|
||||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 1); // One event should be queued on slave
|
||||
}
|
||||
|
|
|
@ -33,22 +33,29 @@ struct update {
|
|||
uint8_t updates_array_idx = 0;
|
||||
update updates[32];
|
||||
|
||||
bool isMaster;
|
||||
bool isLeftHand;
|
||||
|
||||
extern "C" {
|
||||
bool is_keyboard_master(void) {
|
||||
return isMaster;
|
||||
}
|
||||
|
||||
bool encoder_update_kb(uint8_t index, bool clockwise) {
|
||||
if (!isLeftHand) {
|
||||
if (!is_keyboard_master()) {
|
||||
// this method has no effect on slave half
|
||||
printf("ignoring update on right hand (%d,%s)\n", index, clockwise ? "CW" : "CC");
|
||||
printf("ignoring update on slave (%d,%s)\n", index, clockwise ? "CW" : "CC");
|
||||
return true;
|
||||
}
|
||||
updates[updates_array_idx % 32] = {index, clockwise};
|
||||
updates_array_idx++;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
bool setAndRead(pin_t pin, bool val) {
|
||||
setPin(pin, val);
|
||||
return encoder_read();
|
||||
return encoder_task();
|
||||
}
|
||||
|
||||
class EncoderSplitTestLeftGreaterThanRight : public ::testing::Test {
|
||||
|
@ -94,7 +101,8 @@ TEST_F(EncoderSplitTestLeftGreaterThanRight, TestInitRight) {
|
|||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseLeft) {
|
||||
TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseLeftMaster) {
|
||||
isMaster = true;
|
||||
isLeftHand = true;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
|
@ -106,9 +114,19 @@ TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseLeft) {
|
|||
EXPECT_EQ(updates_array_idx, 1); // one update received
|
||||
EXPECT_EQ(updates[0].index, 0);
|
||||
EXPECT_EQ(updates[0].clockwise, true);
|
||||
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 0); // No events should be queued on master
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseRightSent) {
|
||||
TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseRightMaster) {
|
||||
isMaster = true;
|
||||
isLeftHand = false;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
|
@ -117,23 +135,60 @@ TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseRightSent) {
|
|||
setAndRead(6, true);
|
||||
setAndRead(7, true);
|
||||
|
||||
uint8_t slave_state[32] = {0};
|
||||
encoder_state_raw(slave_state);
|
||||
EXPECT_EQ(updates_array_idx, 1); // one update received
|
||||
EXPECT_EQ(updates[0].index, 3);
|
||||
EXPECT_EQ(updates[0].clockwise, true);
|
||||
|
||||
EXPECT_EQ(slave_state[0], 0xFF);
|
||||
EXPECT_EQ(slave_state[1], 0);
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 0); // No events should be queued on master
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftGreaterThanRight, TestMultipleEncodersRightReceived) {
|
||||
TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseLeftSlave) {
|
||||
isMaster = false;
|
||||
isLeftHand = true;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
setAndRead(0, false);
|
||||
setAndRead(1, false);
|
||||
setAndRead(0, true);
|
||||
setAndRead(1, true);
|
||||
|
||||
uint8_t slave_state[32] = {1, 0xFF}; // First right encoder is CCW, Second right encoder no change, third right encoder CW
|
||||
encoder_update_raw(slave_state);
|
||||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
|
||||
EXPECT_EQ(updates_array_idx, 2); // two updates received, one for each changed item on the right side
|
||||
EXPECT_EQ(updates[0].index, 3);
|
||||
EXPECT_EQ(updates[0].clockwise, false);
|
||||
EXPECT_EQ(updates[1].index, 4);
|
||||
EXPECT_EQ(updates[1].clockwise, true);
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 1); // One event should be queued on slave
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftGreaterThanRight, TestOneClockwiseRightSlave) {
|
||||
isMaster = false;
|
||||
isLeftHand = false;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
setAndRead(6, false);
|
||||
setAndRead(7, false);
|
||||
setAndRead(6, true);
|
||||
setAndRead(7, true);
|
||||
|
||||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 1); // One event should be queued on slave
|
||||
}
|
||||
|
|
|
@ -33,22 +33,29 @@ struct update {
|
|||
uint8_t updates_array_idx = 0;
|
||||
update updates[32];
|
||||
|
||||
bool isMaster;
|
||||
bool isLeftHand;
|
||||
|
||||
extern "C" {
|
||||
bool is_keyboard_master(void) {
|
||||
return isMaster;
|
||||
}
|
||||
|
||||
bool encoder_update_kb(uint8_t index, bool clockwise) {
|
||||
if (!isLeftHand) {
|
||||
if (!is_keyboard_master()) {
|
||||
// this method has no effect on slave half
|
||||
printf("ignoring update on right hand (%d,%s)\n", index, clockwise ? "CW" : "CC");
|
||||
printf("ignoring update on slave (%d,%s)\n", index, clockwise ? "CW" : "CC");
|
||||
return true;
|
||||
}
|
||||
updates[updates_array_idx % 32] = {index, clockwise};
|
||||
updates_array_idx++;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
bool setAndRead(pin_t pin, bool val) {
|
||||
setPin(pin, val);
|
||||
return encoder_read();
|
||||
return encoder_task();
|
||||
}
|
||||
|
||||
class EncoderSplitTestLeftLessThanRight : public ::testing::Test {
|
||||
|
@ -94,7 +101,8 @@ TEST_F(EncoderSplitTestLeftLessThanRight, TestInitRight) {
|
|||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftLessThanRight, TestOneClockwiseLeft) {
|
||||
TEST_F(EncoderSplitTestLeftLessThanRight, TestOneClockwiseLeftMaster) {
|
||||
isMaster = true;
|
||||
isLeftHand = true;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
|
@ -106,9 +114,19 @@ TEST_F(EncoderSplitTestLeftLessThanRight, TestOneClockwiseLeft) {
|
|||
EXPECT_EQ(updates_array_idx, 1); // one update received
|
||||
EXPECT_EQ(updates[0].index, 0);
|
||||
EXPECT_EQ(updates[0].clockwise, true);
|
||||
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 0); // No events should be queued on master
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftLessThanRight, TestOneClockwiseRightSent) {
|
||||
TEST_F(EncoderSplitTestLeftLessThanRight, TestOneClockwiseRightMaster) {
|
||||
isMaster = true;
|
||||
isLeftHand = false;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
|
@ -117,23 +135,60 @@ TEST_F(EncoderSplitTestLeftLessThanRight, TestOneClockwiseRightSent) {
|
|||
setAndRead(6, true);
|
||||
setAndRead(7, true);
|
||||
|
||||
uint8_t slave_state[32] = {0};
|
||||
encoder_state_raw(slave_state);
|
||||
EXPECT_EQ(updates_array_idx, 1); // one update received
|
||||
EXPECT_EQ(updates[0].index, 3);
|
||||
EXPECT_EQ(updates[0].clockwise, true);
|
||||
|
||||
EXPECT_EQ(slave_state[0], 0);
|
||||
EXPECT_EQ(slave_state[1], 0xFF);
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 0); // No events should be queued on master
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftLessThanRight, TestMultipleEncodersRightReceived) {
|
||||
TEST_F(EncoderSplitTestLeftLessThanRight, TestOneClockwiseLeftSlave) {
|
||||
isMaster = false;
|
||||
isLeftHand = true;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
setAndRead(0, false);
|
||||
setAndRead(1, false);
|
||||
setAndRead(0, true);
|
||||
setAndRead(1, true);
|
||||
|
||||
uint8_t slave_state[32] = {1, 0, 0xFF}; // First right encoder is CCW, Second right encoder no change, third right encoder CW
|
||||
encoder_update_raw(slave_state);
|
||||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
|
||||
EXPECT_EQ(updates_array_idx, 2); // two updates received, one for each changed item on the right side
|
||||
EXPECT_EQ(updates[0].index, 2);
|
||||
EXPECT_EQ(updates[0].clockwise, false);
|
||||
EXPECT_EQ(updates[1].index, 4);
|
||||
EXPECT_EQ(updates[1].clockwise, true);
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 1); // One event should be queued on slave
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestLeftLessThanRight, TestOneClockwiseRightSlave) {
|
||||
isMaster = false;
|
||||
isLeftHand = false;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
setAndRead(6, false);
|
||||
setAndRead(7, false);
|
||||
setAndRead(6, true);
|
||||
setAndRead(7, true);
|
||||
|
||||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 1); // One event should be queued on slave
|
||||
}
|
||||
|
|
|
@ -33,22 +33,29 @@ struct update {
|
|||
uint8_t updates_array_idx = 0;
|
||||
update updates[32];
|
||||
|
||||
bool isMaster;
|
||||
bool isLeftHand;
|
||||
|
||||
extern "C" {
|
||||
bool is_keyboard_master(void) {
|
||||
return isMaster;
|
||||
}
|
||||
|
||||
bool encoder_update_kb(uint8_t index, bool clockwise) {
|
||||
if (!isLeftHand) {
|
||||
if (!is_keyboard_master()) {
|
||||
// this method has no effect on slave half
|
||||
printf("ignoring update on right hand (%d,%s)\n", index, clockwise ? "CW" : "CC");
|
||||
printf("ignoring update on slave (%d,%s)\n", index, clockwise ? "CW" : "CC");
|
||||
return true;
|
||||
}
|
||||
updates[updates_array_idx % 32] = {index, clockwise};
|
||||
updates_array_idx++;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
bool setAndRead(pin_t pin, bool val) {
|
||||
setPin(pin, val);
|
||||
return encoder_read();
|
||||
return encoder_task();
|
||||
}
|
||||
|
||||
class EncoderSplitTestNoLeft : public ::testing::Test {
|
||||
|
@ -82,19 +89,8 @@ TEST_F(EncoderSplitTestNoLeft, TestInitRight) {
|
|||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestNoLeft, TestOneClockwiseLeft) {
|
||||
isLeftHand = true;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
setAndRead(0, false);
|
||||
setAndRead(1, false);
|
||||
setAndRead(0, true);
|
||||
setAndRead(1, true);
|
||||
|
||||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestNoLeft, TestOneClockwiseRightSent) {
|
||||
TEST_F(EncoderSplitTestNoLeft, TestOneClockwiseLeftMaster) {
|
||||
isMaster = true;
|
||||
isLeftHand = false;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
|
@ -103,23 +99,38 @@ TEST_F(EncoderSplitTestNoLeft, TestOneClockwiseRightSent) {
|
|||
setAndRead(2, true);
|
||||
setAndRead(3, true);
|
||||
|
||||
uint8_t slave_state[32] = {0};
|
||||
encoder_state_raw(slave_state);
|
||||
EXPECT_EQ(updates_array_idx, 1); // one update received
|
||||
EXPECT_EQ(updates[0].index, 1);
|
||||
EXPECT_EQ(updates[0].clockwise, true);
|
||||
|
||||
EXPECT_EQ(slave_state[0], 0);
|
||||
EXPECT_EQ(slave_state[1], 0xFF);
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 0); // No events should be queued on master
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestNoLeft, TestMultipleEncodersRightReceived) {
|
||||
isLeftHand = true;
|
||||
TEST_F(EncoderSplitTestNoLeft, TestOneClockwiseRightSlave) {
|
||||
isMaster = false;
|
||||
isLeftHand = false;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
setAndRead(2, false);
|
||||
setAndRead(3, false);
|
||||
setAndRead(2, true);
|
||||
setAndRead(3, true);
|
||||
|
||||
uint8_t slave_state[32] = {1, 0xFF}; // First right encoder is CCW, Second right encoder no change, third right encoder CW
|
||||
encoder_update_raw(slave_state);
|
||||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
|
||||
EXPECT_EQ(updates_array_idx, 2); // two updates received, one for each changed item on the right side
|
||||
EXPECT_EQ(updates[0].index, 0);
|
||||
EXPECT_EQ(updates[0].clockwise, false);
|
||||
EXPECT_EQ(updates[1].index, 1);
|
||||
EXPECT_EQ(updates[1].clockwise, true);
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 1); // One event should be queued on slave
|
||||
}
|
||||
|
|
|
@ -33,22 +33,29 @@ struct update {
|
|||
uint8_t updates_array_idx = 0;
|
||||
update updates[32];
|
||||
|
||||
bool isMaster;
|
||||
bool isLeftHand;
|
||||
|
||||
extern "C" {
|
||||
bool is_keyboard_master(void) {
|
||||
return isMaster;
|
||||
}
|
||||
|
||||
bool encoder_update_kb(uint8_t index, bool clockwise) {
|
||||
if (!isLeftHand) {
|
||||
if (!is_keyboard_master()) {
|
||||
// this method has no effect on slave half
|
||||
printf("ignoring update on right hand (%d,%s)\n", index, clockwise ? "CW" : "CC");
|
||||
printf("ignoring update on slave (%d,%s)\n", index, clockwise ? "CW" : "CC");
|
||||
return true;
|
||||
}
|
||||
updates[updates_array_idx % 32] = {index, clockwise};
|
||||
updates_array_idx++;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
bool setAndRead(pin_t pin, bool val) {
|
||||
setPin(pin, val);
|
||||
return encoder_read();
|
||||
return encoder_task();
|
||||
}
|
||||
|
||||
class EncoderSplitTestNoRight : public ::testing::Test {
|
||||
|
@ -82,37 +89,48 @@ TEST_F(EncoderSplitTestNoRight, TestInitRight) {
|
|||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestNoRight, TestOneClockwiseLeft) {
|
||||
TEST_F(EncoderSplitTestNoRight, TestOneClockwiseLeftMaster) {
|
||||
isMaster = true;
|
||||
isLeftHand = true;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
setAndRead(0, false);
|
||||
setAndRead(1, false);
|
||||
setAndRead(0, true);
|
||||
setAndRead(1, true);
|
||||
setAndRead(2, false);
|
||||
setAndRead(3, false);
|
||||
setAndRead(2, true);
|
||||
setAndRead(3, true);
|
||||
|
||||
EXPECT_EQ(updates_array_idx, 1); // one updates received
|
||||
EXPECT_EQ(updates[0].index, 0);
|
||||
EXPECT_EQ(updates_array_idx, 1); // one update received
|
||||
EXPECT_EQ(updates[0].index, 1);
|
||||
EXPECT_EQ(updates[0].clockwise, true);
|
||||
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 0); // No events should be queued on master
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestNoRight, TestOneClockwiseRightSent) {
|
||||
isLeftHand = false;
|
||||
encoder_init();
|
||||
|
||||
uint8_t slave_state[32] = {0xAA, 0xAA};
|
||||
encoder_state_raw(slave_state);
|
||||
|
||||
EXPECT_EQ(slave_state[0], 0xAA);
|
||||
EXPECT_EQ(slave_state[1], 0xAA);
|
||||
}
|
||||
|
||||
TEST_F(EncoderSplitTestNoRight, TestMultipleEncodersRightReceived) {
|
||||
TEST_F(EncoderSplitTestNoRight, TestOneClockwiseRightSlave) {
|
||||
isMaster = false;
|
||||
isLeftHand = true;
|
||||
encoder_init();
|
||||
// send 4 pulses. with resolution 4, that's one step and we should get 1 update.
|
||||
setAndRead(2, false);
|
||||
setAndRead(3, false);
|
||||
setAndRead(2, true);
|
||||
setAndRead(3, true);
|
||||
|
||||
uint8_t slave_state[32] = {1, 0xFF}; // These values would trigger updates if there were encoders on the other side
|
||||
encoder_update_raw(slave_state);
|
||||
EXPECT_EQ(updates_array_idx, 0); // no updates received
|
||||
|
||||
EXPECT_EQ(updates_array_idx, 0); // no updates received -- no right-hand encoders
|
||||
int events_queued = 0;
|
||||
encoder_events_t events;
|
||||
encoder_retrieve_events(&events);
|
||||
while (events.tail != events.head) {
|
||||
events.tail = (events.tail + 1) % MAX_QUEUED_ENCODER_EVENTS;
|
||||
++events_queued;
|
||||
}
|
||||
EXPECT_EQ(events_queued, 1); // One event should be queued on slave
|
||||
}
|
||||
|
|
|
@ -50,7 +50,7 @@ bool encoder_update_kb(uint8_t index, bool clockwise) {
|
|||
|
||||
bool setAndRead(pin_t pin, bool val) {
|
||||
setPin(pin, val);
|
||||
return encoder_read();
|
||||
return encoder_task();
|
||||
}
|
||||
|
||||
class EncoderSplitTestRole : public ::testing::Test {
|
||||
|
@ -87,9 +87,6 @@ TEST_F(EncoderSplitTestRole, TestPrimaryRight) {
|
|||
setAndRead(6, true);
|
||||
setAndRead(7, true);
|
||||
|
||||
uint8_t slave_state[32] = {0};
|
||||
encoder_state_raw(slave_state);
|
||||
|
||||
EXPECT_EQ(num_updates, 1); // one update received
|
||||
}
|
||||
|
||||
|
@ -116,8 +113,5 @@ TEST_F(EncoderSplitTestRole, TestNotPrimaryRight) {
|
|||
setAndRead(6, true);
|
||||
setAndRead(7, true);
|
||||
|
||||
uint8_t slave_state[32] = {0};
|
||||
encoder_state_raw(slave_state);
|
||||
|
||||
EXPECT_EQ(num_updates, 0); // zero updates received
|
||||
}
|
||||
|
|
|
@ -36,7 +36,3 @@ bool setPin(pin_t pin, bool val) {
|
|||
}
|
||||
|
||||
void last_encoder_activity_trigger(void) {}
|
||||
|
||||
__attribute__((weak)) bool is_keyboard_master(void) {
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -22,9 +22,6 @@
|
|||
#define SPLIT_KEYBOARD
|
||||
typedef uint8_t pin_t;
|
||||
|
||||
void encoder_state_raw(uint8_t* slave_state);
|
||||
void encoder_update_raw(uint8_t* slave_state);
|
||||
|
||||
extern bool pins[];
|
||||
extern bool pinIsInputHigh[];
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ encoder_CONFIG := $(QUANTUM_PATH)/encoder/tests/config_mock.h
|
|||
|
||||
encoder_SRC := \
|
||||
platforms/test/timer.c \
|
||||
drivers/encoder/encoder_quadrature.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/mock.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/encoder_tests.cpp \
|
||||
$(QUANTUM_PATH)/encoder.c
|
||||
|
@ -13,6 +14,7 @@ encoder_split_left_eq_right_CONFIG := $(QUANTUM_PATH)/encoder/tests/config_mock_
|
|||
|
||||
encoder_split_left_eq_right_SRC := \
|
||||
platforms/test/timer.c \
|
||||
drivers/encoder/encoder_quadrature.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/mock_split.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/encoder_tests_split_left_eq_right.cpp \
|
||||
$(QUANTUM_PATH)/encoder.c
|
||||
|
@ -23,6 +25,7 @@ encoder_split_left_gt_right_CONFIG := $(QUANTUM_PATH)/encoder/tests/config_mock_
|
|||
|
||||
encoder_split_left_gt_right_SRC := \
|
||||
platforms/test/timer.c \
|
||||
drivers/encoder/encoder_quadrature.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/mock_split.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/encoder_tests_split_left_gt_right.cpp \
|
||||
$(QUANTUM_PATH)/encoder.c
|
||||
|
@ -33,6 +36,7 @@ encoder_split_left_lt_right_CONFIG := $(QUANTUM_PATH)/encoder/tests/config_mock_
|
|||
|
||||
encoder_split_left_lt_right_SRC := \
|
||||
platforms/test/timer.c \
|
||||
drivers/encoder/encoder_quadrature.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/mock_split.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/encoder_tests_split_left_lt_right.cpp \
|
||||
$(QUANTUM_PATH)/encoder.c
|
||||
|
@ -43,6 +47,7 @@ encoder_split_no_left_CONFIG := $(QUANTUM_PATH)/encoder/tests/config_mock_split_
|
|||
|
||||
encoder_split_no_left_SRC := \
|
||||
platforms/test/timer.c \
|
||||
drivers/encoder/encoder_quadrature.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/mock_split.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/encoder_tests_split_no_left.cpp \
|
||||
$(QUANTUM_PATH)/encoder.c
|
||||
|
@ -53,6 +58,7 @@ encoder_split_no_right_CONFIG := $(QUANTUM_PATH)/encoder/tests/config_mock_split
|
|||
|
||||
encoder_split_no_right_SRC := \
|
||||
platforms/test/timer.c \
|
||||
drivers/encoder/encoder_quadrature.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/mock_split.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/encoder_tests_split_no_right.cpp \
|
||||
$(QUANTUM_PATH)/encoder.c
|
||||
|
@ -63,6 +69,7 @@ encoder_split_role_CONFIG := $(QUANTUM_PATH)/encoder/tests/config_mock_split_rol
|
|||
|
||||
encoder_split_role_SRC := \
|
||||
platforms/test/timer.c \
|
||||
drivers/encoder/encoder_quadrature.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/mock_split.c \
|
||||
$(QUANTUM_PATH)/encoder/tests/encoder_tests_split_role.cpp \
|
||||
$(QUANTUM_PATH)/encoder.c
|
||||
|
|
|
@ -689,7 +689,7 @@ void keyboard_task(void) {
|
|||
#endif
|
||||
|
||||
#ifdef ENCODER_ENABLE
|
||||
if (encoder_read()) {
|
||||
if (encoder_task()) {
|
||||
last_encoder_activity_trigger();
|
||||
activity_has_occurred = true;
|
||||
}
|
||||
|
|
|
@ -31,6 +31,7 @@ enum serial_transaction_id {
|
|||
#ifdef ENCODER_ENABLE
|
||||
GET_ENCODERS_CHECKSUM,
|
||||
GET_ENCODERS_DATA,
|
||||
PUT_ENCODER_TAIL,
|
||||
#endif // ENCODER_ENABLE
|
||||
|
||||
#ifndef DISABLE_SYNC_TIMER
|
||||
|
|
|
@ -234,21 +234,28 @@ static void master_matrix_handlers_slave(matrix_row_t master_matrix[], matrix_ro
|
|||
#ifdef ENCODER_ENABLE
|
||||
|
||||
static bool encoder_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
|
||||
static uint32_t last_update = 0;
|
||||
uint8_t temp_state[NUM_ENCODERS_MAX_PER_SIDE];
|
||||
static uint32_t last_update = 0;
|
||||
encoder_events_t temp_events;
|
||||
|
||||
bool okay = read_if_checksum_mismatch(GET_ENCODERS_CHECKSUM, GET_ENCODERS_DATA, &last_update, temp_state, split_shmem->encoders.state, sizeof(temp_state));
|
||||
if (okay) encoder_update_raw(temp_state);
|
||||
bool okay = read_if_checksum_mismatch(GET_ENCODERS_CHECKSUM, GET_ENCODERS_DATA, &last_update, &temp_events, &split_shmem->encoders.events, sizeof(temp_events));
|
||||
if (okay) {
|
||||
encoder_handle_slave_events(&split_shmem->encoders.events);
|
||||
transport_write(PUT_ENCODER_TAIL, &split_shmem->encoders.events.tail, sizeof(split_shmem->encoders.events.tail));
|
||||
split_shmem->encoders.checksum = crc8(&split_shmem->encoders.events, sizeof(split_shmem->encoders.events));
|
||||
}
|
||||
return okay;
|
||||
}
|
||||
|
||||
static void encoder_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
|
||||
uint8_t encoder_state[NUM_ENCODERS_MAX_PER_SIDE];
|
||||
encoder_state_raw(encoder_state);
|
||||
// Always prepare the encoder state for read.
|
||||
memcpy(split_shmem->encoders.state, encoder_state, sizeof(encoder_state));
|
||||
encoder_retrieve_events(&split_shmem->encoders.events);
|
||||
// Now update the checksum given that the encoders has been written to
|
||||
split_shmem->encoders.checksum = crc8(encoder_state, sizeof(encoder_state));
|
||||
split_shmem->encoders.checksum = crc8(&split_shmem->encoders.events, sizeof(split_shmem->encoders.events));
|
||||
}
|
||||
|
||||
static void encoder_handlers_slave_reset(uint8_t initiator2target_buffer_size, const void *initiator2target_buffer, uint8_t target2initiator_buffer_size, void *target2initiator_buffer) {
|
||||
uint8_t tail_index = *(uint8_t *)initiator2target_buffer;
|
||||
encoder_set_tail_index(tail_index);
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
|
@ -256,7 +263,8 @@ static void encoder_handlers_slave(matrix_row_t master_matrix[], matrix_row_t sl
|
|||
# define TRANSACTIONS_ENCODERS_SLAVE() TRANSACTION_HANDLER_SLAVE_AUTOLOCK(encoder)
|
||||
# define TRANSACTIONS_ENCODERS_REGISTRATIONS \
|
||||
[GET_ENCODERS_CHECKSUM] = trans_target2initiator_initializer(encoders.checksum), \
|
||||
[GET_ENCODERS_DATA] = trans_target2initiator_initializer(encoders.state),
|
||||
[GET_ENCODERS_DATA] = trans_target2initiator_initializer(encoders.events), \
|
||||
[PUT_ENCODER_TAIL] = trans_initiator2target_initializer_cb(encoders.events.tail, encoder_handlers_slave_reset),
|
||||
// clang-format on
|
||||
|
||||
#else // ENCODER_ENABLE
|
||||
|
|
|
@ -65,8 +65,8 @@ typedef struct _split_master_matrix_sync_t {
|
|||
|
||||
#ifdef ENCODER_ENABLE
|
||||
typedef struct _split_slave_encoder_sync_t {
|
||||
uint8_t checksum;
|
||||
uint8_t state[NUM_ENCODERS_MAX_PER_SIDE];
|
||||
uint8_t checksum;
|
||||
encoder_events_t events;
|
||||
} split_slave_encoder_sync_t;
|
||||
#endif // ENCODER_ENABLE
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue