Keep track of encoder activity (#11595)

* Keep track of encoder activity, provide API for either matrix/encoder.

* Fixup build when no RGBLIGHT or Backlight enabled.
This commit is contained in:
Nick Brassel 2021-01-21 22:24:07 +11:00 committed by GitHub
parent bdb757e189
commit 1108210f1b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 8 deletions

View file

@ -94,8 +94,9 @@ void encoder_init(void) {
#endif #endif
} }
static void encoder_update(int8_t index, uint8_t state) { static bool encoder_update(int8_t index, uint8_t state) {
uint8_t i = index; bool changed = false;
uint8_t i = index;
#ifdef ENCODER_RESOLUTIONS #ifdef ENCODER_RESOLUTIONS
int8_t resolution = encoder_resolutions[i]; int8_t resolution = encoder_resolutions[i];
@ -109,40 +110,53 @@ static void encoder_update(int8_t index, uint8_t state) {
encoder_pulses[i] += encoder_LUT[state & 0xF]; encoder_pulses[i] += encoder_LUT[state & 0xF];
if (encoder_pulses[i] >= resolution) { if (encoder_pulses[i] >= resolution) {
encoder_value[index]++; encoder_value[index]++;
changed = true;
encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE); encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
} }
if (encoder_pulses[i] <= -resolution) { // direction is arbitrary here, but this clockwise if (encoder_pulses[i] <= -resolution) { // direction is arbitrary here, but this clockwise
encoder_value[index]--; encoder_value[index]--;
changed = true;
encoder_update_kb(index, ENCODER_CLOCKWISE); encoder_update_kb(index, ENCODER_CLOCKWISE);
} }
encoder_pulses[i] %= resolution; encoder_pulses[i] %= resolution;
return changed;
} }
void encoder_read(void) { bool encoder_read(void) {
bool changed = false;
for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) { for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) {
encoder_state[i] <<= 2; encoder_state[i] <<= 2;
encoder_state[i] |= (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1); encoder_state[i] |= (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1);
encoder_update(i, encoder_state[i]); changed |= encoder_update(i, encoder_state[i]);
} }
return changed;
} }
#ifdef SPLIT_KEYBOARD #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) * NUMBER_OF_ENCODERS); } void encoder_state_raw(uint8_t* slave_state) { memcpy(slave_state, &encoder_value[thisHand], sizeof(uint8_t) * NUMBER_OF_ENCODERS); }
void encoder_update_raw(uint8_t* slave_state) { void encoder_update_raw(uint8_t* slave_state) {
bool changed = false;
for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) { for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) {
uint8_t index = i + thatHand; uint8_t index = i + thatHand;
int8_t delta = slave_state[i] - encoder_value[index]; int8_t delta = slave_state[i] - encoder_value[index];
while (delta > 0) { while (delta > 0) {
delta--; delta--;
encoder_value[index]++; encoder_value[index]++;
changed = true;
encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE); encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
} }
while (delta < 0) { while (delta < 0) {
delta++; delta++;
encoder_value[index]--; encoder_value[index]--;
changed = true;
encoder_update_kb(index, ENCODER_CLOCKWISE); encoder_update_kb(index, ENCODER_CLOCKWISE);
} }
} }
// Update the last encoder input time -- handled external to encoder_read() when we're running a split
if (changed) last_encoder_activity_trigger();
} }
#endif #endif

View file

@ -20,7 +20,7 @@
#include "quantum.h" #include "quantum.h"
void encoder_init(void); void encoder_init(void);
void encoder_read(void); bool encoder_read(void);
void encoder_update_kb(int8_t index, bool clockwise); void encoder_update_kb(int8_t index, bool clockwise);
void encoder_update_user(int8_t index, bool clockwise); void encoder_update_user(int8_t index, bool clockwise);

View file

@ -97,9 +97,19 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
# include "dip_switch.h" # include "dip_switch.h"
#endif #endif
static uint32_t last_input_modification_time = 0;
uint32_t last_input_activity_time(void) { return last_input_modification_time; }
uint32_t last_input_activity_elapsed(void) { return timer_elapsed32(last_input_modification_time); }
static uint32_t last_matrix_modification_time = 0; static uint32_t last_matrix_modification_time = 0;
uint32_t last_matrix_activity_time(void) { return last_matrix_modification_time; } uint32_t last_matrix_activity_time(void) { return last_matrix_modification_time; }
uint32_t last_matrix_activity_elapsed(void) { return timer_elapsed32(last_matrix_modification_time); } uint32_t last_matrix_activity_elapsed(void) { return timer_elapsed32(last_matrix_modification_time); }
void last_matrix_activity_trigger(void) { last_matrix_modification_time = last_input_modification_time = timer_read32(); }
static uint32_t last_encoder_modification_time = 0;
uint32_t last_encoder_activity_time(void) { return last_encoder_modification_time; }
uint32_t last_encoder_activity_elapsed(void) { return timer_elapsed32(last_encoder_modification_time); }
void last_encoder_activity_trigger(void) { last_encoder_modification_time = last_input_modification_time = timer_read32(); }
// Only enable this if console is enabled to print to // Only enable this if console is enabled to print to
#if defined(DEBUG_MATRIX_SCAN_RATE) && defined(CONSOLE_ENABLE) #if defined(DEBUG_MATRIX_SCAN_RATE) && defined(CONSOLE_ENABLE)
@ -338,12 +348,15 @@ void keyboard_task(void) {
#ifdef QMK_KEYS_PER_SCAN #ifdef QMK_KEYS_PER_SCAN
uint8_t keys_processed = 0; uint8_t keys_processed = 0;
#endif #endif
#ifdef ENCODER_ENABLE
bool encoders_changed = false;
#endif
housekeeping_task_kb(); housekeeping_task_kb();
housekeeping_task_user(); housekeeping_task_user();
uint8_t matrix_changed = matrix_scan(); uint8_t matrix_changed = matrix_scan();
if (matrix_changed) last_matrix_modification_time = timer_read32(); if (matrix_changed) last_matrix_activity_trigger();
if (should_process_keypress()) { if (should_process_keypress()) {
for (uint8_t r = 0; r < MATRIX_ROWS; r++) { for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
@ -399,7 +412,8 @@ MATRIX_LOOP_END:
#endif #endif
#ifdef ENCODER_ENABLE #ifdef ENCODER_ENABLE
encoder_read(); encoders_changed = encoder_read();
if (encoders_changed) last_encoder_activity_trigger();
#endif #endif
#ifdef QWIIC_ENABLE #ifdef QWIIC_ENABLE
@ -409,8 +423,12 @@ MATRIX_LOOP_END:
#ifdef OLED_DRIVER_ENABLE #ifdef OLED_DRIVER_ENABLE
oled_task(); oled_task();
# ifndef OLED_DISABLE_TIMEOUT # ifndef OLED_DISABLE_TIMEOUT
// Wake up oled if user is using those fabulous keys! // Wake up oled if user is using those fabulous keys or spinning those encoders!
# ifdef ENCODER_ENABLE
if (matrix_changed || encoders_changed) oled_on();
# else
if (matrix_changed) oled_on(); if (matrix_changed) oled_on();
# endif
# endif # endif
#endif #endif

View file

@ -73,9 +73,15 @@ void keyboard_post_init_user(void);
void housekeeping_task_kb(void); void housekeeping_task_kb(void);
void housekeeping_task_user(void); void housekeeping_task_user(void);
uint32_t last_input_activity_time(void); // Timestamp of the last matrix or encoder activity
uint32_t last_input_activity_elapsed(void); // Number of milliseconds since the last matrix or encoder activity
uint32_t last_matrix_activity_time(void); // Timestamp of the last matrix activity uint32_t last_matrix_activity_time(void); // Timestamp of the last matrix activity
uint32_t last_matrix_activity_elapsed(void); // Number of milliseconds since the last matrix activity uint32_t last_matrix_activity_elapsed(void); // Number of milliseconds since the last matrix activity
uint32_t last_encoder_activity_time(void); // Timestamp of the last encoder activity
uint32_t last_encoder_activity_elapsed(void); // Number of milliseconds since the last encoder activity
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif