5638d7f69d9ef63f72490cd7a5485041ce23e5ce
[tmk_keyboard.git] / keyboard / alps64 / 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 #define LED_ON() do { DDRC |= (1<<5); PORTC |= (1<<5); } while (0)
59 #define LED_OFF() do { DDRC &= ~(1<<5); PORTC &= ~(1<<5); } while (0)
60 #define LED_TGL() do { DDRC |= (1<<5); PINC |= (1<<5); } while (0)
61
62 void matrix_init(void)
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 //debug
75 debug_matrix = true;
76 LED_ON();
77 _delay_ms(500);
78 LED_OFF();
79 }
80
81 uint8_t matrix_scan(void)
82 {
83 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
84 select_row(i);
85 _delay_us(30); // without this wait read unstable value.
86 matrix_row_t cols = read_cols();
87 if (matrix_debouncing[i] != cols) {
88 matrix_debouncing[i] = cols;
89 if (debouncing) {
90 debug("bounce!: "); debug_hex(debouncing); debug("\n");
91 }
92 debouncing = DEBOUNCE;
93 }
94 unselect_rows();
95 }
96
97 if (debouncing) {
98 if (--debouncing) {
99 _delay_ms(1);
100 } else {
101 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
102 matrix[i] = matrix_debouncing[i];
103 }
104 }
105 }
106
107 return 1;
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 /* Column pin configuration
133 * col: 0 1 2 3 4 5 6 7
134 * pin: B0 B1 B2 B3 B4 B5 B6 B7
135 */
136 static void init_cols(void)
137 {
138 // Input with pull-up(DDR:0, PORT:1)
139 DDRB &= ~0b11111111;
140 PORTB |= 0b11111111;
141 }
142
143 /* Returns status of switches(1:on, 0:off) */
144 static matrix_row_t read_cols(void)
145 {
146 // Invert because PIN indicates 'switch on' with low(0) and 'off' with high(1)
147 return ~PINB;
148 }
149
150 /* Row pin configuration
151 * row: 0 1 2 3 4 5 6 7
152 * pin: D0 D1 D2 D3 D4 D5 D6 C2
153 */
154 static void unselect_rows(void)
155 {
156 // Hi-Z(DDR:0, PORT:0) to unselect
157 DDRD &= ~0b01111111;
158 PORTD &= ~0b01111111;
159 DDRC &= ~0b00000100;
160 PORTC &= ~0b00000100;
161 }
162
163 static void select_row(uint8_t row)
164 {
165 // Output low(DDR:1, PORT:0) to select
166 switch (row) {
167 case 0:
168 DDRD |= (1<<0);
169 PORTD &= ~(1<<0);
170 break;
171 case 1:
172 DDRD |= (1<<1);
173 PORTD &= ~(1<<1);
174 break;
175 case 2:
176 DDRD |= (1<<2);
177 PORTD &= ~(1<<2);
178 break;
179 case 3:
180 DDRD |= (1<<3);
181 PORTD &= ~(1<<3);
182 break;
183 case 4:
184 DDRD |= (1<<4);
185 PORTD &= ~(1<<4);
186 break;
187 case 5:
188 DDRD |= (1<<5);
189 PORTD &= ~(1<<5);
190 break;
191 case 6:
192 DDRD |= (1<<6);
193 PORTD &= ~(1<<6);
194 break;
195 case 7:
196 DDRC |= (1<<2);
197 PORTC &= ~(1<<2);
198 break;
199 }
200 }
Imprint / Impressum