Quick Fix: read scan code from PC98
[tmk_keyboard.git] / converter / pc98_usb / matrix.c
CommitLineData
f68c5bf0 1/*
2Copyright 2012 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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 */
45static uint8_t matrix[MATRIX_ROWS];
46#define ROW(code) ((code>>3)&0xF)
47#define COL(code) (code&0x07)
48
49static bool is_modified = false;
50
51
52inline
53uint8_t matrix_rows(void)
54{
55 return MATRIX_ROWS;
56}
57
58inline
59uint8_t matrix_cols(void)
60{
61 return MATRIX_COLS;
62}
63
64void matrix_init(void)
65{
66 print_enable = true;
67 debug_enable = true;
68
69 PC98_RST_DDR |= (1<<PC98_RST_BIT);
70 PC98_RDY_DDR |= (1<<PC98_RDY_BIT);
71 PC98_RTY_DDR |= (1<<PC98_RTY_BIT);
72 PC98_RST_PORT |= (1<<PC98_RST_BIT);
73 PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
74 PC98_RTY_PORT |= (1<<PC98_RTY_BIT);
75
eb776c1b 76 DDRD |= 1<<7;
77
f68c5bf0 78
79 serial_init();
80
81 // PC98 reset
82 PC98_RST_PORT &= ~(1<<PC98_RST_BIT);
83 _delay_us(15);
84 PC98_RST_PORT |= (1<<PC98_RST_BIT);
85 _delay_us(13);
86 PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
87
eb776c1b 88 // PC98 ready
89 PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
90
f68c5bf0 91 // initialize matrix state: all keys off
92 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
93
94 debug("init\n");
95 return;
96}
97
98uint8_t matrix_scan(void)
99{
100 is_modified = false;
101
102 uint8_t code;
103 PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
eb776c1b 104 _delay_us(30);
f68c5bf0 105 code = serial_recv();
106 PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
107 if (!code) return 0;
108
109 debug_hex(code); debug(" ");
110
111/*
112 switch (code) {
113 case 0x7E: // reset fail
114 case 0xFE: // layout
115 case 0xFF: // reset success
116 _delay_ms(500);
117 // ignore response byte
118 debug("(response ignored:");
119 while ((code = serial_recv())) { debug(" "); debug_hex(code); }
120 debug(") ");
121 // FALL THROUGH
122 case 0x7F:
123 // all keys up
124 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
125 return 0;
126 }
127
128 if (code&0x80) {
129 // break code
130 if (matrix_is_on(ROW(code), COL(code))) {
131 matrix[ROW(code)] &= ~(1<<COL(code));
132 is_modified = true;
133 }
134 } else {
135 // make code
136 if (!matrix_is_on(ROW(code), COL(code))) {
137 matrix[ROW(code)] |= (1<<COL(code));
138 is_modified = true;
139 }
140 }
141*/
142 return code;
143}
144
145bool matrix_is_modified(void)
146{
147 return is_modified;
148}
149
150inline
151bool matrix_has_ghost(void)
152{
153 return false;
154}
155
156inline
157bool matrix_is_on(uint8_t row, uint8_t col)
158{
159 return (matrix[row] & (1<<col));
160}
161
162inline
163uint8_t matrix_get_row(uint8_t row)
164{
165 return matrix[row];
166}
167
168void matrix_print(void)
169{
170 print("\nr/c 01234567\n");
171 for (uint8_t row = 0; row < matrix_rows(); row++) {
172 phex(row); print(": ");
173 pbin_reverse(matrix_get_row(row));
174 print("\n");
175 }
176}
177
178uint8_t matrix_key_count(void)
179{
180 uint8_t count = 0;
181 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
182 count += bitpop(matrix[i]);
183 }
184 return count;
185}
Imprint / Impressum