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);
180  void setLedOff() {
181  setLedRaw(0);
182  }
187  void setLedOff(LED a);
192  void setLedOn(LED a);
197  void setLedToggle(LED a);
198 
203  void moveSetBulb(uint8_t r, uint8_t g, uint8_t b);
208  void moveSetBulb(Colors color);
213  void moveSetRumble(uint8_t rumble);
214 
219  void attachOnInit(void (*funcOnInit)(void)) {
220  pFuncOnInit = funcOnInit;
221  };
225  bool PS3Connected;
230 
231 private:
232  /* Mandatory members */
233  BTD *pBtd;
234 
240  void onInit();
241  void (*pFuncOnInit)(void); // Pointer to function called in onInit()
242 
243  void L2CAP_task(); // L2CAP state machine
244 
245  /* Variables filled from HCI event management */
246  int16_t hci_handle;
247  uint8_t remote_name[30]; // First 30 chars of remote name
248  bool activeConnection; // Used to indicate if it's already has established a connection
249 
250  /* variables used by high level L2CAP task */
251  uint8_t l2cap_state;
252  uint16_t l2cap_event_flag; // L2CAP flags of received Bluetooth events
253 
254  unsigned long timer;
255 
256  uint32_t ButtonState;
257  uint32_t OldButtonState;
258  uint32_t ButtonClickState;
259 
260  uint32_t timerHID; // Timer used see if there has to be a delay before a new HID command
261  uint32_t timerBulbRumble; // used to continuously set PS3 Move controller Bulb and rumble values
262 
263  uint8_t l2capinbuf[BULK_MAXPKTSIZE]; // General purpose buffer for L2CAP in data
264  uint8_t HIDBuffer[HID_BUFFERSIZE]; // Used to store HID commands
265  uint8_t HIDMoveBuffer[HID_BUFFERSIZE]; // Used to store HID commands for the Move controller
266 
267  /* L2CAP Channels */
268  uint8_t control_scid[2]; // L2CAP source CID for HID_Control
269  uint8_t control_dcid[2]; // 0x0040
270  uint8_t interrupt_scid[2]; // L2CAP source CID for HID_Interrupt
271  uint8_t interrupt_dcid[2]; // 0x0041
272  uint8_t identifier; // Identifier for connection
273 
274  /* HID Commands */
275  void HID_Command(uint8_t* data, uint8_t nbytes);
276  void HIDMove_Command(uint8_t* data, uint8_t nbytes);
277  void enable_sixaxis(); // Command used to enable the Dualshock 3 and Navigation controller to send data via Bluetooth
278 };
279 #endif
uint8_t getAnalogHat(AnalogHat a)
Definition: PS3BT.cpp:67
bool getButtonClick(Button b)
Definition: PS3BT.cpp:56
bool PS3NavigationConnected
Definition: PS3BT.h:229
String getStatusString()
Definition: PS3BT.cpp:163
virtual void Run()
Definition: PS3BT.cpp:479
Definition: BTD.h:158
Sensor
Definition: PS3Enums.h:141
String getTemperature()
Definition: PS3BT.cpp:144
void moveSetRumble(uint8_t rumble)
Definition: PS3BT.cpp:635
void setAllOff()
Definition: PS3BT.cpp:544
int16_t getSensor(Sensor a)
Definition: PS3BT.cpp:71
double get9DOFValues(Sensor a)
Definition: PS3BT.cpp:115
#define HID_BUFFERSIZE
Definition: PS3BT.h:24
virtual void Reset()
Definition: PS3BT.cpp:213
double getAngle(Angle a)
Definition: PS3BT.cpp:88
Definition: PS3BT.h:65
Status
Definition: PS3Enums.h:182
Colors
Definition: PS3Enums.h:115
void attachOnInit(void(*funcOnInit)(void))
Definition: PS3BT.h:219
Rumble
Definition: PS3Enums.h:210
bool PS3Connected
Definition: PS3BT.h:221
void setLedToggle(LED a)
Definition: PS3BT.cpp:596
void moveSetBulb(uint8_t r, uint8_t g, uint8_t b)
Definition: PS3BT.cpp:622
bool PS3MoveConnected
Definition: PS3BT.h:227
Angle
Definition: PS3Enums.h:177
void setLedOn(LED a)
Definition: PS3BT.cpp:591
#define BULK_MAXPKTSIZE
Definition: BTD.h:33
virtual void ACLData(uint8_t *ACLData)
Definition: PS3BT.cpp:233
void setLedOff()
Definition: PS3BT.h:180
Button
bool getStatus(Status c)
Definition: PS3BT.cpp:159
void setRumbleOn(Rumble mode)
Definition: PS3BT.cpp:564
virtual void disconnect()
Definition: PS3BT.cpp:226
uint8_t getAnalogButton(Button a)
Definition: PS3BT.cpp:63
void setLedRaw(uint8_t value)
Definition: PS3BT.cpp:581
AnalogHat
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)
Definition: PS3BT.cpp:23
bool getButtonPress(Button b)
Definition: PS3BT.cpp:52
void setRumbleOff()
Definition: PS3BT.cpp:555