]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - converter/ibm4704_usb/matrix.c
857dea0f9194dadc25fca4fb8b1d2e0d41652650
[max/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 static void matrix_clear(void);
33
34
35 /*
36  * Matrix Array usage:
37  * IBM 4704 scan codes are assigned into 128(16x8)-cell matrix.
38  *
39  *    8bit wide
40  *   +---------+
41  *  0|         |
42  *  :|   XX    | 00-7F
43  *  f|         |
44  *   +---------+
45  *
46  * Exceptions:
47  */
48 static uint8_t matrix[MATRIX_ROWS];
49
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)
54
55
56 inline
57 uint8_t matrix_rows(void)
58 {
59     return MATRIX_ROWS;
60 }
61
62 inline
63 uint8_t matrix_cols(void)
64 {
65     return MATRIX_COLS;
66 }
67
68 static void enable_break(void)
69 {
70     uint8_t ret;
71     print("Enable break: ");
72     // valid scancode: 00-79h
73     for (uint8_t code = 0; code < 0x7A; code++) {
74         while (ibm4704_send(0x80|code)) _delay_ms(1);
75         // get none when ok, get FD when out of bound
76         _delay_ms(5);
77         if ((ret = ibm4704_recv()) != 0xff) {
78             xprintf("c%02X:r%02X ", code, ret);
79         }
80         _delay_ms(1);
81     }
82     _delay_us(1000);
83     while (ibm4704_send(0xFF)) { _delay_ms(1); } // End
84     print("End\n");
85 }
86
87 void matrix_init(void)
88 {
89     debug_enable = false;
90
91     ibm4704_init();
92     matrix_clear();
93
94     _delay_ms(2000);    // wait for starting up debug console
95
96     print("IBM 4704 converter\n");
97     while (ibm4704_send(0xFE)) _delay_ms(1);    // resend
98     _delay_ms(5);
99     xprintf("Keyboard ID: %02X\n", ibm4704_recv());
100     enable_break();
101 }
102
103 /*
104  * IBM 4704 Scan Code
105  */
106 uint8_t matrix_scan(void)
107 {
108     uint8_t code = ibm4704_recv();
109     if (code==0xFF) {
110         // Not receivd
111         return 0;
112     } else if ((code&0x7F) >= 0x7A) {
113         // 0xFF-FA and 0x7F-7A is not scancode
114         xprintf("Error: %02X\n", code);
115         matrix_clear();
116         return 0;
117     } else if (code&0x80) {
118         dprintf("%02X\n", code);
119         matrix_make(code);
120     } else {
121         dprintf("%02X\n", code);
122         matrix_break(code);
123     }
124     return 1;
125 }
126
127 inline
128 bool matrix_is_on(uint8_t row, uint8_t col)
129 {
130     return (matrix[row] & (1<<col));
131 }
132
133 inline
134 uint8_t matrix_get_row(uint8_t row)
135 {
136     return matrix[row];
137 }
138
139 void matrix_print(void)
140 {
141     print("\nr/c 01234567\n");
142     for (uint8_t row = 0; row < matrix_rows(); row++) {
143         xprintf("%02X: %08b\n", row, bitrev(matrix_get_row(row)));
144     }
145 }
146
147
148
149 inline
150 static void matrix_make(uint8_t code)
151 {
152     matrix[ROW(code)] |= 1<<COL(code);
153 }
154
155 inline
156 static void matrix_break(uint8_t code)
157 {
158     matrix[ROW(code)] &= ~(1<<COL(code));
159 }
160
161 inline
162 static void matrix_clear(void)
163 {
164     for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
165 }