Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyboard power and other special keys #6

Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Next

Keyboard HID extras plus map extra keys

This delta, adds an extra keyboard object to handle those keys that are not part of the main keyboard class.  In particular there are separate HID reports for some of the keys, such as Power keys, and multimedia keys.

These reports might be on separate Interface or in cases where the mouse and keyboard are on the same device, the extra reports may be on the Mouse Interface.

So far I have not tried to combine with Keyboard object as might require multiple inheritance which I would like to avoid.

Also I extended the special key mapping table to map several other keys like F1-12, Arrow, Home/end... To special values where the 0x80 bit is set.  I used the same values as used for the Arduino Keyboard library.  I did not use their defines as they used defines like KEY_F1, which already exists in core, but in core it is the scan code from the keyboard and not the end user value.
  • Loading branch information...
KurtE committed Oct 11, 2017
commit dbe5292471eb0a4c366f2c305d6f328fba5d3a03
@@ -56,7 +56,7 @@
// your best effort to read chapter 4 before asking USB questions!


// #define USBHOST_PRINT_DEBUG
//#define USBHOST_PRINT_DEBUG

/************************************************/
/* Data Types */
@@ -88,6 +88,31 @@ typedef struct Transfer_struct Transfer_t;
class USBDriver;
class USBDriverTimer;

/************************************************/
/* Added Defines */
/************************************************/
#define KEYD_UP 0xDA
#define KEYD_DOWN 0xD9
#define KEYD_LEFT 0xD8
#define KEYD_RIGHT 0xD7
#define KEYD_INSERT 0xD1
#define KEYD_DELETE 0xD4
#define KEYD_PAGE_UP 0xD3
#define KEYD_PAGE_DOWN 0xD6
#define KEYD_HOME 0xD2
#define KEYD_END 0xD5
#define KEYD_F1 0xC2
#define KEYD_F2 0xC3
#define KEYD_F3 0xC4
#define KEYD_F4 0xC5
#define KEYD_F5 0xC6
#define KEYD_F6 0xC7
#define KEYD_F7 0xC8
#define KEYD_F8 0xC9
#define KEYD_F9 0xCA
#define KEYD_F10 0xCB
#define KEYD_F11 0xCC
#define KEYD_F12 0xCD

/************************************************/
/* Data Structure Definitions */
@@ -508,7 +533,7 @@ class USBHIDParser : public USBDriver {
static void driver_ready_for_hid_collection(USBHIDInput *driver);
protected:
enum { TOPUSAGE_LIST_LEN = 4 };
enum { USAGE_LIST_LEN = 12 };
enum { USAGE_LIST_LEN = 24 };
virtual bool claim(Device_t *device, int type, const uint8_t *descriptors, uint32_t len);
virtual void control(const Transfer_t *transfer);
virtual void disconnect();
@@ -763,6 +788,7 @@ class MouseController : public USBHIDInput {
private:
uint8_t collections_claimed = 0;
volatile bool mouseEvent = false;
volatile bool hid_input_begin_ = false;
uint8_t buttons = 0;
int mouseX = 0;
int mouseY = 0;
@@ -792,4 +818,33 @@ class JoystickController : public USBHIDInput {
int16_t axis[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
};


class KeyboardHIDExtrasController : public USBHIDInput {
public:
KeyboardHIDExtrasController(USBHost &host) { USBHIDParser::driver_ready_for_hid_collection(this); }
void clear() { event_ = false;}
bool available() { return event_; }
void attachPress(void (*f)(uint32_t top, uint16_t code)) { keyPressedFunction = f; }
void attachRelease(void (*f)(uint32_t top, uint16_t code)) { keyReleasedFunction = f; }
enum {MAX_KEYS_DOWN=4};
// uint32_t buttons() { return buttons_; }
protected:
virtual bool claim_collection(Device_t *dev, uint32_t topusage);
virtual void hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax);
virtual void hid_input_data(uint32_t usage, int32_t value);
virtual void hid_input_end();
virtual void disconnect_collection(Device_t *dev);
private:
void (*keyPressedFunction)(uint32_t top, uint16_t code);
void (*keyReleasedFunction)(uint32_t top, uint16_t code);
uint32_t topusage_ = 0; // What top report am I processing?
uint8_t collections_claimed_ = 0;
volatile bool event_ = false;
volatile bool hid_input_begin_ = false;
volatile bool hid_input_data_ = false; // did we receive any valid data with report?
uint8_t count_keys_down_ = 0;
uint16_t keys_down[MAX_KEYS_DOWN];
};


#endif
@@ -10,6 +10,7 @@ USBHub hub2(myusb);
USBHub hub3(myusb);
KeyboardController keyboard1(myusb);
KeyboardController keyboard2(myusb);
KeyboardHIDExtrasController hidextras(myusb);
USBHIDParser hid1(myusb);
USBHIDParser hid2(myusb);
USBHIDParser hid3(myusb);
@@ -25,6 +26,8 @@ void setup()
myusb.begin();
keyboard1.attachPress(OnPress);
keyboard2.attachPress(OnPress);
hidextras.attachPress(OnHIDExtrasPress);
hidextras.attachRelease(OnHIDExtrasRelease);
}


@@ -68,16 +71,70 @@ void loop()
}



void OnPress(int key)
{
Serial.print("key '");
Serial.print((char)key);
Serial.print("' ");
Serial.println(key);
//Serial.print("key ");
//Serial.print((char)keyboard1.getKey());
//Serial.print(" ");
//Serial.print((char)keyboard2.getKey());
//Serial.println();
Serial.print("key '");
switch (key) {
case KEYD_UP : Serial.print("UP"); break;
case KEYD_DOWN : Serial.print("DN"); break;
case KEYD_LEFT : Serial.print("LEFT"); break;
case KEYD_RIGHT : Serial.print("RIGHT"); break;
case KEYD_INSERT : Serial.print("Ins"); break;
case KEYD_DELETE : Serial.print("Del"); break;
case KEYD_PAGE_UP : Serial.print("PUP"); break;
case KEYD_PAGE_DOWN: Serial.print("PDN"); break;
case KEYD_HOME : Serial.print("HOME"); break;
case KEYD_END : Serial.print("END"); break;
case KEYD_F1 : Serial.print("F1"); break;
case KEYD_F2 : Serial.print("F2"); break;
case KEYD_F3 : Serial.print("F3"); break;
case KEYD_F4 : Serial.print("F4"); break;
case KEYD_F5 : Serial.print("F5"); break;
case KEYD_F6 : Serial.print("F6"); break;
case KEYD_F7 : Serial.print("F7"); break;
case KEYD_F8 : Serial.print("F8"); break;
case KEYD_F9 : Serial.print("F9"); break;
case KEYD_F10 : Serial.print("F10"); break;
case KEYD_F11 : Serial.print("F11"); break;
case KEYD_F12 : Serial.print("F12"); break;
default: Serial.print((char)key); break;
}
Serial.print("' ");
Serial.print(key);
Serial.print(" MOD: ");
if (keyboard1) {
Serial.print(keyboard1.getModifiers(), HEX);
Serial.print(" OEM: ");
Serial.print(keyboard1.getOemKey(), HEX);
Serial.print(" LEDS: ");
Serial.println(keyboard1.LEDS(), HEX);
} else {
Serial.print(keyboard2.getModifiers(), HEX);
Serial.print(" OEM: ");
Serial.print(keyboard2.getOemKey(), HEX);
Serial.print(" LEDS: ");
Serial.println(keyboard2.LEDS(), HEX);
}

//Serial.print("key ");
//Serial.print((char)keyboard1.getKey());
//Serial.print(" ");
//Serial.print((char)keyboard2.getKey());
//Serial.println();
}
void OnHIDExtrasPress(uint32_t top, uint16_t key)
{
Serial.print("HID (");
Serial.print(top, HEX);
Serial.print(") key press:");
Serial.println(key, HEX);
}

void OnHIDExtrasRelease(uint32_t top, uint16_t key)
{
Serial.print("HID (");
Serial.print(top, HEX);
Serial.print(") key release:");
Serial.println(key, HEX);
}
16 hid.cpp
@@ -57,6 +57,10 @@ bool USBHIDParser::claim(Device_t *dev, int type, const uint8_t *descriptors, ui
// do not claim boot protocol keyboards
if (descriptors[6] == 1 && descriptors[7] == 1) return false;

print("HID Parser Claim: ");
print_hexbytes(descriptors, len);


// hid interface descriptor
uint32_t hidlen = descriptors[9];
if (hidlen < 9) return false;
@@ -198,6 +202,14 @@ void USBHIDParser::disconnect()
// Called when the HID device sends a report
void USBHIDParser::in_data(const Transfer_t *transfer)
{
/*Serial.print("HID: ");
uint8_t *pb = (uint8_t*)transfer->buffer;
for (uint8_t i = 0; i < transfer->length; i++) {
Serial.print(pb[i], HEX);
Serial.print(" ");
}
Serial.println(); */

print("HID: ");
print_hexbytes(transfer->buffer, transfer->length);
const uint8_t *buf = (const uint8_t *)transfer->buffer;
@@ -531,6 +543,10 @@ void USBHIDParser::parse(uint16_t type_and_report_id, const uint8_t *data, uint3
print(" usage = ", u, HEX);
println(" data = 1");
driver->hid_input_data(u, 1);
} else {
print (" usage =", u, HEX);
print(" out of range: ", logical_min, HEX);
println(" ", logical_max, HEX);
}
bitindex += report_size;
}
@@ -44,7 +44,29 @@ typedef struct {
keycode_extra_t keycode_extras[] = {
{M(KEY_ENTER), '\n'},
{M(KEY_ESC), 0x1b},
{M(KEY_TAB), 0x9 }
{M(KEY_TAB), 0x9 },
{M(KEY_UP), KEYD_UP },
{M(KEY_DOWN), KEYD_DOWN },
{M(KEY_LEFT), KEYD_LEFT },
{M(KEY_RIGHT), KEYD_RIGHT },
{M(KEY_INSERT), KEYD_INSERT },
{M(KEY_DELETE), KEYD_DELETE },
{M(KEY_PAGE_UP), KEYD_PAGE_UP },
{M(KEY_PAGE_DOWN), KEYD_PAGE_DOWN },
{M(KEY_HOME), KEYD_HOME },
{M(KEY_END), KEYD_END },
{M(KEY_F1), KEYD_F1 },
{M(KEY_F2), KEYD_F2 },
{M(KEY_F3), KEYD_F3 },
{M(KEY_F4), KEYD_F4 },
{M(KEY_F5), KEYD_F5 },
{M(KEY_F6), KEYD_F6 },
{M(KEY_F7), KEYD_F7 },
{M(KEY_F8), KEYD_F8 },
{M(KEY_F9), KEYD_F9 },
{M(KEY_F10), KEYD_F10 },
{M(KEY_F11), KEYD_F11 },
{M(KEY_F12), KEYD_F12 }
};

// Some of these mapped to key + shift.
@@ -242,9 +264,10 @@ void KeyboardController::key_release(uint32_t mod, uint32_t key)

uint16_t KeyboardController::convert_to_unicode(uint32_t mod, uint32_t key)
{
// TODO: special keys
// TODO: caps lock
// WIP: special keys
// 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
@@ -261,11 +284,19 @@ uint16_t KeyboardController::convert_to_unicode(uint32_t mod, uint32_t key)
}
}
}
}

// If we made it here without doing something then return 0;
if (key & SHIFT_MASK) return 0;
// Check for any of our mapped extra keys - Done early as some of these keys are
// above and some below the SHIFT_MASK value
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;
}
}

// 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 (leds_.capsLock) key ^= SHIFT_MASK; // Caps lock will switch the Shift;
for (int i=0; i < 96; i++) {
@@ -275,12 +306,6 @@ uint16_t KeyboardController::convert_to_unicode(uint32_t mod, uint32_t key)
}
}

// 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
for (int i=0; i < 96; i++) {
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.