]>
git.gir.st - tmk_keyboard.git/blob - adb_usb/matrix.c
2 Copyright 2011 Jun Wako <wakojun@gmail.com>
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.
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.
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/>.
24 #include <util/delay.h>
34 #if (MATRIX_COLS > 16)
35 # error "MATRIX_COLS must not exceed 16"
37 #if (MATRIX_ROWS > 255)
38 # error "MATRIX_ROWS must not exceed 255"
42 #define CAPS_UP (CAPS | 0x80)
43 #define ROW(key) ((key)>>3&0x0F)
44 #define COL(key) ((key)&0x07)
47 static bool _matrix_is_modified
= false;
49 // matrix state buffer(1:on, 0:off)
50 #if (MATRIX_COLS <= 8)
51 static uint8_t *matrix
;
52 static uint8_t _matrix0
[MATRIX_ROWS
];
54 static uint16_t *matrix
;
55 static uint16_t _matrix0
[MATRIX_ROWS
];
58 #ifdef MATRIX_HAS_GHOST
59 static bool matrix_has_ghost_in_row(uint8_t row
);
61 static void _register_key(uint8_t key
);
65 uint8_t matrix_rows(void)
71 uint8_t matrix_cols(void)
76 void matrix_init(void)
80 // initialize matrix state: all keys off
81 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) _matrix0
[i
] = 0x00;
87 debug_keyboard
= true;
89 print("debug enabled.\n");
93 uint8_t matrix_scan(void)
98 _matrix_is_modified
= false;
99 codes
= adb_host_kbd_recv();
103 #ifdef MATRIX_HAS_LOCKING_CAPS
104 // Send Caps key up event
105 if (matrix_is_on(ROW(CAPS
), COL(CAPS
))) {
106 _matrix_is_modified
= true;
107 _register_key(CAPS_UP
);
110 if (codes
== 0) { // no keys
112 } else if (key0
== 0xFF && key1
!= 0xFF) { // error
115 #ifdef MATRIX_HAS_LOCKING_CAPS
116 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK
)) {
117 // Ignore LockingCaps key down event when CAPS LOCK is on
118 if (key0
== CAPS
&& (key1
== CAPS
|| key1
== 0xFF)) return 0;
119 if (key0
== CAPS
) key0
= key1
;
120 if (key1
== CAPS
) key1
= 0xFF;
121 // Convert LockingCaps key up event into down event
122 if (key0
== CAPS_UP
) key0
= CAPS
;
123 if (key1
== CAPS_UP
) key1
= CAPS
;
126 // Ignore LockingCaps key up event when CAPS LOCK is off
127 if (key0
== CAPS_UP
&& (key1
== CAPS_UP
|| key1
== 0xFF)) return 0;
128 if (key0
== CAPS_UP
) key0
= key1
;
129 if (key1
== CAPS_UP
) key1
= 0xFF;
132 _matrix_is_modified
= true;
134 if (key1
!= 0xFF) // key1 is 0xFF when no second key.
139 print("adb_host_kbd_recv: "); phex16(codes
); print("\n");
144 bool matrix_is_modified(void)
146 return _matrix_is_modified
;
150 bool matrix_has_ghost(void)
152 #ifdef MATRIX_HAS_GHOST
153 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
154 if (matrix_has_ghost_in_row(i
))
162 bool matrix_is_on(uint8_t row
, uint8_t col
)
164 return (matrix
[row
] & (1<<col
));
168 #if (MATRIX_COLS <= 8)
169 uint8_t matrix_get_row(uint8_t row
)
171 uint16_t matrix_get_row(uint8_t row
)
177 void matrix_print(void)
179 #if (MATRIX_COLS <= 8)
180 print("\nr/c 01234567\n");
182 print("\nr/c 0123456789ABCDEF\n");
184 for (uint8_t row
= 0; row
< matrix_rows(); row
++) {
185 phex(row
); print(": ");
186 #if (MATRIX_COLS <= 8)
187 pbin_reverse(matrix_get_row(row
));
189 pbin_reverse16(matrix_get_row(row
));
191 #ifdef MATRIX_HAS_GHOST
192 if (matrix_has_ghost_in_row(row
)) {
200 uint8_t matrix_key_count(void)
203 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
204 #if (MATRIX_COLS <= 8)
205 count
+= bitpop(matrix
[i
]);
207 count
+= bitpop16(matrix
[i
]);
213 #ifdef MATRIX_HAS_GHOST
215 static bool matrix_has_ghost_in_row(uint8_t row
)
217 // no ghost exists in case less than 2 keys on
218 if (((matrix
[row
] - 1) & matrix
[row
]) == 0)
221 // ghost exists in case same state as other row
222 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) {
223 if (i
!= row
&& (matrix
[i
] & matrix
[row
]) == matrix
[row
])
231 static void _register_key(uint8_t key
)
237 matrix
[row
] &= ~(1<<col
);
239 matrix
[row
] |= (1<<col
);