diff --git a/hardware/arduino/sam/libraries/USBHost/KeyboardController.cpp b/hardware/arduino/sam/libraries/USBHost/KeyboardController.cpp new file mode 100644 index 000000000..70c769f64 --- /dev/null +++ b/hardware/arduino/sam/libraries/USBHost/KeyboardController.cpp @@ -0,0 +1,40 @@ +/* + Copyright (c) 2012 Arduino. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +extern "C" { +void __keyboardControllerEmptyCallback() { } +} + +void keyPressed() __attribute__ ((weak, alias("__keyboardControllerEmptyCallback"))); +void keyReleased() __attribute__ ((weak, alias("__keyboardControllerEmptyCallback"))); + +void KeyboardController::OnKeyDown(uint8_t _mod, uint8_t _oemKey) { + modifiers = _mod; + keyOem = _oemKey; + key = OemToAscii(_mod, _oemKey); + keyPressed(); +} + +void KeyboardController::OnKeyUp(uint8_t _mod, uint8_t _oemKey) { + modifiers = _mod; + keyOem = _oemKey; + key = OemToAscii(_mod, _oemKey); + keyReleased(); +} diff --git a/hardware/arduino/sam/libraries/USBHost/KeyboardController.h b/hardware/arduino/sam/libraries/USBHost/KeyboardController.h new file mode 100644 index 000000000..e1b891bff --- /dev/null +++ b/hardware/arduino/sam/libraries/USBHost/KeyboardController.h @@ -0,0 +1,54 @@ +/* + Copyright (c) 2012 Arduino. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef KEYBOARD_CONTROLLER_H +#define KEYBOARD_CONTROLLER_H + +#include + +enum KeyboardModifiers { + LeftCtrl = 1, + LeftShift = 2, + Alt = 4, + LeftCmd = 8, + RightCtrl = 16, + RightShift = 32, + AltGr = 64, + RightCmd = 128 +}; + +class KeyboardController : public KeyboardReportParser { +public: + KeyboardController(USBHost &usb) : hostKeyboard(&usb), key(0), keyOem(0), modifiers(0) { + hostKeyboard.SetReportParser(0, this); + }; + + uint8_t getKey() { return key; }; + uint8_t getModifiers() { return modifiers; }; + uint8_t getOemKey() { return keyOem; }; + +protected: + virtual void OnKeyDown(uint8_t mod, uint8_t key); + virtual void OnKeyUp(uint8_t mod, uint8_t key); + +private: + HIDBoot hostKeyboard; + uint8_t key, keyOem, modifiers; +}; + +#endif diff --git a/hardware/arduino/sam/libraries/USBHost/MouseController.h b/hardware/arduino/sam/libraries/USBHost/MouseController.h index ad3412298..4c8c65e9c 100644 --- a/hardware/arduino/sam/libraries/USBHost/MouseController.h +++ b/hardware/arduino/sam/libraries/USBHost/MouseController.h @@ -27,8 +27,6 @@ enum MouseButton { RIGHT_BUTTON = 0x04 }; -extern MouseButton mouseButton; - class MouseController : public MouseReportParser { public: diff --git a/hardware/arduino/sam/libraries/USBHost/examples/KeyboardController/KeyboardController.ino b/hardware/arduino/sam/libraries/USBHost/examples/KeyboardController/KeyboardController.ino new file mode 100644 index 000000000..507349387 --- /dev/null +++ b/hardware/arduino/sam/libraries/USBHost/examples/KeyboardController/KeyboardController.ino @@ -0,0 +1,78 @@ +/* + Keyboard Controller HID Example + + Shows the output of a USB Keyboard connected to the USB + controller of an Arduino Due Board. + + created 8 Oct 2012 + by Cristian Maglie + */ + +// Require keyboard control library +#include + +// Initialize USB Controller +USBHost usb; + +// Attach keyboard controller to USB +KeyboardController keyboard(usb); + +// This function intercepts key press +void keyPressed() { + Serial.print("Pressed: "); + printKey(); +} + +// This function intercepts key release +void keyReleased() { + Serial.print("Released: "); + printKey(); +} + +void printKey() { + // getOemKey() returns the OEM-code associated with the key + Serial.print(" key:"); + Serial.print(keyboard.getOemKey()); + + // getModifiers() returns a bits field with the modifiers-keys + int mod = keyboard.getModifiers(); + Serial.print(" mod:"); + Serial.print(mod); + + Serial.print(" => "); + + if (mod & LeftCtrl) + Serial.print("L-Ctrl "); + if (mod & LeftShift) + Serial.print("L-Shift "); + if (mod & Alt) + Serial.print("Alt "); + if (mod & LeftCmd) + Serial.print("L-Cmd "); + if (mod & RightCtrl) + Serial.print("R-Ctrl "); + if (mod & RightShift) + Serial.print("R-Shift "); + if (mod & AltGr) + Serial.print("AltGr "); + if (mod & RightCmd) + Serial.print("R-Cmd "); + + // getKey() returns the ASCII translation of OEM key + // combined with modifiers. + Serial.write(keyboard.getKey()); + Serial.println(); +} + +void setup() +{ + Serial.begin(115200); + Serial.println("Program started"); + delay(200); +} + +void loop() +{ + // Process USB tasks + usb.Task(); +} diff --git a/hardware/arduino/sam/libraries/USBHost/examples/KeyboardControllerHID/KeyboardControllerHID.ino b/hardware/arduino/sam/libraries/USBHost/examples/KeyboardControllerHID/KeyboardControllerHID.ino deleted file mode 100644 index 2d46c479e..000000000 --- a/hardware/arduino/sam/libraries/USBHost/examples/KeyboardControllerHID/KeyboardControllerHID.ino +++ /dev/null @@ -1,73 +0,0 @@ -#include - -class KbdRptParser : -public KeyboardReportParser -{ - void PrintKey(uint8_t mod, uint8_t key); - -protected: - virtual void OnKeyDown(uint8_t mod, uint8_t key); - virtual void OnKeyUp(uint8_t mod, uint8_t key); - virtual void OnKeyPressed(uint8_t key); -}; - -void KbdRptParser::PrintKey(uint8_t m, uint8_t key) -{ - MODIFIERKEYS mod; - - *((uint8_t*)&mod) = m; - Serial1.print((mod.bmLeftCtrl == 1) ? "C" : " "); - Serial1.print((mod.bmLeftShift == 1) ? "S" : " "); - Serial1.print((mod.bmLeftAlt == 1) ? "A" : " "); - Serial1.print((mod.bmLeftGUI == 1) ? "G" : " "); - - Serial1.print("<"); - Serial1.print(key); - Serial1.print(">"); - - Serial1.print((mod.bmRightCtrl == 1) ? "C" : " "); - Serial1.print((mod.bmRightShift == 1) ? "S" : " "); - Serial1.print((mod.bmRightAlt == 1) ? "A" : " "); - Serial1.print((mod.bmRightGUI == 1) ? "G" : " "); -} - -void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key) -{ - Serial1.print("DOWN "); - PrintKey(mod, key); - uint8_t c = OemToAscii(mod, key); - - if (c) - OnKeyPressed(c); -} - -void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key) -{ - Serial1.print("UP "); - PrintKey(mod, key); -} - -void KbdRptParser::OnKeyPressed(uint8_t key) -{ - Serial1.print("ASCII: "); - Serial1.println(key); -} - -USBHost Usb; -HIDBoot Kbd(&Usb); -KbdRptParser Prs; - -void setup() -{ - Serial1.begin(115200); - Serial1.println("Program started!"); - delay(200); - - Kbd.SetReportParser(0, &Prs); -} - -void loop() -{ - Usb.Task(); -} - diff --git a/hardware/arduino/sam/libraries/USBHost/examples/MouseController/MouseController.ino b/hardware/arduino/sam/libraries/USBHost/examples/MouseController/MouseController.ino index d86c35c0c..731a252eb 100644 --- a/hardware/arduino/sam/libraries/USBHost/examples/MouseController/MouseController.ino +++ b/hardware/arduino/sam/libraries/USBHost/examples/MouseController/MouseController.ino @@ -1,3 +1,12 @@ +/* + Mouse Controller HID Example + + Shows the output of a USB Mouse connected to the USB + controller of an Arduino Due Board. + + created 8 Oct 2012 + by Cristian Maglie + */ // Require mouse control library #include