USB_Host_Shield_2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
PS3BT.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 _ps3bt_h_
19 #define _ps3bt_h_
20 
21 #include "BTD.h"
22 #include "PS3Enums.h"
23 
24 #define HID_BUFFERSIZE 50 // size of the buffer for the Playstation Motion Controller
25 #define OUTPUT_REPORT_BUFFER_SIZE 48 //Size of the output report buffer for the controllers
26 
27 /* Bluetooth L2CAP states for L2CAP_task() */
28 #define L2CAP_WAIT 0
29 #define L2CAP_CONTROL_REQUEST 1
30 #define L2CAP_CONTROL_SUCCESS 2
31 #define L2CAP_INTERRUPT_SETUP 3
32 #define L2CAP_INTERRUPT_REQUEST 4
33 #define L2CAP_INTERRUPT_SUCCESS 5
34 #define L2CAP_HID_ENABLE_SIXAXIS 6
35 #define L2CAP_HID_PS3_LED 7
36 #define L2CAP_DONE 8
37 #define L2CAP_INTERRUPT_DISCONNECT 9
38 #define L2CAP_CONTROL_DISCONNECT 10
39 
40 /* L2CAP event flags */
41 #define L2CAP_FLAG_CONNECTION_CONTROL_REQUEST 0x01
42 #define L2CAP_FLAG_CONFIG_CONTROL_REQUEST 0x02
43 #define L2CAP_FLAG_CONFIG_CONTROL_SUCCESS 0x04
44 #define L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST 0x08
45 #define L2CAP_FLAG_CONFIG_INTERRUPT_REQUEST 0x10
46 #define L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS 0x20
47 #define L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE 0x40
48 #define L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE 0x80
49 
50 /*Macros for L2CAP event flag tests */
51 #define l2cap_connection_request_control_flag (l2cap_event_flag & L2CAP_FLAG_CONNECTION_CONTROL_REQUEST)
52 #define l2cap_config_request_control_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_CONTROL_REQUEST)
53 #define l2cap_config_success_control_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_CONTROL_SUCCESS)
54 #define l2cap_connection_request_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST)
55 #define l2cap_config_request_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_INTERRUPT_REQUEST)
56 #define l2cap_config_success_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS)
57 #define l2cap_disconnect_response_control_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE)
58 #define l2cap_disconnect_response_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE)
59 
60 class PS3BT : public BluetoothService {
61 public:
62  PS3BT(BTD *pBtd, uint8_t btadr5=0, uint8_t btadr4=0, uint8_t btadr3=0, uint8_t btadr2=0, uint8_t btadr1=0, uint8_t btadr0=0);
63 
64  // BluetoothService implementation
65  virtual void ACLData(uint8_t* ACLData); // Used to pass acldata to the services
66  virtual void Run(); // Used to run part of the state maschine
67  virtual void Reset(); // Use this to reset the service
68  virtual void disconnect(); // Use this void to disconnect any of the controllers
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  int16_t getSensor(Sensor a);
83  double getAngle(Angle a);
84  double get9DOFValues(Sensor a);
85  bool getStatus(Status c);
86  String getStatusString();
87  String getTemperature();
88 
89  /* HID Commands */
90  /* Commands for Dualshock 3 and Navigation controller */
91  void setAllOff();
92  void setRumbleOff();
93  void setRumbleOn(Rumble mode);
94  void setLedOff(LED a);
95  void setLedOn(LED a);
96  void setLedToggle(LED a);
97 
98  /* Commands for Motion controller only */
99  void moveSetBulb(uint8_t r, uint8_t g, uint8_t b); // Use this to set the Color using RGB values
100  void moveSetBulb(Colors color); // Use this to set the Color using the predefined colors in "enum Colors"
101  void moveSetRumble(uint8_t rumble);
102 
103  bool PS3Connected; // Variable used to indicate if the normal playstation controller is successfully connected
104  bool PS3MoveConnected; // Variable used to indicate if the move controller is successfully connected
105  bool PS3NavigationConnected; // Variable used to indicate if the navigation controller is successfully connected
106 
107 private:
108  /* mandatory members */
109  BTD *pBtd;
110 
111  void L2CAP_task(); // L2CAP state machine
112 
113  /* Variables filled from HCI event management */
114  int16_t hci_handle;
115  uint8_t remote_name[30]; // First 30 chars of remote name
116  bool activeConnection; // Used to indicate if it's already has established a connection
117 
118  /* variables used by high level L2CAP task */
119  uint8_t l2cap_state;
120  uint16_t l2cap_event_flag; // L2CAP flags of received bluetooth events
121 
122  unsigned long timer;
123 
124  uint32_t ButtonState;
125  uint32_t OldButtonState;
126  uint32_t ButtonClickState;
127 
128  uint32_t timerHID; // Timer used see if there has to be a delay before a new HID command
129  uint32_t timerBulbRumble;// used to continuously set PS3 Move controller Bulb and rumble values
130 
131  uint8_t l2capinbuf[BULK_MAXPKTSIZE]; // General purpose buffer for L2CAP in data
132  uint8_t HIDBuffer[HID_BUFFERSIZE]; // Used to store HID commands
133  uint8_t HIDMoveBuffer[HID_BUFFERSIZE]; // Used to store HID commands for the Move controller
134 
135  /* L2CAP Channels */
136  uint8_t control_scid[2]; // L2CAP source CID for HID_Control
137  uint8_t control_dcid[2]; // 0x0040
138  uint8_t interrupt_scid[2]; // L2CAP source CID for HID_Interrupt
139  uint8_t interrupt_dcid[2]; // 0x0041
140  uint8_t identifier; // Identifier for connection
141 
142  /* HID Commands */
143  void HID_Command(uint8_t* data, uint8_t nbytes);
144  void HIDMove_Command(uint8_t* data, uint8_t nbytes);
145  void enable_sixaxis(); // Command used to enable the Dualshock 3 and Navigation controller to send data via Bluetooth
146 };
147 #endif