mirror of
https://github.com/arduino/Arduino.git
synced 2025-01-18 07:52:14 +01:00
Added USB KeyboardController library
This commit is contained in:
parent
a17c422b47
commit
74c2705aff
@ -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 <KeyboardController.h>
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
54
hardware/arduino/sam/libraries/USBHost/KeyboardController.h
Normal file
54
hardware/arduino/sam/libraries/USBHost/KeyboardController.h
Normal file
@ -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 <hidboot.h>
|
||||||
|
|
||||||
|
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<HID_PROTOCOL_KEYBOARD> hostKeyboard;
|
||||||
|
uint8_t key, keyOem, modifiers;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -27,8 +27,6 @@ enum MouseButton {
|
|||||||
RIGHT_BUTTON = 0x04
|
RIGHT_BUTTON = 0x04
|
||||||
};
|
};
|
||||||
|
|
||||||
extern MouseButton mouseButton;
|
|
||||||
|
|
||||||
class MouseController : public MouseReportParser
|
class MouseController : public MouseReportParser
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -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 <KeyboardController.h>
|
||||||
|
|
||||||
|
// 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();
|
||||||
|
}
|
@ -1,73 +0,0 @@
|
|||||||
#include <hidboot.h>
|
|
||||||
|
|
||||||
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<HID_PROTOCOL_KEYBOARD> Kbd(&Usb);
|
|
||||||
KbdRptParser Prs;
|
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
Serial1.begin(115200);
|
|
||||||
Serial1.println("Program started!");
|
|
||||||
delay(200);
|
|
||||||
|
|
||||||
Kbd.SetReportParser(0, &Prs);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
Usb.Task();
|
|
||||||
}
|
|
||||||
|
|
@ -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
|
// Require mouse control library
|
||||||
#include <MouseController.h>
|
#include <MouseController.h>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user