2 Copyright 2014 Ralf Schmitt <ralf@bunkertor.net>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <util/delay.h>
26 #include "backlight.h"
32 static uint8_t debouncing
= DEBOUNCE
;
34 /* matrix state(1:on, 0:off) */
35 static matrix_row_t matrix
[MATRIX_ROWS
];
36 static matrix_row_t matrix_debouncing
[MATRIX_ROWS
];
38 static uint16_t read_inputs(void);
39 static void init_inputs(void);
40 static void init_outputs(void);
41 static void reset_inputs(void);
42 static void reset_outputs(void);
43 static void select_output(uint8_t col
);
46 uint8_t matrix_rows(void)
52 uint8_t matrix_cols(void)
57 void matrix_init(void)
59 backlight_init_ports();
63 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) {
65 matrix_debouncing
[i
] = 0;
69 uint8_t matrix_scan(void)
71 for (uint8_t col
= 0; col
< MATRIX_COLS
; col
++) {
76 uint16_t rows
= read_inputs();
77 for (uint8_t row
= 0; row
< MATRIX_ROWS
; row
++) {
78 bool prev_bit
= matrix_debouncing
[row
] & ((matrix_row_t
)1<<col
);
79 bool curr_bit
= rows
& (1<<row
);
80 if (prev_bit
!= curr_bit
) {
81 matrix_debouncing
[row
] ^= ((matrix_row_t
)1<<col
);
83 dprint("bounce!: "); dprintf("%02X", debouncing
); dprintln();
85 debouncing
= DEBOUNCE
;
94 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
95 matrix
[i
] = matrix_debouncing
[i
];
103 bool matrix_is_modified(void)
105 if (debouncing
) return false;
110 bool matrix_is_on(uint8_t row
, uint8_t col
)
112 return (matrix
[row
] & ((matrix_row_t
)1<<col
));
116 matrix_row_t
matrix_get_row(uint8_t row
)
121 void matrix_print(void)
123 print("\nr/c 0123456789ABCDEF\n");
124 for (uint8_t row
= 0; row
< MATRIX_ROWS
; row
++) {
125 phex(row
); print(": ");
126 pbin_reverse16(matrix_get_row(row
));
131 uint8_t matrix_key_count(void)
134 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
135 count
+= bitpop16(matrix
[i
]);
140 static void init_inputs(void)
142 DDRE
&= ~0b01000000; // PE6 (Col 0)
143 DDRB
&= ~0b00001111; // PB0 (Col 1), PB1 (Col 2), PB2 (Col 3), PB3 (Col 4)
144 DDRF
&= ~0b00000001; // PF0 (Col 5)
145 DDRD
&= ~0b00100011; // PD0 (Col 6), PD1 (Col 8 TKL), PD5 (Col 7)
148 static uint16_t read_inputs(void)
150 return (PINE
&(1<<6) ? 0 : (1<<0)) | // PE6 (Col 0)
151 (PINB
&(1<<0) ? 0 : (1<<1)) | // PB0 (Col 1)
152 (PINB
&(1<<1) ? 0 : (1<<2)) | // PB1 (Col 2)
153 (PINB
&(1<<2) ? 0 : (1<<3)) | // PB2 (Col 3)
154 (PINB
&(1<<3) ? 0 : (1<<4)) | // PB3 (Col 4)
155 (PINF
&(1<<0) ? 0 : (1<<5)) | // PF0 (Col 5)
156 (PIND
&(1<<0) ? 0 : (1<<6)) | // PD0 (Col 6)
157 (PIND
&(1<<5) ? 0 : (1<<7)) | // PD5 (Col 7)
158 (PIND
&(1<<1) ? 0 : (1<<8)); // PD1 (Col 8 TKL)
161 static void reset_inputs(void)
163 PORTE
|= 0b01000000; // PE6 (Col 0)
164 PORTB
|= 0b00001111; // PB0 (Col 1), PB1 (Col 2), PB2 (Col 3), PB3 (Col 4)
165 PORTF
|= 0b00000001; // PF0 (Col 5)
166 PORTD
|= 0b00100011; // PD0 (Col 6), PD1 (Col 8 TKL), PD5 (Col 7)
169 static void init_outputs(void)
171 DDRB
|= 0b00010000; // PB4 (Row 0)
172 DDRE
|= 0b00000100; // PE2 (Row 1)
173 DDRF
|= 0b11110010; // PF4 (Row 2), PF7 (Row 3), PF1 (Row 4), PF6 (Row 5), PF5 (Row 7)
174 DDRC
|= 0b11000000; // PC6 (Row 6), PC7 (Row 9)
175 DDRD
|= 0b10000000; // PD7 (Row 8)
178 static void reset_outputs(void)
180 PORTB
|= 0b00010000; // PB4 (Row 0)
181 PORTE
|= 0b00000100; // PE2 (Row 1)
182 PORTF
|= 0b11110010; // PF4 (Row 2), PF7 (Row 3), PF1 (Row 4), PF6 (Row 5), PF5 (Row 7)
183 PORTC
|= 0b11000000; // PC6 (Row 6), PC7 (Row 9)
184 PORTD
|= 0b10000000; // PD7 (Row 8)
187 static void select_output(uint8_t col
)