]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - converter/ibm4704_usb/matrix.c
c02d9e50bba988a58b416f27bfca4b4470dedcaa
[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
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 static void enable_break(void)
56 {
57     print("Enable break: ");
58     while (ibm4704_send(0xFC)) { _delay_ms(10); }
59     // valid scancode: 00-79h
60     for (uint8_t code = 0; code < 0x7F; code++) {
61         while (ibm4704_send(0x80|code)) _delay_ms(10);
62         _delay_ms(5);   // wait for response
63         // No response(FF) when ok, FD when out of bound
64         xprintf("s%02X:r%02X ", code, ibm4704_recv());
65     }
66     while (ibm4704_send(0xFF)) { _delay_ms(10); } // End
67     print("End\n");
68 }
69
70
71 void matrix_setup(void)
72 {
73     ibm4704_init();
74 }
75
76 void matrix_init(void)
77 {
78     debug_enable = true;
79
80     print("IBM 4704 converter\n");
81     matrix_clear();
82     _delay_ms(2000);    // wait for keyboard starting up
83     xprintf("Keyboard ID: %02X\n", ibm4704_recv());
84     enable_break();
85 }
86
87 /*
88  * IBM 4704 Scan Code
89  */
90 uint8_t matrix_scan(void)
91 {
92     uint8_t code = ibm4704_recv();
93     if (code==0xFF) {
94         // Not receivd
95         return 0;
96     } else if ((code&0x7F) >= 0x7C) {
97         // 0xFF-FC and 0x7F-7C is not scancode
98         xprintf("Error: %02X\n", code);
99         matrix_clear();
100         return 0;
101     } else if (code&0x80) {
102         dprintf("%02X\n", code);
103         matrix_make(code);
104     } else {
105         dprintf("%02X\n", code);
106         matrix_break(code);
107     }
108     return 1;
109 }
110
111 inline
112 uint8_t matrix_get_row(uint8_t row)
113 {
114     return matrix[row];
115 }
116
117 inline
118 static void matrix_make(uint8_t code)
119 {
120     matrix[ROW(code)] |= 1<<COL(code);
121 }
122
123 inline
124 static void matrix_break(uint8_t code)
125 {
126     matrix[ROW(code)] &= ~(1<<COL(code));
127 }
128
129 void matrix_clear(void)
130 {
131     for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
132 }