-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJoystickWin.cpp
139 lines (123 loc) · 4.73 KB
/
JoystickWin.cpp
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
/*
JoystickWin.cpp
*/
#include "JoystickWin.h"
#if defined(_USING_HID)
static const uint8_t _hidReportDescriptor[] PROGMEM = {
0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
0x09, 0x04, // Usage (Joystick)
0xA1, 0x01, // Collection (Application)
0x85, 0x04, // Report ID (4)
0x05, 0x09, // Usage Page (Button)
0x19, 0x01, // Usage Minimum (0x01)
0x29, 0x20, // Usage Maximum (0x20)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x01, // Logical Maximum (1)
0x75, 0x01, // Report Size (1)
0x95, 0x20, // Report Count (32)
0x55, 0x00, // Unit Exponent (0)
0x65, 0x00, // Unit (None)
0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
0x09, 0x39, // Usage (Hat switch)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x07, // Logical Maximum (7)
0x35, 0x00, // Physical Minimum (0)
0x46, 0x3B, 0x01, // Physical Maximum (315)
0x65, 0x14, // Unit (System: English Rotation, Length: Centimeter)
0x75, 0x04, // Report Size (4)
0x95, 0x01, // Report Count (1)
0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x09, 0x39, // Usage (Hat switch)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x07, // Logical Maximum (7)
0x35, 0x00, // Physical Minimum (0)
0x46, 0x3B, 0x01, // Physical Maximum (315)
0x65, 0x14, // Unit (System: English Rotation, Length: Centimeter)
0x75, 0x04, // Report Size (4)
0x95, 0x01, // Report Count (1)
0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x09, 0x01, // Usage (Pointer)
0x15, 0x00, // Logical Minimum (0)
0x27, 0xFF, 0xFF, 0x00, 0x00, // Logical Maximum (65534)
0x75, 0x10, // Report Size (16)
0x95, 0x06, // Report Count (6)
0xA1, 0x00, // Collection (Physical)
0x09, 0x30, // Usage (X)
0x09, 0x31, // Usage (Y)
0x09, 0x32, // Usage (Z)
0x09, 0x33, // Usage (Rx)
0x09, 0x34, // Usage (Ry)
0x09, 0x35, // Usage (Rz)
0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0xC0, // End Collection
0x05, 0x02, // Usage Page (Sim Ctrls)
0x15, 0x00, // Logical Minimum (0)
0x27, 0xFF, 0xFF, 0x00, 0x00, // Logical Maximum (65534)
0x75, 0x10, // Report Size (16)
0x95, 0x05, // Report Count (5)
0xA1, 0x00, // Collection (Physical)
0x09, 0xBA, // Usage (Rudder)
0x09, 0xBB, // Usage (Throttle)
0x09, 0xC4, // Usage (Accelerator)
0x09, 0xC5, // Usage (Brake)
0x09, 0xC8, // Usage (Steering)
0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0xC0, // End Collection
0xC0, // End Collection
};
//================================================================================
//================================================================================
// JoystickWin
JoystickWin_::JoystickWin_(void) {
static HIDSubDescriptor node(_hidReportDescriptor, sizeof(_hidReportDescriptor));
HID().AppendDescriptor(&node);
}
void JoystickWin_::begin(void) {
_hid_report.buttons = 0;
_hid_report.hat1 = 8;
_hid_report.hat2 = 8;
_hid_report.x = AXIS_MID;
_hid_report.y = AXIS_MID;
_hid_report.z = AXIS_MID;
_hid_report.rx = AXIS_MID;
_hid_report.ry = AXIS_MID;
_hid_report.rz = AXIS_MID;
_hid_report.rudder = AXIS_MID;
_hid_report.throttle = AXIS_MID;
_hid_report.accelerator = AXIS_MID;
_hid_report.brake = AXIS_MID;
_hid_report.steering = AXIS_MID;
sendReport();
}
void JoystickWin_::end(void) {
begin();
}
void JoystickWin_::sendReport() {
HID().SendReport(4, (void *)&_hid_report, sizeof(_hid_report));
}
void JoystickWin_::press(int b) {
b = constrain(b, 0, BUTTONS_MAX - 1);
_hid_report.buttons |= (uint32_t)(1UL << b);
}
void JoystickWin_::release(int b) {
b = constrain(b, 0, BUTTONS_MAX - 1);
_hid_report.buttons &= ~(uint32_t)(1UL << b);
}
void JoystickWin_::releaseAll() {
_hid_report.buttons = 0;
}
bool JoystickWin_::isPressed(int b) {
b = constrain(b, 0, BUTTONS_MAX - 1);
return ((_hid_report.buttons & (uint32_t)(1UL << b)) != 0) ? true : false;
}
void JoystickWin_::hat1(int d) {
d = constrain(d, 0, 15);
_hid_report.hat1 = d;
}
void JoystickWin_::hat2(int d) {
d = constrain(d, 0, 15);
_hid_report.hat2 = d;
}
JoystickWin_ JoystickWin;
#endif