-
-
Notifications
You must be signed in to change notification settings - Fork 406
/
Copy pathRawHID.h
178 lines (146 loc) · 4.43 KB
/
RawHID.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
Copyright (c) 2014-2015 NicoHood
See the readme for credit to other people.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// Include guard
#pragma once
#include <Arduino.h>
#include "HID.h"
#include "HID-Settings.h"
// RawHID might never work with multireports, because of OS problems
// therefore we have to make it a single report with no ID. No other HID device will be supported then.
#undef RAWHID_USAGE_PAGE
#define RAWHID_USAGE_PAGE 0xFFC0 // recommended: 0xFF00 to 0xFFFF
#undef RAWHID_USAGE
#define RAWHID_USAGE 0x0C00 // recommended: 0x0100 to 0xFFFF
// Keep one byte offset for the reportID if used
#if (HID_REPORTID_RAWHID)
#define RAWHID_SIZE (USB_EP_SIZE-1)
#error RAWHID does not work properly with a report ID and multiple reports.
#error Please remove this manually if you know what you are doing.
#else
#define RAWHID_SIZE (USB_EP_SIZE)
#endif
#undef RAWHID_TX_SIZE
#define RAWHID_TX_SIZE RAWHID_SIZE
#undef RAWHID_RX_SIZE
#define RAWHID_RX_SIZE RAWHID_SIZE
typedef union ATTRIBUTE_PACKED {
// a RAWHID_TX_SIZE byte buffer for tx
uint8_t whole8[0];
uint16_t whole16[0];
uint32_t whole32[0];
uint8_t buff[RAWHID_TX_SIZE];
} HID_RawKeyboardTXReport_Data_t;
typedef union ATTRIBUTE_PACKED {
// a RAWHID_TX_SIZE byte buffer for rx
uint8_t whole8[0];
uint16_t whole16[0];
uint32_t whole32[0];
uint8_t buff[RAWHID_RX_SIZE];
} HID_RawKeyboardRXReport_Data_t;
class RawHID_ : public PluggableUSBModule, public Stream
{
public:
RawHID_(void);
void setFeatureReport(void* report, int length){
if(length > 0){
featureReport = (uint8_t*)report;
featureLength = length;
// Disable feature report by default
disableFeatureReport();
}
}
int availableFeatureReport(void){
if(featureLength < 0){
return featureLength & ~0x8000;
}
return 0;
}
void enableFeatureReport(void){
featureLength &= ~0x8000;
}
void disableFeatureReport(void){
featureLength |= 0x8000;
}
void begin(void* report, int length){
if(length > 0){
data = (uint8_t*)report;
dataLength = length;
dataAvailable = 0;
}
}
void end(void){
disable();
dataLength = 0;
}
void enable(void){
dataAvailable = 0;
}
void disable(void){
dataAvailable = -1;
}
virtual int available(void){
if(dataAvailable < 0){
return 0;
}
return dataAvailable;
}
virtual int read(){
// Check if we have data available
if(dataAvailable > 0)
{
// Get next data byte (from the start to the end)
return data[dataLength - dataAvailable--];
}
return -1;
}
virtual int peek(){
// Check if we have data available
if(dataAvailable > 0){
return data[dataLength - dataAvailable];
}
return -1;
}
virtual void flush(void){
// Writing will always flush by the USB driver
}
// Wrapper for a single byte
using Print::write;
virtual size_t write(uint8_t b){
return write(&b, 1);
}
virtual size_t write(uint8_t *buffer, size_t size){
return USB_Send(pluggedEndpoint | TRANSFER_RELEASE, buffer, size);
}
protected:
// Implementation of the PUSBListNode
int getInterface(uint8_t* interfaceCount);
int getDescriptor(USBSetup& setup);
bool setup(USBSetup& setup);
EPTYPE_DESCRIPTOR_SIZE epType[1];
uint8_t protocol;
uint8_t idle;
// Buffer pointers to hold the received data
int dataLength;
int dataAvailable;
uint8_t* data;
uint8_t* featureReport;
int featureLength;
};
extern RawHID_ RawHID;