]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - macway/matrix.c
Added locking capslock key support and default now.
[max/tmk_keyboard.git] / 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 "util.h"
27 #include "matrix.h"
28
29
30 #if (MATRIX_COLS > 16)
31 #   error "MATRIX_COLS must not exceed 16"
32 #endif
33 #if (MATRIX_ROWS > 255)
34 #   error "MATRIX_ROWS must not exceed 255"
35 #endif
36
37
38 // matrix state buffer(1:on, 0:off)
39 #if (MATRIX_COLS <= 8)
40 static uint8_t *matrix;
41 static uint8_t *matrix_prev;
42 static uint8_t _matrix0[MATRIX_ROWS];
43 static uint8_t _matrix1[MATRIX_ROWS];
44 #else
45 static uint16_t *matrix;
46 static uint16_t *matrix_prev;
47 static uint16_t _matrix0[MATRIX_ROWS];
48 static uint16_t _matrix1[MATRIX_ROWS];
49 #endif
50
51 #ifdef MATRIX_HAS_GHOST
52 static bool matrix_has_ghost_in_row(uint8_t row);
53 #endif
54 static uint8_t read_col(void);
55 static void unselect_rows(void);
56 static void select_row(uint8_t row);
57
58
59 inline
60 uint8_t matrix_rows(void)
61 {
62     return MATRIX_ROWS;
63 }
64
65 inline
66 uint8_t matrix_cols(void)
67 {
68     return MATRIX_COLS;
69 }
70
71 void matrix_init(void)
72 {
73     // initialize row and col
74     unselect_rows();
75     // Input with pull-up(DDR:0, PORT:1)
76     DDRB = 0x00;
77     PORTB = 0xFF;
78
79     // initialize matrix state: all keys off
80     for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
81     for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
82     matrix = _matrix0;
83     matrix_prev = _matrix1;
84 }
85
86 uint8_t matrix_scan(void)
87 {
88     uint8_t *tmp;
89
90     tmp = matrix_prev;
91     matrix_prev = matrix;
92     matrix = tmp;
93
94     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
95         unselect_rows();
96         select_row(i);
97         _delay_us(30);  // without this wait read unstable value.
98         matrix[i] = ~read_col();
99     }
100     unselect_rows();
101     return 1;
102 }
103
104 bool matrix_is_modified(void)
105 {
106     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
107         if (matrix[i] != matrix_prev[i])
108             return true;
109     }
110     return false;
111 }
112
113 inline
114 bool matrix_has_ghost(void)
115 {
116 #ifdef MATRIX_HAS_GHOST
117     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
118         if (matrix_has_ghost_in_row(i))
119             return true;
120     }
121 #endif
122     return false;
123 }
124
125 inline
126 bool matrix_is_on(uint8_t row, uint8_t col)
127 {
128     return (matrix[row] & (1<<col));
129 }
130
131 inline
132 #if (MATRIX_COLS <= 8)
133 uint8_t matrix_get_row(uint8_t row)
134 #else
135 uint16_t matrix_get_row(uint8_t row)
136 #endif
137 {
138     return matrix[row];
139 }
140
141 void matrix_print(void)
142 {
143     print("\nr/c 01234567\n");
144     for (uint8_t row = 0; row < matrix_rows(); row++) {
145         phex(row); print(": ");
146 #if (MATRIX_COLS <= 8)
147         pbin_reverse(matrix_get_row(row));
148 #else
149         pbin_reverse16(matrix_get_row(row));
150 #endif
151 #ifdef MATRIX_HAS_GHOST
152         if (matrix_has_ghost_in_row(row)) {
153             print(" <ghost");
154         }
155 #endif
156         print("\n");
157     }
158 }
159
160 uint8_t matrix_key_count(void)
161 {
162     uint8_t count = 0;
163     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
164 #if (MATRIX_COLS <= 8)
165         count += bitpop(matrix[i]);
166 #else
167         count += bitpop16(matrix[i]);
168 #endif
169     }
170     return count;
171 }
172
173 #ifdef MATRIX_HAS_GHOST
174 inline
175 static bool matrix_has_ghost_in_row(uint8_t row)
176 {
177     // no ghost exists in case less than 2 keys on
178     if (((matrix[row] - 1) & matrix[row]) == 0)
179         return false;
180
181     // ghost exists in case same state as other row
182     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
183         if (i != row && (matrix[i] & matrix[row]) == matrix[row])
184             return true;
185     }
186     return false;
187 }
188 #endif
189
190 inline
191 static uint8_t read_col(void)
192 {
193     return PINB;
194 }
195
196 inline
197 static void unselect_rows(void)
198 {
199     // Hi-Z(DDR:0, PORT:0) to unselect
200     DDRC  &= ~0b11000000; // PC: 7,6
201     PORTC &= ~0b11000000;
202     DDRD  &= ~0b11000111; // PD: 7,6,2,1,0
203     PORTD &= ~0b11000111;
204     DDRF  &= ~0b11000000; // PF: 7,6
205     PORTF &= ~0b11000000;
206 }
207
208 inline
209 static void select_row(uint8_t row)
210 {
211     // Output low(DDR:1, PORT:0) to select
212     // row: 0    1    2    3    4    5    6    7    8
213     // pin: PD0, PC7, PD7, PF6, PD6, PD1, PD2, PC6, PF7
214     switch (row) {
215         case 0:
216             DDRD  |= (1<<0);
217             PORTD &= ~(1<<0);
218             break;
219         case 1:
220             DDRC  |= (1<<7);
221             PORTC &= ~(1<<7);
222             break;
223         case 2:
224             DDRD  |= (1<<7);
225             PORTD &= ~(1<<7);
226             break;
227         case 3:
228             DDRF  |= (1<<6);
229             PORTF &= ~(1<<6);
230             break;
231         case 4:
232             DDRD  |= (1<<6);
233             PORTD &= ~(1<<6);
234             break;
235         case 5:
236             DDRD  |= (1<<1);
237             PORTD &= ~(1<<1);
238             break;
239         case 6:
240             DDRD  |= (1<<2);
241             PORTD &= ~(1<<2);
242             break;
243         case 7:
244             DDRC  |= (1<<6);
245             PORTC &= ~(1<<6);
246             break;
247         case 8:
248             DDRF  |= (1<<7);
249             PORTF &= ~(1<<7);
250             break;
251     }
252 }