]> git.donarmstrong.com Git - tmk_firmware.git/blob - keyboard/macway/matrix.c
Remove MCU dependent code from common/keyboard.c
[tmk_firmware.git] / keyboard / macway / matrix.c
1 /*
2 Copyright 2011 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 #ifdef MATRIX_HAS_GHOST
41 static bool matrix_has_ghost_in_row(uint8_t row);
42 #endif
43 static matrix_row_t read_cols(void);
44 static void unselect_rows(void);
45 static void select_row(uint8_t row);
46
47
48 inline
49 uint8_t matrix_rows(void)
50 {
51     return MATRIX_ROWS;
52 }
53
54 inline
55 uint8_t matrix_cols(void)
56 {
57     return MATRIX_COLS;
58 }
59
60 void matrix_init(void)
61 {
62     // initialize row and col
63     unselect_rows();
64     // Input with pull-up(DDR:0, PORT:1)
65     DDRB = 0x00;
66     PORTB = 0xFF;
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
102     return 1;
103 }
104
105 bool matrix_is_modified(void)
106 {
107     if (debouncing) return false;
108     return true;
109 }
110
111 inline
112 bool matrix_is_on(uint8_t row, uint8_t col)
113 {
114     return (matrix[row] & ((matrix_row_t)1<<col));
115 }
116
117 inline
118 matrix_row_t matrix_get_row(uint8_t row)
119 {
120     return matrix[row];
121 }
122
123 void matrix_print(void)
124 {
125     print("\nr/c 01234567\n");
126     for (uint8_t row = 0; row < matrix_rows(); row++) {
127         phex(row); print(": ");
128         pbin_reverse(matrix_get_row(row));
129 #ifdef MATRIX_HAS_GHOST
130         if (matrix_has_ghost_in_row(row)) {
131             print(" <ghost");
132         }
133 #endif
134         print("\n");
135     }
136 }
137
138 #ifdef MATRIX_HAS_GHOST
139 inline
140 static bool matrix_has_ghost_in_row(uint8_t row)
141 {
142     // no ghost exists in case less than 2 keys on
143     if (((matrix[row] - 1) & matrix[row]) == 0)
144         return false;
145
146     // ghost exists in case same state as other row
147     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
148         if (i != row && (matrix[i] & matrix[row]))
149             return true;
150     }
151     return false;
152 }
153 #endif
154
155 inline
156 static matrix_row_t read_cols(void)
157 {
158     return ~PINB;
159 }
160
161 inline
162 static void unselect_rows(void)
163 {
164     // Hi-Z(DDR:0, PORT:0) to unselect
165     DDRC  &= ~0b01000000; // PC: 6
166     PORTC &= ~0b01000000;
167     DDRD  &= ~0b11100111; // PD: 7,6,5,2,1,0
168     PORTD &= ~0b11100111;
169     DDRF  &= ~0b11000000; // PF: 7,6
170     PORTF &= ~0b11000000;
171 }
172
173 inline
174 static void select_row(uint8_t row)
175 {
176     // Output low(DDR:1, PORT:0) to select
177     // row: 0    1    2    3    4    5    6    7    8
178     // pin: PD0, PD5, PD7, PF6, PD6, PD1, PD2, PC6, PF7
179     switch (row) {
180         case 0:
181             DDRD  |= (1<<0);
182             PORTD &= ~(1<<0);
183             break;
184         case 1:
185             DDRD  |= (1<<5);
186             PORTD &= ~(1<<5);
187             break;
188         case 2:
189             DDRD  |= (1<<7);
190             PORTD &= ~(1<<7);
191             break;
192         case 3:
193             DDRF  |= (1<<6);
194             PORTF &= ~(1<<6);
195             break;
196         case 4:
197             DDRD  |= (1<<6);
198             PORTD &= ~(1<<6);
199             break;
200         case 5:
201             DDRD  |= (1<<1);
202             PORTD &= ~(1<<1);
203             break;
204         case 6:
205             DDRD  |= (1<<2);
206             PORTD &= ~(1<<2);
207             break;
208         case 7:
209             DDRC  |= (1<<6);
210             PORTC &= ~(1<<6);
211             break;
212         case 8:
213             DDRF  |= (1<<7);
214             PORTF &= ~(1<<7);
215             break;
216     }
217 }