]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/bpiphany/pegasushoof/matrix.c
Keyboard: Move Bathroom Epiphany boards into its own directory (#3936)
[qmk_firmware.git] / keyboards / bpiphany / pegasushoof / matrix.c
1 /*
2 Copyright 2014 Ralf Schmitt <ralf@bunkertor.net>
3 Copyright 2016 Daniel Svensson <dsvensson@gmail.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <stdint.h>
20 #include <stdbool.h>
21 #include <avr/io.h>
22 #include <util/delay.h>
23 #include "wait.h"
24 #include "print.h"
25 #include "debug.h"
26 #include "util.h"
27 #include "matrix.h"
28
29 static uint8_t debouncing = DEBOUNCING_DELAY;
30 static matrix_row_t matrix[MATRIX_ROWS];
31 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
32
33 static matrix_row_t read_cols(void);
34 static void select_row(uint8_t col);
35
36 __attribute__ ((weak))
37 void matrix_init_kb(void) {
38     matrix_init_user();
39 }
40
41 __attribute__ ((weak))
42 void matrix_scan_kb(void) {
43     matrix_scan_user();
44 }
45
46 __attribute__ ((weak))
47 void matrix_init_user(void) {
48 }
49
50 __attribute__ ((weak))
51 void matrix_scan_user(void) {
52 }
53
54 inline uint8_t matrix_rows(void)
55 {
56         return MATRIX_ROWS;
57 }
58
59 inline uint8_t matrix_cols(void)
60 {
61         return MATRIX_COLS;
62 }
63
64 void matrix_init(void)
65 {
66         /* Column output pins */
67         DDRD  |=  0b01111011;
68         /* Row input pins */
69         DDRC  &= ~0b10000000;
70         DDRB  &= ~0b01111111;
71         PORTC |=  0b10000000;
72         PORTB |=  0b01111111;
73
74         for (uint8_t i=0; i < MATRIX_ROWS; i++)  {
75                 matrix[i] = 0;
76                 matrix_debouncing[i] = 0;
77         }
78
79         matrix_init_quantum();
80 }
81
82 uint8_t matrix_scan(void)
83 {
84         for (uint8_t col = 0; col < MATRIX_COLS; col++) {
85                 select_row(col);
86                 wait_us(30);
87                 matrix_row_t rows = read_cols();
88                 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
89                         bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
90                         bool curr_bit = rows & (1<<row);
91                         if (prev_bit != curr_bit) {
92                                 matrix_debouncing[row] ^= (matrix_row_t) 1 << col;
93                                 debouncing = DEBOUNCING_DELAY;
94                         }
95                 }
96         }
97
98         if (debouncing) {
99                 if (--debouncing) {
100                         wait_ms(1);
101                 } else {
102                         for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
103                                 matrix[i] = matrix_debouncing[i];
104                         }
105                 }
106         }
107
108         matrix_scan_quantum();
109
110         return 1;
111 }
112
113 bool matrix_is_modified(void)
114 {
115         if (debouncing)
116                 return false;
117         return true;
118 }
119
120 inline
121 bool matrix_is_on(uint8_t row, uint8_t col)
122 {
123         return matrix[row] & 1 << col;
124 }
125
126 inline
127 matrix_row_t matrix_get_row(uint8_t row)
128 {
129         return matrix[row];
130 }
131
132 void matrix_print(void)
133 {
134         print("\nr/c 0123456789ABCDEF\n");
135         for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
136                 phex(row); print(": ");
137                 pbin_reverse16(matrix_get_row(row));
138                 print("\n");
139         }
140 }
141
142 uint8_t matrix_key_count(void)
143 {
144         uint8_t count = 0;
145         for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
146                 count += bitpop16(matrix[i]);
147         }
148         return count;
149 }
150
151 static matrix_row_t read_cols(void)
152 {
153         return
154                 (PINB & (1 << 5) ? 0 : 1 << 0) |
155                 (PINC & (1 << 7) ? 0 : 1 << 1) |
156                 (PINB & (1 << 4) ? 0 : 1 << 2) |
157                 (PINB & (1 << 6) ? 0 : 1 << 3) |
158                 (PINB & (1 << 1) ? 0 : 1 << 4) |
159                 (PINB & (1 << 0) ? 0 : 1 << 5) |
160                 (PINB & (1 << 3) ? 0 : 1 << 6) |
161                 (PINB & (1 << 2) ? 0 : 1 << 7);
162 }
163
164 static void select_row(uint8_t col)
165 {
166         switch (col) {
167                 case 0:
168                         PORTD = (PORTD & ~0b01111011) | 0b00110011;
169                         break;
170                 case 1:
171                         PORTD = (PORTD & ~0b01111011) | 0b01110000;
172                         break;
173                 case 2:
174                         PORTD = (PORTD & ~0b01111011) | 0b00010011;
175                         break;
176                 case 3:
177                         PORTD = (PORTD & ~0b01111011) | 0b01101000;
178                         break;
179                 case 4:
180                         PORTD = (PORTD & ~0b01111011) | 0b00001011;
181                         break;
182                 case 5:
183                         PORTD = (PORTD & ~0b01111011) | 0b00111011;
184                         break;
185                 case 6:
186                         PORTD = (PORTD & ~0b01111011) | 0b01111000;
187                         break;
188                 case 7:
189                         PORTD = (PORTD & ~0b01111011) | 0b01100001;
190                         break;
191                 case 8:
192                         PORTD = (PORTD & ~0b01111011) | 0b01101001;
193                         break;
194                 case 9:
195                         PORTD = (PORTD & ~0b01111011) | 0b01110001;
196                         break;
197                 case 10:
198                         PORTD = (PORTD & ~0b01111011) | 0b01101010;
199                         break;
200                 case 11:
201                         PORTD = (PORTD & ~0b01111011) | 0b01100010;
202                         break;
203                 case 12:
204                         PORTD = (PORTD & ~0b01111011) | 0b01111001;
205                         break;
206                 case 13:
207                         PORTD = (PORTD & ~0b01111011) | 0b01100000;
208                         break;
209                 case 14:
210                         PORTD = (PORTD & ~0b01111011) | 0b01000011;
211                         break;
212                 case 15:
213                         PORTD = (PORTD & ~0b01111011) | 0b00011011;
214                         break;
215                 case 16:
216                         PORTD = (PORTD & ~0b01111011) | 0b00100011;
217                         break;
218                 case 17:
219                         PORTD = (PORTD & ~0b01111011) | 0b00101011;
220                         break;
221         }
222 }