aboutsummaryrefslogtreecommitdiff
path: root/converter/ps2_usb/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'converter/ps2_usb/matrix.c')
-rw-r--r--converter/ps2_usb/matrix.c49
1 files changed, 42 insertions, 7 deletions
diff --git a/converter/ps2_usb/matrix.c b/converter/ps2_usb/matrix.c
index 4187ea060..45344c0f7 100644
--- a/converter/ps2_usb/matrix.c
+++ b/converter/ps2_usb/matrix.c
@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
19#include <stdbool.h> 19#include <stdbool.h>
20#include <avr/io.h> 20#include <avr/io.h>
21#include <util/delay.h> 21#include <util/delay.h>
22#include "action.h"
22#include "print.h" 23#include "print.h"
23#include "util.h" 24#include "util.h"
24#include "debug.h" 25#include "debug.h"
@@ -28,6 +29,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
28 29
29static void matrix_make(uint8_t code); 30static void matrix_make(uint8_t code);
30static void matrix_break(uint8_t code); 31static void matrix_break(uint8_t code);
32static void matrix_clear(void);
31#ifdef MATRIX_HAS_GHOST 33#ifdef MATRIX_HAS_GHOST
32static bool matrix_has_ghost_in_row(uint8_t row); 34static bool matrix_has_ghost_in_row(uint8_t row);
33#endif 35#endif
@@ -83,6 +85,7 @@ uint8_t matrix_cols(void)
83 85
84void matrix_init(void) 86void matrix_init(void)
85{ 87{
88 debug_enable = true;
86 ps2_host_init(); 89 ps2_host_init();
87 90
88 // initialize matrix state: all keys off 91 // initialize matrix state: all keys off
@@ -185,8 +188,8 @@ uint8_t matrix_scan(void)
185 matrix_break(PAUSE); 188 matrix_break(PAUSE);
186 } 189 }
187 190
188 uint8_t code; 191 uint8_t code = ps2_host_recv();
189 while ((code = ps2_host_recv())) { 192 if (!ps2_error) {
190 switch (state) { 193 switch (state) {
191 case INIT: 194 case INIT:
192 switch (code) { 195 switch (code) {
@@ -207,11 +210,19 @@ uint8_t matrix_scan(void)
207 matrix_make(PRINT_SCREEN); 210 matrix_make(PRINT_SCREEN);
208 state = INIT; 211 state = INIT;
209 break; 212 break;
213 case 0x00: // Overrun [3]p.25
214 matrix_clear();
215 clear_keyboard();
216 print("Overrun\n");
217 state = INIT;
218 break;
210 default: // normal key make 219 default: // normal key make
211 if (code < 0x80) { 220 if (code < 0x80) {
212 matrix_make(code); 221 matrix_make(code);
213 } else { 222 } else {
214 debug("unexpected scan code at INIT: "); debug_hex(code); debug("\n"); 223 matrix_clear();
224 clear_keyboard();
225 xprintf("unexpected scan code at INIT: %02X\n", code);
215 } 226 }
216 state = INIT; 227 state = INIT;
217 } 228 }
@@ -232,7 +243,9 @@ uint8_t matrix_scan(void)
232 if (code < 0x80) { 243 if (code < 0x80) {
233 matrix_make(code|0x80); 244 matrix_make(code|0x80);
234 } else { 245 } else {
235 debug("unexpected scan code at E0: "); debug_hex(code); debug("\n"); 246 matrix_clear();
247 clear_keyboard();
248 xprintf("unexpected scan code at E0: %02X\n", code);
236 } 249 }
237 state = INIT; 250 state = INIT;
238 } 251 }
@@ -247,11 +260,18 @@ uint8_t matrix_scan(void)
247 matrix_break(PRINT_SCREEN); 260 matrix_break(PRINT_SCREEN);
248 state = INIT; 261 state = INIT;
249 break; 262 break;
263 case 0xF0:
264 matrix_clear();
265 clear_keyboard();
266 xprintf("unexpected scan code at F0: F0(clear and cont.)\n");
267 break;
250 default: 268 default:
251 if (code < 0x80) { 269 if (code < 0x80) {
252 matrix_break(code); 270 matrix_break(code);
253 } else { 271 } else {
254 debug("unexpected scan code at F0: "); debug_hex(code); debug("\n"); 272 matrix_clear();
273 clear_keyboard();
274 xprintf("unexpected scan code at F0: %02X\n", code);
255 } 275 }
256 state = INIT; 276 state = INIT;
257 } 277 }
@@ -266,7 +286,9 @@ uint8_t matrix_scan(void)
266 if (code < 0x80) { 286 if (code < 0x80) {
267 matrix_break(code|0x80); 287 matrix_break(code|0x80);
268 } else { 288 } else {
269 debug("unexpected scan code at E0_F0: "); debug_hex(code); debug("\n"); 289 matrix_clear();
290 clear_keyboard();
291 xprintf("unexpected scan code at E0_F0: %02X\n", code);
270 } 292 }
271 state = INIT; 293 state = INIT;
272 } 294 }
@@ -357,8 +379,15 @@ uint8_t matrix_scan(void)
357 default: 379 default:
358 state = INIT; 380 state = INIT;
359 } 381 }
360 phex(code);
361 } 382 }
383
384 // TODO: request RESEND when error occurs?
385/*
386 if (PS2_IS_FAILED(ps2_error)) {
387 uint8_t ret = ps2_host_send(PS2_RESEND);
388 xprintf("Resend: %02X\n", ret);
389 }
390*/
362 return 1; 391 return 1;
363} 392}
364 393
@@ -450,3 +479,9 @@ static void matrix_break(uint8_t code)
450 is_modified = true; 479 is_modified = true;
451 } 480 }
452} 481}
482
483inline
484static void matrix_clear(void)
485{
486 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
487}