]> git.gir.st - tmk_keyboard.git/blob - converter/ibm4704_usb/matrix.c
8b9b518dbcd88fc3acd066f75f62e135a163abaf
[tmk_keyboard.git] / converter / ibm4704_usb / matrix.c
1 /*
2 Copyright 2014 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 #include <stdint.h>
19 #include <stdbool.h>
20 #include <avr/io.h>
21 #include <util/delay.h>
22 #include "action.h"
23 #include "print.h"
24 #include "debug.h"
25 #include "util.h"
26 #include "ibm4704.h"
27 #include "matrix.h"
28
29
30 static void matrix_make(uint8_t code);
31 static void matrix_break(uint8_t code);
32
33
34 /*
35 * Matrix Array usage:
36 * IBM 4704 scan codes are assigned into 128(16x8)-cell matrix.
37 *
38 * 8bit wide
39 * +---------+
40 * 0| |
41 * :| XX | 00-7F
42 * f| |
43 * +---------+
44 *
45 * Exceptions:
46 */
47 static uint8_t matrix[MATRIX_ROWS];
48
49 // scan code bits 7654 3210
50 // R:row/C:column -RRR RCCC
51 #define ROW(code) ((code>>3)&0x0f)
52 #define COL(code) (code&0x07)
53
54
55 inline
56 uint8_t matrix_rows(void)
57 {
58 return MATRIX_ROWS;
59 }
60
61 inline
62 uint8_t matrix_cols(void)
63 {
64 return MATRIX_COLS;
65 }
66
67 static void enable_break(void)
68 {
69 print("Enable break: ");
70 while (ibm4704_send(0xFC)) { _delay_ms(10); }
71 // valid scancode: 00-79h
72 for (uint8_t code = 0; code < 0x7F; code++) {
73 while (ibm4704_send(0x80|code)) _delay_ms(10);
74 _delay_ms(5); // wait for response
75 // No response(FF) when ok, FD when out of bound
76 xprintf("s%02X:r%02X ", code, ibm4704_recv());
77 }
78 while (ibm4704_send(0xFF)) { _delay_ms(10); } // End
79 print("End\n");
80 }
81
82
83 void matrix_setup(void)
84 {
85 ibm4704_init();
86 }
87
88 void matrix_init(void)
89 {
90 debug_enable = true;
91
92 print("IBM 4704 converter\n");
93 matrix_clear();
94 _delay_ms(2000); // wait for keyboard starting up
95 xprintf("Keyboard ID: %02X\n", ibm4704_recv());
96 enable_break();
97 }
98
99 /*
100 * IBM 4704 Scan Code
101 */
102 uint8_t matrix_scan(void)
103 {
104 uint8_t code = ibm4704_recv();
105 if (code==0xFF) {
106 // Not receivd
107 return 0;
108 } else if ((code&0x7F) >= 0x7C) {
109 // 0xFF-FC and 0x7F-7C is not scancode
110 xprintf("Error: %02X\n", code);
111 matrix_clear();
112 return 0;
113 } else if (code&0x80) {
114 dprintf("%02X\n", code);
115 matrix_make(code);
116 } else {
117 dprintf("%02X\n", code);
118 matrix_break(code);
119 }
120 return 1;
121 }
122
123 inline
124 bool matrix_is_on(uint8_t row, uint8_t col)
125 {
126 return (matrix[row] & (1<<col));
127 }
128
129 inline
130 uint8_t matrix_get_row(uint8_t row)
131 {
132 return matrix[row];
133 }
134
135 void matrix_print(void)
136 {
137 print("\nr/c 01234567\n");
138 for (uint8_t row = 0; row < matrix_rows(); row++) {
139 xprintf("%02X: %08b\n", row, bitrev(matrix_get_row(row)));
140 }
141 }
142
143
144
145 inline
146 static void matrix_make(uint8_t code)
147 {
148 matrix[ROW(code)] |= 1<<COL(code);
149 }
150
151 inline
152 static void matrix_break(uint8_t code)
153 {
154 matrix[ROW(code)] &= ~(1<<COL(code));
155 }
156
157 void matrix_clear(void)
158 {
159 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
160 }
Imprint / Impressum