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