summaryrefslogtreecommitdiff
path: root/main.c
blob: 8006bdcad50c2dc1332242c948cce2e999ead4ec (plain)
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
// USB HID test
// Copyright (C) 2014 Florian Jung
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.



/* Setup:

   lfuse: 0xe0
   hfuse: 0xd9

   attach a 12MHz quartz to the atmega8, using the appropriate XTAL pins.

   connect avr.PD{2,3} over 68 Ohm to usb.DATA{+,-}
   connect usb.DATA{+,-} over 3v6-Z-Diode ( ----|<---- ) to GND
   connect usb.DATA- over 1.5kOhm to usb.Vcc
   connect usb.GND to avr.GND.

   supply 3.3V to the 3.3V line of the gamecube controller.
   supply 5V to the 5V line of the gamecube controller.
   connect the GND and SHIELD lines of the controller to the common GND.
   connect the DATA line of the gamecube controller to the C5 pin.

   !!! ensure that the PORTC has bit 0 cleared at any time. !!!
   !!! otherwise, +5V is supplied to the pin; the gamecube  !!!
   !!! controller, however, may shortcut the line down to   !!!
   !!! GND at any time, leading to HARDWARE DAMAGE.         !!!
*/

#define F_CPU 12000000L

#include <avr/io.h>
#include <avr/delay.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include "usbdrv/usbdrv.h"

static char buffer[300]; // contains the exploded gamecube bits, i.e. each bit sent/received occupies one byte here.

#define floUsbHidReportDescriptorLen 862
PROGMEM const char floUsbHidReportDescriptor[862] = {

0x05, 0x01,		//USAGE_PAGE (Generic Desktop)
0x15, 0x00,		//LOGICAL_MINIMUM (0)
0x09, 0x04,		//USAGE (Joystick)
0xA1, 0x01,		//COLLECTION (Application)
// Start Joystick input definition
0x85, 0x01,		//    REPORT_ID (1)
0x05, 0x02,		//    USAGE_PAGE (Simulation Controls)
0x09, 0xBB,		//    USAGE (Throttle)
0x15, 0x81,		//    LOGICAL_MINIMUM (-127)
0x25, 0x7F,		//    LOGICAL_MAXIMUM (127)
0x75, 0x08,		//    REPORT_SIZE (8)
0x95, 0x01,		//    REPORT_COUNT (1)
0x81, 0x02,		//    INPUT (Data,Var,Abs)
0x05, 0x01,		//    USAGE_PAGE (Generic Desktop)
// Define the axes
0x09, 0x01,		//    USAGE (Pointer)
0xA1, 0x00,		//    COLLECTION (Physical)
0x09, 0x30,		//        USAGE (X)
0x09, 0x31,		//        USAGE (Y)
0x95, 0x02,		//        REPORT_COUNT (2)
0x81, 0x02,		//        INPUT (Data,Var,Abs)
0xC0,			//    END_COLLECTION
// Define the hat switch
0x09, 0x39,		//    USAGE (Hat switch)
0x15, 0x00,		//    LOGICAL_MINIMUM (0)
0x25, 0x03,		//    LOGICAL_MAXIMUM (3)
0x35, 0x00,		//    PHYSICAL_MINIMUM (0)
0x46, 0x0E, 0x01,	//    PHYSICAL_MAXIMUM (270)
0x66, 0x14, 0x00,	//    UNIT (Eng Rot:Angular Pos)
0x75, 0x04,		//    REPORT_SIZE (4)
0x95, 0x01,		//    REPORT_COUNT (1)
0x81, 0x02,		//    INPUT (Data,Var,Abs)
0x45, 0x00,		//    PHYSICAL_MAXIMUM (0)
0x66, 0x00, 0x00,	//    UNIT (None)
// Define the buttons
0x05, 0x09,		//    USAGE_PAGE (Button)
0x19, 0x01,		//    USAGE_MINIMUM (Button 1)
0x29, 0x04,		//    USAGE_MAXIMUM (Button 4)
0x25, 0x01,		//    LOGICAL_MAXIMUM (1)
0x95, 0x04,		//    REPORT_COUNT (4)
0x75, 0x01,		//    REPORT_SIZE (1)
0x35, 0x00,		//    PHYSICAL_MINIMUM (0)
0x81, 0x02,		//    INPUT (Data,Var,Abs)
// End Joystick Input definition




// Start Force Feedback command definitions
0x05, 0x0F,		//    USAGE_PAGE (Physical Interface)
// Effect Report Definition
0x09, 0x21,		//    USAGE (Set Effect Report)
0xA1, 0x02,		//    COLLECTION (Logical)
0x09, 0x22,		//        USAGE (Effect Block Index)
0x25, 0x7F,		//        LOGICAL_MAXIMUM (127)
0x75, 0x07,		//        REPORT_SIZE (7)
0x95, 0x01,		//        REPORT_COUNT (1)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0x09, 0x24,		//        USAGE (ROM Flag)
0x25, 0x01,		//        LOGICAL_MAXIMUM (1)
0x75, 0x01,		//        REPORT_SIZE (1)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
// Define the available effect types. Effect Type is a named array that will
//  accept any of the ET usages listed.
0x09, 0x25,		//        USAGE (Effect Type)
0xA1, 0x02,		//        COLLECTION (Logical)
0x09, 0x26,		//            USAGE (ET Constant Force)
0x09, 0x27,		//            USAGE (ET Ramp)
0x09, 0x30,		//            USAGE (ET Square)
0x09, 0x31,		//            USAGE (ET Sine)
0x09, 0x32,		//            USAGE (ET Triangle)
0x09, 0x33,		//            USAGE (ET Sawtooth Up)
0x09, 0x34,		//            USAGE (ET Sawtooth Down)
0x09, 0x40,		//            USAGE (ET Spring)
0x09, 0x41,		//            USAGE (ET Damper)
0x09, 0x42,		//            USAGE (ET Inertia)
0x15, 0x01,		//            LOGICAL_MINIMUM (1)
0x25, 0x0A,		//            LOGICAL_MAXIMUM (10)
0x75, 0x08,		//            REPORT_SIZE (8)
0x91, 0x00,		//            OUTPUT (Data,Ary,Abs)
0xC0,			//        END_COLLECTION
0x09, 0x50,		//        USAGE (Duration)
0x09, 0x54,		//        USAGE (Trigger Repeat Interval)
0x15, 0x00,		//        LOGICAL_MINIMUM (0)
0x26, 0x10, 0x27,	//        LOGICAL_MAXIMUM (10000)
0x46, 0x10, 0x27,	//        PHYSICAL_MAXIMUM (10000)
0x75, 0x10,		//        REPORT_SIZE (16)
0x66, 0x03, 0x10,	//        UNIT (Eng Lin:Time)
0x55, 0x0D,		//        UNIT_EXPONENT (-3)
0x95, 0x02,		//        REPORT_COUNT (2)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0x55, 0x0A,		//        UNIT_EXPONENT (-6)
0x09, 0x51,		//        USAGE (Sample Period)
0x95, 0x01,		//        REPORT_COUNT (1)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0x45, 0x00,		//        PHYSICAL_MAXIMUM (0)
0x55, 0x00,		//        UNIT_EXPONENT (0)
0x65, 0x00,		//        UNIT (None)
0x09, 0x52,		//        USAGE (Gain)
0x09, 0x53,		//        USAGE (Trigger Button)
0x25, 0x7F,		//        LOGICAL_MAXIMUM (127)
0x75, 0x08,		//        REPORT_SIZE (8)
0x95, 0x02,		//        REPORT_COUNT (2)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0x09, 0x55,		//        USAGE (Axes Enable)  (Tie these axes to the stick)
0xA1, 0x02,		//        COLLECTION (Logical)
0x05, 0x01,		//            USAGE_PAGE (Generic Desktop)
0x09, 0x01,		//            USAGE (Pointer)
0xA1, 0x00,		//            COLLECTION (Physical)
0x09, 0x30,		//                USAGE (X)
0x09, 0x31,		//                USAGE (Y)
0x25, 0x01,		//                LOGICAL_MAXIMUM (1)
0x75, 0x01,		//                REPORT_SIZE (1)
0x95, 0x02,		//                REPORT_COUNT (2)
0x91, 0x02,		//                OUTPUT (Data,Var,Abs)
0xC0,			//            END_COLLECTION
0xC0,			//        END_COLLECTION

0x95, 0x06,		//        REPORT_COUNT (6)  (6-bit pad)
0x91, 0x03,		//        OUTPUT (Cnst,Var,Abs)
0x05, 0x0F,		//        USAGE_PAGE (Physical Interface)
0x09, 0x57,		//        USAGE (Direction)
0xA1, 0x02,		//        COLLECTION (Logical)
0x05, 0x01,		//            USAGE_PAGE (Generic Desktop)
0x09, 0x01,		//            USAGE (Pointer)//  Tie these axes to the stick
0xA1, 0x00,		//            COLLECTION (Physical)
0x09, 0x30,		//                USAGE (X)
0x09, 0x31,		//                USAGE (Y)
0x15, 0x00,		//                LOGICAL_MINIMUM (0)
0x26, 0xFF, 0x00,	//                LOGICAL_MAXIMUM (255)
0x46, 0x68, 0x01,	//                PHYSICAL_MAXIMUM (360)
0x66, 0x14, 0x00,	//                UNIT (Eng Rot:Angular Pos)
0x75, 0x08,		//                REPORT_SIZE (8)
0x95, 0x02,		//                REPORT_COUNT (2)
0x91, 0x02,		//                OUTPUT (Data,Var,Abs)
0x65, 0x00,		//                UNIT (None)
0x45, 0x00,		//                PHYSICAL_MAXIMUM (0)
0xC0,			//            END_COLLECTION
0xC0,			//        END_COLLECTION
0x05, 0x0F,		//        USAGE_PAGE (Physical Interface)
0x09, 0x58,		//        USAGE (Type Specific Block Offset)
0xA1, 0x02,		//        COLLECTION (Logical)
0x0B, 0x01, 0x00, 0x0A, 0x00,	//    USAGE (Ordinals:Instance 1)
0x0B, 0x02, 0x00, 0x0A, 0x00,	//    USAGE (Ordinals:Instance 2)
0x26, 0xFD, 0x7F,	//            LOGICAL_MAXIMUM (32765)//  32K RAM or ROM max.
0x75, 0x10,		//            REPORT_SIZE (16)
0x95, 0x02,		//            REPORT_COUNT (2)
0x91, 0x02,		//            OUTPUT (Data,Var,Abs)
0xC0,			//        END_COLLECTION
0xC0,			//    END_COLLECTION
// Envelope Report Definition
0x09, 0x5A,		//    USAGE (Set Envelope Report)
0xA1, 0x02,		//    COLLECTION (Logical)
0x85, 0x02,		//        REPORT_ID (2)
0x09, 0x23,		//        USAGE (Parameter Block Offset)
0x26, 0xFD, 0x7F,	//        LOGICAL_MAXIMUM (32765)//  32K RAM or ROM max
0x75, 0x0F,		//        REPORT_SIZE (15)
0x95, 0x01,		//        REPORT_COUNT (1)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0x09, 0x24,		//        USAGE (ROM Flag)
0x25, 0x01,		//        LOGICAL_MAXIMUM (1)
0x75, 0x01,		//        REPORT_SIZE (1)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0x09, 0x5B,		//        USAGE (Attack Level)
0x09, 0x5D,		//        USAGE (Fade Level)
0x26, 0xFF, 0x00,	//        LOGICAL_MAXIMUM (255)
0x75, 0x08,		//        REPORT_SIZE (8)
0x95, 0x02,		//        REPORT_COUNT (2)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0x09, 0x5C,		//        USAGE (Attack Time)
0x09, 0x5E,		//        USAGE (Fade Time)
0x26, 0x10, 0x27,	//        LOGICAL_MAXIMUM (10000)
0x46, 0x10, 0x27,	//        PHYSICAL_MAXIMUM (10000)
0x66, 0x03, 0x10,	//        UNIT (Eng Lin:Time)
0x55, 0x0D,		//        UNIT_EXPONENT (-3)
0x75, 0x10,		//        REPORT_SIZE (16)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0x45, 0x00,		//        PHYSICAL_MAXIMUM (0)
0x65, 0x00,		//        UNIT (None)
0x55, 0x00,		//        UNIT_EXPONENT (0)
0xC0,			//    END_COLLECTION
// Condition Report Definition
0x09, 0x5F,		//    USAGE (Set Condition Report)
0xA1, 0x02,		//    COLLECTION (Logical)
0x85, 0x03,		//        REPORT_ID (3)
0x09, 0x23,		//        USAGE (Parameter Block Offset)
0x26, 0xFD, 0x7F,	//        LOGICAL_MAXIMUM (32765)
0x75, 0x0F,		//        REPORT_SIZE (15)
0x95, 0x01,		//        REPORT_COUNT (1)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0x09, 0x24,		//        USAGE (ROM Flag)
0x25, 0x01,		//        LOGICAL_MAXIMUM (1)
0x75, 0x01,		//        REPORT_SIZE (1)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0x09, 0x60,		//        USAGE (CP Offset)
0x09, 0x61,		//        USAGE (Positive Coefficient)
0x09, 0x62,		//        USAGE (Negative Coefficient)
0x09, 0x63,		//        USAGE (Positive Saturation)
0x09, 0x64,		//        USAGE (Negative Saturation)
0x09, 0x65,		//        USAGE (Dead Band)
0x26, 0xFF, 0x00,	//        LOGICAL_MAXIMUM (255)
0x75, 0x08,		//        REPORT_SIZE (8)
0x95, 0x06,		//        REPORT_COUNT (6)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0xC0,			//    END_COLLECTION
// Periodic Report Definition
0x09, 0x6E,		//    USAGE (Set Periodic Report)
0xA1, 0x02,		//    COLLECTION (Logical)
0x85, 0x04,		//        REPORT_ID (4)
0x09, 0x23,		//        USAGE (Parameter Block Offset)
0x26, 0xFD, 0x7F,	//        LOGICAL_MAXIMUM (32765)
0x75, 0x0F,		//        REPORT_SIZE (15)
0x95, 0x01,		//        REPORT_COUNT (1)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0x09, 0x24,		//        USAGE (ROM Flag)
0x25, 0x01,		//        LOGICAL_MAXIMUM (1)
0x75, 0x01,		//        REPORT_SIZE (1)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0x09, 0x70,		//        USAGE (Magnitude)
0x09, 0x6F,		//        USAGE (Offset)
0x09, 0x71,		//        USAGE (Phase)
0x26, 0xFF, 0x00,	//        LOGICAL_MAXIMUM (255)
0x75, 0x08,		//        REPORT_SIZE (8)
0x95, 0x03,		//        REPORT_COUNT (3)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0x09, 0x72,		//        USAGE (Period)
0x26, 0x10, 0x27,	//        LOGICAL_MAXIMUM (10000)
0x46, 0x10, 0x27,	//        PHYSICAL_MAXIMUM (10000)
0x66, 0x03, 0x10,	//        UNIT (Eng Lin:Time)
0x55, 0x0D,		//        UNIT_EXPONENT (-3)
0x75, 0x10,		//        REPORT_SIZE (16)
0x95, 0x01,		//        REPORT_COUNT (1)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0x45, 0x00,		//        PHYSICAL_MAXIMUM (0)
0x65, 0x00,		//        UNIT (None)
0x55, 0x00,		//        UNIT_EXPONENT (0)
0xC0,			//    END_COLLECTION
// Constant Force Report Definition
0x09, 0x73,		//    USAGE (Set Constant Force Report)
0xA1, 0x02,		//    COLLECTION (Logical)
0x85, 0x05,		//        REPORT_ID (5)
0x09, 0x23,		//        USAGE (Parameter Block Offset)
0x26, 0xFD, 0x7F,	//        LOGICAL_MAXIMUM (32765)
0x75, 0x0F,		//        REPORT_SIZE (15)
0x95, 0x01,		//        REPORT_COUNT (1)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0x09, 0x24,		//        USAGE (ROM Flag)
0x25, 0x01,		//        LOGICAL_MAXIMUM (1)
0x75, 0x01,		//        REPORT_SIZE (1)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0x09, 0x70,		//        USAGE (Magnitude)
0x26, 0xFF, 0x00,	//        LOGICAL_MAXIMUM (255)
0x75, 0x08,		//        REPORT_SIZE (8)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0xC0,			//    END_COLLECTION
// Ramp Force Report Definition
0x09, 0x74,		//    USAGE (Set Ramp Force Report)
0xA1, 0x02,		//    COLLECTION (Logical)
0x85, 0x06,		//        REPORT_ID (6)
0x09, 0x23,		//        USAGE (Parameter Block Offset)
0x26, 0xFD, 0x7F,	//        LOGICAL_MAXIMUM (32765)
0x75, 0x0F,		//        REPORT_SIZE (15)
0x95, 0x01,		//        REPORT_COUNT (1)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0x09, 0x24,		//        USAGE (ROM Flag)
0x25, 0x01,		//        LOGICAL_MAXIMUM (1)
0x75, 0x01,		//        REPORT_SIZE (1)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0x09, 0x75,		//        USAGE (Ramp Start)
0x09, 0x76,		//        USAGE (Ramp End)
0x26, 0xFF, 0x00,	//        LOGICAL_MAXIMUM (255)
0x75, 0x08,		//        REPORT_SIZE (8)
0x95, 0x02,		//        REPORT_COUNT (2)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0xC0,			//    END_COLLECTION
// Custom Force Data Report Definition
//  Downloads are always into RAM space so the ROM usage is not declared.
0x09, 0x68,		//    USAGE (Custom Force Data Report)
0xA1, 0x02,		//    COLLECTION (Logical)
0x85, 0x07,		//        REPORT_ID (7)
0x09, 0x23,		//        USAGE (Parameter Block Offset)
0x26, 0xFD, 0x7F,	//        LOGICAL_MAXIMUM (32765)
0x75, 0x0F,		//        REPORT_SIZE (15)
0x95, 0x01,		//        REPORT_COUNT (1)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0x0B, 0x3B, 0x00, 0x01, 0x00,//        USAGE (Generic Desktop:Byte Count)
0x26, 0x00, 0x01,	//        LOGICAL_MAXIMUM (256)
0x75, 0x09,		//        REPORT_SIZE (9)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0x09, 0x69,		//        USAGE (Custom Force Data)
0x26, 0xFF, 0x00,	//        LOGICAL_MAXIMUM (255)
0x75, 0x08,		//        REPORT_SIZE (8)
0x96, 0x00, 0x01,	//        REPORT_COUNT (256)
0x92, 0x02, 0x01,	//        OUTPUT (Data,Var,Abs,Buf)
0xC0,			//    END_COLLECTION
// Download Force Sample Definition
0x09, 0x66,		//    USAGE (Download Force Sample)
0xA1, 0x02,		//    COLLECTION (Logical)
0x85, 0x08,		//        REPORT_ID (8)
0x05, 0x01,		//        USAGE_PAGE (Generic Desktop)
0x09, 0x01,		//        USAGE (Pointer)
0xA1, 0x02,		//        COLLECTION (Logical)
0x09, 0x30,		//            USAGE (X)
0x09, 0x31,		//            USAGE (Y)
0x15, 0x81,		//            LOGICAL_MINIMUM (-127)
0x25, 0x7F,		//            LOGICAL_MAXIMUM (127)
0x75, 0x08,		//            REPORT_SIZE (8)
0x95, 0x02,		//            REPORT_COUNT (2)
0x91, 0x02,		//            OUTPUT (Data,Var,Abs)
0xC0,			//        END_COLLECTION
0xC0,			//    END_COLLECTION
0x05, 0x0F,		//    USAGE_PAGE (Physical Interface)
// Define the Custom Force parameter block
//  Custom Effects are always RAM based
//  so ROM flags are not declared.
0x09, 0x6B,		//    USAGE (Set Custom Force Report)
0xA1, 0x02,		//    COLLECTION (Logical)
0x85, 0x09,		//        REPORT_ID (9)
 //      Parameter block offset in pool
 //      Custom Force data offset in pool
0x09, 0x23,		//        USAGE (Parameter Block Offset)
0x09, 0x6C,		//        USAGE (Custom Force Data Offset)
0x09, 0x6D,		//        USAGE (Sample Count)
0x15, 0x00,		//        LOGICAL_MINIMUM (0)
0x26, 0xFD, 0x7F,	//        LOGICAL_MAXIMUM (32765)//  32K of RAM or ROM max.
0x95, 0x03,		//        REPORT_COUNT (3)
0x75, 0x10,		//        REPORT_SIZE (16)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0xC0,			//    END_COLLECTION
// Effect Operation Report Definition
0x09, 0x77,		//    USAGE (Effect Operation Report)
0xA1, 0x02,		//    COLLECTION (Logical)
0x85, 0x0A,		//        REPORT_ID (10)
0x09, 0x22,		//        USAGE (Effect Block Index)
0x25, 0x7F,		//        LOGICAL_MAXIMUM (127)
0x75, 0x07,		//        REPORT_SIZE (7)
0x95, 0x01,		//        REPORT_COUNT (1)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0x09, 0x24,		//        USAGE (ROM Flag)
0x25, 0x01,		//        LOGICAL_MAXIMUM (1)
0x75, 0x01,		//        REPORT_SIZE (1)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0x09, 0x78,		//        USAGE (Effect Operation)
0xA1, 0x02,		//        COLLECTION (Logical)
0x09, 0x79,		//            USAGE (Op Effect Start)
0x09, 0x7A,		//            USAGE (Op Effect Start Solo)
0x09, 0x7B,		//            USAGE (Op Effect Stop)
0x15, 0x01,		//            LOGICAL_MINIMUM (1)
0x25, 0x03,		//            LOGICAL_MAXIMUM (3)
0x75, 0x08,		//            REPORT_SIZE (8)
0x91, 0x00,		//            OUTPUT (Data,Ary,Abs)
0xC0,			//        END_COLLECTION
0x09, 0x7C,		//        USAGE (Loop Count)
0x15, 0x00,		//        LOGICAL_MINIMUM (0)
0x26, 0xFF, 0x00,	//        LOGICAL_MAXIMUM (255)
0x91, 0x02,		//        OUTPUT (Data,Var,Abs)
0xC0,			//    END_COLLECTION
// PID Pool Report Definition
0x09, 0x7F,		//    USAGE (PID Pool Report)
0xA1, 0x02,		//    COLLECTION (Logical)
0x85, 0x01,		//    REPORT_ID (1)
0x09, 0x80,		//    USAGE (RAM Pool Size)
0x09, 0x81,		//    USAGE (ROM Pool Size)
0x09, 0x82,		//    USAGE (ROM Effect Block Count)
0x26, 0xFD, 0x7F,	//    LOGICAL_MAXIMUM (32765)
0x95, 0x03,		//    REPORT_COUNT (3)
0x75, 0x10,		//    REPORT_SIZE (16)
0xB1, 0x02,		//    FEATURE (Data,Var,Abs)
0x09, 0xA8,		//    USAGE (Parameter Block Size)
0xA1, 0x02,		//    COLLECTION (Logical)
0x09, 0x21,		//    USAGE (Set Effect Report)
0x09, 0x5A,		//    USAGE (Set Envelope Report)
0x09, 0x5F,		//    USAGE (Set Condition Report)
0x09, 0x6E,		//    USAGE (Set Periodic Report)
0x09, 0x73,		//    USAGE (Set Constant Force Report)
0x09, 0x74,		//    USAGE (Set Ramp Force Report)
0x09, 0x6B,		//    USAGE (Set Custom Force Report)
0x26, 0xFF, 0x00,	//    LOGICAL_MAXIMUM (255)
0x75, 0x08,		//    REPORT_SIZE (8)
0x95, 0x07,		//    REPORT_COUNT (7)
0xB1, 0x02,		//    FEATURE (Data,Var,Abs)
0xC0,			//    END_COLLECTION
0x25, 0x01,		//    LOGICAL_MAXIMUM (1)
0x75, 0x07,		//    REPORT_SIZE (7)
0x95, 0x01,		//    REPORT_COUNT (1)
0xB1, 0x03,		//    FEATURE (Cnst,Var,Abs)//  7-bit pad
0x09, 0x67,		//    USAGE (Isoch Custom Force Enable)
0x75, 0x01,		//    REPORT_SIZE (1)
0xB1, 0x02,		//    FEATURE (Data,Var,Abs)
0xC0,			//    END_COLLECTION
// PID State Report Definition
0x09, 0x92,		//    USAGE (PID State Report)
0xA1, 0x02,		//    COLLECTION (Logical)
0x85, 0x02,		//    REPORT_ID (2)
0x09, 0x22,		//    USAGE (Effect Block Index)
0x25, 0x7F,		//    LOGICAL_MAXIMUM (127)
0x75, 0x07,		//    REPORT_SIZE (7)
0x81, 0x02,		//    INPUT (Data,Var,Abs)
0x09, 0x24,		//    USAGE (ROM Flag)
0x25, 0x01,		//    LOGICAL_MAXIMUM (1)
0x75, 0x01,		//    REPORT_SIZE (1)
0x95, 0x01,		//    REPORT_COUNT (1)
0x81, 0x02,		//    INPUT (Data,Var,Abs)
0x09, 0x94,		//    USAGE (Effect Playing)
0x09, 0xA0,		//    USAGE (Actuators Enabled)
0x09, 0xA4,		//    USAGE (Safety Switch)
0x09, 0xA6,		//    USAGE (Actuator Power)
0x75, 0x01,		//    REPORT_SIZE (1)
0x95, 0x04,		//    REPORT_COUNT (4)
0x81, 0x02,		//    INPUT (Data,Var,Abs)
0x81, 0x03,		//    INPUT (Cnst,Var,Abs)//  4-bit pad
0xC0,			//    END_COLLECTION
// PID Device Control Report Definition
0x09, 0x95,		//    USAGE (PID Device Control Report)
0xA1, 0x02,		//    COLLECTION (Logical)
0x85, 0x0B,		//    REPORT_ID (11)
0x09, 0x96,		//    USAGE (PID Device Control)
0xA1, 0x02,		//    COLLECTION (LOGICAL)
0x09, 0x97,		//    USAGE (0xDC, Enable Actuators)
0x09, 0x98,		//    USAGE (0xDC, Disable Actuators)
0x09, 0x99,		//    USAGE (0xDC, Stop All Effects)
0x09, 0x9A,		//    USAGE (0xDC, Reset)
0x09, 0x9B,		//    USAGE (0xDC, Pause)
0x09, 0x9C,		//    USAGE (0xDC, Continue)
0x15, 0x01,		//    LOGICAL_MINIMUM (1)
0x25, 0x06,		//    LOGICAL_MAXIMUM (6)
0x75, 0x01,		//    REPORT_SIZE (1)
0x95, 0x08,		//    REPORT_COUNT (4)
0x91, 0x02,		//    OUTPUT(Data,Ary,Abs)
0xC0,			//    END_COLLECTION
0xC0,			//    END_COLLECTION
// PID Pool Move Report Definition
0x09, 0x85,		//    USAGE (PID Pool Move Report)
0xA1, 0x02,		//    COLLECTION (Logical)
0x85, 0x0C,		//    REPORT_ID (12)
0x09, 0x86,		//    USAGE (Move Source)
0x09, 0x87,		//    USAGE (Move Destination)
0x09, 0x88,		//    USAGE (Move Length)
0x26, 0xFF, 0x7F,	//    LOGICAL_MAXIMUM (32767)
0x75, 0x10,		//    REPORT_SIZE (16)
0x95, 0x03,		//    REPORT_COUNT (3)
0x92, 0x02, 0x01,	//    OUTPUT (Data,Var,Abs,Buf)
0xC0,			//    END_COLLECTION
// Device Gain Report Definition
0x09, 0x7D,		//    USAGE (Device Gain Report)
0xA1, 0x02,		//    COLLECTION (Logical)
0x85, 0x02,		//    REPORT_ID (2)
0x09, 0x7E,		//    USAGE (Device Gain)
0x26, 0xFF, 0x00,	//    LOGICAL_MAXIMUM (255)
0x75, 0x08,		//    REPORT_SIZE (8)
0x95, 0x01,		//    REPORT_COUNT (1)
0xB1, 0x02,		//    FEATURE (Data,Var,Abs)
0xC0,			//    END_COLLECTION
0xC0			//END_COLLECTION


};

typedef struct {
    uchar   buttons;
    uchar   dpad;
    uchar    x;
    uchar    y;
    uchar    cx;
    uchar    cy;
    uchar   ltrig;
    uchar   rtrig;
} report_t;


report_t reportBuffer;



usbMsgLen_t usbFunctionDescriptor(struct usbRequest* rq)
{
	if ((rq->bmRequestType & USBRQ_TYPE_MASK) != USBRQ_TYPE_STANDARD)
		return 0;

	if (rq->bRequest == USBRQ_GET_DESCRIPTOR  &&  rq->wValue.bytes[1]==USBDESCR_HID_REPORT)
	{
		usbMsgPtr = (void*)floUsbHidReportDescriptor;
		return floUsbHidReportDescriptorLen;
	}

	return 0;
}



/* device is detected, however unreliably. dunno why. */


void debug(int i)
{
	if (i >= 63) i=63;
	PORTB = ~i;
}

USB_PUBLIC usbMsgLen_t usbFunctionSetup(uchar data[8])
{
	usbRequest_t* rq = (usbRequest_t*) data;

    if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS  &&
        (rq->bRequest == USBRQ_HID_GET_REPORT))
    {  
            // wValue: ReportType (highbyte), ReportID (lowbyte)
            usbMsgPtr = (void *)&reportBuffer; // we only have this one
            return sizeof(reportBuffer);
    }

	return 0;
}


#define OUT0 "sbi %2, 5 \n"  /* pull line to GND */
#define OUT1 "cbi %2, 5 \n"  /* tristate the line */

#define WAIT10 "ldi r16, 1 \n rcall delay_loop \n"
#define WAIT34 "ldi r16, 9 \n rcall delay_loop \n"
#define WAIT10MINUS10 ""
#define WAIT34MINUS10 "ldi r16,5 \n rcall delay_loop \n nop \n nop \n"

/* send the first "len" bytes stored in "bytes" to the controller,
 * then receive the reply and store it into the global "buffer". */
int send_recv_gc(char* bytes, int len)
{
	/* Phase 1: Send the data.
	 * Phase 1.1: Explode the bits into buffer. buffer[0..7] will contain the bits of bytes[0],
	 *            buffer[8..15] contains the bits of bytes[1], and so on. MSB first.
	 * Phase 1.2: Actually send the data.
	 *
	 * intermediate Phase: wait for data line to become high again.
	 *
	 * Phase 2: Receive the reply.
	 * Phase 2.1: Busy-loop until the line is pulled down for the first time.
	 * Phase 2.2: Actually receive now:
	 *            A counter is set to 0x80.
	 *            Busy-loop until the line becomes high. Increment the counter in each iteration.
	 *            (The line is now high.)
	 *            Busy-loop until the line becomes low again. Decrement the counter each iteration.
	 *            (Done receiving the bit)
	 *            Write out the counter to buffer[], and proceed with the next bit.
	 *
	 *            If the counter over- or underflows, stop receiving, because the line
	 *            seems to be idle again (i.e. data transfer is finished). That's a timeout
	 *            of ca. 53 us. (when running at 12 MHz)
	 *
	 * buffer[] now contains the counter values of the bits.
	 * if (buffer[42] > 0x80), i.e. (buffer[42] & 0x80), then the line was longer LOW than HIGH -> bit42 = 0
	 * otherwise, the line was longer HIGH than LOW -> bit42 = 1.
	 */

	// The NOPs are there because of symmetry reasons.
	// the "// 2" comments after the assembly directives are the number of cycles this
	// instruction will take to execute.

	char* buf_ptr = buffer;

	/****** SEND PART ******/

	int k=0;
	for (int i=0; i<len; i++)
		for (int j=0x80; j!=0; j=j>>1, k++)
			buffer[k] = ((bytes[i] & j)!=0)?1:0 ;
	
	len=len*8+1;
	
	asm volatile(
		"push r31   ; save Z\n"
		"push r30 \n\n"

		";;;;;;;; SEND PART ;;;;;;;;\n\n"

		"send_loop:   \n"
		"sbiw %3, 1 \n" // 2
		"breq send_done \n" // 1 if not done, 2 if done
		"ld r16, z+ \n" // 2
		"tst r16 \n" // 1
		"brne send_one \n\n" // 1 if zero, 2 if nonzero

		"; otherwise, send zero \n"
		"nop \n" // 1
		OUT0
		WAIT34
		OUT1
		WAIT10MINUS10
		"rjmp send_loop \n\n" // 2

		"send_one: \n"
		OUT0
		WAIT10
		OUT1
		WAIT34MINUS10
		"rjmp send_loop \n\n"

		"delay_loop:\n"
		"; this costs 7 + 3*(r16) cycles \n"
		"dec r16\n"
		"brne delay_loop\n"
		"ret\n\n"

		"send_done: \n"
		"; now send the stop bit and release the line \n"
		"nop \n nop \n nop \n"
		OUT0
		"; instead of WAIT10, do sensible work \n"
		"pop r30    ; restore Z \n"          // 2
		"pop r31 \n"                         // 2
		"clr r16 \n"                         // 1
		"nop \n nop \n nop \n nop \n nop \n" // 5
		OUT1
		"; done :) \n\n\n"


		"; now the final thing is to wait for DATA become high again (should be immediately anyway) \n"
		"send_final_loop: \n"
		"inc r16 \n"
		"breq timeout \n"
		"sbis %1, 5 \n"
		"rjmp send_final_loop \n\n\n"



		";;;;;;;; RECEIVE PART ;;;;;;;;\n\n"


		"clr r16 \n"
		"recv_wait_low_initial_loop: \n"
		"inc r16 \n"
		"breq timeout \n"
		"sbic %1, 5 \n"
		"rjmp recv_wait_low_initial_loop \n" // from low to the start of counting: 6 cycles = 0.5us
		"nop \n"
		"nop \n"
		"nop \n"
		"nop \n"


		"recv_loop: \n"
		"recv_low_begin: \n"
		"ldi r16, 128 \n" // 1
		"recv_low_loop: \n"
		"inc r16 \n"      // 1
		"breq timeout \n" // 1 if no timeout
		"sbis %1, 5 \n"   // 1               // von high auf dec: 6
		"rjmp recv_low_loop \n\n" // 2 if executed, 1 if skipped.
		"nop \n"          // to account for the rjmp recv_loop below.
		"nop \n"
		"nop \n"
		"nop \n"

		"recv_high_begin: \n"
		"nop \n" // to account for the ldi in recv_low_begin.
		"recv_high_loop: \n"
		"dec r16 \n" // 1
		"breq timeout \n" // 1 if no timeout
		"sbic %1, 5 \n" // 1                 // von low auf inc: 6
		"rjmp recv_high_loop \n\n" // 2 if executed, 1 if skipped

		"st z+, r16 \n" // 2
		"rjmp recv_loop \n\n" // 2

		"timeout: \n"
		:	"+z" ((unsigned char volatile*) buf_ptr)
		:	"I" (_SFR_IO_ADDR(PINC)), 
			"I" (_SFR_IO_ADDR(DDRC)),
			"w" (len)
		: "r16", "memory"
		);

		return buf_ptr-buffer;

		// a value of >=128 means "0", <127 means "1" bit.
}

/* reads eight exploded bits from bufptr, and makes a 8bit-char from them.
 * Bit ordering is MSB first. A bit is considered as 1, if bufptr[i] & 0x80 is
 * *not* set, and vice versa. */
char decode_byte(char* bufptr)
{
	char result=0;
	for (int i=0;i<8;i++)
		result|= ( (bufptr[7-i]&0x80)?0:(1<<i) );
	
	return result;
}


int main (void)
{
	unsigned char ltrig=0, rtrig=0; // only used for rumbling test.

	DDRC=0x00;
	PORTC=0x00;

	DDRB=0xFF;

	wdt_enable(WDTO_1S);

	usbInit();

	usbDeviceDisconnect();
	for (int i=0;i<100;i++)
	{
		PORTB=~i;
		wdt_reset();
		_delay_ms(5);
	}
	usbDeviceConnect();

	PORTB=~42;

	sei();
debug(4);
	int n_received;
	
	int toggle=0;
	int count2=0;
	int count3=0;
	while(1)
	{
		wdt_reset();



		// DEBUGGING CODE starts here

		toggle=~toggle; // debug mainloop duration
		debug(toggle);

		count2++;       // debug rumble
		if (count2>=256+ltrig) count2=0;
		count3++;
		if (count3>=25) count3=0;

		// DEBUGGING CODE ends here.



		// USB CODE starts here

		usbPoll();
		
		if(usbInterruptIsReady())
		    usbSetInterrupt((void *)&reportBuffer, sizeof(reportBuffer));

		// USB CODE ends here



		_delay_us(10);



		// GAMECUBE COMMUNICATION starts here

		char getid_msg=0x00;
		send_recv_gc(&getid_msg, 1);  // send get-id message; probably unneccessary.



		char getstatus_msg[] = { 0x40, 0x03, 0x00 };

		if (count2<256 && count3<rtrig/10) // set rumbling bit, if appropriate
			getstatus_msg[2]|=0x01;
		
		n_received=send_recv_gc(getstatus_msg, 3); // send get-status message.
		if (n_received == 64) // sanity check to detect corrupted communication.
		{                     // this happens once in a while because USB interrupt kicks in.
			
			reportBuffer.x=decode_byte(buffer+16);
			reportBuffer.y=~decode_byte(buffer+24);
			
			reportBuffer.cx=decode_byte(buffer+32);
			reportBuffer.cy=~decode_byte(buffer+40);

			reportBuffer.ltrig=decode_byte(buffer+48);
			reportBuffer.rtrig=decode_byte(buffer+56);

			char btn1=decode_byte(buffer+0);
			char btn2=decode_byte(buffer+8);

			reportBuffer.buttons = (btn1 & 0x1F)  |  ((btn2 & 0x70) << 1);
			reportBuffer.dpad = (btn2 & 0x0F);

			ltrig=decode_byte(buffer+48);
			rtrig=decode_byte(buffer+56);
		}

		// GAMECUBE COMMUNICATION ends here.
	}

	return 0; // never reached
}