-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathHID_Arduino.ino
126 lines (103 loc) · 2.6 KB
/
HID_Arduino.ino
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
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <SPI.h>
#include <usbhub.h>
#include <string.h>
#include "hidcustom.h"
signed char delta[3] = {0, 0, 0};
USB Usb;
USBHub Hub(&Usb);
HIDBoot<USB_HID_PROTOCOL_MOUSE> HidMouse(&Usb);
MouseRptParser Prs;
void HandleButtonChange(uint8_t prevState, uint8_t newState, uint8_t button);
void setup()
{
Serial.begin(57600);
Serial.setTimeout(1);
Usb.Init();
HidMouse.SetReportParser(0, &Prs);
Mouse.begin();
}
void loop()
{
memset(delta, 0, sizeof(delta));
Usb.Task();
if (Serial.available() > 0)
{
String command = Serial.readStringUntil('\n');
ParseSerialCommand(command);
}
Mouse.move(delta[0], delta[1], delta[2]);
}
void MouseRptParser::Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
{
MYMOUSEINFO *pmi = reinterpret_cast<MYMOUSEINFO*>(buf);
HandleButtonChange(prevState.mouseInfo.buttons, pmi->buttons, MOUSE_LEFT);
HandleButtonChange(prevState.mouseInfo.buttons, pmi->buttons, MOUSE_RIGHT);
HandleButtonChange(prevState.mouseInfo.buttons, pmi->buttons, MOUSE_MIDDLE);
HandleButtonChange(prevState.mouseInfo.buttons, pmi->buttons, MOUSE_PREV);
HandleButtonChange(prevState.mouseInfo.buttons, pmi->buttons, MOUSE_NEXT);
if (pmi->dX || pmi->dY)
{
OnMouseMove(pmi);
}
if (pmi->wheel)
{
OnWheelMove(pmi);
}
prevState.bInfo[0] = buf[0];
}
void MouseRptParser::OnMouseMove(MYMOUSEINFO *mi)
{
delta[0] = mi->dX;
delta[1] = mi->dY;
}
void MouseRptParser::OnWheelMove(MYMOUSEINFO *mi)
{
Mouse.move(0, 0, mi->wheel);
}
void ParseSerialCommand(const String& command)
{
if (command == "c")
{
Mouse.click();
}
else if (command == "r")
{
Mouse.release();
}
else if (command == "p")
{
Mouse.press();
}
else if (command.startsWith("m"))
{
ExecuteMouseMoveCommand(command);
}
}
void ExecuteMouseMoveCommand(const String& command)
{
String moveCommand = command;
moveCommand.replace("m", "");
int commaIndex = moveCommand.indexOf(',');
int x = moveCommand.substring(0, commaIndex).toInt();
int y = moveCommand.substring(commaIndex + 1).toInt();
Mouse.move(x, y);
}
void HandleButtonChange(uint8_t prevState, uint8_t newState, uint8_t button)
{
bool prevPressed = CHECK_BIT(prevState, button);
bool newPressed = CHECK_BIT(newState, button);
if (prevPressed != newPressed)
{
if (newPressed)
{
Mouse.press(button);
}
else
{
Mouse.release(button);
}
}
}