]> git.donarmstrong.com Git - tmk_firmware.git/blob - converter/pc98_usb/matrix.c
18ad5bfe7c3e28a1823550542358e1d006ff5219
[tmk_firmware.git] / converter / pc98_usb / matrix.c
1 /*
2 Copyright 2012 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 "print.h"
23 #include "util.h"
24 #include "matrix.h"
25 #include "debug.h"
26 #include "protocol/serial.h"
27
28
29 /*
30  * Matrix Array usage:
31  *
32  * ROW: 16(4bits)
33  * COL:  8(3bits)
34  *
35  *    8bit wide
36  *   +---------+
37  *  0|00 ... 07|
38  *  1|08 ... 0F|
39  *  :|   ...   |
40  *  :|   ...   |
41  *  E|70 ... 77|
42  *  F|78 ... 7F|
43  *   +---------+
44  */
45 static uint8_t matrix[MATRIX_ROWS];
46 #define ROW(code)      ((code>>3)&0xF)
47 #define COL(code)      (code&0x07)
48
49 static bool is_modified = false;
50
51
52 inline
53 uint8_t matrix_rows(void)
54 {
55     return MATRIX_ROWS;
56 }
57
58 inline
59 uint8_t matrix_cols(void)
60 {
61     return MATRIX_COLS;
62 }
63
64 static void pc98_inhibit_repeat(void)
65 {
66     uint8_t code;
67
68     while (serial_recv()) ;
69 RETRY:
70     PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
71     _delay_ms(500);
72     serial_send(0x9C);
73
74     PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
75     _delay_ms(100);
76     while (!(code = serial_recv())) ;
77     print("PC98: send 9C: "); print_hex8(code); print("\n");
78     if (code != 0xFA) goto RETRY;
79
80
81
82     PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
83     _delay_ms(100);
84     serial_send(0x70);
85
86     PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
87     _delay_ms(100);
88     //code = serial_recv();
89     while (!(code = serial_recv())) ;
90     print("PC98: send 70: "); print_hex8(code); print("\n");
91     if (code != 0xFA) goto RETRY;
92
93 }
94
95 void matrix_init(void)
96 {
97     print_enable = true;
98 //    debug_enable = true;
99 //    debug_matrix = true;
100
101     PC98_RST_DDR |= (1<<PC98_RST_BIT);
102     PC98_RDY_DDR |= (1<<PC98_RDY_BIT);
103     PC98_RTY_DDR |= (1<<PC98_RTY_BIT);
104     PC98_RST_PORT |= (1<<PC98_RST_BIT);
105     PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
106     PC98_RTY_PORT |= (1<<PC98_RTY_BIT);
107
108
109     serial_init();
110
111     // PC98 reset
112 /*
113     PC98_RST_PORT &= ~(1<<PC98_RST_BIT);
114     _delay_us(15);
115     PC98_RST_PORT |= (1<<PC98_RST_BIT);
116     _delay_us(13);
117     PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
118 */
119
120     _delay_ms(500);
121     pc98_inhibit_repeat();
122
123
124     // PC98 ready
125     PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
126
127     // initialize matrix state: all keys off
128     for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
129
130     debug("init\n");
131     return;
132 }
133
134 uint8_t matrix_scan(void)
135 {
136     is_modified = false;
137
138     uint16_t code;
139     PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
140     _delay_us(30);
141     code = serial_recv2();
142     PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
143     if (code == -1) return 0;
144
145 if (code == 0x60) {
146     pc98_inhibit_repeat();
147
148 /*
149     PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
150     _delay_ms(100);
151     serial_send(0x96);
152     PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
153 */
154
155     return 0;
156 }
157
158     print_hex8(code); print(" ");
159
160     if (code&0x80) {
161         // break code
162         if (matrix_is_on(ROW(code), COL(code))) {
163             matrix[ROW(code)] &= ~(1<<COL(code));
164             is_modified = true;
165         }
166     } else {
167         // make code
168         if (!matrix_is_on(ROW(code), COL(code))) {
169             matrix[ROW(code)] |=  (1<<COL(code));
170             is_modified = true;
171         }
172     }
173     return code;
174 }
175
176 bool matrix_is_modified(void)
177 {
178     return is_modified;
179 }
180
181 inline
182 bool matrix_has_ghost(void)
183 {
184     return false;
185 }
186
187 inline
188 bool matrix_is_on(uint8_t row, uint8_t col)
189 {
190     return (matrix[row] & (1<<col));
191 }
192
193 inline
194 uint8_t matrix_get_row(uint8_t row)
195 {
196     return matrix[row];
197 }
198
199 void matrix_print(void)
200 {
201     print("\nr/c 01234567\n");
202     for (uint8_t row = 0; row < matrix_rows(); row++) {
203         phex(row); print(": ");
204         pbin_reverse(matrix_get_row(row));
205         print("\n");
206     }
207 }
208
209 uint8_t matrix_key_count(void)
210 {
211     uint8_t count = 0;
212     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
213         count += bitpop(matrix[i]);
214     }
215     return count;
216 }