fixed matrix config for proper diode direction
[tmk_keyboard.git] / keyboard / bananasplit / matrix.c
1 /*
2 Copyright 2012 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /*
19 * scan matrix
20 */
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <avr/io.h>
24 #include <util/delay.h>
25 #include "print.h"
26 #include "debug.h"
27 #include "util.h"
28 #include "matrix.h"
29
30
31 #ifndef DEBOUNCE
32 # define DEBOUNCE 5
33 #endif
34 static uint8_t debouncing = DEBOUNCE;
35
36 /* matrix state(1:on, 0:off) */
37 static matrix_row_t matrix[MATRIX_ROWS];
38 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
39
40 static matrix_row_t read_cols(void);
41 static void init_cols(void);
42 static void unselect_rows(void);
43 static void select_row(uint8_t row);
44
45
46 inline
47 uint8_t matrix_rows(void)
48 {
49 return MATRIX_ROWS;
50 }
51
52 inline
53 uint8_t matrix_cols(void)
54 {
55 return MATRIX_COLS;
56 }
57
58 void matrix_init(void)
59 {
60 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
61 MCUCR |= (1<<JTD);
62 MCUCR |= (1<<JTD);
63
64 // initialize row and col
65 unselect_rows();
66 init_cols();
67
68 // initialize matrix state: all keys off
69 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
70 matrix[i] = 0;
71 matrix_debouncing[i] = 0;
72 }
73 }
74
75 uint8_t matrix_scan(void)
76 {
77 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
78 select_row(i);
79 _delay_us(30); // without this wait read unstable value.
80 matrix_row_t cols = read_cols();
81 if (matrix_debouncing[i] != cols) {
82 matrix_debouncing[i] = cols;
83 if (debouncing) {
84 debug("bounce!: "); debug_hex(debouncing); debug("\n");
85 }
86 debouncing = DEBOUNCE;
87 }
88 unselect_rows();
89 }
90
91 if (debouncing) {
92 if (--debouncing) {
93 _delay_ms(1);
94 } else {
95 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
96 matrix[i] = matrix_debouncing[i];
97 }
98 }
99 }
100
101 return 1;
102 }
103
104 bool matrix_is_modified(void)
105 {
106 if (debouncing) return false;
107 return true;
108 }
109
110 inline
111 bool matrix_is_on(uint8_t row, uint8_t col)
112 {
113 return (matrix[row] & ((matrix_row_t)1<<col));
114 }
115
116 inline
117 matrix_row_t matrix_get_row(uint8_t row)
118 {
119 return matrix[row];
120 }
121
122 void matrix_print(void)
123 {
124 print("\nr/c 0123456789ABCDEF\n");
125 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
126 phex(row); print(": ");
127 pbin_reverse16(matrix_get_row(row));
128 print("\n");
129 }
130 }
131
132 uint8_t matrix_key_count(void)
133 {
134 uint8_t count = 0;
135 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
136 count += bitpop16(matrix[i]);
137 }
138 return count;
139 }
140
141 /* Column pin configuration
142 * col: 0 1 2 3 4 5 6 7 8 9 10 11 12 13
143 * pin: F0 F1 E6 C7 C6 B6 D4 B1 B0 B5 B4 D7 D6 B3 (Rev.A)
144 * pin: B7 (Rev.B)
145 */
146 static void init_cols(void)
147 {
148 // Input with pull-up(DDR:0, PORT:1)
149 DDRF &= ~(1<<0 | 1<<1 | 1<<4 | 1<<5);
150 PORTF |= (1<<0 | 1<<1 | 1<<4 | 1<<5);
151 DDRD &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
152 PORTD |= (1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
153 DDRB &= ~(1<<1 | 1<<3);
154 PORTB |= (1<<1 | 1<<3);
155
156 }
157
158 static matrix_row_t read_cols(void)
159 {
160 return (PINF&(1<<5) ? 0 : (1<<0)) |
161 (PINB&(1<<1) ? 0 : (1<<1)) |
162 (PINF&(1<<0) ? 0 : (1<<2)) |
163 (PINF&(1<<1) ? 0 : (1<<3)) |
164 (PINF&(1<<4) ? 0 : (1<<4)) |
165 (PINB&(1<<3) ? 0 : (1<<5)) |
166 (PIND&(1<<7) ? 0 : (1<<6)) |
167 (PIND&(1<<6) ? 0 : (1<<7)) |
168 (PIND&(1<<4) ? 0 : (1<<8)) |
169 (PIND&(1<<5) ? 0 : (1<<9)) |
170 (PIND&(1<<3) ? 0 : (1<<10)) |
171 (PIND&(1<<2) ? 0 : (1<<11)) |
172 (PIND&(1<<1) ? 0 : (1<<12)) |
173 (PIND&(1<<0) ? 0 : (1<<13));
174
175 }
176
177 /* Row pin configuration
178 * row: 0 1 2 3 4
179 * pin: D0 D1 D2 D3 D5
180 */
181 static void unselect_rows(void)
182 {
183 // Hi-Z(DDR:0, PORT:0) to unselect
184 DDRB &= ~0b01110101;
185 PORTB &= ~0b01110101;
186
187 }
188
189 static void select_row(uint8_t row)
190 {
191 // Output low(DDR:1, PORT:0) to select
192 switch (row) {
193 case 0:
194 DDRB |= (1<<0);
195 PORTB &= ~(1<<0);
196 break;
197 case 1:
198 DDRB |= (1<<2);
199 PORTB &= ~(1<<2);
200 break;
201 case 2:
202 DDRB |= (1<<4);
203 PORTB &= ~(1<<4);
204 break;
205 case 3:
206 DDRB |= (1<<5);
207 PORTB &= ~(1<<5);
208 break;
209 case 4:
210 DDRB |= (1<<6);
211 PORTB &= ~(1<<6);
212 break;
213 }
214 }
Imprint / Impressum