]> git.donarmstrong.com Git - tmk_firmware.git/blob - converter/pc98_usb/matrix.c
Rename file layer_switch to action_layer
[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 void matrix_init(void)
95 {
96     PC98_RST_DDR |= (1<<PC98_RST_BIT);
97     PC98_RDY_DDR |= (1<<PC98_RDY_BIT);
98     PC98_RTY_DDR |= (1<<PC98_RTY_BIT);
99     PC98_RST_PORT |= (1<<PC98_RST_BIT);
100     PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
101     PC98_RTY_PORT |= (1<<PC98_RTY_BIT);
102
103
104     serial_init();
105
106     // PC98 reset
107 /*
108     PC98_RST_PORT &= ~(1<<PC98_RST_BIT);
109     _delay_us(15);
110     PC98_RST_PORT |= (1<<PC98_RST_BIT);
111     _delay_us(13);
112     PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
113 */
114
115     _delay_ms(500);
116     pc98_inhibit_repeat();
117
118
119     // PC98 ready
120     PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
121
122     // initialize matrix state: all keys off
123     for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
124
125     debug("init\n");
126     return;
127 }
128
129 uint8_t matrix_scan(void)
130 {
131     is_modified = false;
132
133     uint16_t code;
134     PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
135     _delay_us(30);
136     code = serial_recv2();
137     PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
138     if (code == -1) return 0;
139
140 if (code == 0x60) {
141     pc98_inhibit_repeat();
142
143 /*
144     PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
145     _delay_ms(100);
146     serial_send(0x96);
147     PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
148 */
149
150     return 0;
151 }
152
153     print_hex8(code); print(" ");
154
155     if (code&0x80) {
156         // break code
157         if (matrix_is_on(ROW(code), COL(code))) {
158             matrix[ROW(code)] &= ~(1<<COL(code));
159             is_modified = true;
160         }
161     } else {
162         // make code
163         if (!matrix_is_on(ROW(code), COL(code))) {
164             matrix[ROW(code)] |=  (1<<COL(code));
165             is_modified = true;
166         }
167     }
168     return code;
169 }
170
171 bool matrix_is_modified(void)
172 {
173     return is_modified;
174 }
175
176 inline
177 bool matrix_has_ghost(void)
178 {
179     return false;
180 }
181
182 inline
183 bool matrix_is_on(uint8_t row, uint8_t col)
184 {
185     return (matrix[row] & (1<<col));
186 }
187
188 inline
189 uint8_t matrix_get_row(uint8_t row)
190 {
191     return matrix[row];
192 }
193
194 void matrix_print(void)
195 {
196     print("\nr/c 01234567\n");
197     for (uint8_t row = 0; row < matrix_rows(); row++) {
198         phex(row); print(": ");
199         pbin_reverse(matrix_get_row(row));
200         print("\n");
201     }
202 }
203
204 uint8_t matrix_key_count(void)
205 {
206     uint8_t count = 0;
207     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
208         count += bitpop(matrix[i]);
209     }
210     return count;
211 }