USB_Host_Shield_2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
PS3USB.h
Go to the documentation of this file.
1 /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
2 
3  This software may be distributed and modified under the terms of the GNU
4  General Public License version 2 (GPL2) as published by the Free Software
5  Foundation and appearing in the file GPL2.TXT included in the packaging of
6  this file. Please note that GPL2 Section 2[b] requires that all works based
7  on this software must also be made publicly available under the terms of
8  the GPL2 ("Copyleft").
9 
10  Contact information
11  -------------------
12 
13  Kristian Lauszus, TKJ Electronics
14  Web : http://www.tkjelectronics.com
15  e-mail : kristianl@tkjelectronics.com
16  */
17 
18 #ifndef _ps3usb_h_
19 #define _ps3usb_h_
20 
21 #if defined(ARDUINO) && ARDUINO >= 100
22 #include "Arduino.h"
23 #else
24 #include "WProgram.h"
25 #endif
26 
27 #include "Usb.h"
28 #include "PS3Enums.h"
29 
30 /* PS3 data taken from descriptors */
31 #define EP_MAXPKTSIZE 64 // max size for data via USB
32 
33 /* Endpoint types */
34 #define EP_INTERRUPT 0x03
35 
36 /* Names we give to the 3 ps3 pipes - this is only used for setting the bluetooth address into the ps3 controllers */
37 #define PS3_CONTROL_PIPE 0
38 #define PS3_OUTPUT_PIPE 1
39 #define PS3_INPUT_PIPE 2
40 
41 //PID and VID of the different devices
42 #define PS3_VID 0x054C // Sony Corporation
43 #define PS3_PID 0x0268 // PS3 Controller DualShock 3
44 #define PS3NAVIGATION_PID 0x042F // Navigation controller
45 #define PS3MOVE_PID 0x03D5 // Motion controller
46 
47 #define PS3_REPORT_BUFFER_SIZE 48 // Size of the output report buffer for the Dualshock and Navigation controllers
48 #define MOVE_REPORT_BUFFER_SIZE 7 // Size of the output report buffer for the Move Controller
49 
50 // used in control endpoint header for HID Commands
51 #define bmREQ_HID_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
52 #define HID_REQUEST_SET_REPORT 0x09
53 
54 #define PS3_MAX_ENDPOINTS 3
55 
56 class PS3USB : public USBDeviceConfig {
57 public:
58  PS3USB(USB *pUsb, uint8_t btadr5=0, uint8_t btadr4=0, uint8_t btadr3=0, uint8_t btadr2=0, uint8_t btadr1=0, uint8_t btadr0=0);
59 
60  // USBDeviceConfig implementation
61  virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
62  virtual uint8_t Release();
63  virtual uint8_t Poll();
64  virtual uint8_t GetAddress() { return bAddress; };
65  virtual bool isReady() { return bPollEnable; };
66 
67  void setBdaddr(uint8_t* BDADDR);
68  void setMoveBdaddr(uint8_t* BDADDR);
69 
70  /* PS3 Controller Commands */
71  /*
72  getButtonPress will return true as long as the button is held down
73  While getButtonClick will only return it once
74  So you instance if you need to increase a variable once you would use getButtonClick,
75  but if you need to drive a robot forward you would use getButtonPress
76  */
77  bool getButtonPress(Button b);
78  bool getButtonClick(Button b);
79 
80  uint8_t getAnalogButton(Button a);
81  uint8_t getAnalogHat(AnalogHat a);
82  uint16_t getSensor(Sensor a);
83  double getAngle(Angle a);
84  bool getStatus(Status c);
85  String getStatusString();
86 
87  /* Commands for Dualshock 3 and Navigation controller */
88  void setAllOff();
89  void setRumbleOff();
90  void setRumbleOn(Rumble mode);
91  void setLedOff(LED a);
92  void setLedOn(LED a);
93  void setLedToggle(LED a);
94 
95  /* Commands for Motion controller only */
96  void moveSetBulb(uint8_t r, uint8_t g, uint8_t b);//Use this to set the Color using RGB values
97  void moveSetBulb(Colors color);//Use this to set the Color using the predefined colors in "enum Colors"
98  void moveSetRumble(uint8_t rumble);
99 
100  bool PS3Connected;// Variable used to indicate if the normal playstation controller is successfully connected
101  bool PS3MoveConnected;// Variable used to indicate if the move controller is successfully connected
102  bool PS3NavigationConnected;// Variable used to indicate if the navigation controller is successfully connected */
103 
104 protected:
105  /* mandatory members */
107  uint8_t bAddress; // device address
108  EpInfo epInfo[PS3_MAX_ENDPOINTS]; //endpoint info structure
109 
110 private:
111  bool bPollEnable;
112 
113  uint32_t timer; // used to continuously set PS3 Move controller Bulb and rumble values
114 
115  uint32_t ButtonState;
116  uint32_t OldButtonState;
117  uint32_t ButtonClickState;
118 
119  uint8_t my_bdaddr[6]; // Change to your dongles Bluetooth address in the constructor
120  uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
121  uint8_t writeBuf[EP_MAXPKTSIZE]; // General purpose buffer for output data
122 
123  void readReport(); // read incoming data
124  void printReport(); // print incoming date - Uncomment for debugging
125 
126  /* Private commands */
127  void PS3_Command(uint8_t* data, uint16_t nbytes);
128  void enable_sixaxis(); // Command used to enable the Dualshock 3 and Navigation controller to send data via USB
129  void Move_Command(uint8_t* data, uint16_t nbytes);
130 };
131 #endif