Browse Source

Keyboard LEDs and data parsing (Kurt & Frank)

main
PaulStoffregen 7 years ago
parent
commit
d3b6abe878
2 changed files with 173 additions and 6 deletions
  1. +24
    -0
      USBHost_t36.h
  2. +149
    -6
      keyboard.cpp

+ 24
- 0
USBHost_t36.h View File





class KeyboardController : public USBDriver /* , public USBHIDInput */ { class KeyboardController : public USBDriver /* , public USBHIDInput */ {
public:
typedef union {
struct {
uint8_t numLock : 1;
uint8_t capsLock : 1;
uint8_t scrollLock : 1;
uint8_t compose : 1;
uint8_t kana : 1;
uint8_t reserved : 3;
};
uint8_t byte;
} KBDLeds_t;
public: public:
KeyboardController(USBHost &host) { init(); } KeyboardController(USBHost &host) { init(); }
KeyboardController(USBHost *host) { init(); } KeyboardController(USBHost *host) { init(); }
uint8_t getOemKey() { return keyOEM; } uint8_t getOemKey() { return keyOEM; }
void attachPress(void (*f)(int unicode)) { keyPressedFunction = f; } void attachPress(void (*f)(int unicode)) { keyPressedFunction = f; }
void attachRelease(void (*f)(int unicode)) { keyReleasedFunction = f; } void attachRelease(void (*f)(int unicode)) { keyReleasedFunction = f; }
void LEDS(uint8_t leds);
uint8_t LEDS() {return leds_.byte;}
void updateLEDS(void);
bool numLock() {return leds_.numLock;}
bool capsLock() {return leds_.capsLock;}
bool scrollLock() {return leds_.scrollLock;}
void numLock(bool f);
void capsLock(bool f);
void scrollLock(bool f);
protected: protected:
virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len); virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
virtual void control(const Transfer_t *transfer); virtual void control(const Transfer_t *transfer);
uint8_t modifiers; uint8_t modifiers;
uint8_t keyOEM; uint8_t keyOEM;
uint8_t prev_report[8]; uint8_t prev_report[8];
KBDLeds_t leds_ = {0};
bool update_leds_ = false;
bool processing_new_data_ = false;
Pipe_t mypipes[2] __attribute__ ((aligned(32))); Pipe_t mypipes[2] __attribute__ ((aligned(32)));
Transfer_t mytransfers[4] __attribute__ ((aligned(32))); Transfer_t mytransfers[4] __attribute__ ((aligned(32)));
}; };

+ 149
- 6
keyboard.cpp View File

#include "USBHost_t36.h" // Read this header first for key info #include "USBHost_t36.h" // Read this header first for key info
#include "keylayouts.h" // from Teensyduino core library #include "keylayouts.h" // from Teensyduino core library


typedef struct {
KEYCODE_TYPE code;
uint8_t ascii;
} keycode_extra_t;

typedef struct {
KEYCODE_TYPE code;
KEYCODE_TYPE codeNumlockOff;
uint8_t charNumlockOn; // We will assume when num lock is on we have all characters...
} keycode_numlock_t;

#ifdef M
#undef M
#endif
#define M(n) ((n) & KEYCODE_MASK)

keycode_extra_t keycode_extras[] = {
{M(KEY_ENTER), '\n'},
{M(KEY_ESC), 0x1b},
{M(KEY_TAB), 0x9 }
};

// Some of these mapped to key + shift.
keycode_numlock_t keycode_numlock[] = {
{M(KEYPAD_SLASH), '/', '/'},
{M(KEYPAD_ASTERIX), '*', '*'},
{M(KEYPAD_MINUS), '-', '-'},
{M(KEYPAD_PLUS), '+', '+'},
{M(KEYPAD_ENTER), '\n', '\n'},
{M(KEYPAD_1), 0x80 | M(KEY_END), '1'},
{M(KEYPAD_2), 0x80 | M(KEY_DOWN), '2'},
{M(KEYPAD_3), 0x80 | M(KEY_PAGE_DOWN), '3'},
{M(KEYPAD_4), 0x80 | M(KEY_LEFT), '4'},
{M(KEYPAD_5), 0x00, '5'},
{M(KEYPAD_6), 0x80 | M(KEY_RIGHT), '6'},
{M(KEYPAD_7), 0x80 | M(KEY_HOME), '7'},
{M(KEYPAD_8), 0x80 | M(KEY_UP), '8'},
{M(KEYPAD_9), 0x80 | M(KEY_PAGE_UP), '9'},
{M(KEYPAD_0), 0x80 | M(KEY_INSERT), '0'},
{M(KEYPAD_PERIOD), 0x80 | M(KEY_DELETE), '.'}
};




void KeyboardController::init() void KeyboardController::init()
{ {
if (descriptors[21] != 3) return false; // must be interrupt type if (descriptors[21] != 3) return false; // must be interrupt type
uint32_t size = descriptors[22] | (descriptors[23] << 8); uint32_t size = descriptors[22] | (descriptors[23] << 8);
println("packet size = ", size); println("packet size = ", size);
if (size != 8) return false; // must be 8 bytes for Keyboard Boot Protocol
if (size != 8) {
return false; // must be 8 bytes for Keyboard Boot Protocol
}
uint32_t interval = descriptors[24]; uint32_t interval = descriptors[24];
println("polling interval = ", interval); println("polling interval = ", interval);
datapipe = new_Pipe(dev, 3, endpoint, 1, 8, interval); datapipe = new_Pipe(dev, 3, endpoint, 1, 8, interval);


void KeyboardController::new_data(const Transfer_t *transfer) void KeyboardController::new_data(const Transfer_t *transfer)
{ {
processing_new_data_ = true;
println("KeyboardController Callback (member)"); println("KeyboardController Callback (member)");
print(" KB Data: "); print(" KB Data: ");
print_hexbytes(transfer->buffer, 8); print_hexbytes(transfer->buffer, 8);
} }
memcpy(prev_report, report, 8); memcpy(prev_report, report, 8);
queue_Data_Transfer(datapipe, report, 8, this); queue_Data_Transfer(datapipe, report, 8, this);
processing_new_data_ = false;

// See if we have any outstanding leds to update
if (update_leds_) {
updateLEDS();
}
}


void KeyboardController::numLock(bool f) {
if (leds_.numLock != f) {
leds_.numLock = f;
updateLEDS();
}
}

void KeyboardController::capsLock(bool f) {
if (leds_.capsLock != f) {
leds_.capsLock = f;
updateLEDS();
}
}

void KeyboardController::scrollLock(bool f) {
if (leds_.scrollLock != f) {
leds_.scrollLock = f;
updateLEDS();
}
} }


void KeyboardController::key_press(uint32_t mod, uint32_t key) void KeyboardController::key_press(uint32_t mod, uint32_t key)
println(" release, key=", key); println(" release, key=", key);
modifiers = mod; modifiers = mod;
keyOEM = key; keyOEM = key;
keyCode = convert_to_unicode(mod, key);
if (keyReleasedFunction) {
keyReleasedFunction(keyCode);

// Look for modifier keys
if (key == M(KEY_NUM_LOCK)) {
numLock(!leds_.numLock);
// Lets toggle Numlock
} else if (key == M(KEY_CAPS_LOCK)) {
capsLock(!leds_.capsLock);

} else if (key == M(KEY_SCROLL_LOCK)) {
scrollLock(!leds_.scrollLock);
} else { } else {
keyReleased();
keyCode = convert_to_unicode(mod, key);
if (keyReleasedFunction) {
keyReleasedFunction(keyCode);
} else {
keyReleased();
}
} }
} }


// TODO: special keys // TODO: special keys
// TODO: caps lock // TODO: caps lock
// TODO: dead key sequences // TODO: dead key sequences
if (key & SHIFT_MASK) {
// Many of these keys will look like they are other keys with shift mask...
// Check for any of our mapped extra keys
for (uint8_t i = 0; i < (sizeof(keycode_numlock)/sizeof(keycode_numlock[0])); i++) {
if (keycode_numlock[i].code == key) {
// See if the user is using numlock or not...
if (leds_.numLock) {
return keycode_numlock[i].charNumlockOn;
} else {
key = keycode_numlock[i].codeNumlockOff;
if (!(key & 0x80)) return key; // we have hard coded value
key &= 0x7f; // mask off the extra and break out to process as other characters...
break;
}
}
}

// If we made it here without doing something then return 0;
if (key & SHIFT_MASK) return 0;
}

if ((mod & 0x02) || (mod & 0x20)) key |= SHIFT_MASK; if ((mod & 0x02) || (mod & 0x20)) key |= SHIFT_MASK;
if (leds_.capsLock) key ^= SHIFT_MASK; // Caps lock will switch the Shift;
for (int i=0; i < 96; i++) { for (int i=0; i < 96; i++) {
if (keycodes_ascii[i] == key) return i + 32;
if (keycodes_ascii[i] == key) {
if ((mod & 1) || (mod & 0x10)) return (i+32) & 0x1f; // Control key is down
return i + 32;
}
}

// Check for any of our mapped extra keys
for (uint8_t i = 0; i < (sizeof(keycode_extras)/sizeof(keycode_extras[0])); i++) {
if (keycode_extras[i].code == key) {
return keycode_extras[i].ascii;
}
} }

#ifdef ISO_8859_1_A0 #ifdef ISO_8859_1_A0
for (int i=0; i < 96; i++) { for (int i=0; i < 96; i++) {
if (keycodes_iso_8859_1[i] == key) return i + 160; if (keycodes_iso_8859_1[i] == key) return i + 160;
return 0; return 0;
} }


void KeyboardController::LEDS(uint8_t leds) {
println("Keyboard setLEDS ", leds, HEX);
leds_.byte = leds;
updateLEDS();
}

void KeyboardController::updateLEDS() {
println("KBD: Update LEDS", leds_.byte, HEX);
if (processing_new_data_) {
println(" Update defered");
update_leds_ = true;
return; // defer until later
}

// Now lets tell keyboard new state.
static uint8_t keyboard_keys_report[1] = {0};
setup_t keys_setup;
keyboard_keys_report[0] = leds_.byte;
queue_Data_Transfer(datapipe, report, 8, this);
mk_setup(keys_setup, 0x21, 9, 0x200, 0, sizeof(keyboard_keys_report)); // hopefully this sets leds
queue_Control_Transfer(device, &keys_setup, keyboard_keys_report, this);

update_leds_ = false;
}







Loading…
Cancel
Save