aboutsummaryrefslogtreecommitdiff
path: root/keyboards/bpiphany/pegasushoof/2015/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/bpiphany/pegasushoof/2015/matrix.c')
-rw-r--r--keyboards/bpiphany/pegasushoof/2015/matrix.c158
1 files changed, 158 insertions, 0 deletions
diff --git a/keyboards/bpiphany/pegasushoof/2015/matrix.c b/keyboards/bpiphany/pegasushoof/2015/matrix.c
new file mode 100644
index 000000000..b05869fed
--- /dev/null
+++ b/keyboards/bpiphany/pegasushoof/2015/matrix.c
@@ -0,0 +1,158 @@
1/*
2 Copyright 2014 Ralf Schmitt <[email protected]>
3 Copyright 2016 Daniel Svensson <[email protected]>
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#include "debounce.h"
29
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// user-defined overridable functions
37
38__attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
39
40__attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
41
42__attribute__((weak)) void matrix_init_user(void) {}
43
44__attribute__((weak)) void matrix_scan_user(void) {}
45
46// helper functions
47
48inline uint8_t matrix_rows(void)
49{
50 return MATRIX_ROWS;
51}
52
53inline uint8_t matrix_cols(void)
54{
55 return MATRIX_COLS;
56}
57
58void matrix_init(void)
59{
60 /* Column output pins */
61 DDRD |= 0b01111011;
62 /* Row input pins */
63 DDRC &= ~0b10000000;
64 DDRB &= ~0b01111111;
65 PORTC |= 0b10000000;
66 PORTB |= 0b01111111;
67
68 for (uint8_t i=0; i < matrix_rows(); i++) {
69 matrix[i] = 0;
70 matrix_debouncing[i] = 0;
71 }
72
73 matrix_init_quantum();
74}
75
76uint8_t matrix_scan(void)
77{
78 bool changed = false;
79 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
80 select_row(col);
81 wait_us(30);
82 matrix_row_t rows = read_cols();
83 for (uint8_t row = 0; row < matrix_rows(); row++) {
84 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
85 bool curr_bit = rows & (1<<row);
86 if ((changed |= prev_bit != curr_bit)) {
87 matrix_debouncing[row] ^= (matrix_row_t) 1 << col;
88 }
89 }
90 }
91
92 debounce(matrix_debouncing, matrix, matrix_rows(), changed);
93 matrix_scan_quantum();
94
95 return (uint8_t)changed;
96}
97
98inline
99matrix_row_t matrix_get_row(uint8_t row)
100{
101 return matrix[row];
102}
103
104void matrix_print(void)
105{
106 print("\nr/c 0123456789ABCDEF\n");
107 for (uint8_t row = 0; row < matrix_rows(); row++) {
108 print_hex8(row); print(": ");
109 print_bin_reverse16(matrix_get_row(row));
110 print("\n");
111 }
112}
113
114uint8_t matrix_key_count(void)
115{
116 uint8_t count = 0;
117 for (uint8_t i = 0; i < matrix_rows(); i++) {
118 count += bitpop16(matrix_get_row(i));
119 }
120 return count;
121}
122
123static matrix_row_t read_cols(void)
124{
125 return
126 (PINB&(1<<5) ? 0 : ((matrix_row_t)1<<0)) |
127 (PINC&(1<<7) ? 0 : ((matrix_row_t)1<<1)) |
128 (PINB&(1<<4) ? 0 : ((matrix_row_t)1<<2)) |
129 (PINB&(1<<6) ? 0 : ((matrix_row_t)1<<3)) |
130 (PINB&(1<<1) ? 0 : ((matrix_row_t)1<<4)) |
131 (PINB&(1<<2) ? 0 : ((matrix_row_t)1<<5)) |
132 (PINB&(1<<3) ? 0 : ((matrix_row_t)1<<6)) |
133 (PINB&(1<<0) ? 0 : ((matrix_row_t)1<<7));
134}
135
136static void select_row(uint8_t col)
137{
138 switch (col) {
139 case 0: PORTD = (PORTD & ~0b01111011) | 0b00011011; break;
140 case 1: PORTD = (PORTD & ~0b01111011) | 0b01000011; break;
141 case 2: PORTD = (PORTD & ~0b01111011) | 0b01100000; break;
142 case 3: PORTD = (PORTD & ~0b01111011) | 0b01111001; break;
143 case 4: PORTD = (PORTD & ~0b01111011) | 0b01100010; break;
144 case 5: PORTD = (PORTD & ~0b01111011) | 0b01101010; break;
145 case 6: PORTD = (PORTD & ~0b01111011) | 0b01110001; break;
146 case 7: PORTD = (PORTD & ~0b01111011) | 0b01101001; break;
147 case 8: PORTD = (PORTD & ~0b01111011) | 0b01100001; break;
148 case 9: PORTD = (PORTD & ~0b01111011) | 0b01111000; break;
149 case 10: PORTD = (PORTD & ~0b01111011) | 0b00100011; break;
150 case 11: PORTD = (PORTD & ~0b01111011) | 0b00101011; break;
151 case 12: PORTD = (PORTD & ~0b01111011) | 0b00110011; break;
152 case 13: PORTD = (PORTD & ~0b01111011) | 0b01110000; break;
153 case 14: PORTD = (PORTD & ~0b01111011) | 0b00010011; break;
154 case 15: PORTD = (PORTD & ~0b01111011) | 0b01101000; break;
155 case 16: PORTD = (PORTD & ~0b01111011) | 0b00001011; break;
156 case 17: PORTD = (PORTD & ~0b01111011) | 0b00111011; break;
157 }
158}