forked from hoantv/VNWheel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FfbReportHandler.cpp
339 lines (292 loc) · 9.15 KB
/
FfbReportHandler.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
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
/*
Force Feedback Joystick
Joystick model specific code for handling force feedback data.
Copyright 2012 Tero Loimuneva (tloimu [at] gmail [dot] com)
Copyright 2013 Saku Kekkonen
Copyright 2016 Jaka Simonic (telesimke [at] gmail [dot] com)
Copyright 2019 Hoan Tran (tranvanhoan206 [at] gmail [dot] com)
Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author disclaim all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
#include "FfbReportHandler.h"
#include "debug.h"
FfbReportHandler::FfbReportHandler() {
nextEID = 1;
devicePaused = 0;
}
FfbReportHandler::~FfbReportHandler() {
FreeAllEffects();
}
uint8_t FfbReportHandler::GetNextFreeEffect(void)
{
if (nextEID == MAX_EFFECTS)
return 0;
uint8_t id = nextEID++;
// Find the next free effect ID for next time
//nextEID=0;
while (gEffectStates[nextEID].state != 0)
{
if (nextEID >= MAX_EFFECTS)
break; // the last spot was taken
nextEID++;
}
gEffectStates[id].state = MEFFECTSTATE_ALLOCATED;
return id;
}
void FfbReportHandler::StopAllEffects(void)
{
for (uint8_t id = 0; id <= MAX_EFFECTS; id++)
StopEffect(id);
}
void FfbReportHandler::StartEffect(uint8_t id)
{
if (id > MAX_EFFECTS)
return;
gEffectStates[id].state = MEFFECTSTATE_PLAYING;
gEffectStates[id].elapsedTime = 0;
gEffectStates[id].startTime = (uint64_t) millis();
}
void FfbReportHandler::StopEffect(uint8_t id)
{
if (id > MAX_EFFECTS)
return;
gEffectStates[id].state &= ~MEFFECTSTATE_PLAYING;
pidBlockLoad.ramPoolAvailable += SIZE_EFFECT;
}
void FfbReportHandler::FreeEffect(uint8_t id)
{
if (id > MAX_EFFECTS)
return;
gEffectStates[id].state = 0;
if (id < nextEID)
nextEID = id;
}
void FfbReportHandler::FreeAllEffects(void)
{
nextEID = 1;
memset((void*)&gEffectStates, 0, sizeof(gEffectStates));
pidBlockLoad.ramPoolAvailable = MEMORY_SIZE;
}
void FfbReportHandler::FfbHandle_EffectOperation(USB_FFBReport_EffectOperation_Output_Data_t* data)
{
if (data->operation == 1)
{ // Start
if (data->loopCount > 0) gEffectStates[data->effectBlockIndex].duration *= data->loopCount;
if (data->loopCount == 0xFF) gEffectStates[data->effectBlockIndex].duration = USB_DURATION_INFINITE;
StartEffect(data->effectBlockIndex);
}
else if (data->operation == 2)
{ // StartSolo
// Stop all first
StopAllEffects();
// Then start the given effect
StartEffect(data->effectBlockIndex);
}
else if (data->operation == 3)
{ // Stop
StopEffect(data->effectBlockIndex);
}
else
{
}
}
void FfbReportHandler::FfbHandle_BlockFree(USB_FFBReport_BlockFree_Output_Data_t* data)
{
uint8_t eid = data->effectBlockIndex;
if (eid == 0xFF)
{ // all effects
FreeAllEffects();
}
else
{
FreeEffect(eid);
}
}
void FfbReportHandler::FfbHandle_DeviceControl(USB_FFBReport_DeviceControl_Output_Data_t* data)
{
uint8_t control = data->control;
if (control == 0x01)
{ // 1=Enable Actuators
pidState.status |= 2;
}
else if (control == 0x02)
{ // 2=Disable Actuators
pidState.status &= ~(0x02);
}
else if (control == 0x03)
{ // 3=Stop All Effects
StopAllEffects();
}
else if (control == 0x04)
{ // 4=Reset
FreeAllEffects();
}
else if (control == 0x05)
{ // 5=Pause
devicePaused = 1;
}
else if (control == 0x06)
{ // 6=Continue
devicePaused = 0;
}
else if (control & (0xFF - 0x3F))
{
}
}
void FfbReportHandler::FfbHandle_DeviceGain(USB_FFBReport_DeviceGain_Output_Data_t* data)
{
deviceGain.gain = data->gain;
}
void FfbReportHandler::FfbHandle_SetCustomForce(USB_FFBReport_SetCustomForce_Output_Data_t* data)
{
}
void FfbReportHandler::FfbHandle_SetCustomForceData(USB_FFBReport_SetCustomForceData_Output_Data_t* data)
{
}
void FfbReportHandler::FfbHandle_SetDownloadForceSample(USB_FFBReport_SetDownloadForceSample_Output_Data_t* data)
{
}
void FfbReportHandler::FfbHandle_SetEffect(USB_FFBReport_SetEffect_Output_Data_t* data)
{
volatile TEffectState* effect = &gEffectStates[data->effectBlockIndex];
effect->duration = data->duration;
effect->directionX = data->directionX;
effect->directionY = data->directionY;
effect->effectType = data->effectType;
effect->gain = data->gain;
effect->enableAxis = data->enableAxis;
// effect->triggerRepeatInterval;
// effect->samplePeriod; // 0..32767 ms
// effect->triggerButton;
}
void FfbReportHandler::SetEnvelope(USB_FFBReport_SetEnvelope_Output_Data_t* data, volatile TEffectState* effect)
{
effect->attackLevel = data->attackLevel;
effect->fadeLevel = data->fadeLevel;
effect->attackTime = data->attackTime;
effect->fadeTime = data->fadeTime;
}
void FfbReportHandler::SetCondition(USB_FFBReport_SetCondition_Output_Data_t* data, volatile TEffectState* effect)
{
effect->cpOffset = data->cpOffset;
effect->positiveCoefficient = data->positiveCoefficient;
effect->negativeCoefficient = data->negativeCoefficient;
effect->positiveSaturation = data->positiveSaturation;
effect->negativeSaturation = data->negativeSaturation;
effect->deadBand = data->deadBand;
}
void FfbReportHandler::SetPeriodic(USB_FFBReport_SetPeriodic_Output_Data_t* data, volatile TEffectState* effect)
{
effect->magnitude = data->magnitude;
effect->offset = data->offset;
effect->phase = data->phase;
effect->period = data->period;
}
void FfbReportHandler::SetConstantForce(USB_FFBReport_SetConstantForce_Output_Data_t* data, volatile TEffectState* effect)
{
// ReportPrint(*effect);
effect->magnitude = data->magnitude;
}
void FfbReportHandler::SetRampForce(USB_FFBReport_SetRampForce_Output_Data_t* data, volatile TEffectState* effect)
{
effect->startMagnitude = data->startMagnitude;
effect->endMagnitude = data->endMagnitude;
}
void FfbReportHandler::FfbOnCreateNewEffect(USB_FFBReport_CreateNewEffect_Feature_Data_t* inData)
{
pidBlockLoad.reportId = 6;
pidBlockLoad.effectBlockIndex = GetNextFreeEffect();
if (pidBlockLoad.effectBlockIndex == 0)
{
pidBlockLoad.loadStatus = 2; // 1=Success,2=Full,3=Error
}
else
{
pidBlockLoad.loadStatus = 1; // 1=Success,2=Full,3=Error
volatile TEffectState* effect = &gEffectStates[pidBlockLoad.effectBlockIndex];
memset((void*)effect, 0, sizeof(TEffectState));
effect->state = MEFFECTSTATE_ALLOCATED;
pidBlockLoad.ramPoolAvailable -= SIZE_EFFECT;
}
}
uint8_t* FfbReportHandler::FfbOnPIDPool()
{
FreeAllEffects();
pidPoolReport.reportId = 7;
pidPoolReport.ramPoolSize = MEMORY_SIZE;
pidPoolReport.maxSimultaneousEffects = MAX_EFFECTS;
pidPoolReport.memoryManagement = 3;
return (uint8_t*)&pidPoolReport;
}
uint8_t* FfbReportHandler::FfbOnPIDBlockLoad()
{
return (uint8_t*)&pidBlockLoad;
}
uint8_t* FfbReportHandler::FfbOnPIDStatus()
{
return (uint8_t*)&pidState;
}
void FfbReportHandler::FfbOnUsbData(uint8_t* data, uint16_t len)
{
uint8_t effectId = data[1]; // effectBlockIndex is always the second byte.
switch (data[0]) // reportID
{
case 1:
FfbHandle_SetEffect((USB_FFBReport_SetEffect_Output_Data_t*)data);
break;
case 2:
SetEnvelope((USB_FFBReport_SetEnvelope_Output_Data_t*)data, &gEffectStates[effectId]);
break;
case 3:
SetCondition((USB_FFBReport_SetCondition_Output_Data_t*)data, &gEffectStates[effectId]);
break;
case 4:
SetPeriodic((USB_FFBReport_SetPeriodic_Output_Data_t*)data, &gEffectStates[effectId]);
break;
case 5:
SetConstantForce((USB_FFBReport_SetConstantForce_Output_Data_t*)data, &gEffectStates[effectId]);
break;
case 6:
SetRampForce((USB_FFBReport_SetRampForce_Output_Data_t*)data, &gEffectStates[effectId]);
break;
case 7:
FfbHandle_SetCustomForceData((USB_FFBReport_SetCustomForceData_Output_Data_t*)data);
break;
case 8:
FfbHandle_SetDownloadForceSample((USB_FFBReport_SetDownloadForceSample_Output_Data_t*)data);
break;
case 9:
break;
case 10:
FfbHandle_EffectOperation((USB_FFBReport_EffectOperation_Output_Data_t*)data);
break;
case 11:
FfbHandle_BlockFree((USB_FFBReport_BlockFree_Output_Data_t*)data);
break;
case 12:
FfbHandle_DeviceControl((USB_FFBReport_DeviceControl_Output_Data_t*)data);
break;
case 13:
FfbHandle_DeviceGain((USB_FFBReport_DeviceGain_Output_Data_t*)data);
break;
case 14:
FfbHandle_SetCustomForce((USB_FFBReport_SetCustomForce_Output_Data_t*)data);
break;
default:
break;
}
};