aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Ong <the.onga@gmail.com>2019-04-16 05:58:03 +1000
committerDrashna Jaelre <drashna@live.com>2019-04-15 12:58:03 -0700
commitd0fb7005d51be7c876e63e87778d080c8a733a13 (patch)
tree1fe6cdf52154517bdca40dc731315914ad8939ab
parentffc82ebdb2ee00c14dd225eb057d209d4584a623 (diff)
downloadqmk_firmware-d0fb7005d51be7c876e63e87778d080c8a733a13.tar.gz
qmk_firmware-d0fb7005d51be7c876e63e87778d080c8a733a13.zip
Eager pk/pr optimization (#5621)
* Optimizations to eager_pk * eager_pr also uses changed boolean now.
-rw-r--r--quantum/debounce/eager_pk.c89
-rw-r--r--quantum/debounce/eager_pr.c74
2 files changed, 72 insertions, 91 deletions
diff --git a/quantum/debounce/eager_pk.c b/quantum/debounce/eager_pk.c
index b8ad09cee..aa0f63a9d 100644
--- a/quantum/debounce/eager_pk.c
+++ b/quantum/debounce/eager_pk.c
@@ -24,23 +24,21 @@ No further inputs are accepted until DEBOUNCE milliseconds have occurred.
24#include <stdlib.h> 24#include <stdlib.h>
25 25
26#ifndef DEBOUNCE 26#ifndef DEBOUNCE
27 #define DEBOUNCE 5 27# define DEBOUNCE 5
28#endif 28#endif
29 29
30
31#if (MATRIX_COLS <= 8) 30#if (MATRIX_COLS <= 8)
32# define ROW_SHIFTER ((uint8_t)1) 31# define ROW_SHIFTER ((uint8_t)1)
33#elif (MATRIX_COLS <= 16) 32#elif (MATRIX_COLS <= 16)
34# define ROW_SHIFTER ((uint16_t)1) 33# define ROW_SHIFTER ((uint16_t)1)
35#elif (MATRIX_COLS <= 32) 34#elif (MATRIX_COLS <= 32)
36# define ROW_SHIFTER ((uint32_t)1) 35# define ROW_SHIFTER ((uint32_t)1)
37#endif 36#endif
38 37
39
40
41#define debounce_counter_t uint8_t 38#define debounce_counter_t uint8_t
42 39
43static debounce_counter_t *debounce_counters; 40static debounce_counter_t *debounce_counters;
41static bool counters_need_update;
44 42
45#define DEBOUNCE_ELAPSED 251 43#define DEBOUNCE_ELAPSED 251
46#define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1) 44#define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1)
@@ -48,39 +46,39 @@ static debounce_counter_t *debounce_counters;
48void update_debounce_counters(uint8_t num_rows, uint8_t current_time); 46void update_debounce_counters(uint8_t num_rows, uint8_t current_time);
49void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time); 47void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time);
50 48
51//we use num_rows rather than MATRIX_ROWS to support split keyboards 49// we use num_rows rather than MATRIX_ROWS to support split keyboards
52void debounce_init(uint8_t num_rows) 50void debounce_init(uint8_t num_rows) {
53{ 51 debounce_counters = (debounce_counter_t *)malloc(num_rows * MATRIX_COLS * sizeof(debounce_counter_t));
54 debounce_counters = (debounce_counter_t*)malloc(num_rows*MATRIX_COLS * sizeof(debounce_counter_t)); 52 int i = 0;
55 int i = 0; 53 for (uint8_t r = 0; r < num_rows; r++) {
56 for (uint8_t r = 0; r < num_rows; r++) 54 for (uint8_t c = 0; c < MATRIX_COLS; c++) {
57 {
58 for (uint8_t c = 0; c < MATRIX_COLS; c++)
59 {
60 debounce_counters[i++] = DEBOUNCE_ELAPSED; 55 debounce_counters[i++] = DEBOUNCE_ELAPSED;
61 } 56 }
62 } 57 }
63} 58}
64 59
65void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) 60void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
66{
67 uint8_t current_time = timer_read() % MAX_DEBOUNCE; 61 uint8_t current_time = timer_read() % MAX_DEBOUNCE;
68 update_debounce_counters(num_rows, current_time); 62 if (counters_need_update) {
69 transfer_matrix_values(raw, cooked, num_rows, current_time); 63 update_debounce_counters(num_rows, current_time);
64 }
65
66 if (changed) {
67 transfer_matrix_values(raw, cooked, num_rows, current_time);
68 }
70} 69}
71 70
72//If the current time is > debounce counter, set the counter to enable input. 71// If the current time is > debounce counter, set the counter to enable input.
73void update_debounce_counters(uint8_t num_rows, uint8_t current_time) 72void update_debounce_counters(uint8_t num_rows, uint8_t current_time) {
74{ 73 counters_need_update = false;
75 debounce_counter_t *debounce_pointer = debounce_counters; 74 debounce_counter_t *debounce_pointer = debounce_counters;
76 for (uint8_t row = 0; row < num_rows; row++) 75 for (uint8_t row = 0; row < num_rows; row++) {
77 { 76 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
78 for (uint8_t col = 0; col < MATRIX_COLS; col++) 77 if (*debounce_pointer != DEBOUNCE_ELAPSED) {
79 {
80 if (*debounce_pointer != DEBOUNCE_ELAPSED)
81 {
82 if (TIMER_DIFF(current_time, *debounce_pointer, MAX_DEBOUNCE) >= DEBOUNCE) { 78 if (TIMER_DIFF(current_time, *debounce_pointer, MAX_DEBOUNCE) >= DEBOUNCE) {
83 *debounce_pointer = DEBOUNCE_ELAPSED; 79 *debounce_pointer = DEBOUNCE_ELAPSED;
80 } else {
81 counters_need_update = true;
84 } 82 }
85 } 83 }
86 debounce_pointer++; 84 debounce_pointer++;
@@ -89,33 +87,22 @@ void update_debounce_counters(uint8_t num_rows, uint8_t current_time)
89} 87}
90 88
91// upload from raw_matrix to final matrix; 89// upload from raw_matrix to final matrix;
92void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time) 90void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time) {
93{
94 debounce_counter_t *debounce_pointer = debounce_counters; 91 debounce_counter_t *debounce_pointer = debounce_counters;
95 for (uint8_t row = 0; row < num_rows; row++) 92 for (uint8_t row = 0; row < num_rows; row++) {
96 { 93 matrix_row_t delta = raw[row] ^ cooked[row];
97 matrix_row_t existing_row = cooked[row]; 94 matrix_row_t existing_row = cooked[row];
98 matrix_row_t raw_row = raw[row]; 95 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
99
100 for (uint8_t col = 0; col < MATRIX_COLS; col++)
101 {
102 matrix_row_t col_mask = (ROW_SHIFTER << col); 96 matrix_row_t col_mask = (ROW_SHIFTER << col);
103 bool final_value = raw_row & col_mask; 97 if ((delta & col_mask) && *debounce_pointer == DEBOUNCE_ELAPSED) {
104 bool existing_value = existing_row & col_mask; 98 *debounce_pointer = current_time;
105 if (*debounce_pointer == DEBOUNCE_ELAPSED && 99 counters_need_update = true;
106 (existing_value != final_value)) 100 existing_row ^= col_mask; // flip the bit.
107 {
108 *debounce_pointer = current_time;
109 existing_row ^= col_mask; //flip the bit.
110 } 101 }
111 debounce_pointer++; 102 debounce_pointer++;
112 } 103 }
113 cooked[row] = existing_row; 104 cooked[row] = existing_row;
114 } 105 }
115}
116
117bool debounce_active(void)
118{
119 return true;
120} 106}
121 107
108bool debounce_active(void) { return true; }
diff --git a/quantum/debounce/eager_pr.c b/quantum/debounce/eager_pr.c
index 9eb9480a7..5b460f663 100644
--- a/quantum/debounce/eager_pr.c
+++ b/quantum/debounce/eager_pr.c
@@ -24,13 +24,13 @@ No further inputs are accepted until DEBOUNCE milliseconds have occurred.
24#include <stdlib.h> 24#include <stdlib.h>
25 25
26#ifndef DEBOUNCE 26#ifndef DEBOUNCE
27 #define DEBOUNCE 5 27# define DEBOUNCE 5
28#endif 28#endif
29 29
30
31#define debounce_counter_t uint8_t 30#define debounce_counter_t uint8_t
32 31
33static debounce_counter_t *debounce_counters; 32static debounce_counter_t *debounce_counters;
33static bool counters_need_update;
34 34
35#define DEBOUNCE_ELAPSED 251 35#define DEBOUNCE_ELAPSED 251
36#define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1) 36#define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1)
@@ -38,33 +38,35 @@ static debounce_counter_t *debounce_counters;
38void update_debounce_counters(uint8_t num_rows, uint8_t current_time); 38void update_debounce_counters(uint8_t num_rows, uint8_t current_time);
39void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time); 39void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time);
40 40
41//we use num_rows rather than MATRIX_ROWS to support split keyboards 41// we use num_rows rather than MATRIX_ROWS to support split keyboards
42void debounce_init(uint8_t num_rows) 42void debounce_init(uint8_t num_rows) {
43{ 43 debounce_counters = (debounce_counter_t *)malloc(num_rows * sizeof(debounce_counter_t));
44 debounce_counters = (debounce_counter_t*)malloc(num_rows*sizeof(debounce_counter_t)); 44 for (uint8_t r = 0; r < num_rows; r++) {
45 for (uint8_t r = 0; r < num_rows; r++)
46 {
47 debounce_counters[r] = DEBOUNCE_ELAPSED; 45 debounce_counters[r] = DEBOUNCE_ELAPSED;
48 } 46 }
49} 47}
50 48
51void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) 49void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
52{
53 uint8_t current_time = timer_read() % MAX_DEBOUNCE; 50 uint8_t current_time = timer_read() % MAX_DEBOUNCE;
54 update_debounce_counters(num_rows, current_time); 51 if (counters_need_update) {
55 transfer_matrix_values(raw, cooked, num_rows, current_time); 52 update_debounce_counters(num_rows, current_time);
53 }
54
55 if (changed) {
56 transfer_matrix_values(raw, cooked, num_rows, current_time);
57 }
56} 58}
57 59
58//If the current time is > debounce counter, set the counter to enable input. 60// If the current time is > debounce counter, set the counter to enable input.
59void update_debounce_counters(uint8_t num_rows, uint8_t current_time) 61void update_debounce_counters(uint8_t num_rows, uint8_t current_time) {
60{ 62 counters_need_update = false;
61 debounce_counter_t *debounce_pointer = debounce_counters; 63 debounce_counter_t *debounce_pointer = debounce_counters;
62 for (uint8_t row = 0; row < num_rows; row++) 64 for (uint8_t row = 0; row < num_rows; row++) {
63 { 65 if (*debounce_pointer != DEBOUNCE_ELAPSED) {
64 if (*debounce_pointer != DEBOUNCE_ELAPSED)
65 {
66 if (TIMER_DIFF(current_time, *debounce_pointer, MAX_DEBOUNCE) >= DEBOUNCE) { 66 if (TIMER_DIFF(current_time, *debounce_pointer, MAX_DEBOUNCE) >= DEBOUNCE) {
67 *debounce_pointer = DEBOUNCE_ELAPSED; 67 *debounce_pointer = DEBOUNCE_ELAPSED;
68 } else {
69 counters_need_update = true;
68 } 70 }
69 } 71 }
70 debounce_pointer++; 72 debounce_pointer++;
@@ -72,29 +74,21 @@ void update_debounce_counters(uint8_t num_rows, uint8_t current_time)
72} 74}
73 75
74// upload from raw_matrix to final matrix; 76// upload from raw_matrix to final matrix;
75void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time) 77void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time) {
76{
77 debounce_counter_t *debounce_pointer = debounce_counters; 78 debounce_counter_t *debounce_pointer = debounce_counters;
78 for (uint8_t row = 0; row < num_rows; row++) 79 for (uint8_t row = 0; row < num_rows; row++) {
79 { 80 matrix_row_t existing_row = cooked[row];
80 matrix_row_t existing_row = cooked[row]; 81 matrix_row_t raw_row = raw[row];
81 matrix_row_t raw_row = raw[row]; 82
82 83 // determine new value basd on debounce pointer + raw value
83 //determine new value basd on debounce pointer + raw value 84 if (*debounce_pointer == DEBOUNCE_ELAPSED && (existing_row != raw_row)) {
84 if (*debounce_pointer == DEBOUNCE_ELAPSED && 85 *debounce_pointer = current_time;
85 (existing_row != raw_row)) 86 cooked[row] = raw_row;
86 { 87 counters_need_update = true;
87 *debounce_pointer = current_time;
88 existing_row = raw_row;
89 } 88 }
90 cooked[row] = existing_row;
91
92 debounce_pointer++;
93 }
94}
95 89
96bool debounce_active(void) 90 debounce_pointer++;
97{ 91 }
98 return true;
99} 92}
100 93
94bool debounce_active(void) { return true; }