2 Copyright 2014 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/>.
21 #include <util/delay.h>
30 static void matrix_make(uint8_t code);
31 static void matrix_break(uint8_t code);
32 static void matrix_clear(void);
37 * IBM 4704 scan codes are assigned into 128(16x8)-cell matrix.
48 static uint8_t matrix[MATRIX_ROWS];
50 // scan code bits 7654 3210
51 // R:row/C:column -RRR RCCC
52 #define ROW(code) ((code>>3)&0x0f)
53 #define COL(code) (code&0x07)
57 uint8_t matrix_rows(void)
63 uint8_t matrix_cols(void)
68 static void enable_break(void)
71 for (uint8_t code = 0; code < 0x80; code++) {
72 while (ibm4704_send(0x80|code) != 0) {
78 xprintf("%0X: %0X ", code, ret);
83 while (ibm4704_send(0xFF) != 0) {
88 void matrix_init(void)
97 //while (ibm4704_send(0x8C) != 0) ;
98 //while (ibm4704_send(0xFF) != 0) ;
104 uint8_t matrix_scan(void)
106 uint8_t code = ibm4704_recv();
110 } else if ((code&0x78)==0x78) {
111 // 0xFF-F8 and 0x7F-78 is not scan code
112 dprintf("Error: %0X\n", code);
115 } else if (code&0x80) {
124 bool matrix_has_ghost(void)
130 bool matrix_is_on(uint8_t row, uint8_t col)
132 return (matrix[row] & (1<<col));
136 uint8_t matrix_get_row(uint8_t row)
141 void matrix_print(void)
143 print("\nr/c 01234567\n");
144 for (uint8_t row = 0; row < matrix_rows(); row++) {
145 // TODO: use new function
146 phex(row); print(": ");
147 pbin_reverse(matrix_get_row(row));
152 uint8_t matrix_key_count(void)
155 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
156 count += bitpop(matrix[i]);
164 static void matrix_make(uint8_t code)
166 matrix[ROW(code)] |= 1<<COL(code);
170 static void matrix_break(uint8_t code)
172 matrix[ROW(code)] &= ~(1<<COL(code));
176 static void matrix_clear(void)
178 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;