aboutsummaryrefslogtreecommitdiff
path: root/keyboards/bpiphany/pegasushoof/2013/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/bpiphany/pegasushoof/2013/matrix.c')
-rw-r--r--keyboards/bpiphany/pegasushoof/2013/matrix.c222
1 files changed, 222 insertions, 0 deletions
diff --git a/keyboards/bpiphany/pegasushoof/2013/matrix.c b/keyboards/bpiphany/pegasushoof/2013/matrix.c
new file mode 100644
index 000000000..037f323c0
--- /dev/null
+++ b/keyboards/bpiphany/pegasushoof/2013/matrix.c
@@ -0,0 +1,222 @@
1/*
2Copyright 2014 Ralf Schmitt <[email protected]>
3Copyright 2016 Daniel Svensson <[email protected]>
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 2 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along 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
29static uint8_t debouncing = DEBOUNCE;
30static matrix_row_t matrix[MATRIX_ROWS];
31static matrix_row_t matrix_debouncing[MATRIX_ROWS];
32
33static matrix_row_t read_cols(void);
34static void select_row(uint8_t col);
35
36__attribute__ ((weak))
37void matrix_init_kb(void) {
38 matrix_init_user();
39}
40
41__attribute__ ((weak))
42void matrix_scan_kb(void) {
43 matrix_scan_user();
44}
45
46__attribute__ ((weak))
47void matrix_init_user(void) {
48}
49
50__attribute__ ((weak))
51void matrix_scan_user(void) {
52}
53
54inline uint8_t matrix_rows(void)
55{
56 return MATRIX_ROWS;
57}
58
59inline uint8_t matrix_cols(void)
60{
61 return MATRIX_COLS;
62}
63
64void 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
82uint8_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 = DEBOUNCE;
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
113bool matrix_is_modified(void)
114{
115 if (debouncing)
116 return false;
117 return true;
118}
119
120inline
121bool matrix_is_on(uint8_t row, uint8_t col)
122{
123 return matrix[row] & 1 << col;
124}
125
126inline
127matrix_row_t matrix_get_row(uint8_t row)
128{
129 return matrix[row];
130}
131
132void matrix_print(void)
133{
134 print("\nr/c 0123456789ABCDEF\n");
135 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
136 print_hex8(row); print(": ");
137 print_bin_reverse16(matrix_get_row(row));
138 print("\n");
139 }
140}
141
142uint8_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
151static 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
164static 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}