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 
66 class PS3BT : public BluetoothService {
67 public:
75  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);
76 
82  virtual void ACLData(uint8_t* ACLData);
84  virtual void Run();
86  virtual void Reset();
88  virtual void disconnect();
100  bool getButtonPress(Button b);
101  bool getButtonClick(Button b);
112  uint8_t getAnalogButton(Button a);
118  uint8_t getAnalogHat(AnalogHat a);
127  int16_t getSensor(Sensor a);
133  double getAngle(Angle a);
139  double get9DOFValues(Sensor a);
145  bool getStatus(Status c);
150  String getStatusString();
155  String getTemperature();
156 
158  void setAllOff();
160  void setRumbleOff();
165  void setRumbleOn(Rumble mode);
170  void setLedOff(LED a);
175  void setLedOn(LED a);
180  void setLedToggle(LED a);
181 
186  void moveSetBulb(uint8_t r, uint8_t g, uint8_t b);
191  void moveSetBulb(Colors color);
196  void moveSetRumble(uint8_t rumble);
205 
206 private:
207  /* mandatory members */
208  BTD *pBtd;
209 
210  void L2CAP_task(); // L2CAP state machine
211 
212  /* Variables filled from HCI event management */
213  int16_t hci_handle;
214  uint8_t remote_name[30]; // First 30 chars of remote name
215  bool activeConnection; // Used to indicate if it's already has established a connection
216 
217  /* variables used by high level L2CAP task */
218  uint8_t l2cap_state;
219  uint16_t l2cap_event_flag; // L2CAP flags of received bluetooth events
220 
221  unsigned long timer;
222 
223  uint32_t ButtonState;
224  uint32_t OldButtonState;
225  uint32_t ButtonClickState;
226 
227  uint32_t timerHID; // Timer used see if there has to be a delay before a new HID command
228  uint32_t timerBulbRumble;// used to continuously set PS3 Move controller Bulb and rumble values
229 
230  uint8_t l2capinbuf[BULK_MAXPKTSIZE]; // General purpose buffer for L2CAP in data
231  uint8_t HIDBuffer[HID_BUFFERSIZE]; // Used to store HID commands
232  uint8_t HIDMoveBuffer[HID_BUFFERSIZE]; // Used to store HID commands for the Move controller
233 
234  /* L2CAP Channels */
235  uint8_t control_scid[2]; // L2CAP source CID for HID_Control
236  uint8_t control_dcid[2]; // 0x0040
237  uint8_t interrupt_scid[2]; // L2CAP source CID for HID_Interrupt
238  uint8_t interrupt_dcid[2]; // 0x0041
239  uint8_t identifier; // Identifier for connection
240 
241  /* HID Commands */
242  void HID_Command(uint8_t* data, uint8_t nbytes);
243  void HIDMove_Command(uint8_t* data, uint8_t nbytes);
244  void enable_sixaxis(); // Command used to enable the Dualshock 3 and Navigation controller to send data via Bluetooth
245 };
246 #endif