-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHIDjoy.h
289 lines (253 loc) · 12.3 KB
/
HIDjoy.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
#include <Arduino.h>
#include <USBComposite.h>
#include "FFBdesc.h"
#include "FFBenum.h"
// =====================================================================
enum {
// INPUT
JOYSTICK=0, // id=1
PID_STATE, // id=2
// OUTPUT
SET_EFFECT, // id=1
SET_ENVELOPE, // id=2
SET_CONDITION, // id=3
SET_PERIODIC, // id=4
SET_CONSTANT_FORCE, // id=5
SET_RAMP_FORCE, // id=6
CUSTOM_FORCE_DATA, // id=7
DOWNLOAD_FORCE, // id=8
EFFECT_OPERATION, // id=10
PID_BLOCK_FREE, // id=11
PID_DEVICE_CONTROL, // id=12
DEVICE_GAIN, // id=13
SET_CUSTOM_FORCE, // id=14
// FEATURE
CREATE_NEW_EFFECT, // id=1
BLOCK_LOAD, // id=2
PID_POOL, // id=3
// COUNT
REPORTS_COUNT
} reports_e;
class HID_Joystick {
protected :
USBHID* HID;
HIDReporter reporter[REPORTS_COUNT];
HIDBuffer_t buffer[REPORTS_COUNT];
struct {
// INPUT
struct {
ReportJoy_t Joystick;
ReportStatePID_t StatePID;
} input;
// OUTPUT
struct {
ReportSetEffect_t SetEffect;
ReportSetEnvelope_t SetEnvelope;
ReportSetCondition_t SetCondition;
ReportSetPeriodic_t SetPeriodic;
ReportSetConstantForce_t SetConstantForce;
ReportSetRampForce_t SetRampForce;
ReportCustomForceData_t CustomForceData;
ReportDownloadForce_t DownloadForce;
ReportEffectOperation_t EffectOperation;
ReportBlockFree_t BlockFree;
ReportDeviceControl_t DeviceControl;
ReportDeviceGain_t DeviceGain;
ReportSetCustomForce_t SetCustomForce;
} output;
// FEATURE
struct {
ReportNewEffect_t NewEffect;
ReportLoadPID_t BlockLoad;
ReportPoolPID_t PoolPID;
} feature;
} data;
volatile uint8_t bufSetEffect[HID_BUFFER_ALLOCATE_SIZE(SIZE_EFFREP,1)] = {0};
volatile uint8_t bufSetEnvelope[HID_BUFFER_ALLOCATE_SIZE(SIZE_ENVREP,1)] = {0};
volatile uint8_t bufSetCondition[HID_BUFFER_ALLOCATE_SIZE(SIZE_CONDREP,1)] = {0};
volatile uint8_t bufSetPeriodic[HID_BUFFER_ALLOCATE_SIZE(SIZE_PRIDREP,1)] = {0};
volatile uint8_t bufSetConstantForce[HID_BUFFER_ALLOCATE_SIZE(SIZE_CONSTREP,1)] = {0};
volatile uint8_t bufSetRampForce[HID_BUFFER_ALLOCATE_SIZE(SIZE_RAMPREP,1)] = {0};
volatile uint8_t bufDeviceControl[HID_BUFFER_ALLOCATE_SIZE(SIZE_CTRLREP,1)] = {0};
volatile uint8_t bufDeviceGain[HID_BUFFER_ALLOCATE_SIZE(SIZE_GAINREP,1)] = {0};
volatile uint8_t bufNewEffect[HID_BUFFER_ALLOCATE_SIZE(SIZE_NEWEFREP,1)] = {0};
volatile uint8_t bufBlockLoad[HID_BUFFER_ALLOCATE_SIZE(SIZE_BLKLDREP,1)] = {0};
volatile uint8_t bufPoolPID[HID_BUFFER_ALLOCATE_SIZE(SIZE_POOLREP,1)] = {0};
bool manualReport = false;
inline void safeSendReportJoy(void) {
if(!manualReport) reporter[JOYSTICK].sendReport();
}
private :
uint8_t printBuf(volatile uint8_t* data, uint8_t size) {
for(uint8_t i=0; i<size; i++) {
CompositeSerial.print(data[i], HEX);
CompositeSerial.print(" ");
}
CompositeSerial.println();
}
public :
void button(uint8_t button, bool val);
void hat(uint8_t num, int16_t dir);
void Xrotate(uint16_t val);
void Yrotate(uint16_t val);
void sliderLeft(uint16_t val);
void sliderRight(uint16_t val);
void steering(uint8_t val); // рулевое управление
void throttle(uint8_t val); // газ
void accelerator(uint8_t val); // ускоритель
void brake(uint8_t val); // тормоз
void clutch(uint8_t val); // сцепление
void shifter(uint8_t val); // переключатель
HID_Joystick(USBHID* _HID) :
HID(_HID),
reporter({
(*_HID, (uint8_t*)&data.input.Joystick, sizeof(data.input.Joystick), TLID),
(*_HID, (uint8_t*)&data.input.StatePID, sizeof(data.input.StatePID), TLID+HID_ID_STATE)
})
// // INPUT reporters
// reporter[JOYSTICK] (*_HID, (uint8_t*)&data.input.Joystick, sizeof(data.input.Joystick), TLID),
// reporter[PID_STATE] (*_HID, (uint8_t*)&data.input.StatePID, sizeof(data.input.StatePID), TLID+HID_ID_STATE),
// OUTPUT reporters
// reporter[PID_DEVICE_CONTROL](*_HID, (uint8_t*)&data.output.DeviceControl, sizeof(data.output.DeviceControl), TLID+HID_ID_CTRLREP),
// reporter[DEVICE_GAIN] (*_HID, (uint8_t*)&data.output.DeviceGain, sizeof(data.output.DeviceGain), TLID+HID_ID_GAINREP),
// // FEATURE reporters
// reporter[CREATE_NEW_EFFECT] (*_HID, (uint8_t*)&data.feature.NewEffect, sizeof(data.feature.NewEffect), TLID+HID_ID_NEWEFREP),
// reporter[BLOCK_LOAD] (*_HID, (uint8_t*)&data.feature.BlockLoad, sizeof(data.feature.BlockLoad), TLID+HID_ID_BLKLDREP),
// reporter[PID_POOL] (*_HID, (uint8_t*)&data.feature.PoolPID, sizeof(data.feature.PoolPID), TLID+HID_ID_POOLREP),
// // OUTPUT buffers
// buffer[SET_EFFECT] (bufSetEffect, HID_BUFFER_SIZE(SIZE_EFFREP, 1), TLID+HID_ID_EFFREP, HID_BUFFER_MODE_NO_WAIT),
// buffer[SET_ENVELOPE] (bufSetEnvelope, HID_BUFFER_SIZE(SIZE_ENVREP, 1), TLID+HID_ID_ENVREP, HID_BUFFER_MODE_NO_WAIT),
// buffer[SET_CONDITION] (bufSetCondition, HID_BUFFER_SIZE(SIZE_CONDREP, 1), TLID+HID_ID_CONDREP, HID_BUFFER_MODE_NO_WAIT),
// buffer[SET_PERIODIC] (bufSetPeriodic, HID_BUFFER_SIZE(SIZE_PRIDREP, 1), TLID+HID_ID_PRIDREP, HID_BUFFER_MODE_NO_WAIT),
// buffer[SET_CONSTANT_FORCE](bufSetConstantForce, HID_BUFFER_SIZE(SIZE_CONSTREP,1), TLID+HID_ID_CONSTREP, HID_BUFFER_MODE_NO_WAIT),
// buffer[SET_RAMP_FORCE] (bufSetRampForce, HID_BUFFER_SIZE(SIZE_RAMPREP, 1), TLID+HID_ID_RAMPREP, HID_BUFFER_MODE_NO_WAIT),
// buffer[PID_DEVICE_CONTROL](bufDeviceControl, HID_BUFFER_SIZE(SIZE_CTRLREP, 1), TLID+HID_ID_CTRLREP, HID_BUFFER_MODE_NO_WAIT),
// buffer[DEVICE_GAIN] (bufDeviceGain, HID_BUFFER_SIZE(SIZE_GAINREP, 1), TLID+HID_ID_GAINREP, HID_BUFFER_MODE_NO_WAIT),
// // FEATURE buffers
// buffer[CREATE_NEW_EFFECT] (bufNewEffect, HID_BUFFER_SIZE(SIZE_NEWEFREP,1), TLID+HID_ID_NEWEFREP, HID_BUFFER_MODE_NO_WAIT),
// buffer[BLOCK_LOAD] (bufBlockLoad, HID_BUFFER_SIZE(SIZE_BLKLDREP,1), TLID+HID_ID_BLKLDREP, HID_BUFFER_MODE_NO_WAIT),
// buffer[PID_POOL] (bufPoolPID, HID_BUFFER_SIZE(SIZE_POOLREP, 1), TLID+HID_ID_POOLREP, HID_BUFFER_MODE_NO_WAIT)
{
//
data.input.Joystick.buttons = 0;
data.input.Joystick.hat = 255;
data.input.Joystick.rx = 32767;
data.input.Joystick.ry = 32767;
data.input.Joystick.sliderLeft = 0;
data.input.Joystick.sliderRight = 0;
data.input.Joystick.steering = 127;
data.input.Joystick.accelerator = 0;
data.input.Joystick.throttle = 0;
data.input.Joystick.brake = 0;
data.input.Joystick.shifter = 0;
data.input.Joystick.clutch = 0;
//
data.input.StatePID.deviceIsPaused = false;
data.input.StatePID.actuatorEnabled = false;
data.input.StatePID.safetySwitch = false;
data.input.StatePID.actuatorOverrideSwitch = false;
data.input.StatePID.actuatorPower = false;
data.input.StatePID.effectPlaying = false;
data.input.StatePID.effectBlockIndex = 1;
data.input.StatePID.padding = 0;
//
}
void begin(void) {
// add OUTPUT buffers
HID->addOutputBuffer(&buffer[SET_EFFECT]);
HID->addOutputBuffer(&buffer[SET_ENVELOPE]);
HID->addOutputBuffer(&buffer[SET_CONDITION]);
HID->addOutputBuffer(&buffer[SET_PERIODIC]);
HID->addOutputBuffer(&buffer[SET_CONSTANT_FORCE]);
HID->addOutputBuffer(&buffer[SET_RAMP_FORCE]);
HID->addOutputBuffer(&buffer[PID_DEVICE_CONTROL]);
HID->addOutputBuffer(&buffer[DEVICE_GAIN]);
// add FEATURE buffers
HID->addFeatureBuffer(&buffer[CREATE_NEW_EFFECT]);
HID->addFeatureBuffer(&buffer[BLOCK_LOAD]);
HID->addFeatureBuffer(&buffer[PID_POOL]);
data.feature.NewEffect.effectType = 1;
data.feature.NewEffect.byteCount = 1;
data.feature.BlockLoad.effectBlockIndex = 1;
data.feature.BlockLoad.loadStatus = 1;
data.feature.BlockLoad.ramPoolAvailable = 1;
data.feature.PoolPID.ramPoolSize = 0xFFFF;
data.feature.PoolPID.maxSimultaneousEffects = 0x0A; // FFP supports playing up to 10 simultaneous effects
data.feature.PoolPID.memoryManagement = 3;
reporter[CREATE_NEW_EFFECT].setFeature((uint8_t*)&data.feature.NewEffect);
reporter[BLOCK_LOAD].setFeature((uint8_t*)&data.feature.BlockLoad);
reporter[PID_POOL].setFeature((uint8_t*)&data.feature.PoolPID);
}
void end(void) {}
inline void send(void) {
reporter[JOYSTICK].sendReport();
}
inline void setManualReportMode(bool mode) {
manualReport = mode;
}
inline bool getManualReportMode() {
return manualReport;
}
void handleINPUT()
{
}
void handleOUTPUT()
{
if(reporter[SET_EFFECT].getOutput((uint8_t*)&data.output.SetEffect)) {
CompositeSerial.println("getOutput(SET_EFFECT)");
}
if(reporter[SET_ENVELOPE].getOutput((uint8_t*)&data.output.SetEnvelope)) {
CompositeSerial.println("getOutput(SET_ENVELOPE)");
}
if(reporter[SET_CONDITION].getOutput((uint8_t*)&data.output.SetCondition)) {
CompositeSerial.println("getOutput(SET_CONDITION)");
}
if(reporter[SET_PERIODIC].getOutput((uint8_t*)&data.output.SetPeriodic)) {
CompositeSerial.println("getOutput(SET_PERIODIC)");
}
if(reporter[SET_CONSTANT_FORCE].getOutput((uint8_t*)&data.output.SetConstantForce)) {
CompositeSerial.println("getOutput(SET_CONSTANT_FORCE)");
}
if(reporter[SET_RAMP_FORCE].getOutput((uint8_t*)&data.output.SetRampForce)) {
CompositeSerial.println("getOutput(SET_RAMP_FORCE)");
}
if(reporter[CUSTOM_FORCE_DATA].getOutput((uint8_t*)&data.output.CustomForceData)) {
CompositeSerial.println("getOutput(CUSTOM_FORCE_DATA)");
}
if(reporter[DOWNLOAD_FORCE].getOutput((uint8_t*)&data.output.DownloadForce)) {
CompositeSerial.println("getOutput(DOWNLOAD_FORCE)");
}
if(reporter[EFFECT_OPERATION].getOutput((uint8_t*)&data.output.EffectOperation)) {
CompositeSerial.println("getOutput(EFFECT_OPERATION)");
}
if(reporter[PID_BLOCK_FREE].getOutput((uint8_t*)&data.output.BlockFree)) {
CompositeSerial.println("getOutput(PID_BLOCK_FREE)");
}
if(reporter[PID_DEVICE_CONTROL].getOutput((uint8_t*)&data.output.DeviceControl)) {
CompositeSerial.println("getOutput(PID_DEVICE_CONTROL)");
}
if(reporter[DEVICE_GAIN].getOutput((uint8_t*)&data.output.DeviceGain)) {
CompositeSerial.println("getOutput(DEVICE_GAIN)");
}
if(reporter[SET_CUSTOM_FORCE].getOutput((uint8_t*)&data.output.SetCustomForce)) {
CompositeSerial.println("getOutput(SET_CUSTOM_FORCE)");
}
}
void handleFEATURE()
{
if(reporter[CREATE_NEW_EFFECT].getFeature((uint8_t*)&data.feature.NewEffect)) {
CompositeSerial.println("getFature(CREATE_NEW_EFFECT)");
}
if(reporter[BLOCK_LOAD].getFeature((uint8_t*)&data.feature.BlockLoad)) {
CompositeSerial.println("getFature(BLOCK_LOAD)");
}
if(reporter[PID_POOL].getFeature((uint8_t*)&data.feature.PoolPID)) {
CompositeSerial.println("getFature(PID_POOL)");
}
}
void test() {
handleINPUT();
handleOUTPUT();
handleFEATURE();
}
};