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 
26 /* Bluetooth L2CAP states for L2CAP_task() */
27 #define L2CAP_WAIT 0
28 #define L2CAP_CONTROL_REQUEST 1
29 #define L2CAP_CONTROL_SUCCESS 2
30 #define L2CAP_INTERRUPT_SETUP 3
31 #define L2CAP_INTERRUPT_REQUEST 4
32 #define L2CAP_INTERRUPT_SUCCESS 5
33 #define L2CAP_HID_ENABLE_SIXAXIS 6
34 #define L2CAP_HID_PS3_LED 7
35 #define L2CAP_DONE 8
36 #define L2CAP_INTERRUPT_DISCONNECT 9
37 #define L2CAP_CONTROL_DISCONNECT 10
38 
39 /* L2CAP event flags */
40 #define L2CAP_FLAG_CONNECTION_CONTROL_REQUEST 0x01
41 #define L2CAP_FLAG_CONFIG_CONTROL_REQUEST 0x02
42 #define L2CAP_FLAG_CONFIG_CONTROL_SUCCESS 0x04
43 #define L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST 0x08
44 #define L2CAP_FLAG_CONFIG_INTERRUPT_REQUEST 0x10
45 #define L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS 0x20
46 #define L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE 0x40
47 #define L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE 0x80
48 
49 /*Macros for L2CAP event flag tests */
50 #define l2cap_connection_request_control_flag (l2cap_event_flag & L2CAP_FLAG_CONNECTION_CONTROL_REQUEST)
51 #define l2cap_config_request_control_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_CONTROL_REQUEST)
52 #define l2cap_config_success_control_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_CONTROL_SUCCESS)
53 #define l2cap_connection_request_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST)
54 #define l2cap_config_request_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_INTERRUPT_REQUEST)
55 #define l2cap_config_success_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS)
56 #define l2cap_disconnect_response_control_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE)
57 #define l2cap_disconnect_response_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE)
58 
65 class PS3BT : public BluetoothService {
66 public:
74  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);
75 
81  virtual void ACLData(uint8_t* ACLData);
83  virtual void Run();
85  virtual void Reset();
87  virtual void disconnect();
99  bool getButtonPress(Button b);
100  bool getButtonClick(Button b);
111  uint8_t getAnalogButton(Button a);
117  uint8_t getAnalogHat(AnalogHat a);
126  int16_t getSensor(Sensor a);
132  double getAngle(Angle a);
138  double get9DOFValues(Sensor a);
144  bool getStatus(Status c);
149  String getStatusString();
154  String getTemperature();
155 
157  void setAllOff();
159  void setRumbleOff();
164  void setRumbleOn(Rumble mode);
172  void setRumbleOn(uint8_t rightDuration, uint8_t rightPower, uint8_t leftDuration, uint8_t leftPower);
173 
178  void setLedRaw(uint8_t value);
183  void setLedOff(LED a);
188  void setLedOn(LED a);
193  void setLedToggle(LED a);
194 
199  void moveSetBulb(uint8_t r, uint8_t g, uint8_t b);
204  void moveSetBulb(Colors color);
209  void moveSetRumble(uint8_t rumble);
210 
215  void attachOnInit(void (*funcOnInit)(void)) {
216  pFuncOnInit = funcOnInit;
217  };
221  bool PS3Connected;
226 
227 private:
228  /* Mandatory members */
229  BTD *pBtd;
230 
236  void onInit();
237  void (*pFuncOnInit)(void); // Pointer to function called in onInit()
238 
239  void L2CAP_task(); // L2CAP state machine
240 
241  /* Variables filled from HCI event management */
242  int16_t hci_handle;
243  uint8_t remote_name[30]; // First 30 chars of remote name
244  bool activeConnection; // Used to indicate if it's already has established a connection
245 
246  /* variables used by high level L2CAP task */
247  uint8_t l2cap_state;
248  uint16_t l2cap_event_flag; // L2CAP flags of received Bluetooth events
249 
250  unsigned long timer;
251 
252  uint32_t ButtonState;
253  uint32_t OldButtonState;
254  uint32_t ButtonClickState;
255 
256  uint32_t timerHID; // Timer used see if there has to be a delay before a new HID command
257  uint32_t timerBulbRumble; // used to continuously set PS3 Move controller Bulb and rumble values
258 
259  uint8_t l2capinbuf[BULK_MAXPKTSIZE]; // General purpose buffer for L2CAP in data
260  uint8_t HIDBuffer[HID_BUFFERSIZE]; // Used to store HID commands
261  uint8_t HIDMoveBuffer[HID_BUFFERSIZE]; // Used to store HID commands for the Move controller
262 
263  /* L2CAP Channels */
264  uint8_t control_scid[2]; // L2CAP source CID for HID_Control
265  uint8_t control_dcid[2]; // 0x0040
266  uint8_t interrupt_scid[2]; // L2CAP source CID for HID_Interrupt
267  uint8_t interrupt_dcid[2]; // 0x0041
268  uint8_t identifier; // Identifier for connection
269 
270  /* HID Commands */
271  void HID_Command(uint8_t* data, uint8_t nbytes);
272  void HIDMove_Command(uint8_t* data, uint8_t nbytes);
273  void enable_sixaxis(); // Command used to enable the Dualshock 3 and Navigation controller to send data via Bluetooth
274 };
275 #endif