summaryrefslogtreecommitdiff
path: root/USB_Host_Shield/examples/PS3LCD.pde
blob: ae103e0ac9528b12e519794045a8ecb0426faa12 (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


/* MAX3421E USB Host controller LCD/PS3 demonstration */
#include <Spi.h>
#include <Max3421e.h>
#include <Usb.h>
#include <Max_LCD.h>
#include <MemoryFree.h>
#include <avr/pgmspace.h>

/*The application will work in reduced host mode, so we can save program and data
memory space. After verifying the PID and VID we will use known values for the 
configuration values for device, interface, endpoints and HID */

/* PS3 data taken from descriptors */
#define PS3_ADDR        1
#define PS3_VID_LO      0x4c  // Sony VID
#define PS3_VID_HI      0x05
#define PS3_PID_LO      0x68  // Batch Device
#define PS3_PID_HI      0x02
#define PS3_CONFIGURATION 1
#define PS3_IF          0
#define PS3_NUM_EP      3
#define EP_MAXPKTSIZE   64
#define EP_INTERRUPT    0x03 
#define EP_POLL         0x01
#define CONTROL_EP      0
#define OUTPUT_EP       1
#define REPORT_EP       2


#define PS3_F4_REPORT_LEN 4
#define PS3_F5_REPORT_LEN 8
#define PS3_01_REPORT_LEN 48
#define HID_REPORT_FEATURE 3 
#define HID_REPORT_OUTPUT  2
#define PS3_F4_REPORT_ID  0xF4
#define PS3_01_REPORT_ID  0x01
#define PS3_F5_REPORT_ID 0xF5

/* Defines for the PS3 Data in the HID Report
*/
#define LAnalogX    buf[6]
#define LAnalogY    buf[7]
#define RAnalogX    buf[8]
#define RAnalogY    buf[9]
#define buttons1    buf[2]
#define buttons2    buf[3]
#define buttons3    buf[4]
#define buttonchange ((buttons1 != oldbuttons1) | (buttons2 != oldbuttons2))
#define buSelect    (buttons1 & 0x01)
#define buLAnalog   (buttons1 & 0x02)
#define buRAnalog   (buttons1 & 0x04)
#define buStart     (buttons1 & 0x08)
#define buUp        (buttons1 & 0x10)
#define buRight     (buttons1 & 0x20)
#define buDown      (buttons1 & 0x40)
#define buLeft      (buttons1 & 0x80)
#define buL2        (buttons2 & 0x01)
#define buR2        (buttons2 & 0x02)
#define buL1        (buttons2 & 0x04)
#define buR1        (buttons2 & 0x08)
#define buTriangle  (buttons2 & 0x10)
#define buCircle    (buttons2 & 0x20)
#define buCross     (buttons2 & 0x40)
#define buSquare    (buttons2 & 0x80)
#define buPS        (buttons3 & 0x01)
#define AnalogUp    buf[14]
#define AnalogRight buf[15]
#define AnalogDown  buf[16]
#define AnalogLeft  buf[17]
#define AnalogL2    buf[18]
#define AnalogR2    buf[19]
#define AnalogL1    buf[20]
#define AnalogR1    buf[21]
#define AnalogTriangle buf[22]
#define AnalogCircle buf[23]
#define AnalogCross  buf[24]
#define AnalogSquare buf[25]
#define AccelX      (((unsigned char)buf[42] | (unsigned char)buf[41] << 8)-512)
#define AccelY      (((unsigned char)buf[44] | (unsigned char)buf[43] << 8)-512)
#define AccelZ      (((unsigned char)buf[46] | (unsigned char)buf[45] << 8)-512)
#define GyroX       (((unsigned char)buf[48] | (unsigned char)buf[47] << 8)-512)



/*Menu screens

*/
#define Root    0
#define Basic   1
#define Buttons 2
#define Joystick 3
#define Pressure 4
#define Accelerometer 5
#define LED 6
#define Bdaddr 7
#define Freememory 8


/* Menu Text
*/

prog_char menutext_0[] PROGMEM = "Select Test";
prog_char menutext_1[] PROGMEM = "Basic Tests";  
prog_char menutext_2[] PROGMEM = "Buttons Test";
prog_char menutext_3[] PROGMEM = "Joystick Test";
prog_char menutext_4[] PROGMEM = "Pressure Test";
prog_char menutext_5[] PROGMEM = "Motion Test";
prog_char menutext_6[] PROGMEM = "LED/Rumble Test";
prog_char menutext_7[] PROGMEM = "Bluetooth Addr";
prog_char menutext_8[] PROGMEM = "Free Memory";


PROGMEM const char *menu_table[] = 	  
{   
  menutext_0,
  menutext_1,
  menutext_2,
  menutext_3,
  menutext_4,
  menutext_5,
  menutext_6,
  menutext_7,
  menutext_8 };

prog_char output_01_report[] PROGMEM = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                     0x00, 0x02, 0xff, 0x27, 0x10, 0x00, 0x32, 0xff, 
                                     0x27, 0x10, 0x00, 0x32, 0xff, 0x27, 0x10, 0x00, 
                                     0x32, 0xff, 0x27, 0x10, 0x00, 0x32, 0x00, 0x00, 
                                     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

prog_char feature_F4_report[] PROGMEM = {0x42, 0x0c, 0x00, 0x00};

EP_RECORD ep_record[ PS3_NUM_EP ];  //endpoint record structure for the PS3 controller

char buf[ 64 ] = { 0 };      //General purpose buffer for usb data
char oldbuttons1, oldbuttons2;
char screen, selscreen;
char lcdbuffer[17];
unsigned char bdaddr[6];
char bdcursor;
char ledrum;
char lrcursor;


void setup();
void loop();

MAX3421E Max;
USB Usb;
Max_LCD LCD;

void setup() {
  // set up the LCD's number of rows and columns: 
  LCD.begin(16, 2);
  LCD.home();
  LCD.print("PS3 Controller");
  LCD.setCursor(0,1);
  LCD.print("Wait for connect");
  Serial.begin( 9600 );
  Serial.println("PS3 Controller Start");
  Serial.print("freeMemory() reports ");
  Serial.println( freeMemory() );
  Max.powerOn();
  delay(200);
}

void loop() {
  

    Max.Task();
    Usb.Task();
    if( Usb.getUsbTaskState() == USB_STATE_CONFIGURING ) {  //wait for addressing state
        PS3_init();
        process_report();
        Usb.setUsbTaskState( USB_STATE_RUNNING );
    }
    if( Usb.getUsbTaskState() == USB_STATE_RUNNING ) {  //poll the PS3 Controller 
        PS3_poll();
    }
}
/* Initialize PS3 Controller */
void PS3_init( void )
{
 byte rcode = 0;  //return code
 byte i;
/**/

    /* Initialize data structures for endpoints of device 1*/
    ep_record[ CONTROL_EP ] = *( Usb.getDevTableEntry( 0,0 ));  //copy endpoint 0 parameters
    ep_record[ OUTPUT_EP ].epAddr = 0x02;    // PS3 output endpoint
    ep_record[ OUTPUT_EP ].Attr  = EP_INTERRUPT;
    ep_record[ OUTPUT_EP ].MaxPktSize = EP_MAXPKTSIZE;
    ep_record[ OUTPUT_EP ].Interval  = EP_POLL;
    ep_record[ OUTPUT_EP ].sndToggle = bmSNDTOG0;
    ep_record[ OUTPUT_EP ].rcvToggle = bmRCVTOG0;
    ep_record[ REPORT_EP ].epAddr = 0x01;    // PS3 report endpoint
    ep_record[ REPORT_EP ].Attr  = EP_INTERRUPT;
    ep_record[ REPORT_EP ].MaxPktSize = EP_MAXPKTSIZE;
    ep_record[ REPORT_EP ].Interval  = EP_POLL;
    ep_record[ REPORT_EP ].sndToggle = bmSNDTOG0;
    ep_record[ REPORT_EP ].rcvToggle = bmRCVTOG0;
    
    Usb.setDevTableEntry( PS3_ADDR, ep_record );              //plug kbd.endpoint parameters to devtable
    
    /* read the device descriptor and check VID and PID*/
    rcode = Usb.getDevDescr( PS3_ADDR, ep_record[ CONTROL_EP ].epAddr, DEV_DESCR_LEN , buf );
    if( rcode ) {
        Serial.print("Error attempting read device descriptor. Return code :");
        Serial.println( rcode, HEX );
        while(1);  //stop
    }
    if((buf[ 8 ] != PS3_VID_LO) || (buf[ 9 ] != PS3_VID_HI) || (buf[ 10 ] != PS3_PID_LO) || (buf[ 11 ] != PS3_PID_HI) ) {
        Serial.print("Unsupported USB Device");
          while(1);  //stop   
    }
    
    /* Configure device */
    rcode = Usb.setConf( PS3_ADDR, ep_record[ CONTROL_EP ].epAddr, PS3_CONFIGURATION );                    
    if( rcode ) {
        Serial.print("Error attempting to configure PS3 controller. Return code :");
        Serial.println( rcode, HEX );
        while(1);  //stop
    }
    
 
    /* Set the PS3 controller to send reports */
    for (i=0; i < PS3_F4_REPORT_LEN; i++) buf[i] = pgm_read_byte_near( feature_F4_report + i); 
    rcode = Usb.setReport( PS3_ADDR, ep_record[ CONTROL_EP ].epAddr, PS3_F4_REPORT_LEN,  PS3_IF, HID_REPORT_FEATURE, PS3_F4_REPORT_ID , buf );
    if( rcode ) {
        Serial.print("Set report error: ");
        Serial.println( rcode, HEX );
        while(1);  //stop
       
    }
    
    /* Set the PS3 controller LED 1 On */
    for (i=0; i < PS3_01_REPORT_LEN; i++) buf[i] = pgm_read_byte_near( output_01_report + i); 
    rcode = Usb.setReport( PS3_ADDR, ep_record[ CONTROL_EP ].epAddr, PS3_01_REPORT_LEN,  PS3_IF, HID_REPORT_OUTPUT, PS3_01_REPORT_ID , buf );
    if( rcode ) {
        Serial.print("Set report error: ");
        Serial.println( rcode, HEX );
        while(1);  //stop
       
    }
    
    LCD.print("PS3 initialized");
    Serial.println("PS3 initialized");
    delay(200);
    screen = Root;
    selscreen = Basic;
    LCD.clear();
    LCD.home();
    LCD.print("Main Menu");
    LCD.setCursor(0,1);
    strcpy_P(lcdbuffer, (char*)pgm_read_word(&(menu_table[selscreen]))); 
    LCD.print(lcdbuffer);

    
}

/* Poll PS3 and print result */

void PS3_poll( void )
{
 
 byte rcode = 0;     //return code
    /* poll PS3 */
    rcode = Usb.inTransfer(PS3_ADDR, ep_record[ REPORT_EP ].epAddr, PS3_01_REPORT_LEN, buf );
    if( rcode != 0 ) {
       return;
    }
    process_report();
    return;
}

void process_report(void)
{
  byte i, j, mask;
  if(buPS){
    screen = Root;
    selscreen = Basic;
    LCD.clear();
    LCD.home();
    LCD.print("Main Menu");
    LCD.setCursor(0,1);
    strcpy_P(lcdbuffer, (char*)pgm_read_word(&(menu_table[selscreen]))); 
    LCD.print(lcdbuffer);
    oldbuttons1 = buttons1;
    oldbuttons2 = buttons2;

  }
  
  switch (screen){
    
    case Root:     
      if(buttonchange){
        if(buDown) selscreen--;
        else if(buUp | buSelect) selscreen++;
        else if(buStart) {
          screen = selscreen;
          LCD.clear();
          oldbuttons1 = buttons1;
          oldbuttons2 = buttons2;
          break;
        }
        else {
          oldbuttons1 = buttons1;
          oldbuttons2 = buttons2;
          break;
          
        }
        if (selscreen == 0) selscreen = 1;
        if (selscreen > 8) selscreen = 1;
        LCD.clear();
        LCD.home();
        LCD.print("Main Menu:");
        LCD.setCursor(0,1);
        strcpy_P(lcdbuffer, (char*)pgm_read_word(&(menu_table[selscreen]))); 
        LCD.print(lcdbuffer);
        oldbuttons1 = buttons1;
        oldbuttons2 = buttons2;
      }
      break;
    
    case Basic:
      if(buttonchange){
        LCD.home();
        if (buL1) LCD.print('X');
        else LCD.print(' ');
        LCD.print("  Test L/R");
        LCD.setCursor(0,1);
        if (buL2) LCD.print('X');
        else LCD.print(' ');
        LCD.print("  Buttons");
        LCD.setCursor(15,0);
        if (buR1) LCD.print('X');
        else LCD.print(' ');
        
        LCD.setCursor(15,1);
        if (buR2) LCD.print('X');
        else LCD.print(' ');
      }
      
      break;
    
    case Buttons:  
      if(buttonchange){
        LCD.home();
        LCD.print("0123456789ABCDEF");
        LCD.setCursor(0,1);
        mask = 1;
        for( i = 0; i < 8; i++){
          if (buttons1 & mask) lcdbuffer[i] = '^';
          else lcdbuffer[i] = ' ';
          mask <<= 1;
        }
        mask = 1;
        for( i = 0; i < 8; i++){
          if (buttons2 & mask) lcdbuffer[i+8] = '^';
          else lcdbuffer[i+8] = ' ';
          mask <<= 1;
        }
        LCD.print(lcdbuffer);
        oldbuttons1 = buttons1;
        oldbuttons2 = buttons2;
 
      }
      
      break;
    
    case Joystick:
      LCD.home();
      LCD.print('^');
      LCD.print((unsigned char)LAnalogY, DEC);
      LCD.print("  ");
      LCD.setCursor(8,0);
      LCD.print('^');
      LCD.print((unsigned char)RAnalogY, DEC);
      LCD.print("  ");
      LCD.setCursor(0,1);
      LCD.print('>');
      LCD.print((unsigned char)LAnalogX, DEC);
      LCD.print("  ");
      LCD.setCursor(8,1);
      LCD.print('>');
      LCD.print((unsigned char)RAnalogX, DEC);
      LCD.print("  ");
      break;
      
    case Pressure:
      LCD.home();
      LCD.print((unsigned char)AnalogUp, DEC);
      LCD.print(" ");
      LCD.print((unsigned char)AnalogDown, DEC);
      LCD.print(" ");
      LCD.print((unsigned char)AnalogLeft, DEC);
      LCD.print(" ");
      LCD.print((unsigned char)AnalogRight, DEC);
      LCD.print(" ");
      LCD.print((unsigned char)AnalogL1, DEC);
      LCD.print(" ");
      LCD.print((unsigned char)AnalogR1, DEC);
      LCD.print("      ");
 
      LCD.setCursor(0,1);
      LCD.print((unsigned char)AnalogCircle, DEC);
      LCD.print(" ");
      LCD.print((unsigned char)AnalogTriangle, DEC);
      LCD.print(" ");
      LCD.print((unsigned char)AnalogSquare, DEC);
      LCD.print(" ");
      LCD.print((unsigned char)AnalogCross, DEC);
      LCD.print(" ");
      LCD.print((unsigned char)AnalogL2, DEC);
      LCD.print(" ");
      LCD.print((unsigned char)AnalogR2, DEC);
      LCD.print("      ");
     
      break;
      
    case Accelerometer:
      LCD.home();
      LCD.print('X');
      LCD.print(AccelX, DEC);
      LCD.print("  ");
      LCD.setCursor(8,0);
      LCD.print('Y');
      LCD.print(AccelY, DEC);
      LCD.print("  ");
      LCD.setCursor(0,1);
      LCD.print('Z');
      LCD.print(AccelZ, DEC);
      LCD.print("  ");
      LCD.setCursor(8,1);
      LCD.print('G');
      LCD.print(GyroX, DEC);
      LCD.print("  ");
      break;
      
    case LED:
      if(buttonchange){
        oldbuttons1 = buttons1;
        oldbuttons2 = buttons2;
        if (buRight) lrcursor++;
        else if (buLeft) lrcursor--;
        else if (buUp) ledrum |= 1 << lrcursor;
        else if (buDown) ledrum &= ~(1 << lrcursor);
        if (lrcursor > 7) lrcursor = 0;
        if (lrcursor < 0) lrcursor = 7;
      }
      LCD.home();
      LCD.print("1 2 3 4 S W ");
      LCD.setCursor(0,1);
      j = 0;
      for (i=0; i < 6; i++){
        if(ledrum & 1 << i) lcdbuffer[j] = 'X';
        else lcdbuffer[j] = ' ';
        j++; ;
        lcdbuffer[j] = ' ';
        j++;
      }
      lcdbuffer[j] = 0;
      LCD.print(lcdbuffer);
      LCD.setCursor((lrcursor * 2),1);
      LCD.cursor();
      /* default buffer values */
      for (i=0; i < PS3_01_REPORT_LEN; i++) buf[i] = pgm_read_byte_near( output_01_report + i); 
      /* LED setings */
      buf[9] = (ledrum & 0x0f) << 1;
      /* rumble settings */
      if (ledrum & 0x30){
        buf[1] = buf[3] = 0xfe;
        if (ledrum & 0x10) buf[4] = 0xff;
        else buf[2] = 0xff;
      }
      
      Usb.setReport( PS3_ADDR, ep_record[ CONTROL_EP ].epAddr, PS3_01_REPORT_LEN,  PS3_IF, HID_REPORT_OUTPUT, PS3_01_REPORT_ID , buf );

      delay(100);
      LCD.noCursor();
      break;
      
    case Bdaddr:
      if(buttonchange){
        oldbuttons1 = buttons1;
        oldbuttons2 = buttons2;
 
        if (buRight) bdcursor++;
        else if (buLeft) bdcursor--;
        if (bdcursor > 11) bdcursor = 0;
        if (bdcursor < 0) bdcursor = 11;
        if(buUp){
          if(bdcursor % 2){
            if ((bdaddr[bdcursor /2] & 0x0f) == 0x0f) bdaddr[bdcursor /2] &= 0xf0;
            bdaddr[bdcursor / 2] += 0x1;           
          }
          else{
            if ((bdaddr[bdcursor /2] & 0xf0) == 0xf0) bdaddr[bdcursor /2] &= 0x0f;
            bdaddr[bdcursor / 2] += 0x10;       
          }
          
        }
        else if (buDown){
          if(bdcursor % 2){
            if ((bdaddr[bdcursor /2] & 0x0f) == 0x0) bdaddr[bdcursor /2] |= 0x0f;
            bdaddr[bdcursor / 2] -= 0x1;           
          }
          else{
            if ((bdaddr[bdcursor /2] & 0xf0) == 0x0) bdaddr[bdcursor /2] |= 0xf0;
            bdaddr[bdcursor / 2] -= 0x10;       
          }
         
         }
         if( buCross){
           buf[0] = 0x01;
           buf[1] = 0x00;
           for (i=0; i < 6; i++){
             buf[i+2] = bdaddr[i];
           }
          Serial.println( "bdaddr"); 
          Usb.setReport( PS3_ADDR, ep_record[ CONTROL_EP ].epAddr, PS3_F5_REPORT_LEN,  PS3_IF, HID_REPORT_FEATURE, PS3_F5_REPORT_ID , buf ); 
        }
      }
      LCD.home();
      LCD.print("R: ");
      Usb.getReport( PS3_ADDR, ep_record[ CONTROL_EP ].epAddr, PS3_F5_REPORT_LEN,  PS3_IF, HID_REPORT_FEATURE, PS3_F5_REPORT_ID , buf );
      for( i=0; i < 6; i++){
        if ((unsigned char)buf[i+2] < 16) LCD.print ('0');
        LCD.print((unsigned char)buf[i + 2], HEX);
      }
      
      LCD.setCursor(0,1);
      LCD.print("W: ");
      for( i=0; i < 6; i++){
        if (bdaddr[i] < 16) LCD.print ('0');
        LCD.print(bdaddr[i], HEX);
      }
      LCD.setCursor(3 + bdcursor ,1);
      LCD.cursor();
      
      delay(100);
      LCD.noCursor();
      break;
      
    case Freememory:
      LCD.home();
      LCD.print("Free Memory ");
      LCD.print( freeMemory(), DEC );
      LCD.setCursor(0,1);
      break;
    
    default:
      break;
      
  }
  
  return;
}