| 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 "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 |
// HID Request Type HID1.11 Page 51 7.2.1 Get_Report Request |
| 58 |
#define HID_REPORT_TYPE_INPUT 1 |
| 59 |
#define HID_REPORT_TYPE_OUTPUT 2 |
| 60 |
#define HID_REPORT_TYPE_FEATURE 3 |
| 61 |
|
| 62 |
typedef struct |
| 63 |
{ |
| 64 |
uint8_t len; // 9 |
| 65 |
uint8_t dtype; // 0x21 |
| 66 |
uint8_t addr; |
| 67 |
uint8_t versionL; // 0x101 |
| 68 |
uint8_t versionH; // 0x101 |
| 69 |
uint8_t country; |
| 70 |
uint8_t desctype; // 0x22 report |
| 71 |
uint8_t descLenL; |
| 72 |
uint8_t descLenH; |
| 73 |
} HIDDescDescriptor; |
| 74 |
|
| 75 |
typedef struct |
| 76 |
{ |
| 77 |
InterfaceDescriptor hid; |
| 78 |
HIDDescDescriptor desc; |
| 79 |
EndpointDescriptor in; |
| 80 |
} HIDDescriptor; |
| 81 |
|
| 82 |
class HIDSubDescriptor { |
| 83 |
public: |
| 84 |
HIDSubDescriptor *next = NULL; |
| 85 |
HIDSubDescriptor(const void *d, const uint16_t l) : data(d), length(l) { } |
| 86 |
|
| 87 |
const void* data; |
| 88 |
const uint16_t length; |
| 89 |
}; |
| 90 |
|
| 91 |
class HID_ : public PluggableUSBModule |
| 92 |
{ |
| 93 |
public: |
| 94 |
HID_(void); |
| 95 |
int begin(void); |
| 96 |
int SendReport(uint8_t id, const void* data, int len); |
| 97 |
void AppendDescriptor(HIDSubDescriptor* node); |
| 98 |
|
| 99 |
protected: |
| 100 |
// Implementation of the PluggableUSBModule |
| 101 |
int getInterface(uint8_t* interfaceCount); |
| 102 |
int getDescriptor(USBSetup& setup); |
| 103 |
bool setup(USBSetup& setup); |
| 104 |
uint8_t getShortName(char* name); |
| 105 |
|
| 106 |
private: |
| 107 |
uint8_t epType[1]; |
| 108 |
|
| 109 |
HIDSubDescriptor* rootNode; |
| 110 |
uint16_t descriptorSize; |
| 111 |
|
| 112 |
uint8_t protocol; |
| 113 |
uint8_t idle; |
| 114 |
}; |
| 115 |
|
| 116 |
// Replacement for global singleton. |
| 117 |
// This function prevents static-initialization-order-fiasco |
| 118 |
// https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use |
| 119 |
HID_& HID(); |
| 120 |
|
| 121 |
#define D_HIDREPORT(length) { 9, 0x21, 0x01, 0x01, 0, 1, 0x22, lowByte(length), highByte(length) } |
| 122 |
|
| 123 |
#endif // USBCON |
| 124 |
|
| 125 |
#endif // HID_h |