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 uncomment "#define DEBUG_USB_HOST" in message.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(&BUTTONS[(uint8_t)b]));
54 }
55 
57  uint32_t button = pgm_read_dword(&BUTTONS[(uint8_t)b]);
58  bool click = (ButtonClickState & button);
59  ButtonClickState &= ~button; // clear "click" event
60  return click;
61 }
62 
64  if (l2capinbuf == NULL)
65  return 0;
66  return (uint8_t)(l2capinbuf[pgm_read_byte(&ANALOGBUTTONS[(uint8_t)a])]);
67 }
68 
70  if (l2capinbuf == NULL)
71  return 0;
72  return (uint8_t)(l2capinbuf[(uint8_t)a + 15]);
73 }
74 
76  if (l2capinbuf == NULL)
77  return 0;
78  if (PS3Connected) {
79  if (a == aX || a == aY || a == aZ || a == gZ)
80  return ((l2capinbuf[(uint16_t)a] << 8) | l2capinbuf[(uint16_t)a + 1]);
81  else
82  return 0;
83  } else if (PS3MoveConnected) {
84  if (a == mXmove || a == mYmove) // These are all 12-bits long
85  return (((l2capinbuf[(uint16_t)a] & 0x0F) << 8) | (l2capinbuf[(uint16_t)a + 1]));
86  else if (a == mZmove || a == tempMove) // The tempearature is also 12 bits long
87  return ((l2capinbuf[(uint16_t)a] << 4) | ((l2capinbuf[(uint16_t)a + 1] & 0xF0) >> 4));
88  else // aXmove, aYmove, aZmove, gXmove, gYmove and gZmove
89  return (l2capinbuf[(uint16_t)a] | (l2capinbuf[(uint16_t)a + 1] << 8));
90  } else
91  return 0;
92 }
93 
95  double accXval;
96  double accYval;
97  double accZval;
98 
99  if (PS3Connected) {
100  // Data for the Kionix KXPC4 used in the DualShock 3
101  const double zeroG = 511.5; // 1.65/3.3*1023 (1,65V)
102  accXval = -((double)getSensor(aX) - zeroG);
103  accYval = -((double)getSensor(aY) - zeroG);
104  accZval = -((double)getSensor(aZ) - zeroG);
105  } else if (PS3MoveConnected) {
106  // It's a Kionix KXSC4 inside the Motion controller
107  const uint16_t zeroG = 0x8000;
108  accXval = -(int16_t)(getSensor(aXmove) - zeroG);
109  accYval = (int16_t)(getSensor(aYmove) - zeroG);
110  accZval = (int16_t)(getSensor(aZmove) - zeroG);
111  } else
112  return 0;
113 
114  // Convert to 360 degrees resolution
115  // atan2 outputs the value of -π to π (radians)
116  // We are then converting it to 0 to 2π and then to degrees
117  if (a == Pitch) {
118  double angle = (atan2(accYval, accZval) + PI) * RAD_TO_DEG;
119  return angle;
120  } else {
121  double angle = (atan2(accXval, accZval) + PI) * RAD_TO_DEG;
122  return angle;
123  }
124 }
125 
126 double PS3BT::get9DOFValues(Sensor a) { // Thanks to Manfred Piendl
127  if (!PS3MoveConnected)
128  return 0;
129  int16_t value = getSensor(a);
130  if (a == mXmove || a == mYmove || a == mZmove) {
131  if (value > 2047)
132  value -= 0x1000;
133  return (double)value / 3.2; // unit: muT = 10^(-6) Tesla
134  } else if (a == aXmove || a == aYmove || a == aZmove) {
135  if (value < 0)
136  value += 0x8000;
137  else
138  value -= 0x8000;
139  return (double)value / 442.0; // unit: m/(s^2)
140  } else if (a == gXmove || a == gYmove || a == gZmove) {
141  if (value < 0)
142  value += 0x8000;
143  else
144  value -= 0x8000;
145  if (a == gXmove)
146  return (double)value / 11.6; // unit: deg/s
147  else if (a == gYmove)
148  return (double)value / 11.2; // unit: deg/s
149  else // gZmove
150  return (double)value / 9.6; // unit: deg/s
151  } else
152  return 0;
153 }
154 
156  if (PS3MoveConnected) {
157  int16_t input = getSensor(tempMove);
158 
159  String output = String(input / 100);
160  output += ".";
161  if (input % 100 < 10)
162  output += "0";
163  output += String(input % 100);
164 
165  return output;
166  } else
167  return "Error";
168 }
169 
171  if (l2capinbuf == NULL)
172  return false;
173  if (l2capinbuf[(uint16_t)c >> 8] == ((uint8_t)c & 0xff))
174  return true;
175  return false;
176 }
177 
180  char statusOutput[100];
181 
182  strcpy(statusOutput, "ConnectionStatus: ");
183 
184  if (getStatus(Plugged)) strcat(statusOutput, "Plugged");
185  else if (getStatus(Unplugged)) strcat(statusOutput, "Unplugged");
186  else strcat(statusOutput, "Error");
187 
188 
189  strcat(statusOutput, " - PowerRating: ");
190  if (getStatus(Charging)) strcat(statusOutput, "Charging");
191  else if (getStatus(NotCharging)) strcat(statusOutput, "Not Charging");
192  else if (getStatus(Shutdown)) strcat(statusOutput, "Shutdown");
193  else if (getStatus(Dying)) strcat(statusOutput, "Dying");
194  else if (getStatus(Low)) strcat(statusOutput, "Low");
195  else if (getStatus(High)) strcat(statusOutput, "High");
196  else if (getStatus(Full)) strcat(statusOutput, "Full");
197  else strcat(statusOutput, "Error");
198 
199  strcat(statusOutput, " - WirelessStatus: ");
200 
201  if (getStatus(CableRumble)) strcat(statusOutput, "Cable - Rumble is on");
202  else if (getStatus(Cable)) strcat(statusOutput, "Cable - Rumble is off");
203  else if (getStatus(BluetoothRumble)) strcat(statusOutput, "Bluetooth - Rumble is on");
204  else if (getStatus(Bluetooth)) strcat(statusOutput, "Bluetooth - Rumble is off");
205  else strcat(statusOutput, "Error");
206 
207  return statusOutput;
208 
209  } else if (PS3MoveConnected) {
210  char statusOutput[50];
211 
212  strcpy(statusOutput, "PowerRating: ");
213 
214  if (getStatus(MoveCharging)) strcat(statusOutput, "Charging");
215  else if (getStatus(MoveNotCharging)) strcat(statusOutput, "Not Charging");
216  else if (getStatus(MoveShutdown)) strcat(statusOutput, "Shutdown");
217  else if (getStatus(MoveDying)) strcat(statusOutput, "Dying");
218  else if (getStatus(MoveLow)) strcat(statusOutput, "Low");
219  else if (getStatus(MoveHigh)) strcat(statusOutput, "High");
220  else if (getStatus(MoveFull)) strcat(statusOutput, "Full");
221  else strcat(statusOutput, "Error");
222 
223  return statusOutput;
224  } else
225  return "Error";
226 }
227 
228 void PS3BT::Reset() {
229  PS3Connected = false;
230  PS3MoveConnected = false;
231  PS3NavigationConnected = false;
232  activeConnection = false;
233  l2cap_event_flag = 0; // Reset flags
234  l2cap_state = L2CAP_WAIT;
235 
236  // Needed for PS3 Dualshock Controller commands to work via bluetooth
237  for (uint8_t i = 0; i < PS3_REPORT_BUFFER_SIZE; i++)
238  HIDBuffer[i + 2] = pgm_read_byte(&PS3_REPORT_BUFFER[i]); // First two bytes reserved for report type and ID
239 }
240 
241 void PS3BT::disconnect() { // Use this void to disconnect any of the controllers
242  //First the HID interrupt channel has to be disconencted, then the HID control channel and finally the HCI connection
243  pBtd->l2cap_disconnection_request(hci_handle, 0x0A, interrupt_scid, interrupt_dcid);
244  Reset();
245  l2cap_state = L2CAP_INTERRUPT_DISCONNECT;
246 }
247 
248 void PS3BT::ACLData(uint8_t* ACLData) {
249  if (!pBtd->l2capConnectionClaimed && !PS3Connected && !PS3MoveConnected && !PS3NavigationConnected && !activeConnection && !pBtd->connectToWii && !pBtd->incomingWii && !pBtd->pairWithWii) {
250  if (ACLData[8] == L2CAP_CMD_CONNECTION_REQUEST) {
251  if ((ACLData[12] | (ACLData[13] << 8)) == HID_CTRL_PSM) {
252  pBtd->l2capConnectionClaimed = true; // Claim that the incoming connection belongs to this service
253  activeConnection = true;
254  hci_handle = pBtd->hci_handle; // Store the HCI Handle for the connection
255  l2cap_state = L2CAP_WAIT;
256  for (uint8_t i = 0; i < 30; i++)
257  remote_name[i] = pBtd->remote_name[i]; // Store the remote name for the connection
258 #ifdef DEBUG_USB_HOST
259  if (pBtd->hci_version < 3) { // Check the HCI Version of the Bluetooth dongle
260  Notify(PSTR("\r\nYour dongle may not support reading the analog buttons, sensors and status\r\nYour HCI Version is: "), 0x80);
261  Notify(pBtd->hci_version, 0x80);
262  Notify(PSTR("\r\nBut should be at least 3\r\nThis means that it doesn't support Bluetooth Version 2.0+EDR"), 0x80);
263  }
264 #endif
265  }
266  }
267  }
268  if (((ACLData[0] | (ACLData[1] << 8)) == (hci_handle | 0x2000))) { //acl_handle_ok
269  for (uint8_t i = 0; i < BULK_MAXPKTSIZE; i++)
270  l2capinbuf[i] = ACLData[i];
271  if ((l2capinbuf[6] | (l2capinbuf[7] << 8)) == 0x0001) { //l2cap_control - Channel ID for ACL-U
272  if (l2capinbuf[8] == L2CAP_CMD_COMMAND_REJECT) {
273 #ifdef DEBUG_USB_HOST
274  Notify(PSTR("\r\nL2CAP Command Rejected - Reason: "), 0x80);
275  D_PrintHex<uint8_t > (l2capinbuf[13], 0x80);
276  Notify(PSTR(" "), 0x80);
277  D_PrintHex<uint8_t > (l2capinbuf[12], 0x80);
278  Notify(PSTR(" Data: "), 0x80);
279  D_PrintHex<uint8_t > (l2capinbuf[17], 0x80);
280  Notify(PSTR(" "), 0x80);
281  D_PrintHex<uint8_t > (l2capinbuf[16], 0x80);
282  Notify(PSTR(" "), 0x80);
283  D_PrintHex<uint8_t > (l2capinbuf[15], 0x80);
284  Notify(PSTR(" "), 0x80);
285  D_PrintHex<uint8_t > (l2capinbuf[14], 0x80);
286 #endif
287  } else if (l2capinbuf[8] == L2CAP_CMD_CONNECTION_REQUEST) {
288 #ifdef EXTRADEBUG
289  Notify(PSTR("\r\nL2CAP Connection Request - PSM: "), 0x80);
290  D_PrintHex<uint8_t > (l2capinbuf[13], 0x80);
291  Notify(PSTR(" "), 0x80);
292  D_PrintHex<uint8_t > (l2capinbuf[12], 0x80);
293  Notify(PSTR(" SCID: "), 0x80);
294  D_PrintHex<uint8_t > (l2capinbuf[15], 0x80);
295  Notify(PSTR(" "), 0x80);
296  D_PrintHex<uint8_t > (l2capinbuf[14], 0x80);
297  Notify(PSTR(" Identifier: "), 0x80);
298  D_PrintHex<uint8_t > (l2capinbuf[9], 0x80);
299 #endif
300  if ((l2capinbuf[12] | (l2capinbuf[13] << 8)) == HID_CTRL_PSM) {
301  identifier = l2capinbuf[9];
302  control_scid[0] = l2capinbuf[14];
303  control_scid[1] = l2capinbuf[15];
304  l2cap_event_flag |= L2CAP_FLAG_CONNECTION_CONTROL_REQUEST;
305  } else if ((l2capinbuf[12] | (l2capinbuf[13] << 8)) == HID_INTR_PSM) {
306  identifier = l2capinbuf[9];
307  interrupt_scid[0] = l2capinbuf[14];
308  interrupt_scid[1] = l2capinbuf[15];
309  l2cap_event_flag |= L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST;
310  }
311  } else if (l2capinbuf[8] == L2CAP_CMD_CONFIG_RESPONSE) {
312  if ((l2capinbuf[16] | (l2capinbuf[17] << 8)) == 0x0000) { // Success
313  if (l2capinbuf[12] == control_dcid[0] && l2capinbuf[13] == control_dcid[1]) {
314  //Notify(PSTR("\r\nHID Control Configuration Complete"), 0x80);
315  l2cap_event_flag |= L2CAP_FLAG_CONFIG_CONTROL_SUCCESS;
316  } else if (l2capinbuf[12] == interrupt_dcid[0] && l2capinbuf[13] == interrupt_dcid[1]) {
317  //Notify(PSTR("\r\nHID Interrupt Configuration Complete"), 0x80);
318  l2cap_event_flag |= L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS;
319  }
320  }
321  } else if (l2capinbuf[8] == L2CAP_CMD_CONFIG_REQUEST) {
322  if (l2capinbuf[12] == control_dcid[0] && l2capinbuf[13] == control_dcid[1]) {
323  //Notify(PSTR("\r\nHID Control Configuration Request"), 0x80);
324  identifier = l2capinbuf[9];
325  l2cap_event_flag |= L2CAP_FLAG_CONFIG_CONTROL_REQUEST;
326  } else if (l2capinbuf[12] == interrupt_dcid[0] && l2capinbuf[13] == interrupt_dcid[1]) {
327  //Notify(PSTR("\r\nHID Interrupt Configuration Request"), 0x80);
328  identifier = l2capinbuf[9];
329  l2cap_event_flag |= L2CAP_FLAG_CONFIG_INTERRUPT_REQUEST;
330  }
331  } else if (l2capinbuf[8] == L2CAP_CMD_DISCONNECT_REQUEST) {
332  if (l2capinbuf[12] == control_dcid[0] && l2capinbuf[13] == control_dcid[1]) {
333 #ifdef DEBUG_USB_HOST
334  Notify(PSTR("\r\nDisconnect Request: Control Channel"), 0x80);
335 #endif
336  identifier = l2capinbuf[9];
337  pBtd->l2cap_disconnection_response(hci_handle, identifier, control_dcid, control_scid);
338  Reset();
339  } else if (l2capinbuf[12] == interrupt_dcid[0] && l2capinbuf[13] == interrupt_dcid[1]) {
340 #ifdef DEBUG_USB_HOST
341  Notify(PSTR("\r\nDisconnect Request: Interrupt Channel"), 0x80);
342 #endif
343  identifier = l2capinbuf[9];
344  pBtd->l2cap_disconnection_response(hci_handle, identifier, interrupt_dcid, interrupt_scid);
345  Reset();
346  }
347  } else if (l2capinbuf[8] == L2CAP_CMD_DISCONNECT_RESPONSE) {
348  if (l2capinbuf[12] == control_scid[0] && l2capinbuf[13] == control_scid[1]) {
349  //Notify(PSTR("\r\nDisconnect Response: Control Channel"), 0x80);
350  identifier = l2capinbuf[9];
351  l2cap_event_flag |= L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE;
352  } else if (l2capinbuf[12] == interrupt_scid[0] && l2capinbuf[13] == interrupt_scid[1]) {
353  //Notify(PSTR("\r\nDisconnect Response: Interrupt Channel"), 0x80);
354  identifier = l2capinbuf[9];
355  l2cap_event_flag |= L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE;
356  }
357  }
358 #ifdef EXTRADEBUG
359  else {
360  Notify(PSTR("\r\nL2CAP Unknown Signaling Command: "), 0x80);
361  D_PrintHex<uint8_t > (l2capinbuf[8], 0x80);
362  }
363 #endif
364  } else if (l2capinbuf[6] == interrupt_dcid[0] && l2capinbuf[7] == interrupt_dcid[1]) { // l2cap_interrupt
365  //Notify(PSTR("\r\nL2CAP Interrupt"), 0x80);
367  /* Read Report */
368  if (l2capinbuf[8] == 0xA1) { // HID_THDR_DATA_INPUT
370  ButtonState = (uint32_t)(l2capinbuf[11] | ((uint16_t)l2capinbuf[12] << 8) | ((uint32_t)l2capinbuf[13] << 16));
371  else if (PS3MoveConnected)
372  ButtonState = (uint32_t)(l2capinbuf[10] | ((uint16_t)l2capinbuf[11] << 8) | ((uint32_t)l2capinbuf[12] << 16));
373 
374  //Notify(PSTR("\r\nButtonState", 0x80);
375  //PrintHex<uint32_t>(ButtonState, 0x80);
376 
377  if (ButtonState != OldButtonState) {
378  ButtonClickState = ButtonState & ~OldButtonState; // Update click state variable
379  OldButtonState = ButtonState;
380  }
381 
382 #ifdef PRINTREPORT // Uncomment "#define PRINTREPORT" to print the report send by the PS3 Controllers
383  for (uint8_t i = 10; i < 58; i++) {
384  D_PrintHex<uint8_t > (l2capinbuf[i], 0x80);
385  Notify(PSTR(" "), 0x80);
386  }
387  Notify(PSTR("\r\n"), 0x80);
388 #endif
389  }
390  }
391  }
392  L2CAP_task();
393  }
394 }
395 
396 void PS3BT::L2CAP_task() {
397  switch (l2cap_state) {
398  case L2CAP_WAIT:
400 #ifdef DEBUG_USB_HOST
401  Notify(PSTR("\r\nHID Control Incoming Connection Request"), 0x80);
402 #endif
403  pBtd->l2cap_connection_response(hci_handle, identifier, control_dcid, control_scid, PENDING);
404  delay(1);
405  pBtd->l2cap_connection_response(hci_handle, identifier, control_dcid, control_scid, SUCCESSFUL);
406  identifier++;
407  delay(1);
408  pBtd->l2cap_config_request(hci_handle, identifier, control_scid);
409  l2cap_state = L2CAP_CONTROL_REQUEST;
410  }
411  break;
414 #ifdef DEBUG_USB_HOST
415  Notify(PSTR("\r\nHID Control Configuration Request"), 0x80);
416 #endif
417  pBtd->l2cap_config_response(hci_handle, identifier, control_scid);
418  l2cap_state = L2CAP_CONTROL_SUCCESS;
419  }
420  break;
421 
424 #ifdef DEBUG_USB_HOST
425  Notify(PSTR("\r\nHID Control Successfully Configured"), 0x80);
426 #endif
427  l2cap_state = L2CAP_INTERRUPT_SETUP;
428  }
429  break;
432 #ifdef DEBUG_USB_HOST
433  Notify(PSTR("\r\nHID Interrupt Incoming Connection Request"), 0x80);
434 #endif
435  pBtd->l2cap_connection_response(hci_handle, identifier, interrupt_dcid, interrupt_scid, PENDING);
436  delay(1);
437  pBtd->l2cap_connection_response(hci_handle, identifier, interrupt_dcid, interrupt_scid, SUCCESSFUL);
438  identifier++;
439  delay(1);
440  pBtd->l2cap_config_request(hci_handle, identifier, interrupt_scid);
441 
442  l2cap_state = L2CAP_INTERRUPT_REQUEST;
443  }
444  break;
447 #ifdef DEBUG_USB_HOST
448  Notify(PSTR("\r\nHID Interrupt Configuration Request"), 0x80);
449 #endif
450  pBtd->l2cap_config_response(hci_handle, identifier, interrupt_scid);
451  l2cap_state = L2CAP_INTERRUPT_SUCCESS;
452  }
453  break;
456 #ifdef DEBUG_USB_HOST
457  Notify(PSTR("\r\nHID Interrupt Successfully Configured"), 0x80);
458 #endif
459  if (remote_name[0] == 'M') { // First letter in Motion Controller ('M')
460  for (uint8_t i = 0; i < BULK_MAXPKTSIZE; i++) // Reset l2cap in buffer as it sometimes read it as a button has been pressed
461  l2capinbuf[i] = 0;
462  ButtonState = 0;
463  OldButtonState = 0;
464 
465  l2cap_state = L2CAP_HID_PS3_LED;
466  } else
467  l2cap_state = L2CAP_HID_ENABLE_SIXAXIS;
468  timer = millis();
469  }
470  break;
471 
472  /* These states are handled in Run() */
473 
476 #ifdef DEBUG_USB_HOST
477  Notify(PSTR("\r\nDisconnected Interrupt Channel"), 0x80);
478 #endif
479  identifier++;
480  pBtd->l2cap_disconnection_request(hci_handle, identifier, control_scid, control_dcid);
481  l2cap_state = L2CAP_CONTROL_DISCONNECT;
482  }
483  break;
484 
487 #ifdef DEBUG_USB_HOST
488  Notify(PSTR("\r\nDisconnected Control Channel"), 0x80);
489 #endif
490  pBtd->hci_disconnect(hci_handle);
491  hci_handle = -1; // Reset handle
492  l2cap_event_flag = 0; // Reset flags
493  l2cap_state = L2CAP_WAIT;
494  }
495  break;
496  }
497 }
498 
499 void PS3BT::Run() {
500  switch (l2cap_state) {
502  if (millis() - timer > 1000) { // loop 1 second before sending the command
503  for (uint8_t i = 0; i < BULK_MAXPKTSIZE; i++) // Reset l2cap in buffer as it sometimes read it as a button has been pressed
504  l2capinbuf[i] = 0;
505  ButtonState = 0;
506  OldButtonState = 0;
507 
508  enable_sixaxis();
509  for (uint8_t i = 15; i < 19; i++)
510  l2capinbuf[i] = 0x7F; // Set the analog joystick values to center position
511  l2cap_state = L2CAP_HID_PS3_LED;
512  timer = millis();
513  }
514  break;
515 
516  case L2CAP_HID_PS3_LED:
517  if (millis() - timer > 1000) { // loop 1 second before sending the command
518  if (remote_name[0] == 'P') { // First letter in PLAYSTATION(R)3 Controller ('P')
519  setLedOn(LED1);
520 #ifdef DEBUG_USB_HOST
521  Notify(PSTR("\r\nDualshock 3 Controller Enabled\r\n"), 0x80);
522 #endif
523  PS3Connected = true;
524  } else if (remote_name[0] == 'N') { // First letter in Navigation Controller ('N')
525  setLedOn(LED1); // This just turns LED constantly on, on the Navigation controller
526 #ifdef DEBUG_USB_HOST
527  Notify(PSTR("\r\nNavigation Controller Enabled\r\n"), 0x80);
528 #endif
529  PS3NavigationConnected = true;
530  } else if (remote_name[0] == 'M') { // First letter in Motion Controller ('M')
531  moveSetBulb(Red);
532  timerBulbRumble = millis();
533 #ifdef DEBUG_USB_HOST
534  Notify(PSTR("\r\nMotion Controller Enabled\r\n"), 0x80);
535 #endif
536  PS3MoveConnected = true;
537  }
538  l2cap_state = L2CAP_DONE;
539  }
540  break;
541 
542  case L2CAP_DONE:
543  if (PS3MoveConnected) { //The Bulb and rumble values, has to be send at aproximatly every 5th second for it to stay on
544  if (millis() - timerBulbRumble > 4000) { //Send at least every 4th second
545  HIDMove_Command(HIDMoveBuffer, HID_BUFFERSIZE); //The Bulb and rumble values, has to be written again and again, for it to stay turned on
546  timerBulbRumble = millis();
547  }
548  }
549  break;
550  }
551 }
552 
553 /************************************************************/
554 /* HID Commands */
555 /************************************************************/
556 
557 //Playstation Sixaxis Dualshock and Navigation Controller commands
558 
559 void PS3BT::HID_Command(uint8_t* data, uint8_t nbytes) {
560  if (millis() - timerHID <= 250)// Check if is has been more than 250ms since last command
561  delay((uint32_t)(250 - (millis() - timerHID))); //There have to be a delay between commands
562  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
563  timerHID = millis();
564 }
565 
567  for (uint8_t i = 0; i < PS3_REPORT_BUFFER_SIZE; i++)
568  HIDBuffer[i + 2] = pgm_read_byte(&PS3_REPORT_BUFFER[i]); // First two bytes reserved for report type and ID
569 
570  HID_Command(HIDBuffer, HID_BUFFERSIZE);
571 }
572 
574  HIDBuffer[3] = 0x00;
575  HIDBuffer[4] = 0x00;
576  HIDBuffer[5] = 0x00;
577  HIDBuffer[6] = 0x00;
578 
579  HID_Command(HIDBuffer, HID_BUFFERSIZE);
580 }
581 
583  uint8_t power[2] = { 0xff, 0x00 }; // Defaults to RumbleLow
584  if (mode == RumbleHigh) {
585  power[0] = 0x00;
586  power[1] = 0xff;
587  }
588  setRumbleOn(0xfe, power[0], 0xfe, power[1]);
589 }
590 
591 void PS3BT::setRumbleOn(uint8_t rightDuration, uint8_t rightPower, uint8_t leftDuration, uint8_t leftPower) {
592  HIDBuffer[3] = rightDuration;
593  HIDBuffer[4] = rightPower;
594  HIDBuffer[5] = leftDuration;
595  HIDBuffer[6] = leftPower;
596  HID_Command(HIDBuffer, HID_BUFFERSIZE);
597 }
598 
599 void PS3BT::setLedRaw(uint8_t value) {
600  HIDBuffer[11] = value << 1;
601  HID_Command(HIDBuffer, HID_BUFFERSIZE);
602 }
604  HIDBuffer[11] &= ~((uint8_t)((pgm_read_byte(&LEDS[(uint8_t)a]) & 0x0f) << 1));
605  HID_Command(HIDBuffer, HID_BUFFERSIZE);
606 }
607 
609  HIDBuffer[11] |= (uint8_t)((pgm_read_byte(&LEDS[(uint8_t)a]) & 0x0f) << 1);
610  HID_Command(HIDBuffer, HID_BUFFERSIZE);
611 }
612 
614  HIDBuffer[11] ^= (uint8_t)((pgm_read_byte(&LEDS[(uint8_t)a]) & 0x0f) << 1);
615  HID_Command(HIDBuffer, HID_BUFFERSIZE);
616 }
617 
618 void PS3BT::enable_sixaxis() { //Command used to enable the Dualshock 3 and Navigation controller to send data via USB
619  uint8_t cmd_buf[6];
620  cmd_buf[0] = 0x53; // HID BT Set_report (0x50) | Report Type (Feature 0x03)
621  cmd_buf[1] = 0xF4; // Report ID
622  cmd_buf[2] = 0x42; // Special PS3 Controller enable commands
623  cmd_buf[3] = 0x03;
624  cmd_buf[4] = 0x00;
625  cmd_buf[5] = 0x00;
626 
627  HID_Command(cmd_buf, 6);
628 }
629 
630 //Playstation Move Controller commands
631 
632 void PS3BT::HIDMove_Command(uint8_t* data, uint8_t nbytes) {
633  if (millis() - timerHID <= 250)// Check if is has been less than 200ms since last command
634  delay((uint32_t)(250 - (millis() - timerHID))); //There have to be a delay between commands
635  pBtd->L2CAP_Command(hci_handle, data, nbytes, interrupt_scid[0], interrupt_scid[1]); // The Move controller sends it's data via the intterrupt channel
636  timerHID = millis();
637 }
638 
639 void PS3BT::moveSetBulb(uint8_t r, uint8_t g, uint8_t b) { //Use this to set the Color using RGB values
640  //set the Bulb's values into the write buffer
641  HIDMoveBuffer[3] = r;
642  HIDMoveBuffer[4] = g;
643  HIDMoveBuffer[5] = b;
644 
645  HIDMove_Command(HIDMoveBuffer, HID_BUFFERSIZE);
646 }
647 
648 void PS3BT::moveSetBulb(Colors color) { //Use this to set the Color using the predefined colors in enum
649  moveSetBulb((uint8_t)(color >> 16), (uint8_t)(color >> 8), (uint8_t)(color));
650 }
651 
652 void PS3BT::moveSetRumble(uint8_t rumble) {
653 #ifdef DEBUG_USB_HOST
654  if (rumble < 64 && rumble != 0) // The rumble value has to at least 64, or approximately 25% (64/255*100)
655  Notify(PSTR("\r\nThe rumble value has to at least 64, or approximately 25%"), 0x80);
656 #endif
657  //set the rumble value into the write buffer
658  HIDMoveBuffer[7] = rumble;
659 
660  HIDMove_Command(HIDMoveBuffer, HID_BUFFERSIZE);
661 }