From 98cca5656a1ff9e6e537115539d83f67bc52af09 Mon Sep 17 00:00:00 2001 From: bbx10node Date: Sat, 13 Feb 2016 10:10:24 -1000 Subject: [PATCH 1/2] Add ASCII control codes 0x00 CTRL-@ through 0x1F CTRL-_ Also return ASCII codes for keys: ESCAPE, DELETE, TAB, and BACKSPACE keys. Return correct code for ENTER and keypad ENTER keys. --- src/hidboot.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++++++--- src/hidboot.h | 4 +++ 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/src/hidboot.cpp b/src/hidboot.cpp index fb85bd7..c3cdbe7 100644 --- a/src/hidboot.cpp +++ b/src/hidboot.cpp @@ -132,7 +132,7 @@ uint8_t KeyboardReportParser::HandleLockingKeys(HID *hid, uint8_t key) const uint8_t KeyboardReportParser::numKeys[] = { '!', '@', '#', '$', '%', '^', '&', '*', '(', ')' }; const uint8_t KeyboardReportParser::symKeysUp[] = { '_', '+', '{', '}', '|', '~', ':', '"', '~', '<', '>', '?' }; const uint8_t KeyboardReportParser::symKeysLo[] = { '-', '=', '[', ']', '\\', ' ', ';', '\'', '`', ',', '.', '/' }; -const uint8_t KeyboardReportParser::padKeys[] = { '/', '*', '-', '+', 0x13 }; +const uint8_t KeyboardReportParser::padKeys[] = { '/', '*', '-', '+', 0x0D }; /** * \brief Manage keyboard OEM to ASCII conversion. @@ -142,13 +142,34 @@ const uint8_t KeyboardReportParser::padKeys[] = { '/', '*', '-', '+', 0x13 }; * * \return Keyboard corresponding ASCII value on success, 0 otherwise. */ + +/* Key modifier bit map. + * 0 LEFT CTRL + * 1 LEFT SHIFT + * 2 LEFT ALT + * 3 LEFT GUI + * 4 RIGHT CTRL + * 5 RIGHT SHIFT + * 6 RIGHT ALT + * 7 RIGHT GUI + * + * shift = (mod & 0x22) means shift is non-zero + * if either or both shift keys are active + * ctrl = (mod & 0x11) means ctrl is non-zero + * if either or both control keys are active + */ + uint8_t KeyboardReportParser::OemToAscii(uint8_t mod, uint8_t key) { uint8_t shift = (mod & 0x22); + uint8_t ctrl = (mod & 0x11); // [a-z] if (key > 0x03 && key < 0x1e) { + // [^a-^z] + if (ctrl) return (key - 3); + // Upper case letters if ( (kbdLockingKeys.kbdLeds.bmCapsLock == 0 && (mod & 2)) || (kbdLockingKeys.kbdLeds.bmCapsLock == 1 && (mod & 2) == 0) ) @@ -162,7 +183,17 @@ uint8_t KeyboardReportParser::OemToAscii(uint8_t mod, uint8_t key) else if (key > 0x1d && key < 0x27) { if (shift) - return (numKeys[key - 0x1e]); + { + if (ctrl) + { + if (key == 0x23) + return (0x1E); /* RS ^^ */ + else + return (0x00); + } + else + return (numKeys[key - 0x1e]); + } else return (key - 0x1e + '1'); } @@ -173,7 +204,33 @@ uint8_t KeyboardReportParser::OemToAscii(uint8_t mod, uint8_t key) return (key - 0x59 + '1'); } else if (key > 0x2c && key < 0x39) - return ((shift) ? symKeysUp[key-0x2d] : symKeysLo[key-0x2d]); + { + if (shift) + { + if (ctrl) + { + if (key == 0x2d) + return (0x1F); /* US ^_ */ + else + return (0x00); + } + else + return symKeysUp[key-0x2d]; + } + else + { + if (ctrl) + switch (key) + { + case 0x2F: return 0x1B; /* ESC ^[ */ + case 0x31: return 0x1C; /* FS ^\ */ + case 0x30: return 0x1D; /* GS ^] */ + default: return 0x00; + } + else + return symKeysLo[key-0x2d]; + } + } else if (key > 0x53 && key < 0x59) return padKeys[key - 0x54]; else @@ -181,7 +238,11 @@ uint8_t KeyboardReportParser::OemToAscii(uint8_t mod, uint8_t key) switch (key) { case KEY_SPACE: return (0x20); - case KEY_ENTER: return (0x13); + case KEY_ENTER: return (0x0D); + case KEY_ESCAPE: return (0x1B); + case KEY_DELETE: return (0x08); + case KEY_DELETE_FORWARD: return (0x7F); // ASCII DEL + case KEY_TAB: return (0x09); case KEY_ZERO: return ((shift) ? ')' : '0'); case KEY_ZERO2: return ((kbdLockingKeys.kbdLeds.bmNumLock == 1) ? '0' : 0); case KEY_PERIOD: return ((kbdLockingKeys.kbdLeds.bmNumLock == 1) ? '.' : 0); diff --git a/src/hidboot.h b/src/hidboot.h index 66f630f..aff7c73 100644 --- a/src/hidboot.h +++ b/src/hidboot.h @@ -29,6 +29,10 @@ e-mail : support@circuitsathome.com #define KEY_ZERO 0x27 #define KEY_ZERO2 0x62 #define KEY_ENTER 0x28 +#define KEY_ESCAPE 0x29 +#define KEY_DELETE 0x2A // Backspace +#define KEY_DELETE_FORWARD 0x4C // Delete +#define KEY_TAB 0x2B #define KEY_PERIOD 0x63 /** From ff3d990ae410cbaeac39f9f61de0dda5f720b72c Mon Sep 17 00:00:00 2001 From: Donna Whisnant Date: Sat, 13 Feb 2016 19:54:55 -0600 Subject: [PATCH 2/2] Reworked USBHost::Task() to use a delta-time rather than compare with the absolute time from millis(). Changed the definition of "delay" to be the remaining milliseconds to delay rather than the absolute time of the end of the delay. This fixes timer wrapping issues caused when millis() wraps back to 0. And it allows the Task() function to not have be continually called as often when higher priority tasks need to supercede it, and prevents restart delays after an extended period of missed calls to Task(). --- src/Usb.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Usb.cpp b/src/Usb.cpp index ce74b48..f976574 100644 --- a/src/Usb.cpp +++ b/src/Usb.cpp @@ -715,8 +715,16 @@ void USBHost::Task(void) uint32_t rcode = 0; volatile uint32_t tmpdata = 0; static uint32_t delay = 0; + static uint32_t lasttime = 0; + uint32_t currenttime = 0; + uint32_t deltatime = 0; uint32_t lowspeed = 0; + currenttime = millis(); + deltatime = (currenttime - lasttime); + delay = ((delay > deltatime) ? (delay - deltatime) : 0); + lasttime = currenttime; + // Update USB task state on Vbus change tmpdata = UHD_GetVBUSState(); switch (tmpdata) @@ -740,7 +748,7 @@ void USBHost::Task(void) // Attached state if ((usb_task_state & USB_STATE_MASK) == USB_STATE_DETACHED) { - delay = millis() + USB_SETTLE_DELAY; + delay = USB_SETTLE_DELAY; usb_task_state = USB_ATTACHED_SUBSTATE_SETTLE; //FIXME TODO: lowspeed = 0 ou 1; already done by hardware? } @@ -780,7 +788,7 @@ void USBHost::Task(void) case USB_ATTACHED_SUBSTATE_SETTLE: // Settle time for just attached device - if (delay < millis()) + if (!delay) { TRACE_USBHOST(printf(" + USB_ATTACHED_SUBSTATE_SETTLE\r\n");) usb_task_state = USB_ATTACHED_SUBSTATE_RESET_DEVICE; @@ -809,7 +817,7 @@ void USBHost::Task(void) usb_task_state = USB_ATTACHED_SUBSTATE_WAIT_SOF; // Wait 20ms after Bus Reset (USB spec) - delay = millis() + 20; + delay = 20; } break; @@ -817,7 +825,7 @@ void USBHost::Task(void) // Wait for SOF received first if (Is_uhd_sof()) { - if (delay < millis()) + if (!delay) { TRACE_USBHOST(printf(" + USB_ATTACHED_SUBSTATE_WAIT_SOF\r\n");)