]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - converter/pc98_usb/matrix.c
54bb30414d7b458859ecf21f7d137386f0c2815a
[max/tmk_keyboard.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 "led.h"
26 #include "debug.h"
27 #include "protocol/serial.h"
28
29
30 /*
31  * Matrix Array usage:
32  *
33  * ROW: 16(4bits)
34  * COL:  8(3bits)
35  *
36  *    8bit wide
37  *   +---------+
38  *  0|00 ... 07|
39  *  1|08 ... 0F|
40  *  :|   ...   |
41  *  :|   ...   |
42  *  E|70 ... 77|
43  *  F|78 ... 7F|
44  *   +---------+
45  */
46 static uint8_t matrix[MATRIX_ROWS];
47 #define ROW(code)      ((code>>3)&0xF)
48 #define COL(code)      (code&0x07)
49
50
51 static void pc98_send(uint8_t data)
52 {
53     PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
54     _delay_ms(1);
55     serial_send(data);
56     _delay_ms(1);
57     PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
58 }
59
60 static int16_t pc98_wait_response(void)
61 {
62     int16_t code = -1;
63     uint8_t timeout = 255;
64     while (timeout-- && (code = serial_recv2()) == -1) _delay_ms(1);
65     return code;
66 }
67
68 static void pc98_inhibit_repeat(void)
69 {
70     uint16_t code;
71
72     // clear recv buffer
73     while (serial_recv()) ;
74 RETRY:
75     _delay_ms(100);
76     pc98_send(0x9C);
77     code = pc98_wait_response();
78     if (code != -1) xprintf("PC98: send 9C: %02X\n", code);
79     if (code != 0xFA) goto RETRY;
80
81     _delay_ms(100);
82     pc98_send(0x70);
83     code = pc98_wait_response();
84     if (code != -1) xprintf("PC98: send 70: %02X\n", code);
85     if (code != 0xFA) goto RETRY;
86 }
87
88 void matrix_init(void)
89 {
90     debug_keyboard = true;
91     PC98_RST_DDR |= (1<<PC98_RST_BIT);
92     PC98_RDY_DDR |= (1<<PC98_RDY_BIT);
93     PC98_RTY_DDR |= (1<<PC98_RTY_BIT);
94     PC98_RST_PORT |= (1<<PC98_RST_BIT);
95     PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
96     PC98_RTY_PORT |= (1<<PC98_RTY_BIT);
97
98
99     serial_init();
100
101     // PC98 reset
102 /*
103     PC98_RST_PORT &= ~(1<<PC98_RST_BIT);
104     _delay_us(15);
105     PC98_RST_PORT |= (1<<PC98_RST_BIT);
106     _delay_us(13);
107     PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
108 */
109
110     _delay_ms(500);
111     pc98_inhibit_repeat();
112
113
114     // PC98 ready
115     PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
116
117     // initialize matrix state: all keys off
118     for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
119
120     debug("init\n");
121     return;
122 }
123
124 uint8_t matrix_scan(void)
125 {
126     uint16_t code;
127     PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
128     //_delay_us(30);
129     code = serial_recv2();
130     PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
131     if (code == -1) return 0;
132
133     print_hex8(code); print(" ");
134
135     if (code&0x80) {
136         // break code
137         if (matrix_is_on(ROW(code), COL(code))) {
138             matrix[ROW(code)] &= ~(1<<COL(code));
139         }
140     } else {
141         // make code
142         if (!matrix_is_on(ROW(code), COL(code))) {
143             matrix[ROW(code)] |=  (1<<COL(code));
144         }
145     }
146     return code;
147 }
148
149 inline
150 uint8_t matrix_get_row(uint8_t row)
151 {
152     return matrix[row];
153 }
154
155 void led_set(uint8_t usb_led)
156 {
157 #ifdef PC98_LED_CONTROL
158     uint8_t led_state = 0x70;
159     if (usb_led & (1<<USB_LED_NUM_LOCK))    led_state |= (1<<0);
160     if (usb_led & (1<<USB_LED_CAPS_LOCK))   led_state |= (1<<2);
161     xprintf("led_set: %02X\n", led_state);
162
163     pc98_send(0x9D);
164     _delay_ms(100);
165     pc98_send(led_state);
166     // responses(FA or FC) will be ignored in matrix_scan()
167 #endif
168 }