]>
git.gir.st - tmk_keyboard.git/blob - converter/adb_usb/matrix.c
ee17f655f2ddff1dc289f085fafe588404aa74d5
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>
32 #if (MATRIX_COLS > 16)
33 # error "MATRIX_COLS must not exceed 16"
35 #if (MATRIX_ROWS > 255)
36 # error "MATRIX_ROWS must not exceed 255"
40 static bool is_modified
= false;
42 // matrix state buffer(1:on, 0:off)
43 #if (MATRIX_COLS <= 8)
44 static uint8_t matrix
[MATRIX_ROWS
];
46 static uint16_t matrix
[MATRIX_ROWS
];
49 #ifdef MATRIX_HAS_GHOST
50 static bool matrix_has_ghost_in_row(uint8_t row
);
52 static void register_key(uint8_t key
);
56 uint8_t matrix_rows(void)
62 uint8_t matrix_cols(void)
67 void matrix_init(void)
71 // initialize matrix state: all keys off
72 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) matrix
[i
] = 0x00;
76 debug_keyboard
= true;
78 print("debug enabled.\n");
82 uint8_t matrix_scan(void)
88 codes
= adb_host_kbd_recv();
92 if (debug_matrix
&& codes
) {
93 print("adb_host_kbd_recv: "); phex16(codes
); print("\n");
96 if (codes
== 0) { // no keys
98 } else if (codes
== 0x7F7F) { // power key press
100 } else if (codes
== 0xFFFF) { // power key release
102 } else if (key0
== 0xFF) { // error
103 xprintf("adb_host_kbd_recv: ERROR(%02X)\n", codes
);
107 if (key1
!= 0xFF) // key1 is 0xFF when no second key.
114 bool matrix_is_modified(void)
120 bool matrix_has_ghost(void)
122 #ifdef MATRIX_HAS_GHOST
123 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
124 if (matrix_has_ghost_in_row(i
))
132 bool matrix_is_on(uint8_t row
, uint8_t col
)
134 return (matrix
[row
] & (1<<col
));
138 #if (MATRIX_COLS <= 8)
139 uint8_t matrix_get_row(uint8_t row
)
141 uint16_t matrix_get_row(uint8_t row
)
147 void matrix_print(void)
149 if (!debug_matrix
) return;
150 #if (MATRIX_COLS <= 8)
151 print("r/c 01234567\n");
153 print("r/c 0123456789ABCDEF\n");
155 for (uint8_t row
= 0; row
< matrix_rows(); row
++) {
156 phex(row
); print(": ");
157 #if (MATRIX_COLS <= 8)
158 pbin_reverse(matrix_get_row(row
));
160 pbin_reverse16(matrix_get_row(row
));
162 #ifdef MATRIX_HAS_GHOST
163 if (matrix_has_ghost_in_row(row
)) {
171 uint8_t matrix_key_count(void)
174 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
175 #if (MATRIX_COLS <= 8)
176 count
+= bitpop(matrix
[i
]);
178 count
+= bitpop16(matrix
[i
]);
184 #ifdef MATRIX_HAS_GHOST
186 static bool matrix_has_ghost_in_row(uint8_t row
)
188 // no ghost exists in case less than 2 keys on
189 if (((matrix
[row
] - 1) & matrix
[row
]) == 0)
192 // ghost exists in case same state as other row
193 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) {
194 if (i
!= row
&& (matrix
[i
] & matrix
[row
]) == matrix
[row
])
202 static void register_key(uint8_t key
)
208 matrix
[row
] &= ~(1<<col
);
210 matrix
[row
] |= (1<<col
);