aboutsummaryrefslogtreecommitdiff
path: root/keyboard/atomic/matrix_pcb.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboard/atomic/matrix_pcb.c')
-rw-r--r--keyboard/atomic/matrix_pcb.c231
1 files changed, 231 insertions, 0 deletions
diff --git a/keyboard/atomic/matrix_pcb.c b/keyboard/atomic/matrix_pcb.c
new file mode 100644
index 000000000..47fb459a8
--- /dev/null
+++ b/keyboard/atomic/matrix_pcb.c
@@ -0,0 +1,231 @@
1/*
2Copyright 2012 Jun Wako
3Generated by planckkeyboard.com (2014 Jack Humbert)
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/*
20 * scan matrix
21 */
22#include <stdint.h>
23#include <stdbool.h>
24#include <avr/io.h>
25#include <util/delay.h>
26#include "print.h"
27#include "debug.h"
28#include "util.h"
29#include "matrix.h"
30#include "backlight.h" // TODO fix this dependency
31
32
33#ifndef DEBOUNCE
34# define DEBOUNCE 10
35#endif
36static uint8_t debouncing = DEBOUNCE;
37
38/* matrix state(1:on, 0:off) */
39static matrix_row_t matrix[MATRIX_ROWS];
40static matrix_row_t matrix_debouncing[MATRIX_ROWS];
41
42static matrix_row_t read_cols(void);
43static void init_cols(void);
44static void unselect_rows(void);
45static void select_row(uint8_t row);
46
47inline
48uint8_t matrix_rows(void)
49{
50 return MATRIX_ROWS;
51}
52
53inline
54uint8_t matrix_cols(void)
55{
56 return MATRIX_COLS;
57}
58
59void matrix_init(void)
60{
61 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
62 MCUCR |= (1<<JTD);
63 MCUCR |= (1<<JTD);
64
65 backlight_init_ports();
66
67 // Turn status LED on
68 DDRE |= (1<<6);
69 PORTE |= (1<<6);
70
71 // initialize row and col
72 unselect_rows();
73 init_cols();
74
75 // initialize matrix state: all keys off
76 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
77 matrix[i] = 0;
78 matrix_debouncing[i] = 0;
79 }
80}
81
82uint8_t matrix_scan(void)
83{
84 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
85 select_row(i);
86 _delay_us(30); // without this wait read unstable value.
87 matrix_row_t cols = read_cols();
88 if (matrix_debouncing[i] != cols) {
89 matrix_debouncing[i] = cols;
90 if (debouncing) {
91 debug("bounce!: "); debug_hex(debouncing); debug("\n");
92 }
93 debouncing = DEBOUNCE;
94 }
95 unselect_rows();
96 }
97
98 if (debouncing) {
99 if (--debouncing) {
100 _delay_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 return 1;
109}
110
111bool matrix_is_modified(void)
112{
113 if (debouncing) return false;
114 return true;
115}
116
117inline
118bool matrix_is_on(uint8_t row, uint8_t col)
119{
120 return (matrix[row] & ((matrix_row_t)1<col));
121}
122
123inline
124matrix_row_t matrix_get_row(uint8_t row)
125{
126 return matrix[row];
127}
128
129void matrix_print(void)
130{
131 print("\nr/c 0123456789ABCDEF\n");
132 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
133 phex(row); print(": ");
134 pbin_reverse16(matrix_get_row(row));
135 print("\n");
136 }
137}
138
139uint8_t matrix_key_count(void)
140{
141 uint8_t count = 0;
142 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
143 count += bitpop16(matrix[i]);
144 }
145 return count;
146}
147
148//
149// Atomic PCB Rev 0 Pin Assignments
150//
151// Column: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
152// Pin: F1, F0, B0, C7, F4, F5, F6, F7, D4, D6, B4, D7, D3, D2, D1
153//
154
155static void init_cols(void)
156{
157 DDRB &= ~(1<<4 | 1<<0);
158 PORTB |= (1<<4 | 1<<0);
159 DDRC &= ~(1<<7);
160 PORTC |= (1<<7);
161 DDRD &= ~(1<<7 | 1<<6 | 1<<4 | 1<<2 | 1<<3 | 1<<1);
162 PORTD |= (1<<7 | 1<<6 | 1<<4 | 1<<2 | 1<<3 | 1<<1);
163 DDRF &= ~(1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
164 PORTF |= (1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
165
166}
167
168static matrix_row_t read_cols(void)
169{
170 return (PINF&(1<<1) ? 0 : (1<<0)) |
171 (PINF&(1<<0) ? 0 : (1<<1)) |
172 (PINB&(1<<0) ? 0 : (1<<2)) |
173 (PINC&(1<<7) ? 0 : (1<<3)) |
174 (PINF&(1<<4) ? 0 : (1<<4)) |
175 (PINF&(1<<5) ? 0 : (1<<5)) |
176 (PINF&(1<<6) ? 0 : (1<<6)) |
177 (PINF&(1<<7) ? 0 : (1<<7)) |
178 (PIND&(1<<4) ? 0 : (1<<8)) |
179 (PIND&(1<<6) ? 0 : (1<<9)) |
180 (PINB&(1<<4) ? 0 : (1<<10)) |
181 (PIND&(1<<7) ? 0 : (1<<11)) |
182 (PIND&(1<<3) ? 0 : (1<<12)) |
183 (PIND&(1<<2) ? 0 : (1<<13)) |
184 (PIND&(1<<1) ? 0 : (1<<14));
185
186}
187
188static void unselect_rows(void)
189{
190 DDRB &= ~(1<<5 | 1<<6);
191 PORTB |= (1<<5 | 1<<6);
192 DDRD &= ~(1<<0 | 1<<5);
193 PORTD |= (1<<0 | 1<<5);
194 DDRC &= ~(1<<6);
195 PORTC |= (1<<6);
196
197}
198
199//
200// Atomic PCB Rev 0 Pin Assignments
201//
202// Row: 0, 1, 2, 3, 4
203// Pin: D0, D5, B5, B6, C6
204//
205
206static void select_row(uint8_t row)
207{
208 switch (row) {
209 case 0:
210 DDRD |= (1<<0);
211 PORTD &= ~(1<<0);
212 break;
213 case 1:
214 DDRD |= (1<<5);
215 PORTD &= ~(1<<5);
216 break;
217 case 2:
218 DDRB |= (1<<5);
219 PORTB &= ~(1<<5);
220 break;
221 case 3:
222 DDRB |= (1<<6);
223 PORTB &= ~(1<<6);
224 break;
225 case 4:
226 DDRC |= (1<<6);
227 PORTC &= ~(1<<6);
228 break;
229
230 }
231} \ No newline at end of file