aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Brassel <nick@tzarc.org>2021-01-21 22:24:07 +1100
committerGitHub <noreply@github.com>2021-01-21 22:24:07 +1100
commit1108210f1bee89c29b1bcd6105d0a7b48a53ca04 (patch)
tree683f90022bcfb142021862bdc4fc454cfbd7870e
parentbdb757e189bf72c0ef382a924328642a629e06d5 (diff)
downloadqmk_firmware-1108210f1bee89c29b1bcd6105d0a7b48a53ca04.tar.gz
qmk_firmware-1108210f1bee89c29b1bcd6105d0a7b48a53ca04.zip
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.
-rw-r--r--quantum/encoder.c22
-rw-r--r--quantum/encoder.h2
-rw-r--r--tmk_core/common/keyboard.c24
-rw-r--r--tmk_core/common/keyboard.h6
4 files changed, 46 insertions, 8 deletions
diff --git a/quantum/encoder.c b/quantum/encoder.c
index 7ca31afed..2ed64c1e3 100644
--- a/quantum/encoder.c
+++ b/quantum/encoder.c
@@ -94,8 +94,9 @@ void encoder_init(void) {
94#endif 94#endif
95} 95}
96 96
97static void encoder_update(int8_t index, uint8_t state) { 97static bool encoder_update(int8_t index, uint8_t state) {
98 uint8_t i = index; 98 bool changed = false;
99 uint8_t i = index;
99 100
100#ifdef ENCODER_RESOLUTIONS 101#ifdef ENCODER_RESOLUTIONS
101 int8_t resolution = encoder_resolutions[i]; 102 int8_t resolution = encoder_resolutions[i];
@@ -109,40 +110,53 @@ static void encoder_update(int8_t index, uint8_t state) {
109 encoder_pulses[i] += encoder_LUT[state & 0xF]; 110 encoder_pulses[i] += encoder_LUT[state & 0xF];
110 if (encoder_pulses[i] >= resolution) { 111 if (encoder_pulses[i] >= resolution) {
111 encoder_value[index]++; 112 encoder_value[index]++;
113 changed = true;
112 encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE); 114 encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
113 } 115 }
114 if (encoder_pulses[i] <= -resolution) { // direction is arbitrary here, but this clockwise 116 if (encoder_pulses[i] <= -resolution) { // direction is arbitrary here, but this clockwise
115 encoder_value[index]--; 117 encoder_value[index]--;
118 changed = true;
116 encoder_update_kb(index, ENCODER_CLOCKWISE); 119 encoder_update_kb(index, ENCODER_CLOCKWISE);
117 } 120 }
118 encoder_pulses[i] %= resolution; 121 encoder_pulses[i] %= resolution;
122 return changed;
119} 123}
120 124
121void encoder_read(void) { 125bool encoder_read(void) {
126 bool changed = false;
122 for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) { 127 for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) {
123 encoder_state[i] <<= 2; 128 encoder_state[i] <<= 2;
124 encoder_state[i] |= (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1); 129 encoder_state[i] |= (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1);
125 encoder_update(i, encoder_state[i]); 130 changed |= encoder_update(i, encoder_state[i]);
126 } 131 }
132 return changed;
127} 133}
128 134
129#ifdef SPLIT_KEYBOARD 135#ifdef SPLIT_KEYBOARD
136void last_encoder_activity_trigger(void);
137
130void encoder_state_raw(uint8_t* slave_state) { memcpy(slave_state, &encoder_value[thisHand], sizeof(uint8_t) * NUMBER_OF_ENCODERS); } 138void encoder_state_raw(uint8_t* slave_state) { memcpy(slave_state, &encoder_value[thisHand], sizeof(uint8_t) * NUMBER_OF_ENCODERS); }
131 139
132void encoder_update_raw(uint8_t* slave_state) { 140void encoder_update_raw(uint8_t* slave_state) {
141 bool changed = false;
133 for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) { 142 for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) {
134 uint8_t index = i + thatHand; 143 uint8_t index = i + thatHand;
135 int8_t delta = slave_state[i] - encoder_value[index]; 144 int8_t delta = slave_state[i] - encoder_value[index];
136 while (delta > 0) { 145 while (delta > 0) {
137 delta--; 146 delta--;
138 encoder_value[index]++; 147 encoder_value[index]++;
148 changed = true;
139 encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE); 149 encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
140 } 150 }
141 while (delta < 0) { 151 while (delta < 0) {
142 delta++; 152 delta++;
143 encoder_value[index]--; 153 encoder_value[index]--;
154 changed = true;
144 encoder_update_kb(index, ENCODER_CLOCKWISE); 155 encoder_update_kb(index, ENCODER_CLOCKWISE);
145 } 156 }
146 } 157 }
158
159 // Update the last encoder input time -- handled external to encoder_read() when we're running a split
160 if (changed) last_encoder_activity_trigger();
147} 161}
148#endif 162#endif
diff --git a/quantum/encoder.h b/quantum/encoder.h
index ec09a8cc4..db6f220da 100644
--- a/quantum/encoder.h
+++ b/quantum/encoder.h
@@ -20,7 +20,7 @@
20#include "quantum.h" 20#include "quantum.h"
21 21
22void encoder_init(void); 22void encoder_init(void);
23void encoder_read(void); 23bool encoder_read(void);
24 24
25void encoder_update_kb(int8_t index, bool clockwise); 25void encoder_update_kb(int8_t index, bool clockwise);
26void encoder_update_user(int8_t index, bool clockwise); 26void encoder_update_user(int8_t index, bool clockwise);
diff --git a/tmk_core/common/keyboard.c b/tmk_core/common/keyboard.c
index aea09169f..0ca454612 100644
--- a/tmk_core/common/keyboard.c
+++ b/tmk_core/common/keyboard.c
@@ -97,9 +97,19 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
97# include "dip_switch.h" 97# include "dip_switch.h"
98#endif 98#endif
99 99
100static uint32_t last_input_modification_time = 0;
101uint32_t last_input_activity_time(void) { return last_input_modification_time; }
102uint32_t last_input_activity_elapsed(void) { return timer_elapsed32(last_input_modification_time); }
103
100static uint32_t last_matrix_modification_time = 0; 104static uint32_t last_matrix_modification_time = 0;
101uint32_t last_matrix_activity_time(void) { return last_matrix_modification_time; } 105uint32_t last_matrix_activity_time(void) { return last_matrix_modification_time; }
102uint32_t last_matrix_activity_elapsed(void) { return timer_elapsed32(last_matrix_modification_time); } 106uint32_t last_matrix_activity_elapsed(void) { return timer_elapsed32(last_matrix_modification_time); }
107void last_matrix_activity_trigger(void) { last_matrix_modification_time = last_input_modification_time = timer_read32(); }
108
109static uint32_t last_encoder_modification_time = 0;
110uint32_t last_encoder_activity_time(void) { return last_encoder_modification_time; }
111uint32_t last_encoder_activity_elapsed(void) { return timer_elapsed32(last_encoder_modification_time); }
112void last_encoder_activity_trigger(void) { last_encoder_modification_time = last_input_modification_time = timer_read32(); }
103 113
104// Only enable this if console is enabled to print to 114// Only enable this if console is enabled to print to
105#if defined(DEBUG_MATRIX_SCAN_RATE) && defined(CONSOLE_ENABLE) 115#if defined(DEBUG_MATRIX_SCAN_RATE) && defined(CONSOLE_ENABLE)
@@ -338,12 +348,15 @@ void keyboard_task(void) {
338#ifdef QMK_KEYS_PER_SCAN 348#ifdef QMK_KEYS_PER_SCAN
339 uint8_t keys_processed = 0; 349 uint8_t keys_processed = 0;
340#endif 350#endif
351#ifdef ENCODER_ENABLE
352 bool encoders_changed = false;
353#endif
341 354
342 housekeeping_task_kb(); 355 housekeeping_task_kb();
343 housekeeping_task_user(); 356 housekeeping_task_user();
344 357
345 uint8_t matrix_changed = matrix_scan(); 358 uint8_t matrix_changed = matrix_scan();
346 if (matrix_changed) last_matrix_modification_time = timer_read32(); 359 if (matrix_changed) last_matrix_activity_trigger();
347 360
348 if (should_process_keypress()) { 361 if (should_process_keypress()) {
349 for (uint8_t r = 0; r < MATRIX_ROWS; r++) { 362 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
@@ -399,7 +412,8 @@ MATRIX_LOOP_END:
399#endif 412#endif
400 413
401#ifdef ENCODER_ENABLE 414#ifdef ENCODER_ENABLE
402 encoder_read(); 415 encoders_changed = encoder_read();
416 if (encoders_changed) last_encoder_activity_trigger();
403#endif 417#endif
404 418
405#ifdef QWIIC_ENABLE 419#ifdef QWIIC_ENABLE
@@ -409,8 +423,12 @@ MATRIX_LOOP_END:
409#ifdef OLED_DRIVER_ENABLE 423#ifdef OLED_DRIVER_ENABLE
410 oled_task(); 424 oled_task();
411# ifndef OLED_DISABLE_TIMEOUT 425# ifndef OLED_DISABLE_TIMEOUT
412 // Wake up oled if user is using those fabulous keys! 426 // Wake up oled if user is using those fabulous keys or spinning those encoders!
427# ifdef ENCODER_ENABLE
428 if (matrix_changed || encoders_changed) oled_on();
429# else
413 if (matrix_changed) oled_on(); 430 if (matrix_changed) oled_on();
431# endif
414# endif 432# endif
415#endif 433#endif
416 434
diff --git a/tmk_core/common/keyboard.h b/tmk_core/common/keyboard.h
index cc5b2e5e4..88b3896e9 100644
--- a/tmk_core/common/keyboard.h
+++ b/tmk_core/common/keyboard.h
@@ -73,9 +73,15 @@ void keyboard_post_init_user(void);
73void housekeeping_task_kb(void); 73void housekeeping_task_kb(void);
74void housekeeping_task_user(void); 74void housekeeping_task_user(void);
75 75
76uint32_t last_input_activity_time(void); // Timestamp of the last matrix or encoder activity
77uint32_t last_input_activity_elapsed(void); // Number of milliseconds since the last matrix or encoder activity
78
76uint32_t last_matrix_activity_time(void); // Timestamp of the last matrix activity 79uint32_t last_matrix_activity_time(void); // Timestamp of the last matrix activity
77uint32_t last_matrix_activity_elapsed(void); // Number of milliseconds since the last matrix activity 80uint32_t last_matrix_activity_elapsed(void); // Number of milliseconds since the last matrix activity
78 81
82uint32_t last_encoder_activity_time(void); // Timestamp of the last encoder activity
83uint32_t last_encoder_activity_elapsed(void); // Number of milliseconds since the last encoder activity
84
79#ifdef __cplusplus 85#ifdef __cplusplus
80} 86}
81#endif 87#endif