2015-06-05 17:54:46 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2015, Arduino LLC
|
|
|
|
Original code (pre-library): Copyright (c) 2011, Peter Barrett
|
|
|
|
|
2015-10-01 12:50:18 +02:00
|
|
|
Permission to use, copy, modify, and/or distribute this software for
|
|
|
|
any purpose with or without fee is hereby granted, provided that the
|
|
|
|
above copyright notice and this permission notice appear in all copies.
|
2015-06-05 17:54:46 +02:00
|
|
|
|
2015-10-01 12:50:18 +02:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
|
|
|
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
|
|
|
|
BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
|
|
|
|
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
|
|
|
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
|
|
|
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
2015-06-05 17:54:46 +02:00
|
|
|
|
|
|
|
#ifndef HID_h
|
|
|
|
#define HID_h
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2015-06-08 10:54:12 +02:00
|
|
|
#include <Arduino.h>
|
2015-09-29 17:10:48 +02:00
|
|
|
#include "PluggableUSB.h"
|
2015-06-05 17:54:46 +02:00
|
|
|
|
|
|
|
#if defined(USBCON)
|
|
|
|
|
2015-07-15 16:15:46 +02:00
|
|
|
#define _USING_HID
|
|
|
|
|
2015-09-30 16:19:21 +02:00
|
|
|
// HID 'Driver'
|
|
|
|
// ------------
|
2015-07-01 10:08:03 +02:00
|
|
|
#define HID_GET_REPORT 0x01
|
|
|
|
#define HID_GET_IDLE 0x02
|
|
|
|
#define HID_GET_PROTOCOL 0x03
|
|
|
|
#define HID_SET_REPORT 0x09
|
|
|
|
#define HID_SET_IDLE 0x0A
|
|
|
|
#define HID_SET_PROTOCOL 0x0B
|
|
|
|
|
|
|
|
#define HID_HID_DESCRIPTOR_TYPE 0x21
|
|
|
|
#define HID_REPORT_DESCRIPTOR_TYPE 0x22
|
|
|
|
#define HID_PHYSICAL_DESCRIPTOR_TYPE 0x23
|
|
|
|
|
2015-10-10 12:49:45 +02:00
|
|
|
// HID subclass HID1.11 Page 8 4.2 Subclass
|
|
|
|
#define HID_SUBCLASS_NONE 0
|
|
|
|
#define HID_SUBCLASS_BOOT_INTERFACE 1
|
|
|
|
|
|
|
|
// HID Keyboard/Mouse bios compatible protocols HID1.11 Page 9 4.3 Protocols
|
|
|
|
#define HID_PROTOCOL_NONE 0
|
|
|
|
#define HID_PROTOCOL_KEYBOARD 1
|
|
|
|
#define HID_PROTOCOL_MOUSE 2
|
|
|
|
|
|
|
|
// Normal or bios protocol (Keyboard/Mouse) HID1.11 Page 54 7.2.5 Get_Protocol Request
|
|
|
|
// "protocol" variable is used for this purpose.
|
|
|
|
#define HID_BOOT_PROTOCOL 0
|
|
|
|
#define HID_REPORT_PROTOCOL 1
|
|
|
|
|
2015-06-05 17:54:46 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
2015-09-28 15:44:32 +02:00
|
|
|
uint8_t len; // 9
|
|
|
|
uint8_t dtype; // 0x21
|
|
|
|
uint8_t addr;
|
|
|
|
uint8_t versionL; // 0x101
|
|
|
|
uint8_t versionH; // 0x101
|
|
|
|
uint8_t country;
|
|
|
|
uint8_t desctype; // 0x22 report
|
|
|
|
uint8_t descLenL;
|
|
|
|
uint8_t descLenH;
|
2015-06-05 17:54:46 +02:00
|
|
|
} HIDDescDescriptor;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2015-09-28 15:44:32 +02:00
|
|
|
InterfaceDescriptor hid;
|
|
|
|
HIDDescDescriptor desc;
|
|
|
|
EndpointDescriptor in;
|
2015-06-05 17:54:46 +02:00
|
|
|
} HIDDescriptor;
|
|
|
|
|
2015-10-12 12:17:42 +02:00
|
|
|
class HIDSubDescriptor {
|
2015-09-30 16:19:21 +02:00
|
|
|
public:
|
2015-10-12 12:17:42 +02:00
|
|
|
HIDSubDescriptor *next = NULL;
|
|
|
|
HIDSubDescriptor(const void *d, const uint16_t l) : data(d), length(l) { }
|
2015-09-30 16:19:21 +02:00
|
|
|
|
|
|
|
const void* data;
|
2015-10-12 11:59:08 +02:00
|
|
|
const uint16_t length;
|
2015-09-30 16:19:21 +02:00
|
|
|
};
|
|
|
|
|
2015-10-12 12:14:07 +02:00
|
|
|
class HID_ : public PluggableUSBModule
|
2015-09-30 16:19:21 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
HID_(void);
|
|
|
|
int begin(void);
|
|
|
|
void SendReport(uint8_t id, const void* data, int len);
|
2015-10-12 12:17:42 +02:00
|
|
|
void AppendDescriptor(HIDSubDescriptor* node);
|
2015-09-30 16:19:21 +02:00
|
|
|
|
2015-09-30 16:35:43 +02:00
|
|
|
protected:
|
2015-10-12 12:14:07 +02:00
|
|
|
// Implementation of the PluggableUSBModule
|
2015-10-07 13:21:21 +02:00
|
|
|
int getInterface(uint8_t* interfaceCount);
|
2015-10-07 19:02:40 +02:00
|
|
|
int getDescriptor(USBSetup& setup);
|
|
|
|
bool setup(USBSetup& setup);
|
2015-09-30 16:19:21 +02:00
|
|
|
|
2015-09-30 16:35:43 +02:00
|
|
|
private:
|
2015-09-30 18:45:09 +02:00
|
|
|
uint8_t epType[1];
|
2015-09-30 16:19:21 +02:00
|
|
|
|
2015-10-12 12:17:42 +02:00
|
|
|
HIDSubDescriptor* rootNode;
|
2015-10-01 12:50:18 +02:00
|
|
|
uint16_t descriptorSize;
|
2015-09-30 16:19:21 +02:00
|
|
|
|
2015-09-30 17:13:54 +02:00
|
|
|
uint8_t protocol;
|
|
|
|
uint8_t idle;
|
2015-09-30 16:19:21 +02:00
|
|
|
};
|
|
|
|
|
2015-10-01 17:35:26 +02:00
|
|
|
// Replacement for global singleton.
|
|
|
|
// This function prevents static-initialization-order-fiasco
|
|
|
|
// https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use
|
|
|
|
HID_& HID();
|
|
|
|
|
2015-09-30 20:53:05 +02:00
|
|
|
#define D_HIDREPORT(length) { 9, 0x21, 0x01, 0x01, 0, 1, 0x22, lowByte(length), highByte(length) }
|
2015-06-05 17:54:46 +02:00
|
|
|
|
2015-10-01 12:50:18 +02:00
|
|
|
#endif // USBCON
|
2015-06-05 17:54:46 +02:00
|
|
|
|
2015-10-01 12:50:18 +02:00
|
|
|
#endif // HID_h
|