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