aboutsummaryrefslogtreecommitdiff
path: root/keyboard/hbkb/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboard/hbkb/matrix.c')
-rw-r--r--keyboard/hbkb/matrix.c65
1 files changed, 21 insertions, 44 deletions
diff --git a/keyboard/hbkb/matrix.c b/keyboard/hbkb/matrix.c
index a24d24b8c..f6830a0f7 100644
--- a/keyboard/hbkb/matrix.c
+++ b/keyboard/hbkb/matrix.c
@@ -32,32 +32,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
32 * COL: PD0-7 32 * COL: PD0-7
33 * ROW: PB0-7, PF4-7 33 * ROW: PB0-7, PF4-7
34 */ 34 */
35
36#if (MATRIX_COLS > 16)
37# error "MATRIX_COLS must not exceed 16"
38#endif
39#if (MATRIX_ROWS > 255)
40# error "MATRIX_ROWS must not exceed 255"
41#endif
42
43
44#ifndef DEBOUNCE 35#ifndef DEBOUNCE
45# define DEBOUNCE 0 36# define DEBOUNCE 10
46#endif 37#endif
47static uint8_t debouncing = DEBOUNCE; 38static uint8_t debouncing = DEBOUNCE;
48 39
49// matrix state buffer(1:on, 0:off) 40// matrix state buffer(1:on, 0:off)
50#if (MATRIX_COLS <= 8)
51static uint8_t *matrix; 41static uint8_t *matrix;
52static uint8_t *matrix_prev; 42static uint8_t *matrix_debouncing;
53static uint8_t _matrix0[MATRIX_ROWS]; 43static uint8_t matrix0[MATRIX_ROWS];
54static uint8_t _matrix1[MATRIX_ROWS]; 44static uint8_t matrix1[MATRIX_ROWS];
55#else
56static uint16_t *matrix;
57static uint16_t *matrix_prev;
58static uint16_t _matrix0[MATRIX_ROWS];
59static uint16_t _matrix1[MATRIX_ROWS];
60#endif
61 45
62#ifdef MATRIX_HAS_GHOST 46#ifdef MATRIX_HAS_GHOST
63static bool matrix_has_ghost_in_row(uint8_t row); 47static bool matrix_has_ghost_in_row(uint8_t row);
@@ -100,37 +84,35 @@ void matrix_init(void)
100 PORTD = 0xFF; 84 PORTD = 0xFF;
101 85
102 // initialize matrix state: all keys off 86 // initialize matrix state: all keys off
103 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00; 87 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix0[i] = 0x00;
104 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00; 88 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix1[i] = 0x00;
105 matrix = _matrix0; 89 matrix = matrix0;
106 matrix_prev = _matrix1; 90 matrix_debouncing = matrix1;
107} 91}
108 92
109uint8_t matrix_scan(void) 93uint8_t matrix_scan(void)
110{ 94{
111 if (!debouncing) {
112 uint8_t *tmp = matrix_prev;
113 matrix_prev = matrix;
114 matrix = tmp;
115 }
116
117 for (uint8_t i = 0; i < MATRIX_ROWS; i++) { 95 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
118 unselect_rows();
119 select_row(i); 96 select_row(i);
120 _delay_us(30); // without this wait read unstable value. 97 _delay_us(30); // without this wait read unstable value.
121 if (matrix[i] != (uint8_t)~read_col()) { 98 if (matrix_debouncing[i] != read_col()) {
122 matrix[i] = (uint8_t)~read_col(); 99 matrix_debouncing[i] = read_col();
123 if (debouncing) { 100 if (debouncing) {
124 debug("bounce!: "); debug_hex(debouncing); print("\n"); 101 debug("bounce!: "); debug_hex(debouncing); debug("\n");
125 } 102 }
126 _delay_ms(1); // TODO: work around. HAHAHAHAHAAHA
127 debouncing = DEBOUNCE; 103 debouncing = DEBOUNCE;
128 } 104 }
105 unselect_rows();
129 } 106 }
130 unselect_rows();
131 107
132 if (debouncing) { 108 if (debouncing) {
133 debouncing--; 109 if (--debouncing) {
110 _delay_ms(1);
111 } else {
112 uint8_t *tmp = matrix;
113 matrix = matrix_debouncing;
114 matrix_debouncing = tmp;
115 }
134 } 116 }
135 117
136 return 1; 118 return 1;
@@ -139,12 +121,7 @@ uint8_t matrix_scan(void)
139bool matrix_is_modified(void) 121bool matrix_is_modified(void)
140{ 122{
141 if (debouncing) return false; 123 if (debouncing) return false;
142 for (uint8_t i = 0; i < MATRIX_ROWS; i++) { 124 return true;
143 if (matrix[i] != matrix_prev[i]) {
144 return true;
145 }
146 }
147 return false;
148} 125}
149 126
150inline 127inline
@@ -202,7 +179,7 @@ static bool matrix_has_ghost_in_row(uint8_t row)
202inline 179inline
203static uint8_t read_col(void) 180static uint8_t read_col(void)
204{ 181{
205 return PIND; 182 return ~PIND;
206} 183}
207 184
208inline 185inline