@@ -24,63 +24,61 @@ No further inputs are accepted until DEBOUNCE milliseconds have occurred.
24
24
#include <stdlib.h>
25
25
26
26
#ifndef DEBOUNCE
27
- # define DEBOUNCE 5
27
+ # define DEBOUNCE 5
28
28
#endif
29
29
30
-
31
30
#if (MATRIX_COLS <= 8 )
32
- # define ROW_SHIFTER ((uint8_t)1)
31
+ # define ROW_SHIFTER ((uint8_t)1)
33
32
#elif (MATRIX_COLS <= 16 )
34
- # define ROW_SHIFTER ((uint16_t)1)
33
+ # define ROW_SHIFTER ((uint16_t)1)
35
34
#elif (MATRIX_COLS <= 32 )
36
- # define ROW_SHIFTER ((uint32_t)1)
35
+ # define ROW_SHIFTER ((uint32_t)1)
37
36
#endif
38
37
39
-
40
-
41
38
#define debounce_counter_t uint8_t
42
39
43
40
static debounce_counter_t * debounce_counters ;
41
+ static bool counters_need_update ;
44
42
45
43
#define DEBOUNCE_ELAPSED 251
46
44
#define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1)
47
45
48
46
void update_debounce_counters (uint8_t num_rows , uint8_t current_time );
49
47
void 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
52
- void debounce_init (uint8_t num_rows )
53
- {
54
- debounce_counters = (debounce_counter_t * )malloc (num_rows * MATRIX_COLS * sizeof (debounce_counter_t ));
55
- int i = 0 ;
56
- for (uint8_t r = 0 ; r < num_rows ; r ++ )
57
- {
58
- for (uint8_t c = 0 ; c < MATRIX_COLS ; c ++ )
59
- {
49
+ // we use num_rows rather than MATRIX_ROWS to support split keyboards
50
+ void debounce_init (uint8_t num_rows ) {
51
+ debounce_counters = (debounce_counter_t * )malloc (num_rows * MATRIX_COLS * sizeof (debounce_counter_t ));
52
+ int i = 0 ;
53
+ for (uint8_t r = 0 ; r < num_rows ; r ++ ) {
54
+ for (uint8_t c = 0 ; c < MATRIX_COLS ; c ++ ) {
60
55
debounce_counters [i ++ ] = DEBOUNCE_ELAPSED ;
61
56
}
62
57
}
63
58
}
64
59
65
- void debounce (matrix_row_t raw [], matrix_row_t cooked [], uint8_t num_rows , bool changed )
66
- {
60
+ void debounce (matrix_row_t raw [], matrix_row_t cooked [], uint8_t num_rows , bool changed ) {
67
61
uint8_t current_time = timer_read () % MAX_DEBOUNCE ;
68
- update_debounce_counters (num_rows , current_time );
69
- transfer_matrix_values (raw , cooked , num_rows , current_time );
62
+ if (counters_need_update ) {
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.
73
- void update_debounce_counters (uint8_t num_rows , uint8_t current_time )
74
- {
71
+ // If the current time is > debounce counter, set the counter to enable input.
72
+ void update_debounce_counters (uint8_t num_rows , uint8_t current_time ) {
73
+ counters_need_update = false;
75
74
debounce_counter_t * debounce_pointer = debounce_counters ;
76
- for (uint8_t row = 0 ; row < num_rows ; row ++ )
77
- {
78
- for (uint8_t col = 0 ; col < MATRIX_COLS ; col ++ )
79
- {
80
- if (* debounce_pointer != DEBOUNCE_ELAPSED )
81
- {
75
+ for (uint8_t row = 0 ; row < num_rows ; row ++ ) {
76
+ for (uint8_t col = 0 ; col < MATRIX_COLS ; col ++ ) {
77
+ if (* debounce_pointer != DEBOUNCE_ELAPSED ) {
82
78
if (TIMER_DIFF (current_time , * debounce_pointer , MAX_DEBOUNCE ) >= DEBOUNCE ) {
83
79
* debounce_pointer = DEBOUNCE_ELAPSED ;
80
+ } else {
81
+ counters_need_update = true;
84
82
}
85
83
}
86
84
debounce_pointer ++ ;
@@ -89,33 +87,22 @@ void update_debounce_counters(uint8_t num_rows, uint8_t current_time)
89
87
}
90
88
91
89
// upload from raw_matrix to final matrix;
92
- void transfer_matrix_values (matrix_row_t raw [], matrix_row_t cooked [], uint8_t num_rows , uint8_t current_time )
93
- {
90
+ void transfer_matrix_values (matrix_row_t raw [], matrix_row_t cooked [], uint8_t num_rows , uint8_t current_time ) {
94
91
debounce_counter_t * debounce_pointer = debounce_counters ;
95
- for (uint8_t row = 0 ; row < num_rows ; row ++ )
96
- {
97
- matrix_row_t existing_row = cooked [row ];
98
- matrix_row_t raw_row = raw [row ];
99
-
100
- for (uint8_t col = 0 ; col < MATRIX_COLS ; col ++ )
101
- {
92
+ for (uint8_t row = 0 ; row < num_rows ; row ++ ) {
93
+ matrix_row_t delta = raw [row ] ^ cooked [row ];
94
+ matrix_row_t existing_row = cooked [row ];
95
+ for (uint8_t col = 0 ; col < MATRIX_COLS ; col ++ ) {
102
96
matrix_row_t col_mask = (ROW_SHIFTER << col );
103
- bool final_value = raw_row & col_mask ;
104
- bool existing_value = existing_row & col_mask ;
105
- if (* debounce_pointer == DEBOUNCE_ELAPSED &&
106
- (existing_value != final_value ))
107
- {
108
- * debounce_pointer = current_time ;
109
- existing_row ^= col_mask ; //flip the bit.
97
+ if ((delta & col_mask ) && * debounce_pointer == DEBOUNCE_ELAPSED ) {
98
+ * debounce_pointer = current_time ;
99
+ counters_need_update = true;
100
+ existing_row ^= col_mask ; // flip the bit.
110
101
}
111
102
debounce_pointer ++ ;
112
103
}
113
104
cooked [row ] = existing_row ;
114
- }
115
- }
116
-
117
- bool debounce_active (void )
118
- {
119
- return true;
105
+ }
120
106
}
121
107
108
+ bool debounce_active (void ) { return true; }
0 commit comments