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