aboutsummaryrefslogtreecommitdiff
path: root/keyboards/al1/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/al1/matrix.c')
-rw-r--r--keyboards/al1/matrix.c184
1 files changed, 184 insertions, 0 deletions
diff --git a/keyboards/al1/matrix.c b/keyboards/al1/matrix.c
new file mode 100644
index 000000000..f7ed7fbb0
--- /dev/null
+++ b/keyboards/al1/matrix.c
@@ -0,0 +1,184 @@
1#include <stdint.h>
2#include <stdbool.h>
3#include <avr/io.h>
4#include <util/delay.h>
5#include "print.h"
6#include "debug.h"
7#include "util.h"
8#include "matrix.h"
9
10#ifndef DEBOUNCING_DELAY
11# define DEBOUNCING_DELAY 5
12#endif
13static uint8_t debouncing = DEBOUNCING_DELAY;
14
15static matrix_row_t matrix[MATRIX_ROWS];
16static matrix_row_t matrix_debouncing[MATRIX_ROWS];
17
18static uint8_t read_rows(void);
19static void init_rows(void);
20static void unselect_cols(void);
21static void select_col(uint8_t col);
22
23inline uint8_t matrix_rows(void) {
24 return MATRIX_ROWS;
25}
26
27inline uint8_t matrix_cols(void) {
28 return MATRIX_COLS;
29}
30
31
32void matrix_init(void) {
33 // initialize row and col
34 unselect_cols();
35 init_rows();
36
37 // initialize matrix state: all keys off
38 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
39 matrix[i] = 0;
40 matrix_debouncing[i] = 0;
41 }
42 matrix_init_quantum();
43}
44
45uint8_t matrix_scan(void) {
46 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
47 select_col(col);
48 _delay_us(3);
49 uint8_t rows = read_rows();
50 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
51 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
52 bool curr_bit = rows & (1<<row);
53 if (prev_bit != curr_bit) {
54 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
55 debouncing = DEBOUNCING_DELAY;
56 }
57 }
58 unselect_cols();
59 }
60
61 if (debouncing) {
62 if (--debouncing) {
63 _delay_ms(1);
64 } else {
65 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
66 matrix[i] = matrix_debouncing[i];
67 }
68 }
69 }
70
71 matrix_scan_quantum();
72 return 1;
73}
74
75bool matrix_is_modified(void) {
76 if (debouncing)
77 return false;
78 else
79 return true;
80}
81
82inline bool matrix_is_on(uint8_t row, uint8_t col) {
83 return (matrix[row] & ((matrix_row_t)1<<col));
84}
85
86inline matrix_row_t matrix_get_row(uint8_t row) {
87 return matrix[row];
88}
89
90void matrix_print(void) {
91 print("\nr/c 0123456789ABCDEF\n");
92 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
93 xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
94 }
95}
96
97uint8_t matrix_key_count(void) {
98 uint8_t count = 0;
99 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
100 count += bitpop32(matrix[i]);
101 }
102 return count;
103}
104
105/* Row pin configuration
106 *
107 * row: 0 1 2 3 4 5
108 * pin: C7 B1 B2 C6 B4 B5
109 *
110 */
111static void init_rows(void)
112{
113 DDRC &= ~0b11000000;
114 DDRB &= ~0b00110110;
115 PORTC |= 0b11000000;
116 PORTB |= 0b00110110;
117}
118
119static uint8_t read_rows(void) {
120 return (PINC&(1<<PC7) ? 0 : (1<<0)) |
121 (PINB&(1<<PB1) ? 0 : (1<<1)) |
122 (PINB&(1<<PB2) ? 0 : (1<<2)) |
123 (PINC&(1<<PC6) ? 0 : (1<<3)) |
124 (PINB&(1<<PB4) ? 0 : (1<<4)) |
125 (PINB&(1<<PB5) ? 0 : (1<<5));
126}
127
128/* Row pin configuration
129 * pin: D3 D7 D6 D5 D4
130 * row: off 0 x x x x
131 * 0 1 0 0 0 0
132 * 1 1 0 0 0 1
133 * 2 1 0 0 1 0
134 * 3 1 0 0 1 1
135 * 4 1 0 1 0 0
136 * 5 1 0 1 0 1
137 * 6 1 0 1 1 0
138 * 7 1 0 1 1 1
139 * 8 1 1 0 0 0
140 * 9 1 1 0 0 1
141 * 10 1 1 0 1 0
142 * 11 1 1 0 1 1
143 * 12 1 1 1 0 0
144 * 13 1 1 1 0 1
145 * 14 1 1 1 1 0
146 * 15 1 1 1 1 1
147 */
148static void unselect_cols(void)
149{
150 // output high(DDR:1, PORT:1) to unselect
151 DDRB |= (1 << PD3);
152 PORTB |= (1 << PD3);
153}
154
155static void select_col(uint8_t col) {
156 DDRD |= (1<<PD3 | 1<<PD4 | 1<<PD5 | 1<<PD6 | 1<<PD7);
157
158 PORTD &= ~(1<<PD3);
159
160 if (col & (1<<0)) {
161 PORTD |= (1<<PD4);
162 }
163 else {
164 PORTD &= ~(1<<PD4);
165 }
166 if (col & (1<<1)) {
167 PORTD |= (1<<PD5);
168 }
169 else {
170 PORTD &= ~(1<<PD5);
171 }
172 if (col & (1<<2)) {
173 PORTD |= (1<<PD6);
174 }
175 else {
176 PORTD &= ~(1<<PD6);
177 }
178 if (col & (1<<3)) {
179 PORTD |= (1<<PD7);
180 }
181 else {
182 PORTD &= ~(1<<PD7);
183 }
184} \ No newline at end of file