-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathffb.c
1623 lines (1339 loc) · 45 KB
/
ffb.c
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Force Feedback Joystick
Joystick model specific code for handling force feedback data.
This code is for Microsoft Sidewinder Force Feedback Pro joystick.
Copyright 2012 Tero Loimuneva (tloimu [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 "ffb.h"
#include <stdint.h>
#include <avr/io.h>
#include <util/delay.h>
//#include <LUFA/Drivers/Board/LEDs.h>
//#include "joystick.h"
//#include "debug.h"
#include "3DPro.h"
#define USART_BAUD 31250
// Effect management
volatile uint8_t nextEID = 2; // FFP effect indexes starts from 2 (yes, we waste memory for two effects...)
volatile USB_FFBReport_PIDStatus_Input_Data_t pidState; // For holding device status flags
void SendPidStateForEffect(uint8_t eid, uint8_t effectState);
void SendPidStateForEffect(uint8_t eid, uint8_t effectState)
{
pidState.effectBlockIndex = effectState;
pidState.effectBlockIndex = 0;
}
const uint16_t USB_DURATION_INFINITE = 0x7FFF;
const uint16_t MIDI_DURATION_INFINITE = 0;
// Bit-masks for effect states
const uint8_t MEffectState_Free = 0x00;
const uint8_t MEffectState_Allocated = 0x01;
const uint8_t MEffectState_Playing = 0x02;
const uint8_t MEffectState_SentToJoystick = 0x04;
typedef struct {
uint8_t state; // see constants <MEffectState_*>
uint16_t usb_duration, usb_fadeTime; // used to calculate fadeTime to MIDI, since in USB it is given as time difference from the end while in MIDI it is given as time from start
// These are used to calculate effects of USB gain to MIDI data
uint8_t usb_gain, usb_offset, usb_attackLevel, usb_fadeLevel;
uint8_t usb_magnitude;
FFP_MIDI_Effect_Basic data; // For FFP, this is enough for all types of effects - cast for other effect types when necessary
} TEffectState;
static volatile TEffectState gEffectStates[MAX_EFFECTS+1]; // one for each effect (array index 0 is unused to simplify things)
volatile TDisabledEffectTypes gDisabledEffects;
// All effects data start with this data
static uint8_t sCommonEffectHeader[] = {0x00, 0x01, 0x0a, 0x01};
uint8_t GetNextFreeEffect(void);
void StartEffect(uint8_t id);
void StopEffect(uint8_t id);
void StopAllEffects(void);
void FreeEffect(uint8_t id);
void FreeAllEffects(void);
uint8_t GetNextFreeEffect(void)
{
if (nextEID == MAX_EFFECTS)
return 0;
uint8_t id = nextEID++;
// Find the next free effect ID for next time
while (gEffectStates[nextEID].state != 0)
{
if (nextEID >= MAX_EFFECTS)
break; // the last spot was taken
nextEID++;
}
gEffectStates[id].state = MEffectState_Allocated;
memset((void*) &gEffectStates[id].data, 0, sizeof(gEffectStates[id].data));
return id;
}
void StopAllEffects(void)
{
uint8_t id;
for (id = 1; id <= MAX_EFFECTS; id++)
StopEffect(id);
}
void StartEffect(uint8_t id)
{
if (id > MAX_EFFECTS)
return;
gEffectStates[id].state |= MEffectState_Playing;
}
void StopEffect(uint8_t id)
{
if (id > MAX_EFFECTS)
return;
gEffectStates[id].state &= ~MEffectState_Playing;
}
void FreeEffect(uint8_t id)
{
if (id > MAX_EFFECTS)
return;
gEffectStates[id].state = 0;
if (id < nextEID)
nextEID = id;
}
void FreeAllEffects(void)
{
nextEID = 2;
memset((void*) gEffectStates, 0, sizeof(gEffectStates));
}
// Utilities
// Function: CalculateSysExChecksum
// Checksum for MIDI SysEx message content.
// See e.g. algorhitm used by Roland
// - sum all bytes (after "address" i.e. 4 bytes)
// - reminder from division by 128
// - subtract the reminder from 128 => checksum
uint8_t CalculateSysExChecksum(uint8_t *data, uint8_t len);
uint8_t CalculateSysExChecksum(uint8_t *data, uint8_t len)
{
uint8_t cs = 0;
uint8_t i;
for (i = 0; i < len; i++) // skip the "address" part
{
cs += data[i]; // let them overflow, which effectively leaves the reminder part
}
cs &= 0x7F; // take the potential final 128 off
if (cs != 0)
cs = 0x80 - cs;
return cs;
}
uint16_t UsbUint16ToMidiUint14(uint16_t inUsbValue)
{
if (inUsbValue == 0xFFFF)
return 0x0000;
return (inUsbValue & 0x7F00) + ((inUsbValue & 0x00FF) >> 1); // loss of the MSB-bit!
}
int16_t UsbInt8ToMidiInt14(int8_t inUsbValue)
{
int16_t value;
if (inUsbValue < 0)
{
value = inUsbValue;
value += 0x7f80;
}
else
value = inUsbValue;
return value;
}
// Calculates the final value of the given <value> when taking in given <gain> into account.
// Returns MIDI value (i.e. max 0..7f).
uint8_t CalcGain(uint8_t usbValue, uint8_t gain)
{
int16_t v = usbValue;
return (((v * gain) / 256) >> 2 ) & 0x7f;
}
void FfbSendSysEx(void *midi_data, uint16_t len)
{
uint8_t mark = 0xF0; // SysEx Start
FfbSendData(&mark, 1);
FfbSendData(sCommonEffectHeader, sizeof(sCommonEffectHeader));
FfbSendData((uint8_t*) midi_data, len);
uint8_t checksum = CalculateSysExChecksum((uint8_t*) midi_data, len);
FfbSendData(&checksum, 1);
mark = 0xF7; // SysEx End
FfbSendData(&mark, 1);
}
// Send to MIDI Control Change "B5"
void FfbSendEffectOper(uint8_t effectId, uint8_t operation)
{
uint8_t midi_cmd[3];
midi_cmd[0] = 0xB5;
midi_cmd[1] = operation;
midi_cmd[2] = effectId;
FfbSendData(midi_cmd, 3);
}
// Send to MIDI effect data modification to the given address of the given effect
void FfbSendModify(uint8_t effectId, uint8_t address, uint16_t value)
{
// Modify + Address
uint8_t midi_cmd[3];
midi_cmd[0] = 0xB5;
midi_cmd[1] = address;
midi_cmd[2] = effectId;
FfbSendData(midi_cmd, 3);
// New value
midi_cmd[0] = 0xA5;
midi_cmd[1] = value & 0x7F;
midi_cmd[2] = (value & 0x7F00) >> 8;
FfbSendData(midi_cmd, 3);
}
// Lengths of each report type
const uint16_t OutReportSize[] = {
sizeof(USB_FFBReport_SetEffect_Output_Data_t), // 1
sizeof(USB_FFBReport_SetEnvelope_Output_Data_t), // 2
sizeof(USB_FFBReport_SetCondition_Output_Data_t), // 3
sizeof(USB_FFBReport_SetPeriodic_Output_Data_t), // 4
sizeof(USB_FFBReport_SetConstantForce_Output_Data_t), // 5
sizeof(USB_FFBReport_SetRampForce_Output_Data_t), // 6
sizeof(USB_FFBReport_SetCustomForceData_Output_Data_t), // 7
sizeof(USB_FFBReport_SetDownloadForceSample_Output_Data_t), // 8
0, // 9
sizeof(USB_FFBReport_EffectOperation_Output_Data_t), // 10
sizeof(USB_FFBReport_BlockFree_Output_Data_t), // 11
sizeof(USB_FFBReport_DeviceControl_Output_Data_t), // 12
sizeof(USB_FFBReport_DeviceGain_Output_Data_t), // 13
sizeof(USB_FFBReport_SetCustomForce_Output_Data_t), // 14
};
void FfbSendData(uint8_t *data, uint16_t len);
void FfbSendEnableInterrupts(void);
void FfbHandle_SetEffect(USB_FFBReport_SetEffect_Output_Data_t *data);
void FfbHandle_SetEnvelope(USB_FFBReport_SetEnvelope_Output_Data_t *data);
void FfbHandle_SetCondition(USB_FFBReport_SetCondition_Output_Data_t *data);
void FfbHandle_SetPeriodic(USB_FFBReport_SetPeriodic_Output_Data_t *data);
void FfbHandle_SetConstantForce(USB_FFBReport_SetConstantForce_Output_Data_t *data);
void FfbHandle_SetRampForce(USB_FFBReport_SetRampForce_Output_Data_t *data);
void FfbHandle_SetCustomForceData(USB_FFBReport_SetCustomForceData_Output_Data_t *data);
void FfbHandle_SetDownloadForceSample(USB_FFBReport_SetDownloadForceSample_Output_Data_t *data);
void FfbHandle_EffectOperation(USB_FFBReport_EffectOperation_Output_Data_t *data);
void FfbHandle_BlockFree(USB_FFBReport_BlockFree_Output_Data_t *data);
void FfbHandle_DeviceControl(USB_FFBReport_DeviceControl_Output_Data_t *data);
void FfbHandle_DeviceGain(USB_FFBReport_DeviceGain_Output_Data_t *data);
void FfbHandle_SetCustomForce(USB_FFBReport_SetCustomForce_Output_Data_t *data);
// Handle incoming data from USB and convert it to MIDI data to joystick
void FfbOnUsbData(uint8_t *data, uint16_t len)
{
// Parse incoming USB data and convert it to MIDI data for the joystick
//LEDs_SetAllLEDs(LEDS_ALL_LEDS);
//if (gDebugMode & DEBUG_DETAIL)
//LogReport("<= FfbData:", OutReportSize, data, len);
switch (data[0]) // reportID
{
case 1:
FfbHandle_SetEffect((USB_FFBReport_SetEffect_Output_Data_t*) data);
break;
case 2:
FfbHandle_SetEnvelope((USB_FFBReport_SetEnvelope_Output_Data_t*) data);
break;
case 3:
FfbHandle_SetCondition((USB_FFBReport_SetCondition_Output_Data_t*) data);
break;
case 4:
FfbHandle_SetPeriodic((USB_FFBReport_SetPeriodic_Output_Data_t*) data);
break;
case 5:
FfbHandle_SetConstantForce((USB_FFBReport_SetConstantForce_Output_Data_t*) data);
break;
case 6:
FfbHandle_SetRampForce((USB_FFBReport_SetRampForce_Output_Data_t*) data);
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;
};
//LEDs_SetAllLEDs(LEDS_NO_LEDS);
}
// Define and play constant force to left
uint8_t leftData[] =
{
0xf0, // define
0x00, 0x01, 0x0a, 0x01, 0x23, 0x12, 0x7f, 0x5a, 0x19, 0x00, 0x00, 0x5a, 0x00, 0x7f, 0x64, 0x00, 0x10, 0x4e, 0x7f, 0x00, 0x00, 0x7f, 0x5a, 0x19, 0x7f, 0x01, 0x00, 0x7f, 0x00, 0x00, 0x00, 0x4e,
0xf7,
// 0xb5, 0x20, 0x02 // play
};
// Constant, Ramp, Square, Sine, Triange, SawtoothDown, SawtoothUp, Spring, Damper, Inertia, Friction, Custom?
uint8_t usbToMidiEffectType[] = { 0x12,0x06,0x05,0x02,0x08,0x0A,0x0B,0x0D,0x0E,0x0F,0x10,0x01 };
void FfbOnCreateNewEffect(USB_FFBReport_CreateNewEffect_Feature_Data_t* inData, USB_FFBReport_PIDBlockLoad_Feature_Data_t *outData)
{
LogTextP(PSTR("Create New Effect:\n "));
LogBinaryLf(inData, sizeof(USB_FFBReport_CreateNewEffect_Feature_Data_t));
/*
USB effect data:
uint8_t reportId; // =1
uint8_t effectType; // Enum (1..12): ET 26,27,30,31,32,33,34,40,41,42,43,28
uint16_t byteCount; // 0..511 - only valid with Custom Force
*/
outData->reportId = 6;
outData->effectBlockIndex = GetNextFreeEffect();
if (outData->effectBlockIndex == 0)
outData->loadStatus = 2; // 1=Success,2=Full,3=Error
else
{
outData->loadStatus = 1; // 1=Success,2=Full,3=Error
// Set defaults to the effect data
volatile TEffectState *effect_state = &gEffectStates[outData->effectBlockIndex];
effect_state->usb_duration = USB_DURATION_INFINITE;
effect_state->usb_fadeTime = USB_DURATION_INFINITE;
effect_state->usb_gain = 0xFF;
effect_state->usb_offset = 0;
effect_state->usb_attackLevel = 0xFF;
effect_state->usb_fadeLevel = 0xFF;
volatile FFP_MIDI_Effect_Basic *midi_data = &effect_state->data;
midi_data->magnitude = 0x7f;
midi_data->waveLength = 0x01;
midi_data->waveForm = usbToMidiEffectType[inData->effectType - 1];
midi_data->attackLevel = 0x00;
midi_data->attackTime = 0x0000;
midi_data->fadeLevel = 0x00;
midi_data->fadeTime = 0x0000;
// Constants
midi_data->command = 0x23;
midi_data->unknown1 = 0x7F;
midi_data->unknown2 = 0x0000;
midi_data->unknown3[0] = 0x7F;
midi_data->unknown3[1] = 0x64;
midi_data->unknown3[2] = 0x00;
midi_data->unknown3[3] = 0x10;
midi_data->unknown3[4] = 0x4E;
if (midi_data->waveForm == 0x12) // constant
midi_data->param2 = 0x0000;
else
midi_data->param2 = 0x0101;
}
outData->ramPoolAvailable = 0xFFFF; // =0 or 0xFFFF - don't really know what this is used for?
LogDataLf(" => Usb:", outData->reportId, outData, sizeof(USB_FFBReport_PIDBlockLoad_Feature_Data_t));
_delay_ms(5);
}
void FfbOnPIDPool(USB_FFBReport_PIDPool_Feature_Data_t *data)
{
LogTextP(PSTR("GetReport PID Pool Feature:\n => Usb:"));
FreeAllEffects();
data->reportId = 7;
data->ramPoolSize = 0xFFFF;
data->maxSimultaneousEffects = 0x0A; // FFP supports playing up to 10 simultaneous effects
data->memoryManagement = 3;
LogBinaryLf(data, sizeof(USB_FFBReport_PIDPool_Feature_Data_t));
}
void FfbHandle_SetEffect(USB_FFBReport_SetEffect_Output_Data_t *data)
{
uint8_t eid = data->effectBlockIndex;
/* LogTextP(PSTR("Set Effect:"));
LogBinaryLf(data, sizeof(USB_FFBReport_SetEffect_Output_Data_t));
LogTextP(PSTR(" id =")); LogBinaryLf(&eid, sizeof(eid));
LogTextP(PSTR(" type=")); LogBinaryLf(&data->effectType, sizeof(data->effectType));
LogTextP(PSTR(" gain=")); LogBinaryLf(&data->gain, sizeof(data->gain));
LogTextP(PSTR(" dura=")); LogBinaryLf(&data->duration, sizeof(data->duration));
if (data->enableAxis)
{
LogTextP(PSTR(" X=")); LogBinaryLf(&data->directionX, sizeof(data->directionX));
LogTextP(PSTR(" Y=")); LogBinaryLf(&data->directionY, sizeof(data->directionY));
}
if (data->triggerRepeatInterval)
{
LogTextP(PSTR(" repeat=")); LogBinaryLf(&data->triggerRepeatInterval, sizeof(data->triggerRepeatInterval));
}
if (data->triggerButton)
{
LogTextP(PSTR(" button=")); LogBinaryLf(&data->triggerButton, sizeof(data->triggerButton));
}
*/
/*
USB effect data:
uint8_t reportId; // =1
uint8_t effectBlockIndex; // 1..40
uint8_t effectType; // 1..12 (effect usages: 26,27,30,31,32,33,34,40,41,42,43,28)
uint16_t duration; // 0..32767 ms
uint16_t triggerRepeatInterval; // 0..32767 ms
uint16_t samplePeriod; // 0..32767 ms
uint8_t gain; // 0..255 (physical 0..10000)
uint8_t triggerButton; // button ID (0..8)
uint8_t enableAxis; // bits: 0=X, 1=Y, 2=DirectionEnable
uint8_t directionX; // angle (0=0 .. 180=0..360deg)
uint8_t directionY; // angle (0=0 .. 180=0..360deg)
*/
// Fill in data that is common to all effect MIDI data types
volatile FFP_MIDI_Effect_Basic *common_midi_data = &gEffectStates[eid].data;
if (data->duration == USB_DURATION_INFINITE)
common_midi_data->duration = MIDI_DURATION_INFINITE;
else
common_midi_data->duration = UsbUint16ToMidiUint14(data->duration); // MIDI unit is 2ms
gEffectStates[eid].usb_duration = data->duration; // store for later calculation of <fadeTime>
if (gEffectStates[eid].state & MEffectState_SentToJoystick)
{ // Send update
FfbSendModify(eid, 0x40, common_midi_data->duration);
}
uint8_t midi_data_len = sizeof(FFP_MIDI_Effect_Basic); // default MIDI data size
boolean is_periodic = false;
// Fill in the effect type specific data
switch (data->effectType)
{
case 3: // square (midi: 5)
case 4: // sine (midi: 2) or cosine (midi: 3)
case 5: // triangle (midi: 8)
case 6: // sawtooth up (midi: 0x0a)
case 7: // sawtooth down (midi: 0x0b)
is_periodic = true;
case 1: // constant force (midi: 0x12)
case 2: // ramp up & down (midi: 6 or 7)
{
/*
MIDI effect data:
uint8_t command; // always 0x23 -- start counting checksum from here
uint8_t waveForm; // 2=sine, 5=Square, 6=RampUp, 7=RampDown, 8=Triange, 0x12=Constant
uint8_t unknown1; // ? always 0x7F
uint16_t duration; // unit=2ms
uint16_t unknown2; // ? always 0x0000
uint16_t direction;
uint8_t unknown3[5]; // ? always 7f 64 00 10 4e
uint8_t attackLevel;
uint16_t attackTime;
uint8_t magnitude;
uint16_t fadeTime;
uint8_t fadeLevel;
uint8_t waveLength; // 0x6F..0x01 => 1/Hz
uint8_t unknown5; // ? always 0x00
uint16_t param1; // Constant: positive=7f 00, negative=01 01, Other effects: 01 01
uint16_t param2; // Constant: 00 00, Other effects 01 01
*/
volatile TEffectState *effect = &gEffectStates[eid];
volatile FFP_MIDI_Effect_Basic *midi_data = &effect->data;
// Convert direction
uint16_t usbdir = data->directionX;
usbdir = usbdir * 2;
uint16_t dir = (usbdir & 0x7F) + ( (usbdir & 0x0180) << 1 );
midi_data->direction = dir;
// Recalculate fadeTime for MIDI since change to duration changes the fadeTime too
if (data->duration == USB_DURATION_INFINITE)
midi_data->fadeTime = MIDI_DURATION_INFINITE;
else
{
if (effect->usb_fadeTime == USB_DURATION_INFINITE)
midi_data->fadeTime = MIDI_DURATION_INFINITE;
else
{
if (effect->usb_duration > effect->usb_fadeTime) // add some safety and special case handling
midi_data->fadeTime = UsbUint16ToMidiUint14(effect->usb_duration - effect->usb_fadeTime);
else
midi_data->fadeTime = midi_data->duration;
}
}
// Gain and its effects (magnitude and envelope levels)
boolean gain_changed = (gEffectStates[eid].usb_gain != data->gain);
if (gain_changed)
{
// LogTextP(PSTR(" New gain:"));
// LogBinary(&data->gain, 1);
gEffectStates[eid].usb_gain = data->gain;
midi_data->attackLevel = CalcGain(effect->usb_attackLevel, data->gain);
midi_data->fadeLevel = CalcGain(effect->usb_fadeLevel, data->gain);
if (is_periodic)
{
// Calculate min-max from magnitude and offset, since magnitude may be affected by gain we must calc them here too for periodic effects
uint8_t magnitude = CalcGain(effect->usb_magnitude, effect->usb_gain); // already at MIDI-level i.e. 1/2 of USB level!
midi_data->param1 = UsbInt8ToMidiInt14(effect->usb_offset + magnitude); // max
midi_data->param2 = UsbInt8ToMidiInt14(effect->usb_offset - magnitude); // min
if (effect->state & MEffectState_SentToJoystick)
{
FfbSendModify(eid, 0x74, midi_data->param1);
FfbSendModify(eid, 0x78, midi_data->param2);
}
}
else
midi_data->magnitude = CalcGain(effect->usb_magnitude, data->gain);
}
// Send data to MIDI
if (gEffectStates[eid].state & MEffectState_SentToJoystick)
{ // Send update
FfbSendModify(eid, 0x48, midi_data->direction);
FfbSendModify(eid, 0x60, midi_data->fadeTime);
if (gain_changed)
{
FfbSendModify(eid, 0x6C, midi_data->fadeLevel); // might have changed due gain
FfbSendModify(eid, 0x64, midi_data->attackLevel); // might have changed due gain
if (!is_periodic)
FfbSendModify(eid, 0x74, midi_data->magnitude); // might have changed due gain
}
}
else
{
FfbSendSysEx((uint8_t*) midi_data, sizeof(FFP_MIDI_Effect_Basic));
effect->state |= MEffectState_SentToJoystick;
}
}
break;
case 8: // spring (midi: 0x0d)
case 9: // damper (midi: 0x0e)
case 10: // inertia (midi: 0x0f)
{
/*
MIDI effect data:
uint8_t command; // always 0x23 -- start counting checksum from here
uint8_t waveForm; // 2=sine, 5=Square, 6=RampUp, 7=RampDown, 8=Triange, 0x12=Constant
uint8_t unknown1; // ? always 0x7F
uint16_t duration; // unit=2ms
uint16_t unknown2; // ? always 0x0000
uint16_t coeffAxis0;
uint16_t coeffAxis1;
uint16_t offsetAxis0;
uint16_t offsetAxis1;
*/
// volatile FFP_MIDI_Effect_Spring_Inertia_Damper *midi_data = (FFP_MIDI_Effect_Spring_Inertia_Damper *) &gEffectStates[eid].data;
midi_data_len = sizeof(FFP_MIDI_Effect_Spring_Inertia_Damper);
// Send data to MIDI
if (gEffectStates[eid].state & MEffectState_SentToJoystick)
{ // Send update
}
}
break;
case 11: // friction (midi: 0x10)
{
/*
MIDI effect data:
uint8_t command; // always 0x23 -- start counting checksum from here
uint8_t waveForm; // 2=sine, 5=Square, 6=RampUp, 7=RampDown, 8=Triange, 0x12=Constant
uint8_t unknown1; // ? always 0x7F
uint16_t duration; // unit=2ms
uint16_t unknown2; // ? always 0x0000
uint16_t coeffAxis0;
uint16_t coeffAxis1;
*/
// volatile FFP_MIDI_Effect_Friction *midi_data = (FFP_MIDI_Effect_Friction *) &gEffectStates[eid].data;
midi_data_len = sizeof(FFP_MIDI_Effect_Friction);
// Send data to MIDI
if (gEffectStates[eid].state & MEffectState_SentToJoystick)
{ // Send update
}
}
break;
case 12: // custom (midi: ? does FFP support custom forces?)
{
}
break;
default:
break;
}
// Send full effect data to MIDI if this effect has not been sent yet
if (!(gEffectStates[eid].state & MEffectState_SentToJoystick))
{
FfbSendSysEx((uint8_t*) common_midi_data, midi_data_len);
gEffectStates[eid].state |= MEffectState_SentToJoystick;
}
}
void FfbHandle_SetEnvelope(USB_FFBReport_SetEnvelope_Output_Data_t *data)
{
uint8_t eid = data->effectBlockIndex;
volatile TEffectState *effect = &gEffectStates[eid];
/* LogTextP(PSTR("Set Envelope:"));
LogBinaryLf(data, sizeof(USB_FFBReport_SetEnvelope_Output_Data_t));
LogTextP(PSTR(" id =")); LogBinaryLf(&eid, sizeof(eid));
LogTextP(PSTR(" attack=")); LogBinaryLf(&data->attackLevel, sizeof(data->attackLevel));
LogTextP(PSTR(" fade =")); LogBinaryLf(&data->fadeLevel, sizeof(data->fadeLevel));
LogTextP(PSTR(" attackTime=")); LogBinaryLf(&data->attackTime, sizeof(data->attackTime));
LogTextP(PSTR(" fadeTime =")); LogBinaryLf(&data->fadeTime, sizeof(data->fadeTime));
*/
// FlushDebugBuffer();
/*
USB effect data:
uint8_t reportId; // =2
uint8_t effectBlockIndex; // 1..40
uint8_t attackLevel;
uint8_t fadeLevel;
uint16_t attackTime; // ms
uint16_t fadeTime; // ms
MIDI effect data:
uint8_t command; // always 0x23 -- start counting checksum from here
uint8_t waveForm; // 2=sine, 5=Square, 6=RampUp, 7=RampDown, 8=Triange, 0x12=Constant
uint8_t unknown1; // ? always 0x7F
uint16_t duration; // unit=2ms
uint16_t unknown2; // ? always 0x0000
uint16_t direction;
uint8_t unknown3[5]; // ? always 7f 64 00 10 4e
uint8_t attackLevel;
uint16_t attackTime;
uint8_t magnitude;
uint16_t fadeTime;
uint8_t fadeLevel;
uint8_t waveLength; // 0x6F..0x01 => 1/Hz
uint8_t unknown5; // ? always 0x00
uint16_t param1; // Constant: positive=7f 00, negative=01 01, Other effects: 01 01
uint16_t param2; // Constant: 00 00, Other effects 01 01
*/
volatile FFP_MIDI_Effect_Basic *midi_data = &effect->data;
effect->usb_attackLevel = data->attackLevel;
effect->usb_fadeLevel = data->fadeLevel;
effect->usb_fadeTime = data->fadeTime;
midi_data->attackLevel = CalcGain(data->attackLevel, effect->usb_gain);
midi_data->fadeLevel = CalcGain(data->fadeLevel, effect->usb_gain);
midi_data->attackTime = UsbUint16ToMidiUint14(data->attackTime);
if (data->fadeTime == USB_DURATION_INFINITE)
midi_data->fadeTime = MIDI_DURATION_INFINITE;
else
midi_data->fadeTime = UsbUint16ToMidiUint14(gEffectStates[eid].usb_duration - gEffectStates[eid].usb_fadeTime);
if (gEffectStates[eid].state & MEffectState_SentToJoystick)
{ // Send update
FfbSendModify(eid, 0x60, midi_data->fadeTime);
FfbSendModify(eid, 0x5C, midi_data->attackTime);
FfbSendModify(eid, 0x6C, midi_data->fadeLevel);
FfbSendModify(eid, 0x64, midi_data->attackLevel);
}
}
void FfbHandle_SetCondition(USB_FFBReport_SetCondition_Output_Data_t *data)
{
uint8_t eid = data->effectBlockIndex;
volatile FFP_MIDI_Effect_Basic *common_midi_data = &gEffectStates[eid].data;
/* LogTextP(PSTR("Set Condition:"));
LogBinaryLf(data, sizeof(USB_FFBReport_SetCondition_Output_Data_t));
LogTextP(PSTR(" id =")); LogBinaryLf(&eid, sizeof(eid));
LogTextP(PSTR(" block =")); LogBinaryLf(&data->parameterBlockOffset, sizeof(data->parameterBlockOffset));
LogTextP(PSTR(" offset=")); LogBinaryLf(&data->cpOffset, sizeof(data->cpOffset));
LogTextP(PSTR(" coeff+=")); LogBinaryLf(&data->positiveCoefficient, sizeof(data->positiveCoefficient));
*/
// FlushDebugBuffer();
/*
USB effect data:
uint8_t effectBlockIndex; // 1..40
uint8_t parameterBlockOffset; // bits: 0..3=parameterBlockOffset, 4..5=instance1, 6..7=instance2
int8_t cpOffset; // -128..127
uint8_t positiveCoefficient; // 0..255
MIDI effect data:
uint16_t coeffAxis0;
uint16_t coeffAxis1;
uint16_t offsetAxis0; // not in friction
uint16_t offsetAxis1; // not in friction
*/
switch (common_midi_data->waveForm)
{
case 0x0d: // spring (midi: 0x0d)
case 0x0e: // damper (midi: 0x0e)
case 0x0f: // inertia (midi: 0x0f)
{
volatile FFP_MIDI_Effect_Spring_Inertia_Damper *midi_data = (FFP_MIDI_Effect_Spring_Inertia_Damper *) &gEffectStates[eid].data;
if (data->parameterBlockOffset == 0)
{
midi_data->coeffAxis0 = UsbInt8ToMidiInt14(data->positiveCoefficient);
midi_data->offsetAxis0 = UsbInt8ToMidiInt14(data->cpOffset);
}
else
{
midi_data->coeffAxis1 = UsbInt8ToMidiInt14(data->positiveCoefficient);
if (data->cpOffset == 0x80)
midi_data->offsetAxis1 = 0x007f;
else
midi_data->offsetAxis1 = UsbInt8ToMidiInt14(-data->cpOffset);
}
// Send data to MIDI
if (gEffectStates[eid].state & MEffectState_SentToJoystick)
{ // Send update
if (data->parameterBlockOffset == 0)
{
FfbSendModify(eid, 0x48, midi_data->coeffAxis0);
FfbSendModify(eid, 0x50, midi_data->offsetAxis0);
}
else
{
FfbSendModify(eid, 0x4C, midi_data->coeffAxis1);
FfbSendModify(eid, 0x54, midi_data->offsetAxis1);
}
}
}
break;
case 0x10: // friction (midi: 0x10)
{
volatile FFP_MIDI_Effect_Friction *midi_data = (FFP_MIDI_Effect_Friction *) &gEffectStates[eid].data;
if (data->parameterBlockOffset == 0)
midi_data->coeffAxis0 = UsbInt8ToMidiInt14(data->positiveCoefficient);
else
midi_data->coeffAxis1 = UsbInt8ToMidiInt14(data->positiveCoefficient);
// Send data to MIDI
if (gEffectStates[eid].state & MEffectState_SentToJoystick)
{ // Send update
if (data->parameterBlockOffset == 0)
FfbSendModify(eid, 0x48, midi_data->coeffAxis0);
else
FfbSendModify(eid, 0x4C, midi_data->coeffAxis1);
}
}
break;
default:
break;
}
}
void FfbHandle_SetPeriodic(USB_FFBReport_SetPeriodic_Output_Data_t *data)
{
uint8_t eid = data->effectBlockIndex;
LogTextP(PSTR("Set Periodic:"));
LogBinaryLf(data, sizeof(USB_FFBReport_SetPeriodic_Output_Data_t));
LogTextP(PSTR(" id=")); LogBinaryLf(&eid, sizeof(eid));
LogTextP(PSTR(" magnitude=")); LogBinaryLf(&data->magnitude, sizeof(data->magnitude));
LogTextP(PSTR(" offset =")); LogBinaryLf(&data->offset, sizeof(data->offset));
LogTextP(PSTR(" phase =")); LogBinaryLf(&data->phase, sizeof(data->phase));
LogTextP(PSTR(" period =")); LogBinaryLf(&data->period, sizeof(data->period));
// FlushDebugBuffer();
/*
USB effect data:
uint8_t reportId; // =4
uint8_t effectBlockIndex; // 1..40
uint8_t magnitude;
int8_t offset;
uint8_t phase; // 0..255 (=0..359, exp-2)
uint16_t period; // 0..32767 ms
MIDI effect data:
Offset values other than zero do not work and thus it is ignored on FFP
*/
volatile TEffectState *effect = &gEffectStates[eid];
volatile FFP_MIDI_Effect_Basic *midi_data = &effect->data;
effect->usb_magnitude = data->magnitude;
midi_data->param1 = 0x007f;
midi_data->param2 = 0x0101;
// Calculate waveLength (in MIDI it is in units of 1/Hz and can have value 0x6F..0x01)
if (data->period >= 1000)
midi_data->waveLength = 0x01;
else if (data->period <= 9)
midi_data->waveLength = 0x6F;
else
midi_data->waveLength = (1000 / data->period) & 0x7F;
// Check phase if relevant (+90 phase for sine makes it a cosine)
if (midi_data->waveForm == 2 || midi_data->waveForm == 3) // sine
{
if (data->phase >= 32 && data->phase <= 224)
{
midi_data->waveForm = 3; // cosine
}
else
{
midi_data->waveForm = 2; // sine
}
// Calculate min-max from magnitude and offset
uint8_t magnitude = CalcGain(data->magnitude, effect->usb_gain); // already at MIDI-level i.e. 1/2 of USB level!
midi_data->param1 = UsbInt8ToMidiInt14(data->offset / 2 + magnitude); // max
midi_data->param2 = UsbInt8ToMidiInt14(data->offset / 2 - magnitude); // min
if (effect->state & MEffectState_SentToJoystick)
{
FfbSendModify(eid, 0x74, midi_data->param1);
FfbSendModify(eid, 0x78, midi_data->param2);
}
}
if (effect->state & MEffectState_SentToJoystick)
{
// FfbSendModify(eid, 0x74, midi_data->magnitude); // FFP does not actually support changing magnitude on-fly here
FfbSendModify(eid, 0x70, midi_data->waveLength);
}
}
void FfbHandle_SetConstantForce(USB_FFBReport_SetConstantForce_Output_Data_t *data)
{
uint8_t eid = data->effectBlockIndex;
volatile TEffectState *effect_data = &gEffectStates[eid];
/* LogTextP(PSTR("Set Constant Force:"));
LogBinaryLf(data, sizeof(USB_FFBReport_SetConstantForce_Output_Data_t));
LogTextP(PSTR(" id=")); LogBinaryLf(&eid, sizeof(eid));
LogTextP(PSTR(" magnitude=")); LogBinaryLf(&data->magnitude, sizeof(data->magnitude));
*/
// FlushDebugBuffer();
/*
USB data:
uint8_t reportId; // =5
uint8_t effectBlockIndex; // 1..40
int16_t magnitude; // -255..255
MIDI effect data:
uint8_t command; // always 0x23 -- start counting checksum from here
uint8_t waveForm; // 2=sine, 5=Square, 6=RampUp, 7=RampDown, 8=Triange, 0x12=Constant
uint8_t unknown1; // ? always 0x7F
uint16_t duration; // unit=2ms
uint16_t unknown2; // ? always 0x0000
uint16_t direction;
uint8_t unknown3[5]; // ? always 7f 64 00 10 4e
uint8_t attackLevel;
uint16_t attackTime;
uint8_t magnitude;
uint16_t fadeTime;
uint8_t fadeLevel;
uint8_t waveLength; // 0x6F..0x01 => 1/Hz
uint8_t unknown5; // ? always 0x00
uint16_t param1; // Constant: positive=7f 00, negative=01 01, Other effects: 01 01
uint16_t param2; // Constant: 00 00, Other effects 01 01
*/
volatile FFP_MIDI_Effect_Basic *midi_data = &effect_data->data;
effect_data->usb_magnitude = data->magnitude;
if (data->magnitude >= 0)
{
midi_data->magnitude = CalcGain(data->magnitude, effect_data->usb_gain);
midi_data->param1 = 0x007f;
}
else
{
midi_data->magnitude = CalcGain(-(data->magnitude+1), effect_data->usb_gain);
midi_data->param1 = 0x0101;
}
midi_data->param2 = 0x0000;
if (gEffectStates[eid].state & MEffectState_SentToJoystick)
{ // Send update
FfbSendModify(eid, 0x74, midi_data->magnitude);
FfbSendModify(eid, 0x7C, midi_data->param1);
}
}
void FfbHandle_SetRampForce(USB_FFBReport_SetRampForce_Output_Data_t *data)
{
uint8_t eid = data->effectBlockIndex;
LogTextP(PSTR("Set Ramp Force:"));
LogBinaryLf(data, sizeof(USB_FFBReport_SetRampForce_Output_Data_t));
LogTextP(PSTR(" id=")); LogBinaryLf(&eid, sizeof(eid));
LogTextP(PSTR(" start=")); LogBinaryLf(&data->start, sizeof(data->start));
LogTextP(PSTR(" end =")); LogBinaryLf(&data->end, sizeof(data->end));
// FlushDebugBuffer();
// FFP supports only ramp up from MIN to MAX and ramp down from MAX to MIN?
/*
USB effect data:
uint8_t reportId; // =6
uint8_t effectBlockIndex; // 1..40
int8_t start;
int8_t end;
*/
volatile FFP_MIDI_Effect_Basic *midi_data = &gEffectStates[eid].data;
if (data->start < 0)
midi_data->param1 = 0x0100 | (-(data->start+1));
else
midi_data->param1 = data->start;
midi_data->param2 = UsbInt8ToMidiInt14(data->end);
if (gEffectStates[eid].state & MEffectState_SentToJoystick)
{ // Send update
FfbSendModify(eid, 0x78, midi_data->param1);
FfbSendModify(eid, 0x74, midi_data->param2);
}
}
void FfbHandle_SetCustomForceData(USB_FFBReport_SetCustomForceData_Output_Data_t *data)
{
LogTextLf("Set Custom Force Data");
}
void FfbHandle_SetDownloadForceSample(USB_FFBReport_SetDownloadForceSample_Output_Data_t *data)
{
LogTextLf("Set Download Force Sample");
}