-
Notifications
You must be signed in to change notification settings - Fork 36
/
DigiMouse.h
342 lines (277 loc) · 10.3 KB
/
DigiMouse.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* Based on Obdev's AVRUSB code and under the same license.
*
* TODO: Make a proper file header. :-)
* Modified for Digispark by Digistump
* And now modified by Sean Murphy (duckythescientist) from a keyboard device to a mouse device
* Most of the credit for the joystick code should go to Raphaël Assénat
* And now mouse credit is due to Yiyin Ma and Abby Lin of Cornell
*/
#ifndef __DigiMouse_h__
#define __DigiMouse_h__
#define REPORT_SIZE 4
#include <Arduino.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <string.h>
#include <avr/io.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include "usbdrv.h"
//#include "devdesc.h"
#include "oddebug.h"
#include "usbconfig.h"
static const uchar *rt_usbHidReportDescriptor = NULL;
static uchar rt_usbHidReportDescriptorSize = 0;
static const uchar *rt_usbDeviceDescriptor = NULL;
static uchar rt_usbDeviceDescriptorSize = 0;
#define MOUSEBTN_LEFT_MASK 0x01
#define MOUSEBTN_RIGHT_MASK 0x02
#define MOUSEBTN_MIDDLE_MASK 0x04
// TODO: Work around Arduino 12 issues better.
//#include <WConstants.h>
//#undef int()
typedef uint8_t byte;
/* What was most recently read from the controller */
static unsigned char last_built_report[REPORT_SIZE];
/* What was most recently sent to the host */
static unsigned char last_sent_report[REPORT_SIZE];
uchar reportBuffer[REPORT_SIZE];
// report frequency set to default of 50hz
#define DIGIMOUSE_DEFAULT_REPORT_INTERVAL 20
static unsigned char must_report = 0;
static unsigned char idle_rate = DIGIMOUSE_DEFAULT_REPORT_INTERVAL / 4; // in units of 4ms
// new minimum report frequency system:
static unsigned long last_report_time = 0;
char usb_hasCommed = 0;
const PROGMEM unsigned char mouse_usbHidReportDescriptor[] = { /* USB report descriptor */
(unsigned char) 0x05, (unsigned char) 0x01, // USAGE_PAGE (Generic Desktop)
(unsigned char) 0x09, (unsigned char) 0x02, // USAGE (Mouse)
(unsigned char) 0xa1, (unsigned char) 0x01, // COLLECTION (Application)
(unsigned char) 0x09, (unsigned char) 0x01, // USAGE_PAGE (Pointer)
(unsigned char) 0xa1, (unsigned char) 0x00, // COLLECTION (Physical)
(unsigned char) 0x05, (unsigned char) 0x09, // USAGE_PAGE (Button)
(unsigned char) 0x19, (unsigned char) 0x01, // USAGE_MINIMUM (Button 1)
(unsigned char) 0x29, (unsigned char) 0x03, // USAGE_MAXIMUM (Button 3)
(unsigned char) 0x15, (unsigned char) 0x00, // LOGICAL_MINIMUM (0)
(unsigned char) 0x25, (unsigned char) 0x01, // LOGICAL_MAXIMUM (1)
(unsigned char) 0x95, (unsigned char) 0x03, // REPORT_COUNT (3)
(unsigned char) 0x75, (unsigned char) 0x01, // REPORT_SIZE (1)
(unsigned char) 0x81, (unsigned char) 0x02, // INPUT (Data,Var,Abs)
(unsigned char) 0x95, (unsigned char) 0x01, // REPORT_COUNT (1)
(unsigned char) 0x75, (unsigned char) 0x05, // REPORT_SIZE (5)
(unsigned char) 0x81, (unsigned char) 0x01, // Input(Cnst)
(unsigned char) 0x05, (unsigned char) 0x01, // USAGE_PAGE(Generic Desktop)
(unsigned char) 0x09, (unsigned char) 0x30, // USAGE(X)
(unsigned char) 0x09, (unsigned char) 0x31, // USAGE(Y)
(unsigned char) 0x15, (unsigned char) 0x81, // LOGICAL_MINIMUM (-127)
(unsigned char) 0x25, (unsigned char) 0x7f, // LOGICAL_MAXIMUM (127)
(unsigned char) 0x75, (unsigned char) 0x08, // REPORT_SIZE (8)
(unsigned char) 0x95, (unsigned char) 0x02, // REPORT_COUNT (2)
(unsigned char) 0x81, (unsigned char) 0x06, // INPUT (Data,Var,Rel)
(unsigned char) 0x09, (unsigned char) 0x38, // Usage (Wheel)
(unsigned char) 0x95, (unsigned char) 0x01, // Report Count (1),
(unsigned char) 0x81, (unsigned char) 0x06, // Input (Data, Variable, Relative)
(unsigned char) 0xc0, // END_COLLECTION
(unsigned char) 0xc0 // END_COLLECTION
};
#define USBDESCR_DEVICE 1
const unsigned char usbDescrDevice[] PROGMEM = { /* USB device descriptor */
18, /* sizeof(usbDescrDevice): length of descriptor in bytes */
USBDESCR_DEVICE, /* descriptor type */
0x01, 0x01, /* USB version supported */
USB_CFG_DEVICE_CLASS,
USB_CFG_DEVICE_SUBCLASS,
0, /* protocol */
8, /* max packet size */
USB_CFG_VENDOR_ID, /* 2 bytes */
USB_CFG_DEVICE_ID, /* 2 bytes */
USB_CFG_DEVICE_VERSION, /* 2 bytes */
#if USB_CFG_VENDOR_NAME_LEN
1, /* manufacturer string index */
#else
0, /* manufacturer string index */
#endif
#if USB_CFG_DEVICE_NAME_LEN
2, /* product string index */
#else
0, /* product string index */
#endif
#if USB_CFG_SERIAL_NUMBER_LENGTH
3, /* serial number string index */
#else
0, /* serial number string index */
#endif
1, /* number of configurations */
};
void buildReport(unsigned char *reportBuf) {
if (reportBuf != NULL) {
memcpy(reportBuf, last_built_report, REPORT_SIZE);
}
memcpy(last_sent_report, last_built_report, REPORT_SIZE);
}
void clearMove() {
// because we send deltas in movement, so when we send them, we clear them
last_built_report[1] = 0;
last_built_report[2] = 0;
last_built_report[3] = 0;
last_sent_report[1] = 0;
last_sent_report[2] = 0;
last_sent_report[3] = 0;
}
class DigiMouseDevice {
public:
DigiMouseDevice () {
rt_usbHidReportDescriptor = mouse_usbHidReportDescriptor;
rt_usbHidReportDescriptorSize = sizeof(mouse_usbHidReportDescriptor);
rt_usbDeviceDescriptor = usbDescrDevice;
rt_usbDeviceDescriptorSize = sizeof(usbDescrDevice);
}
void begin() {
cli();
usbDeviceDisconnect();
_delay_ms(200);
usbDeviceConnect();
usbInit();
sei();
last_report_time = millis();
}
void refresh() {
update();
}
void poll() {
update();
}
void update() {
usbPoll();
// instead of above code, use millis arduino system to enforce minimum reporting frequency
unsigned long time_since_last_report = millis() - last_report_time;
/* idle_rate == 0, never send idle reports
cf https://www.usb.org/sites/default/files/hid1_11.pdf 7.2.4 "Set_Idle Request":
"When the upper byte of wValue is 0 (zero), the duration is indefinite. The endpoint will
inhibit reporting forever, only reporting when a change is detected in the report data." */
if (idle_rate > 0 && time_since_last_report >= (idle_rate * 4 /* in units of 4ms - usb spec stuff */)) {
last_report_time += idle_rate * 4;
must_report = 1;
}
// if the report has changed, try force an update anyway
if (memcmp(last_built_report, last_sent_report, REPORT_SIZE)) {
must_report = 1;
}
// if we want to send a report, signal the host computer to ask us for it with a usb 'interrupt'
if (must_report) {
if (usbInterruptIsReady()) {
must_report = 0;
buildReport(reportBuffer); // put data into reportBuffer
clearMove(); // clear deltas
usbSetInterrupt(reportBuffer, REPORT_SIZE);
}
}
}
// delay while updating until we are finished
void delay(long milli) {
unsigned long last = millis();
while (milli > 0) {
unsigned long now = millis();
milli -= now - last;
last = now;
update();
}
}
void moveX(char deltaX) {
if (deltaX == -128) deltaX = -127;
last_built_report[1] = *(reinterpret_cast<unsigned char *>(&deltaX));
}
void moveY(char deltaY) {
if (deltaY == -128) deltaY = -127;
last_built_report[2] = *(reinterpret_cast<unsigned char *>(&deltaY));
}
void scroll(char deltaS) {
if (deltaS == -128) deltaS = -127;
last_built_report[3] = *(reinterpret_cast<unsigned char *>(&deltaS));
}
void move(char deltaX, char deltaY, char deltaS) {
if (deltaX == -128) deltaX = -127;
if (deltaY == -128) deltaY = -127;
if (deltaS == -128) deltaS = -127;
last_built_report[1] = *(reinterpret_cast<unsigned char *>(&deltaX));
last_built_report[2] = *(reinterpret_cast<unsigned char *>(&deltaY));
last_built_report[3] = *(reinterpret_cast<unsigned char *>(&deltaS));
}
void move(char deltaX, char deltaY, char deltaS, char buttons) {
if (deltaX == -128) deltaX = -127;
if (deltaY == -128) deltaY = -127;
if (deltaS == -128) deltaS = -127;
last_built_report[0] = buttons;
last_built_report[1] = *(reinterpret_cast<unsigned char *>(&deltaX));
last_built_report[2] = *(reinterpret_cast<unsigned char *>(&deltaY));
last_built_report[3] = *(reinterpret_cast<unsigned char *>(&deltaS));
}
void rightClick(){
last_built_report[0] = MOUSEBTN_RIGHT_MASK;
}
void leftClick(){
last_built_report[0] = MOUSEBTN_LEFT_MASK;
}
void middleClick(){
last_built_report[0] = MOUSEBTN_MIDDLE_MASK;
}
void setButtons(unsigned char buttons) {
last_built_report[0] = buttons;
}
void setValues(unsigned char values[]) {
memcpy(last_built_report, values, REPORT_SIZE);
}
//private: TODO: Make friend?
// what does this even mean? -- Bluebie
};
// create the global singleton DigiMouse
DigiMouseDevice DigiMouse = DigiMouseDevice();
#ifdef __cplusplus
extern "C"{
#endif
// USB_PUBLIC uchar usbFunctionSetup
uchar usbFunctionSetup(uchar data[8]) {
usbRequest_t *rq = (usbRequest_t *)data;
usbMsgPtr = reportBuffer;
if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) { /* class request type */
if (rq->bRequest == USBRQ_HID_GET_REPORT) { /* wValue: ReportType (highbyte), ReportID (lowbyte) */
/* we only have one report type, so don't look at wValue */
//curGamepad->buildReport(reportBuffer);
//return curGamepad->report_size;
return REPORT_SIZE;
} else if (rq->bRequest == USBRQ_HID_GET_IDLE) {
usbMsgPtr = &idle_rate;
return 1;
} else if (rq->bRequest == USBRQ_HID_SET_IDLE) {
idle_rate = rq->wValue.bytes[1];
}
} else {
/* no vendor specific requests implemented */
}
return 0;
}
uchar usbFunctionDescriptor(struct usbRequest *rq) {
if ((rq->bmRequestType & USBRQ_TYPE_MASK) != USBRQ_TYPE_STANDARD) {
return 0;
}
if (rq->bRequest == USBRQ_GET_DESCRIPTOR) {
// USB spec 9.4.3, high byte is descriptor type
switch (rq->wValue.bytes[1]) {
case USBDESCR_DEVICE:
usbMsgPtr = rt_usbDeviceDescriptor;
return rt_usbDeviceDescriptorSize;
break;
case USBDESCR_HID_REPORT:
usbMsgPtr = rt_usbHidReportDescriptor;
return rt_usbHidReportDescriptorSize;
break;
}
}
return 0;
}
#ifdef __cplusplus
} // extern "C"
#endif
#endif // __DigiMouse_h__