]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/alps64/matrix.c
805999d4a1a739051fd2fe0a14ebf1cd08336a74
[qmk_firmware.git] / keyboards / 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 void matrix_init(void)
59 {
60     // initialize row and col
61     unselect_rows();
62     init_cols();
63
64     // initialize matrix state: all keys off
65     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
66         matrix[i] = 0;
67         matrix_debouncing[i] = 0;
68     }
69
70     //debug
71     debug_matrix = true;
72     LED_ON();
73     _delay_ms(500);
74     LED_OFF();
75 }
76
77 uint8_t matrix_scan(void)
78 {
79     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
80         select_row(i);
81         _delay_us(30);  // without this wait read unstable value.
82         matrix_row_t cols = read_cols();
83         if (matrix_debouncing[i] != cols) {
84             matrix_debouncing[i] = cols;
85             if (debouncing) {
86                 debug("bounce!: "); debug_hex(debouncing); debug("\n");
87             }
88             debouncing = DEBOUNCE;
89         }
90         unselect_rows();
91     }
92
93     if (debouncing) {
94         if (--debouncing) {
95             _delay_ms(1);
96         } else {
97             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
98                 matrix[i] = matrix_debouncing[i];
99             }
100         }
101     }
102
103     return 1;
104 }
105
106 inline
107 bool matrix_is_on(uint8_t row, uint8_t col)
108 {
109     return (matrix[row] & ((matrix_row_t)1<<col));
110 }
111
112 inline
113 matrix_row_t matrix_get_row(uint8_t row)
114 {
115     return matrix[row];
116 }
117
118 void matrix_print(void)
119 {
120     print("\nr/c 0123456789ABCDEF\n");
121     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
122         phex(row); print(": ");
123         pbin_reverse16(matrix_get_row(row));
124         print("\n");
125     }
126 }
127
128 /* Column pin configuration
129  * col: 0   1   2   3   4   5   6   7
130  * pin: B0  B1  B2  B3  B4  B5  B6  B7
131  */
132 static void  init_cols(void)
133 {
134     // Input with pull-up(DDR:0, PORT:1)
135     DDRB  &= ~0b11111111;
136     PORTB |=  0b11111111;
137 }
138
139 /* Returns status of switches(1:on, 0:off) */
140 static matrix_row_t read_cols(void)
141 {
142     // Invert because PIN indicates 'switch on' with low(0) and 'off' with high(1)
143     return ~PINB;
144 }
145
146 /* Row pin configuration
147  * row: 0   1   2   3   4   5   6   7
148  * pin: D0  D1  D2  D3  D4  D5  D6  C2
149  */
150 static void unselect_rows(void)
151 {
152     // Hi-Z(DDR:0, PORT:0) to unselect
153     DDRD  &= ~0b01111111;
154     PORTD &= ~0b01111111;
155     DDRC  &= ~0b00000100;
156     PORTC &= ~0b00000100;
157 }
158
159
160 static void select_row(uint8_t row)
161 {
162     // Output low(DDR:1, PORT:0) to select
163     switch (row) {
164         case 0:
165             DDRD  |= (1<<0);
166             PORTD &= ~(1<<0);
167             break;
168         case 1:
169             DDRD  |= (1<<1);
170             PORTD &= ~(1<<1);
171             break;
172         case 2:
173             DDRD  |= (1<<2);
174             PORTD &= ~(1<<2);
175             break;
176         case 3:
177             DDRD  |= (1<<3);
178             PORTD &= ~(1<<3);
179             break;
180         case 4:
181             DDRD  |= (1<<4);
182             PORTD &= ~(1<<4);
183             break;
184         case 5:
185             DDRD  |= (1<<5);
186             PORTD &= ~(1<<5);
187             break;
188         case 6:
189             DDRD  |= (1<<6);
190             PORTD &= ~(1<<6);
191             break;
192         case 7:
193             DDRC  |= (1<<2);
194             PORTC &= ~(1<<2);
195             break;
196     }
197 }