From e4930f866d41baa7e8ac615f22dc3e92ec0f1c5a Mon Sep 17 00:00:00 2001 From: Zach Eveland Date: Tue, 20 Dec 2011 17:00:19 -0500 Subject: changed Keyboard write() method to type(). Made write() an alias for type() to allow subclassing by Stream. --- cores/arduino/HID.cpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'cores/arduino/HID.cpp') diff --git a/cores/arduino/HID.cpp b/cores/arduino/HID.cpp index 8ed1566..d69522f 100644 --- a/cores/arduino/HID.cpp +++ b/cores/arduino/HID.cpp @@ -407,6 +407,11 @@ const uint8_t _asciimap[128] = uint8_t USBPutChar(uint8_t c); size_t Keyboard_::write(uint8_t c) +{ + type(c); +} + +size_t Keyboard_::type(uint8_t c) { // Keydown { -- cgit v1.2.3-18-g5258 From e405a6eb6099a71959c664cab8ca8241634c6b91 Mon Sep 17 00:00:00 2001 From: Zach Eveland Date: Tue, 20 Dec 2011 17:08:07 -0500 Subject: Revert "changed Keyboard write() method to type(). Made write() an alias for type() to allow subclassing by Stream." This reverts commit de1d5fc0cb82874c0dcb766c5fb27ab36c5cb32c. --- cores/arduino/HID.cpp | 5 ----- 1 file changed, 5 deletions(-) (limited to 'cores/arduino/HID.cpp') diff --git a/cores/arduino/HID.cpp b/cores/arduino/HID.cpp index d69522f..8ed1566 100644 --- a/cores/arduino/HID.cpp +++ b/cores/arduino/HID.cpp @@ -407,11 +407,6 @@ const uint8_t _asciimap[128] = uint8_t USBPutChar(uint8_t c); size_t Keyboard_::write(uint8_t c) -{ - type(c); -} - -size_t Keyboard_::type(uint8_t c) { // Keydown { -- cgit v1.2.3-18-g5258 From 4e9fb924b2ea4c1373edc09408db2ac0c402e68c Mon Sep 17 00:00:00 2001 From: Zach Eveland Date: Tue, 20 Dec 2011 17:09:44 -0500 Subject: changed Keyboard write() method to type(). Made write() an alias for type() to allow subclassing by Stream. --- cores/arduino/HID.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'cores/arduino/HID.cpp') diff --git a/cores/arduino/HID.cpp b/cores/arduino/HID.cpp index 8ed1566..e6fb5f7 100644 --- a/cores/arduino/HID.cpp +++ b/cores/arduino/HID.cpp @@ -406,7 +406,8 @@ const uint8_t _asciimap[128] = }; uint8_t USBPutChar(uint8_t c); -size_t Keyboard_::write(uint8_t c) + +size_t Keyboard_::type(uint8_t c) { // Keydown { -- cgit v1.2.3-18-g5258 From 8f5869009ccddad8698969ff2ebe204d7b574b37 Mon Sep 17 00:00:00 2001 From: Zach Eveland Date: Sat, 3 Mar 2012 13:26:57 -0500 Subject: fixed minor compilation warnings for Leonardo --- cores/arduino/HID.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'cores/arduino/HID.cpp') diff --git a/cores/arduino/HID.cpp b/cores/arduino/HID.cpp index e6fb5f7..b60b3c0 100644 --- a/cores/arduino/HID.cpp +++ b/cores/arduino/HID.cpp @@ -144,7 +144,6 @@ u8 _hid_protocol = 1; u8 _hid_idle = 1; #define WEAK __attribute__ ((weak)) -#define WEAK int WEAK HID_GetInterface(u8* interfaceNum) { @@ -245,7 +244,7 @@ void Mouse_::release(uint8_t b) bool Mouse_::isPressed(uint8_t b) { - if (b & _buttons > 0) + if ((b & _buttons) > 0) return true; return false; } -- cgit v1.2.3-18-g5258 From fbea67532a03cbcb35c47209f902797c80c499f7 Mon Sep 17 00:00:00 2001 From: Zach Eveland Date: Sat, 3 Mar 2012 22:54:45 -0500 Subject: added Keyboard methods press(), release(), and releaseAll() Changes mean that a single, persistent key report must be used so keys can be added or removed. Also reimplemented type() using the new methods. --- cores/arduino/HID.cpp | 98 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 70 insertions(+), 28 deletions(-) (limited to 'cores/arduino/HID.cpp') diff --git a/cores/arduino/HID.cpp b/cores/arduino/HID.cpp index b60b3c0..9904993 100644 --- a/cores/arduino/HID.cpp +++ b/cores/arduino/HID.cpp @@ -253,7 +253,7 @@ bool Mouse_::isPressed(uint8_t b) //================================================================================ // Keyboard -Keyboard_::Keyboard_() : _keyMap(0) +Keyboard_::Keyboard_() : _keyMap(0) { } @@ -406,38 +406,80 @@ const uint8_t _asciimap[128] = uint8_t USBPutChar(uint8_t c); -size_t Keyboard_::type(uint8_t c) +size_t Keyboard_::press(uint8_t k) { - // Keydown - { - KeyReport keys = {0}; - if (_keyMap) - _keyMap->charToKey(c,&keys); - else - { - if (c >= 128) { - setWriteError(); - return 0; - } - c = pgm_read_byte(_asciimap + c); - if (!c) { - setWriteError(); - return 0; - } - if (c & 0x80) - { - keys.modifiers |= KEY_MODIFIER_LEFT_SHIFT; - c &= 0x7F; + uint8_t i; + k = pgm_read_byte(_asciimap + k); + if (!k) { + setWriteError(); + return 0; + } + if (k & 0x80) { + _keyReport.modifiers |= KEY_MODIFIER_LEFT_SHIFT; + k &= 0x7F; + } + if (_keyReport.keys[0] != k && _keyReport.keys[1] != k && + _keyReport.keys[2] != k && _keyReport.keys[3] != k && + _keyReport.keys[4] != k && _keyReport.keys[5] != k) { + + for (i=0; i<6; i++) { + if (_keyReport.keys[i] == 0x00) { + _keyReport.keys[i] = k; + break; } - keys.keys[0] = c; } - sendReport(&keys); + if (i == 6) { + setWriteError(); + return 0; + } } - // Keyup - { - KeyReport keys = {0}; - sendReport(&keys); + sendReport(&_keyReport); + return 1; +} + +size_t Keyboard_::release(uint8_t k) +{ + uint8_t i; + k = pgm_read_byte(_asciimap + k); + if (!k) { + return 0; } + if (k & 0x80) { + _keyReport.modifiers |= KEY_MODIFIER_LEFT_SHIFT; + k &= 0x7F; + } + for (i=0; i<6; i++) { + if (_keyReport.keys[i] == k) { + _keyReport.keys[i] = 0x00; + break; + } + } + if (i == 6) { + return 0; + } + sendReport(&_keyReport); + return 1; +} + +void Keyboard_::releaseAll(void) +{ + _keyReport.keys[0] = 0; + _keyReport.keys[1] = 0; + _keyReport.keys[2] = 0; + _keyReport.keys[3] = 0; + _keyReport.keys[4] = 0; + _keyReport.keys[5] = 0; + _keyReport.modifiers = 0; + sendReport(&_keyReport); +} + +size_t Keyboard_::type(uint8_t c) +{ + releaseAll(); + // Keydown + press(c); + // Keyup + releaseAll(); return 1; } -- cgit v1.2.3-18-g5258 From 54fb0bf3f54a37d4dd9370d5f545a52b54ad1775 Mon Sep 17 00:00:00 2001 From: Zach Eveland Date: Sat, 3 Mar 2012 23:06:44 -0500 Subject: Removed support for key mapping in Keyboard. Was no longer being used and would be damn near impossible to support with the new scheme for handling modifiers and non-printing keyboard characters. --- cores/arduino/HID.cpp | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'cores/arduino/HID.cpp') diff --git a/cores/arduino/HID.cpp b/cores/arduino/HID.cpp index 9904993..b508f10 100644 --- a/cores/arduino/HID.cpp +++ b/cores/arduino/HID.cpp @@ -253,7 +253,7 @@ bool Mouse_::isPressed(uint8_t b) //================================================================================ // Keyboard -Keyboard_::Keyboard_() : _keyMap(0) +Keyboard_::Keyboard_() { } @@ -262,11 +262,6 @@ void Keyboard_::sendReport(KeyReport* keys) HID_SendReport(2,keys,sizeof(KeyReport)); } -void Keyboard_::setKeyMap(KeyMap* keyMap) -{ - _keyMap = keyMap; -} - extern const uint8_t _asciimap[128] PROGMEM; -- cgit v1.2.3-18-g5258 From 15660068717c4721c60c9caefc1a8eaff702cc24 Mon Sep 17 00:00:00 2001 From: Zach Eveland Date: Sat, 3 Mar 2012 23:37:39 -0500 Subject: Added Keyboard support for all modifier and all common non-printing keys. --- cores/arduino/HID.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 9 deletions(-) (limited to 'cores/arduino/HID.cpp') diff --git a/cores/arduino/HID.cpp b/cores/arduino/HID.cpp index b508f10..378a157 100644 --- a/cores/arduino/HID.cpp +++ b/cores/arduino/HID.cpp @@ -401,18 +401,32 @@ const uint8_t _asciimap[128] = uint8_t USBPutChar(uint8_t c); +// press() adds the specified key (printing, non-printing, or modifier) +// to the persistent key report and sends the report. Because of the way +// USB HID works, the host acts like the key remains pressed until we +// call release(), releaseAll(), or otherwise clear the report and resend. size_t Keyboard_::press(uint8_t k) { uint8_t i; - k = pgm_read_byte(_asciimap + k); - if (!k) { - setWriteError(); - return 0; - } - if (k & 0x80) { - _keyReport.modifiers |= KEY_MODIFIER_LEFT_SHIFT; - k &= 0x7F; + if (k >= 136) { // it's a non-printing key (not a modifier) + k = k - 136; + } else if (k >= 128) { // it's a modifier key + _keyReport.modifiers |= (1<<(k-128)); + k = 0; + } else { // it's a printing key + k = pgm_read_byte(_asciimap + k); + if (!k) { + setWriteError(); + return 0; + } + if (k & 0x80) { + _keyReport.modifiers |= 0x02; // the left shift modifier + k &= 0x7F; + } } + + // Add k to the key report only if it's not already present + // and if there is an empty slot. if (_keyReport.keys[0] != k && _keyReport.keys[1] != k && _keyReport.keys[2] != k && _keyReport.keys[3] != k && _keyReport.keys[4] != k && _keyReport.keys[5] != k) { @@ -432,17 +446,40 @@ size_t Keyboard_::press(uint8_t k) return 1; } +// release() takes the specified key out of the persistent key report and +// sends the report. This tells the OS the key is no longer pressed and that +// it shouldn't be repeated any more. size_t Keyboard_::release(uint8_t k) { + /* uint8_t i; k = pgm_read_byte(_asciimap + k); if (!k) { return 0; } if (k & 0x80) { - _keyReport.modifiers |= KEY_MODIFIER_LEFT_SHIFT; + _keyReport.modifiers |= 0x02; // the left shift modifier k &= 0x7F; } + */ + uint8_t i; + if (k >= 136) { // it's a non-printing key (not a modifier) + k = k - 136; + } else if (k >= 128) { // it's a modifier key + _keyReport.modifiers &= ~(1<<(k-128)); + k = 0; + } else { // it's a printing key + k = pgm_read_byte(_asciimap + k); + if (!k) { + return 0; + } + if (k & 0x80) { + _keyReport.modifiers &= ~(0x02); // the left shift modifier + k &= 0x7F; + } + } + + // Test the key report to see if k is present. Clear it if it exists. for (i=0; i<6; i++) { if (_keyReport.keys[i] == k) { _keyReport.keys[i] = 0x00; @@ -468,6 +505,7 @@ void Keyboard_::releaseAll(void) sendReport(&_keyReport); } +// type() does a press and release of the specified key. size_t Keyboard_::type(uint8_t c) { releaseAll(); -- cgit v1.2.3-18-g5258 From 9c040a8a2bcc7ec8d128df05479b2e741fe192c2 Mon Sep 17 00:00:00 2001 From: Zach Eveland Date: Mon, 26 Mar 2012 16:02:40 -0400 Subject: added stub methods for begin() and end() to Mouse and Keyboard --- cores/arduino/HID.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'cores/arduino/HID.cpp') diff --git a/cores/arduino/HID.cpp b/cores/arduino/HID.cpp index 378a157..e7ee249 100644 --- a/cores/arduino/HID.cpp +++ b/cores/arduino/HID.cpp @@ -201,7 +201,15 @@ bool WEAK HID_Setup(Setup& setup) //================================================================================ // Mouse -Mouse_::Mouse_() : _buttons(0) +Mouse_::Mouse_(void) : _buttons(0) +{ +} + +void Mouse_::begin(void) +{ +} + +void Mouse_::end(void) { } @@ -253,7 +261,15 @@ bool Mouse_::isPressed(uint8_t b) //================================================================================ // Keyboard -Keyboard_::Keyboard_() +Keyboard_::Keyboard_(void) +{ +} + +void Keyboard_::begin(void) +{ +} + +void Keyboard_::end(void) { } -- cgit v1.2.3-18-g5258 From f646c9186d33b26adeaada088e9549417463cdab Mon Sep 17 00:00:00 2001 From: Zach Eveland Date: Mon, 26 Mar 2012 17:00:16 -0400 Subject: Keyboard.type() now just presses and releases the key indicated - doesn't releaseAll() --- cores/arduino/HID.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'cores/arduino/HID.cpp') diff --git a/cores/arduino/HID.cpp b/cores/arduino/HID.cpp index e7ee249..b0ef6e3 100644 --- a/cores/arduino/HID.cpp +++ b/cores/arduino/HID.cpp @@ -523,13 +523,10 @@ void Keyboard_::releaseAll(void) // type() does a press and release of the specified key. size_t Keyboard_::type(uint8_t c) -{ - releaseAll(); - // Keydown - press(c); - // Keyup - releaseAll(); - return 1; +{ + uint8_t p = press(c); // Keydown + uint8_t r = release(c); // Keyup + return (p&r); } #endif -- cgit v1.2.3-18-g5258 From 58c36f76d51d1fe4859140acf5c9c056b4204da5 Mon Sep 17 00:00:00 2001 From: Zach Eveland Date: Mon, 26 Mar 2012 17:28:02 -0400 Subject: added methods to Keyboard to handle multiple simultaneous key presses or releases (up to six each) --- cores/arduino/HID.cpp | 92 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 81 insertions(+), 11 deletions(-) (limited to 'cores/arduino/HID.cpp') diff --git a/cores/arduino/HID.cpp b/cores/arduino/HID.cpp index b0ef6e3..b556223 100644 --- a/cores/arduino/HID.cpp +++ b/cores/arduino/HID.cpp @@ -467,17 +467,6 @@ size_t Keyboard_::press(uint8_t k) // it shouldn't be repeated any more. size_t Keyboard_::release(uint8_t k) { - /* - uint8_t i; - k = pgm_read_byte(_asciimap + k); - if (!k) { - return 0; - } - if (k & 0x80) { - _keyReport.modifiers |= 0x02; // the left shift modifier - k &= 0x7F; - } - */ uint8_t i; if (k >= 136) { // it's a non-printing key (not a modifier) k = k - 136; @@ -509,6 +498,87 @@ size_t Keyboard_::release(uint8_t k) return 1; } +size_t Keyboard_::press(uint8_t k[], uint8_t len) { + uint8_t i; + uint8_t result = 0; + for (i=0; i Date: Wed, 28 Mar 2012 18:35:26 -0400 Subject: eliminated Keyboard.type() - unnecessary duplication of Keyboard.write() (David Mellis). Also edit KeyboardReprogram example which was the only example using type() --- cores/arduino/HID.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'cores/arduino/HID.cpp') diff --git a/cores/arduino/HID.cpp b/cores/arduino/HID.cpp index b556223..dfcbd9d 100644 --- a/cores/arduino/HID.cpp +++ b/cores/arduino/HID.cpp @@ -591,8 +591,7 @@ void Keyboard_::releaseAll(void) sendReport(&_keyReport); } -// type() does a press and release of the specified key. -size_t Keyboard_::type(uint8_t c) +size_t Keyboard_::write(uint8_t c) { uint8_t p = press(c); // Keydown uint8_t r = release(c); // Keyup -- cgit v1.2.3-18-g5258 From 83feb140138d1c7900619ac2a733885e192987a8 Mon Sep 17 00:00:00 2001 From: Zach Eveland Date: Wed, 28 Mar 2012 18:46:10 -0400 Subject: removed horrible multi-key Keyboard.press() and Keyboard.release() methods Saves 924 bytes of Flash --- cores/arduino/HID.cpp | 81 --------------------------------------------------- 1 file changed, 81 deletions(-) (limited to 'cores/arduino/HID.cpp') diff --git a/cores/arduino/HID.cpp b/cores/arduino/HID.cpp index dfcbd9d..cdf49bd 100644 --- a/cores/arduino/HID.cpp +++ b/cores/arduino/HID.cpp @@ -498,87 +498,6 @@ size_t Keyboard_::release(uint8_t k) return 1; } -size_t Keyboard_::press(uint8_t k[], uint8_t len) { - uint8_t i; - uint8_t result = 0; - for (i=0; i Date: Wed, 28 Mar 2012 19:46:32 -0400 Subject: fixed logic error in Keyboard.release() - now removes every occurrence of a key if it's present more than once --- cores/arduino/HID.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'cores/arduino/HID.cpp') diff --git a/cores/arduino/HID.cpp b/cores/arduino/HID.cpp index cdf49bd..ac63608 100644 --- a/cores/arduino/HID.cpp +++ b/cores/arduino/HID.cpp @@ -435,7 +435,7 @@ size_t Keyboard_::press(uint8_t k) setWriteError(); return 0; } - if (k & 0x80) { + if (k & 0x80) { // it's a capital letter or other character reached with shift _keyReport.modifiers |= 0x02; // the left shift modifier k &= 0x7F; } @@ -478,22 +478,20 @@ size_t Keyboard_::release(uint8_t k) if (!k) { return 0; } - if (k & 0x80) { + if (k & 0x80) { // it's a capital letter or other character reached with shift _keyReport.modifiers &= ~(0x02); // the left shift modifier k &= 0x7F; } } // Test the key report to see if k is present. Clear it if it exists. + // Check all positions in case the key is present more than once (which it shouldn't be) for (i=0; i<6; i++) { - if (_keyReport.keys[i] == k) { + if (0 != k && _keyReport.keys[i] == k) { _keyReport.keys[i] = 0x00; - break; } } - if (i == 6) { - return 0; - } + sendReport(&_keyReport); return 1; } @@ -514,7 +512,7 @@ size_t Keyboard_::write(uint8_t c) { uint8_t p = press(c); // Keydown uint8_t r = release(c); // Keyup - return (p&r); + return (p); // just return the result of press() since release() almost always returns 1 } #endif -- cgit v1.2.3-18-g5258