USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
PS3BT.cpp
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 #include "PS3BT.h"
19 // To enable serial debugging see "settings.h"
20 //#define EXTRADEBUG // Uncomment to get even more debugging data
21 //#define PRINTREPORT // Uncomment to print the report send by the PS3 Controllers
22 
23 PS3BT::PS3BT(BTD *p, uint8_t btadr5, uint8_t btadr4, uint8_t btadr3, uint8_t btadr2, uint8_t btadr1, uint8_t btadr0) :
24 pBtd(p) // pointer to USB class instance - mandatory
25 {
26  if(pBtd)
27  pBtd->registerServiceClass(this); // Register it as a Bluetooth service
28 
29  pBtd->my_bdaddr[5] = btadr5; // Change to your dongle's Bluetooth address instead
30  pBtd->my_bdaddr[4] = btadr4;
31  pBtd->my_bdaddr[3] = btadr3;
32  pBtd->my_bdaddr[2] = btadr2;
33  pBtd->my_bdaddr[1] = btadr1;
34  pBtd->my_bdaddr[0] = btadr0;
35 
36  HIDBuffer[0] = 0x52; // HID BT Set_report (0x50) | Report Type (Output 0x02)
37  HIDBuffer[1] = 0x01; // Report ID
38 
39  // Needed for PS3 Move Controller commands to work via bluetooth
40  HIDMoveBuffer[0] = 0xA2; // HID BT DATA_request (0xA0) | Report Type (Output 0x02)
41  HIDMoveBuffer[1] = 0x02; // Report ID
42 
43  /* Set device cid for the control and intterrupt channelse - LSB */
44  control_dcid[0] = 0x40; // 0x0040
45  control_dcid[1] = 0x00;
46  interrupt_dcid[0] = 0x41; // 0x0041
47  interrupt_dcid[1] = 0x00;
48 
49  Reset();
50 }
51 
53  return (ButtonState & pgm_read_dword(&PS3_BUTTONS[(uint8_t)b]));
54 }
55 
57  uint32_t button = pgm_read_dword(&PS3_BUTTONS[(uint8_t)b]);
58  bool click = (ButtonClickState & button);
59  ButtonClickState &= ~button; // Clear "click" event
60  return click;
61 }
62 
64  return (uint8_t)(l2capinbuf[pgm_read_byte(&PS3_ANALOG_BUTTONS[(uint8_t)a])]);
65 }
66 
68  return (uint8_t)(l2capinbuf[(uint8_t)a + 15]);
69 }
70 
72  if(PS3Connected) {
73  if(a == aX || a == aY || a == aZ || a == gZ)
74  return ((l2capinbuf[(uint16_t)a] << 8) | l2capinbuf[(uint16_t)a + 1]);
75  else
76  return 0;
77  } else if(PS3MoveConnected) {
78  if(a == mXmove || a == mYmove) // These are all 12-bits long
79  return (((l2capinbuf[(uint16_t)a] & 0x0F) << 8) | (l2capinbuf[(uint16_t)a + 1]));
80  else if(a == mZmove || a == tempMove) // The tempearature is also 12 bits long
81  return ((l2capinbuf[(uint16_t)a] << 4) | ((l2capinbuf[(uint16_t)a + 1] & 0xF0) >> 4));
82  else // aXmove, aYmove, aZmove, gXmove, gYmove and gZmove
83  return (l2capinbuf[(uint16_t)a] | (l2capinbuf[(uint16_t)a + 1] << 8));
84  } else
85  return 0;
86 }
87 
89  double accXval, accYval, accZval;
90 
91  if(PS3Connected) {
92  // Data for the Kionix KXPC4 used in the DualShock 3
93  const double zeroG = 511.5; // 1.65/3.3*1023 (1.65V)
94  accXval = -((double)getSensor(aX) - zeroG);
95  accYval = -((double)getSensor(aY) - zeroG);
96  accZval = -((double)getSensor(aZ) - zeroG);
97  } else if(PS3MoveConnected) {
98  // It's a Kionix KXSC4 inside the Motion controller
99  const uint16_t zeroG = 0x8000;
100  accXval = -(int16_t)(getSensor(aXmove) - zeroG);
101  accYval = (int16_t)(getSensor(aYmove) - zeroG);
102  accZval = (int16_t)(getSensor(aZmove) - zeroG);
103  } else
104  return 0;
105 
106  // Convert to 360 degrees resolution
107  // atan2 outputs the value of -π to π (radians)
108  // We are then converting it to 0 to 2π and then to degrees
109  if(a == Pitch)
110  return (atan2(accYval, accZval) + PI) * RAD_TO_DEG;
111  else
112  return (atan2(accXval, accZval) + PI) * RAD_TO_DEG;
113 }
114 
115 double PS3BT::get9DOFValues(SensorEnum a) { // Thanks to Manfred Piendl
116  if(!PS3MoveConnected)
117  return 0;
118  int16_t value = getSensor(a);
119  if(a == mXmove || a == mYmove || a == mZmove) {
120  if(value > 2047)
121  value -= 0x1000;
122  return (double)value / 3.2; // unit: muT = 10^(-6) Tesla
123  } else if(a == aXmove || a == aYmove || a == aZmove) {
124  if(value < 0)
125  value += 0x8000;
126  else
127  value -= 0x8000;
128  return (double)value / 442.0; // unit: m/(s^2)
129  } else if(a == gXmove || a == gYmove || a == gZmove) {
130  if(value < 0)
131  value += 0x8000;
132  else
133  value -= 0x8000;
134  if(a == gXmove)
135  return (double)value / 11.6; // unit: deg/s
136  else if(a == gYmove)
137  return (double)value / 11.2; // unit: deg/s
138  else // gZmove
139  return (double)value / 9.6; // unit: deg/s
140  } else
141  return 0;
142 }
143 
145  if(PS3MoveConnected) {
146  int16_t input = getSensor(tempMove);
147 
148  String output = String(input / 100);
149  output += ".";
150  if(input % 100 < 10)
151  output += "0";
152  output += String(input % 100);
153 
154  return output;
155  } else
156  return "Error";
157 }
158 
160  return (l2capinbuf[(uint16_t)c >> 8] == ((uint8_t)c & 0xff));
161 }
162 
164  char statusOutput[100]; // Max string length plus null character
166  strcpy_P(statusOutput, PSTR("ConnectionStatus: "));
167 
168  if(getStatus(Plugged)) strcat_P(statusOutput, PSTR("Plugged"));
169  else if(getStatus(Unplugged)) strcat_P(statusOutput, PSTR("Unplugged"));
170  else strcat_P(statusOutput, PSTR("Error"));
171 
172  strcat_P(statusOutput, PSTR(" - PowerRating: "));
173 
174  if(getStatus(Charging)) strcat_P(statusOutput, PSTR("Charging"));
175  else if(getStatus(NotCharging)) strcat_P(statusOutput, PSTR("Not Charging"));
176  else if(getStatus(Shutdown)) strcat_P(statusOutput, PSTR("Shutdown"));
177  else if(getStatus(Dying)) strcat_P(statusOutput, PSTR("Dying"));
178  else if(getStatus(Low)) strcat_P(statusOutput, PSTR("Low"));
179  else if(getStatus(High)) strcat_P(statusOutput, PSTR("High"));
180  else if(getStatus(Full)) strcat_P(statusOutput, PSTR("Full"));
181  else strcat_P(statusOutput, PSTR("Error"));
182 
183  strcat_P(statusOutput, PSTR(" - WirelessStatus: "));
184 
185  if(getStatus(CableRumble)) strcat_P(statusOutput, PSTR("Cable - Rumble is on"));
186  else if(getStatus(Cable)) strcat_P(statusOutput, PSTR("Cable - Rumble is off"));
187  else if(getStatus(BluetoothRumble)) strcat_P(statusOutput, PSTR("Bluetooth - Rumble is on"));
188  else if(getStatus(Bluetooth)) strcat_P(statusOutput, PSTR("Bluetooth - Rumble is off"));
189  else strcat_P(statusOutput, PSTR("Error"));
190  } else if(PS3MoveConnected) {
191  strcpy_P(statusOutput, PSTR("PowerRating: "));
192 
193  if(getStatus(MoveCharging)) strcat_P(statusOutput, PSTR("Charging"));
194  else if(getStatus(MoveNotCharging)) strcat_P(statusOutput, PSTR("Not Charging"));
195  else if(getStatus(MoveShutdown)) strcat_P(statusOutput, PSTR("Shutdown"));
196  else if(getStatus(MoveDying)) strcat_P(statusOutput, PSTR("Dying"));
197  else if(getStatus(MoveLow)) strcat_P(statusOutput, PSTR("Low"));
198  else if(getStatus(MoveHigh)) strcat_P(statusOutput, PSTR("High"));
199  else if(getStatus(MoveFull)) strcat_P(statusOutput, PSTR("Full"));
200  else strcat_P(statusOutput, PSTR("Error"));
201  } else
202  strcpy_P(statusOutput, PSTR("Error"));
203 
204  USB_HOST_SERIAL.write(statusOutput);
205 }
206 
207 void PS3BT::Reset() {
208  PS3Connected = false;
209  PS3MoveConnected = false;
210  PS3NavigationConnected = false;
211  activeConnection = false;
212  l2cap_event_flag = 0; // Reset flags
213  l2cap_state = L2CAP_WAIT;
214 
215  // Needed for PS3 Dualshock Controller commands to work via Bluetooth
216  for(uint8_t i = 0; i < PS3_REPORT_BUFFER_SIZE; i++)
217  HIDBuffer[i + 2] = pgm_read_byte(&PS3_REPORT_BUFFER[i]); // First two bytes reserved for report type and ID
218 }
219 
220 void PS3BT::disconnect() { // Use this void to disconnect any of the controllers
221  // First the HID interrupt channel has to be disconnected, then the HID control channel and finally the HCI connection
222  pBtd->l2cap_disconnection_request(hci_handle, ++identifier, interrupt_scid, interrupt_dcid);
223  Reset();
224  l2cap_state = L2CAP_INTERRUPT_DISCONNECT;
225 }
226 
227 void PS3BT::ACLData(uint8_t* ACLData) {
228  if(!pBtd->l2capConnectionClaimed && !PS3Connected && !PS3MoveConnected && !PS3NavigationConnected && !activeConnection && !pBtd->connectToWii && !pBtd->incomingWii && !pBtd->pairWithWii) {
229  if(ACLData[8] == L2CAP_CMD_CONNECTION_REQUEST) {
230  if((ACLData[12] | (ACLData[13] << 8)) == HID_CTRL_PSM) {
231  pBtd->l2capConnectionClaimed = true; // Claim that the incoming connection belongs to this service
232  activeConnection = true;
233  hci_handle = pBtd->hci_handle; // Store the HCI Handle for the connection
234  l2cap_state = L2CAP_WAIT;
235  for(uint8_t i = 0; i < 30; i++)
236  remote_name[i] = pBtd->remote_name[i]; // Store the remote name for the connection
237 #ifdef DEBUG_USB_HOST
238  if(pBtd->hci_version < 3) { // Check the HCI Version of the Bluetooth dongle
239  Notify(PSTR("\r\nYour dongle may not support reading the analog buttons, sensors and status\r\nYour HCI Version is: "), 0x80);
240  Notify(pBtd->hci_version, 0x80);
241  Notify(PSTR("\r\nBut should be at least 3\r\nThis means that it doesn't support Bluetooth Version 2.0+EDR"), 0x80);
242  }
243 #endif
244  }
245  }
246  }
247  //if((ACLData[0] | (uint16_t)ACLData[1] << 8) == (hci_handle | 0x2000U)) { //acl_handle_ok
248  if(UHS_ACL_HANDLE_OK(ACLData, hci_handle)) { //acl_handle_ok
249  memcpy(l2capinbuf, ACLData, BULK_MAXPKTSIZE);
250  if((l2capinbuf[6] | (l2capinbuf[7] << 8)) == 0x0001U) { //l2cap_control - Channel ID for ACL-U
251  if(l2capinbuf[8] == L2CAP_CMD_COMMAND_REJECT) {
252 #ifdef DEBUG_USB_HOST
253  Notify(PSTR("\r\nL2CAP Command Rejected - Reason: "), 0x80);
254  D_PrintHex<uint8_t > (l2capinbuf[13], 0x80);
255  Notify(PSTR(" "), 0x80);
256  D_PrintHex<uint8_t > (l2capinbuf[12], 0x80);
257  Notify(PSTR(" Data: "), 0x80);
258  D_PrintHex<uint8_t > (l2capinbuf[17], 0x80);
259  Notify(PSTR(" "), 0x80);
260  D_PrintHex<uint8_t > (l2capinbuf[16], 0x80);
261  Notify(PSTR(" "), 0x80);
262  D_PrintHex<uint8_t > (l2capinbuf[15], 0x80);
263  Notify(PSTR(" "), 0x80);
264  D_PrintHex<uint8_t > (l2capinbuf[14], 0x80);
265 #endif
266  } else if(l2capinbuf[8] == L2CAP_CMD_CONNECTION_REQUEST) {
267 #ifdef EXTRADEBUG
268  Notify(PSTR("\r\nL2CAP Connection Request - PSM: "), 0x80);
269  D_PrintHex<uint8_t > (l2capinbuf[13], 0x80);
270  Notify(PSTR(" "), 0x80);
271  D_PrintHex<uint8_t > (l2capinbuf[12], 0x80);
272  Notify(PSTR(" SCID: "), 0x80);
273  D_PrintHex<uint8_t > (l2capinbuf[15], 0x80);
274  Notify(PSTR(" "), 0x80);
275  D_PrintHex<uint8_t > (l2capinbuf[14], 0x80);
276  Notify(PSTR(" Identifier: "), 0x80);
277  D_PrintHex<uint8_t > (l2capinbuf[9], 0x80);
278 #endif
279  if((l2capinbuf[12] | (l2capinbuf[13] << 8)) == HID_CTRL_PSM) {
280  identifier = l2capinbuf[9];
281  control_scid[0] = l2capinbuf[14];
282  control_scid[1] = l2capinbuf[15];
284  } else if((l2capinbuf[12] | (l2capinbuf[13] << 8)) == HID_INTR_PSM) {
285  identifier = l2capinbuf[9];
286  interrupt_scid[0] = l2capinbuf[14];
287  interrupt_scid[1] = l2capinbuf[15];
289  }
290  } else if(l2capinbuf[8] == L2CAP_CMD_CONFIG_RESPONSE) {
291  if((l2capinbuf[16] | (l2capinbuf[17] << 8)) == 0x0000) { // Success
292  if(l2capinbuf[12] == control_dcid[0] && l2capinbuf[13] == control_dcid[1]) {
293  //Notify(PSTR("\r\nHID Control Configuration Complete"), 0x80);
295  } else if(l2capinbuf[12] == interrupt_dcid[0] && l2capinbuf[13] == interrupt_dcid[1]) {
296  //Notify(PSTR("\r\nHID Interrupt Configuration Complete"), 0x80);
298  }
299  }
300  } else if(l2capinbuf[8] == L2CAP_CMD_CONFIG_REQUEST) {
301  if(l2capinbuf[12] == control_dcid[0] && l2capinbuf[13] == control_dcid[1]) {
302  //Notify(PSTR("\r\nHID Control Configuration Request"), 0x80);
303  pBtd->l2cap_config_response(hci_handle, l2capinbuf[9], control_scid);
304  } else if(l2capinbuf[12] == interrupt_dcid[0] && l2capinbuf[13] == interrupt_dcid[1]) {
305  //Notify(PSTR("\r\nHID Interrupt Configuration Request"), 0x80);
306  pBtd->l2cap_config_response(hci_handle, l2capinbuf[9], interrupt_scid);
307  }
308  } else if(l2capinbuf[8] == L2CAP_CMD_DISCONNECT_REQUEST) {
309  if(l2capinbuf[12] == control_dcid[0] && l2capinbuf[13] == control_dcid[1]) {
310 #ifdef DEBUG_USB_HOST
311  Notify(PSTR("\r\nDisconnect Request: Control Channel"), 0x80);
312 #endif
313  identifier = l2capinbuf[9];
314  pBtd->l2cap_disconnection_response(hci_handle, identifier, control_dcid, control_scid);
315  Reset();
316  } else if(l2capinbuf[12] == interrupt_dcid[0] && l2capinbuf[13] == interrupt_dcid[1]) {
317 #ifdef DEBUG_USB_HOST
318  Notify(PSTR("\r\nDisconnect Request: Interrupt Channel"), 0x80);
319 #endif
320  identifier = l2capinbuf[9];
321  pBtd->l2cap_disconnection_response(hci_handle, identifier, interrupt_dcid, interrupt_scid);
322  Reset();
323  }
324  } else if(l2capinbuf[8] == L2CAP_CMD_DISCONNECT_RESPONSE) {
325  if(l2capinbuf[12] == control_scid[0] && l2capinbuf[13] == control_scid[1]) {
326  //Notify(PSTR("\r\nDisconnect Response: Control Channel"), 0x80);
327  identifier = l2capinbuf[9];
329  } else if(l2capinbuf[12] == interrupt_scid[0] && l2capinbuf[13] == interrupt_scid[1]) {
330  //Notify(PSTR("\r\nDisconnect Response: Interrupt Channel"), 0x80);
331  identifier = l2capinbuf[9];
333  }
334  }
335 #ifdef EXTRADEBUG
336  else {
337  Notify(PSTR("\r\nL2CAP Unknown Signaling Command: "), 0x80);
338  D_PrintHex<uint8_t > (l2capinbuf[8], 0x80);
339  }
340 #endif
341  } else if(l2capinbuf[6] == interrupt_dcid[0] && l2capinbuf[7] == interrupt_dcid[1]) { // l2cap_interrupt
342  //Notify(PSTR("\r\nL2CAP Interrupt"), 0x80);
344  /* Read Report */
345  if(l2capinbuf[8] == 0xA1) { // HID_THDR_DATA_INPUT
346  lastMessageTime = millis(); // Store the last message time
347 
349  ButtonState = (uint32_t)(l2capinbuf[11] | ((uint16_t)l2capinbuf[12] << 8) | ((uint32_t)l2capinbuf[13] << 16));
350  else if(PS3MoveConnected)
351  ButtonState = (uint32_t)(l2capinbuf[10] | ((uint16_t)l2capinbuf[11] << 8) | ((uint32_t)l2capinbuf[12] << 16));
352 
353  //Notify(PSTR("\r\nButtonState", 0x80);
354  //PrintHex<uint32_t>(ButtonState, 0x80);
355 
356  if(ButtonState != OldButtonState) {
357  ButtonClickState = ButtonState & ~OldButtonState; // Update click state variable
358  OldButtonState = ButtonState;
359  }
360 
361 #ifdef PRINTREPORT // Uncomment "#define PRINTREPORT" to print the report send by the PS3 Controllers
362  for(uint8_t i = 10; i < 58; i++) {
363  D_PrintHex<uint8_t > (l2capinbuf[i], 0x80);
364  Notify(PSTR(" "), 0x80);
365  }
366  Notify(PSTR("\r\n"), 0x80);
367 #endif
368  }
369  }
370  }
371  L2CAP_task();
372  }
373 }
374 
375 void PS3BT::L2CAP_task() {
376  switch(l2cap_state) {
377  case L2CAP_WAIT:
379 #ifdef DEBUG_USB_HOST
380  Notify(PSTR("\r\nHID Control Incoming Connection Request"), 0x80);
381 #endif
382  pBtd->l2cap_connection_response(hci_handle, identifier, control_dcid, control_scid, PENDING);
383  delay(1);
384  pBtd->l2cap_connection_response(hci_handle, identifier, control_dcid, control_scid, SUCCESSFUL);
385  identifier++;
386  delay(1);
387  pBtd->l2cap_config_request(hci_handle, identifier, control_scid);
388  l2cap_state = L2CAP_CONTROL_SUCCESS;
389  }
390  break;
391 
394 #ifdef DEBUG_USB_HOST
395  Notify(PSTR("\r\nHID Control Successfully Configured"), 0x80);
396 #endif
397  l2cap_state = L2CAP_INTERRUPT_SETUP;
398  }
399  break;
400 
403 #ifdef DEBUG_USB_HOST
404  Notify(PSTR("\r\nHID Interrupt Incoming Connection Request"), 0x80);
405 #endif
406  pBtd->l2cap_connection_response(hci_handle, identifier, interrupt_dcid, interrupt_scid, PENDING);
407  delay(1);
408  pBtd->l2cap_connection_response(hci_handle, identifier, interrupt_dcid, interrupt_scid, SUCCESSFUL);
409  identifier++;
410  delay(1);
411  pBtd->l2cap_config_request(hci_handle, identifier, interrupt_scid);
412 
413  l2cap_state = L2CAP_INTERRUPT_CONFIG_REQUEST;
414  }
415  break;
416 
418  if(l2cap_check_flag(L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS)) { // Now the HID channels is established
419 #ifdef DEBUG_USB_HOST
420  Notify(PSTR("\r\nHID Interrupt Successfully Configured"), 0x80);
421 #endif
422  if(remote_name[0] == 'M') { // First letter in Motion Controller ('M')
423  memset(l2capinbuf, 0, BULK_MAXPKTSIZE); // Reset l2cap in buffer as it sometimes read it as a button has been pressed
424  l2cap_state = TURN_ON_LED;
425  } else
426  l2cap_state = PS3_ENABLE_SIXAXIS;
427  timer = millis();
428  }
429  break;
430 
431  /* These states are handled in Run() */
432 
435 #ifdef DEBUG_USB_HOST
436  Notify(PSTR("\r\nDisconnected Interrupt Channel"), 0x80);
437 #endif
438  identifier++;
439  pBtd->l2cap_disconnection_request(hci_handle, identifier, control_scid, control_dcid);
440  l2cap_state = L2CAP_CONTROL_DISCONNECT;
441  }
442  break;
443 
446 #ifdef DEBUG_USB_HOST
447  Notify(PSTR("\r\nDisconnected Control Channel"), 0x80);
448 #endif
449  pBtd->hci_disconnect(hci_handle);
450  hci_handle = -1; // Reset handle
451  l2cap_event_flag = 0; // Reset flags
452  l2cap_state = L2CAP_WAIT;
453  }
454  break;
455  }
456 }
457 
458 void PS3BT::Run() {
459  switch(l2cap_state) {
460  case PS3_ENABLE_SIXAXIS:
461  if(millis() - timer > 1000) { // loop 1 second before sending the command
462  memset(l2capinbuf, 0, BULK_MAXPKTSIZE); // Reset l2cap in buffer as it sometimes read it as a button has been pressed
463  for(uint8_t i = 15; i < 19; i++)
464  l2capinbuf[i] = 0x7F; // Set the analog joystick values to center position
465  enable_sixaxis();
466  l2cap_state = TURN_ON_LED;
467  timer = millis();
468  }
469  break;
470 
471  case TURN_ON_LED:
472  if(millis() - timer > 1000) { // loop 1 second before sending the command
473  if(remote_name[0] == 'P') { // First letter in PLAYSTATION(R)3 Controller ('P')
474 #ifdef DEBUG_USB_HOST
475  Notify(PSTR("\r\nDualshock 3 Controller Enabled\r\n"), 0x80);
476 #endif
477  PS3Connected = true;
478  } else if(remote_name[0] == 'N') { // First letter in Navigation Controller ('N')
479 #ifdef DEBUG_USB_HOST
480  Notify(PSTR("\r\nNavigation Controller Enabled\r\n"), 0x80);
481 #endif
482  PS3NavigationConnected = true;
483  } else if(remote_name[0] == 'M') { // First letter in Motion Controller ('M')
484  timerBulbRumble = millis();
485 #ifdef DEBUG_USB_HOST
486  Notify(PSTR("\r\nMotion Controller Enabled\r\n"), 0x80);
487 #endif
488  PS3MoveConnected = true;
489  }
490  ButtonState = 0; // Clear all values
491  OldButtonState = 0;
492  ButtonClickState = 0;
493 
494  onInit(); // Turn on the LED on the controller
495  l2cap_state = L2CAP_DONE;
496  }
497  break;
498 
499  case L2CAP_DONE:
500  if(PS3MoveConnected) { // The Bulb and rumble values, has to be send at aproximatly every 5th second for it to stay on
501  if(millis() - timerBulbRumble > 4000) { // Send at least every 4th second
502  HIDMove_Command(HIDMoveBuffer, HID_BUFFERSIZE); // The Bulb and rumble values, has to be written again and again, for it to stay turned on
503  timerBulbRumble = millis();
504  }
505  }
506  break;
507  }
508 }
509 
510 /************************************************************/
511 /* HID Commands */
512 /************************************************************/
513 
514 // Playstation Sixaxis Dualshock and Navigation Controller commands
515 
516 void PS3BT::HID_Command(uint8_t* data, uint8_t nbytes) {
517  if(millis() - timerHID <= 150) // Check if is has been more than 150ms since last command
518  delay((uint32_t)(150 - (millis() - timerHID))); // There have to be a delay between commands
519  pBtd->L2CAP_Command(hci_handle, data, nbytes, control_scid[0], control_scid[1]); // Both the Navigation and Dualshock controller sends data via the control channel
520  timerHID = millis();
521 }
522 
524  HIDBuffer[3] = 0x00; // Rumble bytes
525  HIDBuffer[4] = 0x00;
526  HIDBuffer[5] = 0x00;
527  HIDBuffer[6] = 0x00;
528 
529  HIDBuffer[11] = 0x00; // LED byte
530 
531  HID_Command(HIDBuffer, HID_BUFFERSIZE);
532 }
533 
535  HIDBuffer[3] = 0x00;
536  HIDBuffer[4] = 0x00;
537  HIDBuffer[5] = 0x00;
538  HIDBuffer[6] = 0x00;
539 
540  HID_Command(HIDBuffer, HID_BUFFERSIZE);
541 }
542 
544  uint8_t power[2] = {0xff, 0x00}; // Defaults to RumbleLow
545  if(mode == RumbleHigh) {
546  power[0] = 0x00;
547  power[1] = 0xff;
548  }
549  setRumbleOn(0xfe, power[0], 0xfe, power[1]);
550 }
551 
552 void PS3BT::setRumbleOn(uint8_t rightDuration, uint8_t rightPower, uint8_t leftDuration, uint8_t leftPower) {
553  HIDBuffer[3] = rightDuration;
554  HIDBuffer[4] = rightPower;
555  HIDBuffer[5] = leftDuration;
556  HIDBuffer[6] = leftPower;
557  HID_Command(HIDBuffer, HID_BUFFERSIZE);
558 }
559 
560 void PS3BT::setLedRaw(uint8_t value) {
561  HIDBuffer[11] = value << 1;
562  HID_Command(HIDBuffer, HID_BUFFERSIZE);
563 }
564 
566  HIDBuffer[11] &= ~((uint8_t)((pgm_read_byte(&PS3_LEDS[(uint8_t)a]) & 0x0f) << 1));
567  HID_Command(HIDBuffer, HID_BUFFERSIZE);
568 }
569 
571  if(a == OFF)
572  setLedRaw(0);
573  else {
574  HIDBuffer[11] |= (uint8_t)((pgm_read_byte(&PS3_LEDS[(uint8_t)a]) & 0x0f) << 1);
575  HID_Command(HIDBuffer, HID_BUFFERSIZE);
576  }
577 }
578 
580  HIDBuffer[11] ^= (uint8_t)((pgm_read_byte(&PS3_LEDS[(uint8_t)a]) & 0x0f) << 1);
581  HID_Command(HIDBuffer, HID_BUFFERSIZE);
582 }
583 
584 void PS3BT::enable_sixaxis() { // Command used to enable the Dualshock 3 and Navigation controller to send data via Bluetooth
585  uint8_t cmd_buf[6];
586  cmd_buf[0] = 0x53; // HID BT Set_report (0x50) | Report Type (Feature 0x03)
587  cmd_buf[1] = 0xF4; // Report ID
588  cmd_buf[2] = 0x42; // Special PS3 Controller enable commands
589  cmd_buf[3] = 0x03;
590  cmd_buf[4] = 0x00;
591  cmd_buf[5] = 0x00;
592 
593  HID_Command(cmd_buf, 6);
594 }
595 
596 // Playstation Move Controller commands
597 
598 void PS3BT::HIDMove_Command(uint8_t* data, uint8_t nbytes) {
599  if(millis() - timerHID <= 150)// Check if is has been less than 150ms since last command
600  delay((uint32_t)(150 - (millis() - timerHID))); // There have to be a delay between commands
601  pBtd->L2CAP_Command(hci_handle, data, nbytes, interrupt_scid[0], interrupt_scid[1]); // The Move controller sends it's data via the intterrupt channel
602  timerHID = millis();
603 }
604 
605 void PS3BT::moveSetBulb(uint8_t r, uint8_t g, uint8_t b) { // Use this to set the Color using RGB values
606  // Set the Bulb's values into the write buffer
607  HIDMoveBuffer[3] = r;
608  HIDMoveBuffer[4] = g;
609  HIDMoveBuffer[5] = b;
610 
611  HIDMove_Command(HIDMoveBuffer, HID_BUFFERSIZE);
612 }
613 
614 void PS3BT::moveSetBulb(ColorsEnum color) { // Use this to set the Color using the predefined colors in enum
615  moveSetBulb((uint8_t)(color >> 16), (uint8_t)(color >> 8), (uint8_t)(color));
616 }
617 
618 void PS3BT::moveSetRumble(uint8_t rumble) {
619 #ifdef DEBUG_USB_HOST
620  if(rumble < 64 && rumble != 0) // The rumble value has to at least 64, or approximately 25% (64/255*100)
621  Notify(PSTR("\r\nThe rumble value has to at least 64, or approximately 25%"), 0x80);
622 #endif
623  // Set the rumble value into the write buffer
624  HIDMoveBuffer[7] = rumble;
625 
626  HIDMove_Command(HIDMoveBuffer, HID_BUFFERSIZE);
627 }
628 
629 void PS3BT::onInit() {
630  if(pFuncOnInit)
631  pFuncOnInit(); // Call the user function
632  else {
633  if(PS3MoveConnected)
634  moveSetBulb(Red);
635  else // Dualshock 3 or Navigation controller
636  setLedOn(LED1);
637  }
638 }
#define L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE
Definition: BTD.h:139
bool incomingWii
Definition: BTD.h:507
#define UHS_ACL_HANDLE_OK(x, y)
Definition: BTD.h:207
Definition: PS3Enums.h:124
bool PS3NavigationConnected
Definition: PS3BT.h:201
#define L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS
Definition: BTD.h:143
#define L2CAP_INTERRUPT_CONFIG_REQUEST
Definition: BTD.h:115
#define L2CAP_INTERRUPT_SETUP
Definition: BTD.h:113
bool getStatus(StatusEnum c)
Definition: PS3BT.cpp:159
void l2cap_connection_response(uint16_t handle, uint8_t rxid, uint8_t *dcid, uint8_t *scid, uint8_t result)
Definition: BTD.cpp:1231
virtual void Run()
Definition: PS3BT.cpp:458
#define SUCCESSFUL
Definition: BTD.h:177
#define L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST
Definition: BTD.h:142
Definition: BTD.h:230
void setLedOn(LEDEnum a)
Definition: PS3BT.cpp:570
void l2cap_disconnection_request(uint16_t handle, uint8_t rxid, uint8_t *dcid, uint8_t *scid)
Definition: BTD.cpp:1284
uint8_t hci_version
Definition: BTD.h:497
bool pairWithWii
Definition: BTD.h:509
String getTemperature()
Definition: PS3BT.cpp:144
AnalogHatEnum
void moveSetRumble(uint8_t rumble)
Definition: PS3BT.cpp:618
#define TURN_ON_LED
Definition: BTD.h:129
void printStatusString()
Definition: PS3BT.cpp:163
void setAllOff()
Definition: PS3BT.cpp:523
#define L2CAP_DONE
Definition: BTD.h:104
#define L2CAP_CONTROL_SUCCESS
Definition: BTD.h:109
#define L2CAP_WAIT
Definition: BTD.h:103
StatusEnum
Definition: PS3Enums.h:113
#define HID_BUFFERSIZE
Definition: PS3BT.h:24
virtual void Reset()
Definition: PS3BT.cpp:207
char remote_name[30]
Definition: BTD.h:491
const uint32_t PS3_BUTTONS[]
Definition: PS3Enums.h:62
bool getButtonPress(ButtonEnum b)
Definition: PS3BT.cpp:52
LEDEnum
int16_t getSensor(SensorEnum a)
Definition: PS3BT.cpp:71
void l2cap_disconnection_response(uint16_t handle, uint8_t rxid, uint8_t *dcid, uint8_t *scid)
Definition: BTD.cpp:1297
#define Notify(...)
Definition: message.h:44
double getAngle(AngleEnum a)
Definition: PS3BT.cpp:88
RumbleEnum
#define USB_HOST_SERIAL
Definition: settings.h:34
#define HID_CTRL_PSM
Definition: BTD.h:182
Definition: PS3Enums.h:123
bool connectToWii
Definition: BTD.h:503
uint8_t getAnalogHat(AnalogHatEnum a)
Definition: PS3BT.cpp:67
uint16_t hci_handle
Definition: BTD.h:487
bool PS3Connected
Definition: PS3BT.h:193
void hci_disconnect(uint16_t handle)
Definition: BTD.cpp:1142
#define L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE
Definition: BTD.h:145
ButtonEnum
void moveSetBulb(uint8_t r, uint8_t g, uint8_t b)
Definition: PS3BT.cpp:605
bool PS3MoveConnected
Definition: PS3BT.h:199
uint8_t my_bdaddr[6]
Definition: BTD.h:485
void setRumbleOn(RumbleEnum mode)
Definition: PS3BT.cpp:543
const uint8_t PS3_LEDS[]
Definition: PS3Enums.h:43
#define l2cap_check_flag(flag)
Definition: BTD.h:160
#define L2CAP_CMD_CONFIG_REQUEST
Definition: BTD.h:168
#define L2CAP_CMD_DISCONNECT_REQUEST
Definition: BTD.h:170
#define L2CAP_CONTROL_DISCONNECT
Definition: BTD.h:110
#define L2CAP_FLAG_CONNECTION_CONTROL_REQUEST
Definition: BTD.h:136
ColorsEnum
#define BULK_MAXPKTSIZE
Definition: BTD.h:33
#define HID_INTR_PSM
Definition: BTD.h:183
const uint8_t PS3_ANALOG_BUTTONS[]
Definition: PS3Enums.h:92
int8_t registerServiceClass(BluetoothService *pService)
Definition: BTD.h:333
bool l2capConnectionClaimed
Definition: BTD.h:473
AngleEnum
#define PS3_ENABLE_SIXAXIS
Definition: BTD.h:130
#define L2CAP_CMD_DISCONNECT_RESPONSE
Definition: BTD.h:171
virtual void ACLData(uint8_t *ACLData)
Definition: PS3BT.cpp:227
#define L2CAP_CMD_CONFIG_RESPONSE
Definition: BTD.h:169
void setLedOff()
Definition: PS3BT.h:147
void setLedToggle(LEDEnum a)
Definition: PS3BT.cpp:579
#define PS3_REPORT_BUFFER_SIZE
Definition: PS3Enums.h:24
virtual void disconnect()
Definition: PS3BT.cpp:220
void L2CAP_Command(uint16_t handle, uint8_t *data, uint8_t nbytes, uint8_t channelLow=0x01, uint8_t channelHigh=0x00)
Definition: BTD.cpp:1190
void l2cap_config_response(uint16_t handle, uint8_t rxid, uint8_t *scid)
Definition: BTD.cpp:1265
#define PENDING
Definition: BTD.h:176
#define l2cap_set_flag(flag)
Definition: BTD.h:161
void l2cap_config_request(uint16_t handle, uint8_t rxid, uint8_t *dcid)
Definition: BTD.cpp:1248
#define L2CAP_FLAG_CONFIG_CONTROL_SUCCESS
Definition: BTD.h:137
SensorEnum
#define L2CAP_CMD_CONNECTION_REQUEST
Definition: BTD.h:166
double get9DOFValues(SensorEnum a)
Definition: PS3BT.cpp:115
uint8_t getAnalogButton(ButtonEnum a)
Definition: PS3BT.cpp:63
void setLedRaw(uint8_t value)
Definition: PS3BT.cpp:560
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
Definition: PS3Enums.h:125
#define L2CAP_INTERRUPT_DISCONNECT
Definition: BTD.h:116
void setRumbleOff()
Definition: PS3BT.cpp:534
bool getButtonClick(ButtonEnum b)
Definition: PS3BT.cpp:56
const uint8_t PS3_REPORT_BUFFER[PS3_REPORT_BUFFER_SIZE]
Definition: PS3Enums.h:27
#define L2CAP_CMD_COMMAND_REJECT
Definition: BTD.h:165