Skip to content

Commit 292a392

Browse files
committed
added HID library (from Arduino 1.6.6)
1 parent 519948c commit 292a392

File tree

4 files changed

+306
-0
lines changed

4 files changed

+306
-0
lines changed

1.0.2/libraries/HID/HID.cpp

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/* Copyright (c) 2015, Arduino LLC
2+
**
3+
** Original code (pre-library): Copyright (c) 2011, Peter Barrett
4+
**
5+
** Permission to use, copy, modify, and/or distribute this software for
6+
** any purpose with or without fee is hereby granted, provided that the
7+
** above copyright notice and this permission notice appear in all copies.
8+
**
9+
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10+
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11+
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
12+
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
13+
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
14+
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
15+
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16+
** SOFTWARE.
17+
*/
18+
19+
#include "HID.h"
20+
21+
#if defined(USBCON)
22+
23+
HID_& HID()
24+
{
25+
static HID_ obj;
26+
return obj;
27+
}
28+
29+
int HID_::getInterface(uint8_t* interfaceCount)
30+
{
31+
*interfaceCount += 1; // uses 1
32+
HIDDescriptor hidInterface = {
33+
D_INTERFACE(pluggedInterface, 1, USB_DEVICE_CLASS_HUMAN_INTERFACE, HID_SUBCLASS_NONE, HID_PROTOCOL_NONE),
34+
D_HIDREPORT(descriptorSize),
35+
D_ENDPOINT(USB_ENDPOINT_IN(pluggedEndpoint), USB_ENDPOINT_TYPE_INTERRUPT, 0x40, 0x01)
36+
};
37+
return USBD_SendControl(0, &hidInterface, sizeof(hidInterface));
38+
}
39+
40+
int HID_::getDescriptor(USBSetup& setup)
41+
{
42+
// Check if this is a HID Class Descriptor request
43+
if (setup.bmRequestType != REQUEST_DEVICETOHOST_STANDARD_INTERFACE) { return 0; }
44+
if (setup.wValueH != HID_REPORT_DESCRIPTOR_TYPE) { return 0; }
45+
46+
// In a HID Class Descriptor wIndex cointains the interface number
47+
if (setup.wIndex != pluggedInterface) { return 0; }
48+
49+
int total = 0;
50+
HIDSubDescriptor* node;
51+
for (node = rootNode; node; node = node->next) {
52+
int res = USBD_SendControl(0, node->data, node->length);
53+
if (res == -1)
54+
return -1;
55+
total += res;
56+
}
57+
return total;
58+
}
59+
60+
uint8_t HID_::getShortName(char *name)
61+
{
62+
name[0] = 'H';
63+
name[1] = 'I';
64+
name[2] = 'D';
65+
name[3] = 'A' + (descriptorSize & 0x0F);
66+
name[4] = 'A' + ((descriptorSize >> 4) & 0x0F);
67+
return 5;
68+
}
69+
70+
void HID_::AppendDescriptor(HIDSubDescriptor *node)
71+
{
72+
if (!rootNode) {
73+
rootNode = node;
74+
} else {
75+
HIDSubDescriptor *current = rootNode;
76+
while (current->next) {
77+
current = current->next;
78+
}
79+
current->next = node;
80+
}
81+
descriptorSize += node->length;
82+
}
83+
84+
int HID_::SendReport(uint8_t id, const void* data, int len)
85+
{
86+
uint8_t p[64];
87+
p[0] = id;
88+
memcpy(&p[1], data, len);
89+
return USBD_Send(pluggedEndpoint, p, len+1);
90+
}
91+
92+
bool HID_::setup(USBSetup& setup)
93+
{
94+
if (pluggedInterface != setup.wIndex) {
95+
return false;
96+
}
97+
98+
uint8_t request = setup.bRequest;
99+
uint8_t requestType = setup.bmRequestType;
100+
101+
if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE)
102+
{
103+
if (request == HID_GET_REPORT) {
104+
// TODO: HID_GetReport();
105+
return true;
106+
}
107+
if (request == HID_GET_PROTOCOL) {
108+
// TODO: Send8(protocol);
109+
return true;
110+
}
111+
if (request == HID_GET_IDLE) {
112+
// TODO: Send8(idle);
113+
}
114+
}
115+
116+
if (requestType == REQUEST_HOSTTODEVICE_CLASS_INTERFACE)
117+
{
118+
if (request == HID_SET_PROTOCOL) {
119+
// The USB Host tells us if we are in boot or report mode.
120+
// This only works with a real boot compatible device.
121+
protocol = setup.wValueL;
122+
return true;
123+
}
124+
if (request == HID_SET_IDLE) {
125+
idle = setup.wValueL;
126+
return true;
127+
}
128+
if (request == HID_SET_REPORT)
129+
{
130+
//uint8_t reportID = setup.wValueL;
131+
//uint16_t length = setup.wLength;
132+
//uint8_t data[length];
133+
// Make sure to not read more data than USB_EP_SIZE.
134+
// You can read multiple times through a loop.
135+
// The first byte (may!) contain the reportID on a multreport.
136+
//USB_RecvControl(data, length);
137+
}
138+
}
139+
140+
return false;
141+
}
142+
143+
HID_::HID_(void) : PluggableUSBModule(1, 1, epType),
144+
rootNode(NULL), descriptorSize(0),
145+
protocol(1), idle(1)
146+
{
147+
epType[0] = EP_TYPE_INTERRUPT_IN;
148+
PluggableUSB().plug(this);
149+
}
150+
151+
int HID_::begin(void)
152+
{
153+
return 0;
154+
}
155+
156+
#endif /* if defined(USBCON) */

1.0.2/libraries/HID/HID.h

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
Copyright (c) 2015, Arduino LLC
3+
Original code (pre-library): Copyright (c) 2011, Peter Barrett
4+
5+
Permission to use, copy, modify, and/or distribute this software for
6+
any purpose with or without fee is hereby granted, provided that the
7+
above copyright notice and this permission notice appear in all copies.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10+
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11+
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
12+
BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
13+
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
14+
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
15+
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16+
SOFTWARE.
17+
*/
18+
19+
#ifndef HID_h
20+
#define HID_h
21+
22+
#include <stdint.h>
23+
#include <Arduino.h>
24+
#include "USB/PluggableUSB.h"
25+
26+
#if defined(USBCON)
27+
28+
#define _USING_HID
29+
30+
// HID 'Driver'
31+
// ------------
32+
#define HID_GET_REPORT 0x01
33+
#define HID_GET_IDLE 0x02
34+
#define HID_GET_PROTOCOL 0x03
35+
#define HID_SET_REPORT 0x09
36+
#define HID_SET_IDLE 0x0A
37+
#define HID_SET_PROTOCOL 0x0B
38+
39+
#define HID_HID_DESCRIPTOR_TYPE 0x21
40+
#define HID_REPORT_DESCRIPTOR_TYPE 0x22
41+
#define HID_PHYSICAL_DESCRIPTOR_TYPE 0x23
42+
43+
// HID subclass HID1.11 Page 8 4.2 Subclass
44+
#define HID_SUBCLASS_NONE 0
45+
#define HID_SUBCLASS_BOOT_INTERFACE 1
46+
47+
// HID Keyboard/Mouse bios compatible protocols HID1.11 Page 9 4.3 Protocols
48+
#define HID_PROTOCOL_NONE 0
49+
#define HID_PROTOCOL_KEYBOARD 1
50+
#define HID_PROTOCOL_MOUSE 2
51+
52+
// Normal or bios protocol (Keyboard/Mouse) HID1.11 Page 54 7.2.5 Get_Protocol Request
53+
// "protocol" variable is used for this purpose.
54+
#define HID_BOOT_PROTOCOL 0
55+
#define HID_REPORT_PROTOCOL 1
56+
57+
typedef struct
58+
{
59+
uint8_t len; // 9
60+
uint8_t dtype; // 0x21
61+
uint8_t addr;
62+
uint8_t versionL; // 0x101
63+
uint8_t versionH; // 0x101
64+
uint8_t country;
65+
uint8_t desctype; // 0x22 report
66+
uint8_t descLenL;
67+
uint8_t descLenH;
68+
} HIDDescDescriptor;
69+
70+
typedef struct
71+
{
72+
InterfaceDescriptor hid;
73+
HIDDescDescriptor desc;
74+
EndpointDescriptor in;
75+
} HIDDescriptor;
76+
77+
class HIDSubDescriptor {
78+
public:
79+
HIDSubDescriptor *next = NULL;
80+
HIDSubDescriptor(const void *d, const uint16_t l) : data(d), length(l) { }
81+
82+
const void* data;
83+
const uint16_t length;
84+
};
85+
86+
class HID_ : public PluggableUSBModule
87+
{
88+
public:
89+
HID_(void);
90+
int begin(void);
91+
int SendReport(uint8_t id, const void* data, int len);
92+
void AppendDescriptor(HIDSubDescriptor* node);
93+
94+
protected:
95+
// Implementation of the PluggableUSBModule
96+
int getInterface(uint8_t* interfaceCount);
97+
int getDescriptor(USBSetup& setup);
98+
bool setup(USBSetup& setup);
99+
uint8_t getShortName(char* name);
100+
101+
private:
102+
uint32_t epType[1];
103+
104+
HIDSubDescriptor* rootNode;
105+
uint16_t descriptorSize;
106+
107+
uint8_t protocol;
108+
uint8_t idle;
109+
};
110+
111+
// Replacement for global singleton.
112+
// This function prevents static-initialization-order-fiasco
113+
// https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use
114+
HID_& HID();
115+
116+
#define D_HIDREPORT(length) { 9, 0x21, 0x01, 0x01, 0, 1, 0x22, lowByte(length), highByte(length) }
117+
118+
#endif // USBCON
119+
120+
#endif // HID_h

1.0.2/libraries/HID/keywords.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#######################################
2+
# Syntax Coloring Map HID
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
HID KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
begin KEYWORD2
15+
SendReport KEYWORD2
16+
AppendDescriptor KEYWORD2
17+
18+
#######################################
19+
# Constants (LITERAL1)
20+
#######################################
21+
HID_TX LITERAL1
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=HID
2+
version=1.0
3+
author=Arduino
4+
maintainer=Arduino <info@arduino.cc>
5+
sentence=Module for PluggableUSB infrastructure. Exposes an API for devices like Keyboards, Mice and Gamepads
6+
paragraph=
7+
category=Communication
8+
url=http://www.arduino.cc/en/Reference/HID
9+
architectures=sam

0 commit comments

Comments
 (0)