Browse Source

To prevent key stuck clear matrix array when ADB error occurs.

led_matrix
tmk 11 years ago
parent
commit
d8ce19abd0
2 changed files with 40 additions and 34 deletions
  1. 35
    34
      converter/adb_usb/matrix.c
  2. 5
    0
      protocol/adb.h

+ 35
- 34
converter/adb_usb/matrix.c View File

# error "MATRIX_ROWS must not exceed 255" # error "MATRIX_ROWS must not exceed 255"
#endif #endif


#define CAPS 0x39
#define CAPS_UP (CAPS | 0x80)
#define ROW(key) ((key)>>3&0x0F)
#define COL(key) ((key)&0x07)
#define ADB_CAPS_UP (ADB_CAPS | 0x80)




static bool _matrix_is_modified = false;
static bool is_modified = false;


// matrix state buffer(1:on, 0:off) // matrix state buffer(1:on, 0:off)
#if (MATRIX_COLS <= 8) #if (MATRIX_COLS <= 8)
static uint8_t *matrix;
static uint8_t _matrix0[MATRIX_ROWS];
static uint8_t matrix[MATRIX_ROWS];
#else #else
static uint16_t *matrix;
static uint16_t _matrix0[MATRIX_ROWS];
static uint16_t matrix[MATRIX_ROWS];
#endif #endif


#ifdef MATRIX_HAS_GHOST #ifdef MATRIX_HAS_GHOST
static bool matrix_has_ghost_in_row(uint8_t row); static bool matrix_has_ghost_in_row(uint8_t row);
#endif #endif
static void _register_key(uint8_t key);
static void register_key(uint8_t key);




inline inline
adb_host_init(); adb_host_init();


// initialize matrix state: all keys off // initialize matrix state: all keys off
for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
matrix = _matrix0;
for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;


print_enable = true; print_enable = true;
debug_enable = true; debug_enable = true;
uint16_t codes; uint16_t codes;
uint8_t key0, key1; uint8_t key0, key1;


_matrix_is_modified = false;
is_modified = false;
codes = adb_host_kbd_recv(); codes = adb_host_kbd_recv();
key0 = codes>>8; key0 = codes>>8;
key1 = codes&0xFF; key1 = codes&0xFF;


if (debug_enable && codes) {
if (debug_matrix && codes) {
print("adb_host_kbd_recv: "); phex16(codes); print("\n"); print("adb_host_kbd_recv: "); phex16(codes); print("\n");
} }


#ifdef MATRIX_HAS_LOCKING_CAPS #ifdef MATRIX_HAS_LOCKING_CAPS
// Send Caps key up event // Send Caps key up event
if (matrix_is_on(ROW(CAPS), COL(CAPS))) {
_matrix_is_modified = true;
_register_key(CAPS_UP);
if (matrix_is_on(MATRIX_ROW(ADB_CAPS), MATRIX_COL(ADB_CAPS))) {
register_key(ADB_CAPS_UP);
} }
#endif #endif
if (codes == 0) { // no keys if (codes == 0) { // no keys
return 0; return 0;
} else if (key0 == 0xFF && key1 != 0xFF) { // error
return codes&0xFF;
} else if (codes == 0x7F7F) { // power key press
register_key(0x7F);
} else if (codes == 0xFFFF) { // power key release
register_key(0xFF);
} else if (key0 == 0xFF) { // error
if (debug_matrix) print("adb_host_kbd_recv: ERROR(matrix cleared.)\n");
// clear matrix to unregister all keys
for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
return key1;
} else { } else {
#ifdef MATRIX_HAS_LOCKING_CAPS #ifdef MATRIX_HAS_LOCKING_CAPS
if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) { if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) {
// Ignore LockingCaps key down event when CAPS LOCK is on // Ignore LockingCaps key down event when CAPS LOCK is on
if (key0 == CAPS && (key1 == CAPS || key1 == 0xFF)) return 0;
if (key0 == CAPS) key0 = key1;
if (key1 == CAPS) key1 = 0xFF;
if (key0 == ADB_CAPS && (key1 == ADB_CAPS || key1 == 0xFF)) return 0;
if (key0 == ADB_CAPS) key0 = key1;
if (key1 == ADB_CAPS) key1 = 0xFF;
// Convert LockingCaps key up event into down event // Convert LockingCaps key up event into down event
if (key0 == CAPS_UP) key0 = CAPS;
if (key1 == CAPS_UP) key1 = CAPS;
if (key0 == ADB_CAPS_UP) key0 = ADB_CAPS;
if (key1 == ADB_CAPS_UP) key1 = ADB_CAPS;
} else { } else {
// CAPS LOCK off:
// Ignore LockingCaps key up event when CAPS LOCK is off
if (key0 == CAPS_UP && (key1 == CAPS_UP || key1 == 0xFF)) return 0;
if (key0 == CAPS_UP) key0 = key1;
if (key1 == CAPS_UP) key1 = 0xFF;
// ADB_CAPS LOCK off:
// Ignore LockingCaps key up event when ADB_CAPS LOCK is off
if (key0 == ADB_CAPS_UP && (key1 == ADB_CAPS_UP || key1 == 0xFF)) return 0;
if (key0 == ADB_CAPS_UP) key0 = key1;
if (key1 == ADB_CAPS_UP) key1 = 0xFF;
} }
#endif #endif
_matrix_is_modified = true;
_register_key(key0);
register_key(key0);
if (key1 != 0xFF) // key1 is 0xFF when no second key. if (key1 != 0xFF) // key1 is 0xFF when no second key.
_register_key(key1);
register_key(key1);
} }


return 1; return 1;


bool matrix_is_modified(void) bool matrix_is_modified(void)
{ {
return _matrix_is_modified;
return is_modified;
} }


inline inline


void matrix_print(void) void matrix_print(void)
{ {
if (!debug_matrix) return;
#if (MATRIX_COLS <= 8) #if (MATRIX_COLS <= 8)
print("r/c 01234567\n"); print("r/c 01234567\n");
#else #else
#endif #endif


inline inline
static void _register_key(uint8_t key)
static void register_key(uint8_t key)
{ {
uint8_t col, row; uint8_t col, row;
col = key&0x07; col = key&0x07;
} else { } else {
matrix[row] |= (1<<col); matrix[row] |= (1<<col);
} }
is_modified = true;
} }

+ 5
- 0
protocol/adb.h View File

#ifndef ADB_H #ifndef ADB_H
#define ADB_H #define ADB_H


#include <stdint.h>
#include <stdbool.h> #include <stdbool.h>


#if !(defined(ADB_PORT) && \ #if !(defined(ADB_PORT) && \
# error "ADB port setting is required in config.h" # error "ADB port setting is required in config.h"
#endif #endif


#define ADB_POWER 0x7F
#define ADB_CAPS 0x39


// ADB host // ADB host
void adb_host_init(void); void adb_host_init(void);
bool adb_host_psw(void); bool adb_host_psw(void);