aboutsummaryrefslogtreecommitdiff
path: root/libraries/HID/HID.h
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/HID/HID.h')
-rw-r--r--libraries/HID/HID.h133
1 files changed, 78 insertions, 55 deletions
diff --git a/libraries/HID/HID.h b/libraries/HID/HID.h
index 89832a9..a9b3f58 100644
--- a/libraries/HID/HID.h
+++ b/libraries/HID/HID.h
@@ -1,38 +1,34 @@
/*
- HID.h
-
Copyright (c) 2015, Arduino LLC
Original code (pre-library): Copyright (c) 2011, Peter Barrett
- 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.
+ 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.
- 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
-*/
+ 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.
+ */
#ifndef HID_h
#define HID_h
#include <stdint.h>
#include <Arduino.h>
+#include "PluggableUSB.h"
#if defined(USBCON)
#define _USING_HID
-//================================================================================
-//================================================================================
-// HID 'Driver'
-
+// HID 'Driver'
+// ------------
#define HID_GET_REPORT 0x01
#define HID_GET_IDLE 0x02
#define HID_GET_PROTOCOL 0x03
@@ -44,54 +40,81 @@
#define HID_REPORT_DESCRIPTOR_TYPE 0x22
#define HID_PHYSICAL_DESCRIPTOR_TYPE 0x23
-typedef struct __attribute__((packed)) {
- u8 length;
- const void* descriptor;
-} HID_Descriptor;
+// HID subclass HID1.11 Page 8 4.2 Subclass
+#define HID_SUBCLASS_NONE 0
+#define HID_SUBCLASS_BOOT_INTERFACE 1
-class HIDDescriptorListNode {
-public:
- HIDDescriptorListNode *next = NULL;
- const HID_Descriptor * cb;
- HIDDescriptorListNode(const HID_Descriptor *ncb) {cb = ncb;}
-};
+// 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
-class HID_
-{
-public:
- HID_(void);
- int begin(void);
- void SendReport(uint8_t id, const void* data, int len);
- void AppendDescriptor(HIDDescriptorListNode* node);
-};
+// 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
typedef struct
{
- u8 len; // 9
- u8 dtype; // 0x21
- u8 addr;
- u8 versionL; // 0x101
- u8 versionH; // 0x101
- u8 country;
- u8 desctype; // 0x22 report
- u8 descLenL;
- u8 descLenH;
+ 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;
} HIDDescDescriptor;
typedef struct
{
- InterfaceDescriptor hid;
- HIDDescDescriptor desc;
- EndpointDescriptor in;
+ InterfaceDescriptor hid;
+ HIDDescDescriptor desc;
+ EndpointDescriptor in;
} HIDDescriptor;
-#define HID_TX HID_ENDPOINT_INT
+class HIDSubDescriptor {
+public:
+ HIDSubDescriptor *next = NULL;
+ HIDSubDescriptor(const void *d, const uint16_t l) : data(d), length(l) { }
+
+ const void* data;
+ const uint16_t length;
+};
+
+class HID_ : public PluggableUSBModule
+{
+public:
+ HID_(void);
+ int begin(void);
+ int SendReport(uint8_t id, const void* data, int len);
+ void AppendDescriptor(HIDSubDescriptor* node);
+
+protected:
+ // Implementation of the PluggableUSBModule
+ int getInterface(uint8_t* interfaceCount);
+ int getDescriptor(USBSetup& setup);
+ bool setup(USBSetup& setup);
+ uint8_t getShortName(char* name);
+
+private:
+ uint8_t epType[1];
+
+ HIDSubDescriptor* rootNode;
+ uint16_t descriptorSize;
+
+ uint8_t protocol;
+ uint8_t idle;
+};
-#define D_HIDREPORT(_descriptorLength) \
- { 9, 0x21, 0x1, 0x1, 0, 1, 0x22, _descriptorLength, 0 }
+// 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();
-#define WEAK __attribute__ ((weak))
+#define D_HIDREPORT(length) { 9, 0x21, 0x01, 0x01, 0, 1, 0x22, lowByte(length), highByte(length) }
-#endif
+#endif // USBCON
-#endif \ No newline at end of file
+#endif // HID_h