From 0df6d40fce162bdc794255be79dec12713818c30 Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Sun, 7 Apr 2013 17:21:28 +0200 Subject: [PATCH 01/21] Replace do while loop with while loop --- SPP.cpp | 26 +++++++++++++------------- SPP.h | 2 +- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/SPP.cpp b/SPP.cpp index a2415011..08502877 100644 --- a/SPP.cpp +++ b/SPP.cpp @@ -681,7 +681,7 @@ void SPP::RFCOMM_Command(uint8_t* data, uint8_t nbytes) { void SPP::sendRfcomm(uint8_t channel, uint8_t direction, uint8_t CR, uint8_t channelType, uint8_t pfBit, uint8_t* data, uint8_t length) { l2capoutbuf[0] = channel | direction | CR | extendAddress; // RFCOMM Address l2capoutbuf[1] = channelType | pfBit; // RFCOMM Control - l2capoutbuf[2] = length << 1 | 0x01; // Length and format (allways 0x01 bytes format) + l2capoutbuf[2] = length << 1 | 0x01; // Length and format (always 0x01 bytes format) uint8_t i = 0; for (; i < length; i++) l2capoutbuf[i + 3] = data[i]; @@ -729,15 +729,15 @@ uint8_t SPP::calcFcs(uint8_t *data) { void SPP::print(const String &str) { if (!connected) return; - int16_t stringLength = str.length(); // This will be used to store the characters that still needs to be sent + uint8_t stringLength = str.length(); // This will be used to store the characters that still needs to be sent uint8_t length; // This is the length of the string we are sending uint8_t offset = 0; // This is used to keep track of where we are in the string l2capoutbuf[0] = rfcommChannelConnection | 0 | 0 | extendAddress; // RFCOMM Address l2capoutbuf[1] = RFCOMM_UIH; // RFCOMM Control - do { - if (stringLength > (sizeof (l2capoutbuf) - 4)) // Check if the string is larger that the outgoing puffer + while (stringLength) { // We will run this while loop until this variable is 0 + if (stringLength > (sizeof (l2capoutbuf) - 4)) // Check if the string is larger that the outgoing buffer length = sizeof (l2capoutbuf) - 4; else length = stringLength; @@ -752,21 +752,21 @@ void SPP::print(const String &str) { stringLength -= length; offset += length; // Increment the offset - } while (stringLength); // We will run this loop until this variable is less than 0 + } } void SPP::print(const char* str) { if (!connected) return; - int16_t stringLength = strlen(str); // This will be used to store the characters that still needs to be sent + uint8_t stringLength = strlen(str); // This will be used to store the characters that still needs to be sent uint8_t length; // This is the length of the string we are sending uint8_t offset = 0; // This is used to keep track of where we are in the string l2capoutbuf[0] = rfcommChannelConnection | 0 | 0 | extendAddress; // RFCOMM Address l2capoutbuf[1] = RFCOMM_UIH; // RFCOMM Control - do { - if (stringLength > (sizeof (l2capoutbuf) - 4)) // Check if the string is larger that the outgoing puffer + while (stringLength) { // We will run this while loop until this variable is 0 + if (stringLength > (sizeof (l2capoutbuf) - 4)) // Check if the string is larger that the outgoing buffer length = sizeof (l2capoutbuf) - 4; else length = stringLength; @@ -781,10 +781,10 @@ void SPP::print(const char* str) { stringLength -= length; offset += length; // Increment the offset - } while (stringLength); // We will run this loop until this variable is less than 0 + } } -void SPP::print(uint8_t* array, int16_t stringLength) { +void SPP::print(uint8_t* array, uint8_t stringLength) { if (!connected) return; uint8_t length; // This is the length of the string we are sending @@ -793,8 +793,8 @@ void SPP::print(uint8_t* array, int16_t stringLength) { l2capoutbuf[0] = rfcommChannelConnection | 0 | 0 | extendAddress; // RFCOMM Address l2capoutbuf[1] = RFCOMM_UIH; // RFCOMM Control - do { - if (stringLength > (sizeof (l2capoutbuf) - 4)) // Check if the string is larger that the outgoing puffer + while (stringLength) { // We will run this while loop until this variable is 0 + if (stringLength > (sizeof (l2capoutbuf) - 4)) // Check if the string is larger that the outgoing buffer length = sizeof (l2capoutbuf) - 4; else length = stringLength; @@ -809,7 +809,7 @@ void SPP::print(uint8_t* array, int16_t stringLength) { stringLength -= length; offset += length; // Increment the offset - } while (stringLength); // We will run this loop until this variable is less than 0 + } } void SPP::println(const String &str) { diff --git a/SPP.h b/SPP.h index 9dbbad8b..61a5a4c2 100644 --- a/SPP.h +++ b/SPP.h @@ -158,7 +158,7 @@ public: * @param array Array to send. * @param length Number of bytes to send. */ - void print(uint8_t* array, int16_t length); + void print(uint8_t* array, uint8_t length); /** * Same as print(uint8_t* array, uint8_t length), but will include newline and carriage return. * @param array Array to send. From 5b95368438b76760ffa60fe4da12ec255e317de7 Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Mon, 8 Apr 2013 00:18:25 +0200 Subject: [PATCH 02/21] Fixed typos --- SPP.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/SPP.h b/SPP.h index 61a5a4c2..92cb51b0 100644 --- a/SPP.h +++ b/SPP.h @@ -68,7 +68,7 @@ //#define RFCOMM_DM 0x0F #define RFCOMM_DISC 0x43 -#define extendAddress 0x01 // Allways 1 +#define extendAddress 0x01 // Always 1 // Multiplexer message types #define BT_RFCOMM_PN_CMD 0x83 @@ -341,10 +341,10 @@ private: uint16_t hci_handle; // The HCI Handle for the connection - /* Variables used by L2CAP state maschines */ + /* Variables used by L2CAP state machines */ uint8_t l2cap_sdp_state; uint8_t l2cap_rfcomm_state; - uint16_t l2cap_event_flag; // l2cap flags of received bluetooth events + uint16_t l2cap_event_flag; // l2cap flags of received Bluetooth events uint8_t l2capoutbuf[BULK_MAXPKTSIZE]; // General purpose buffer for l2cap out data uint8_t rfcommbuf[10]; // Buffer for RFCOMM Commands @@ -358,7 +358,7 @@ private: /* RFCOMM Variables */ uint8_t rfcommChannel; - uint8_t rfcommChannelConnection; // This is the channel the SPP chanel will be running at + uint8_t rfcommChannelConnection; // This is the channel the SPP channel will be running at uint8_t rfcommDirection; uint8_t rfcommCommandResponse; uint8_t rfcommChannelType; From 60ef04d32f1e0b12e92b53627e3b32679655b98f Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Mon, 8 Apr 2013 00:22:15 +0200 Subject: [PATCH 03/21] Always print incoming SDP data if EXTRADEBUG is uncommented --- SPP.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/SPP.cpp b/SPP.cpp index 08502877..1f6e269c 100644 --- a/SPP.cpp +++ b/SPP.cpp @@ -207,7 +207,9 @@ void SPP::ACLData(uint8_t* l2capinbuf) { l2capResponse2(l2capinbuf[9], l2capinbuf[10]); // L2CAP continuation state firstMessage = true; } - } else { + } else + serviceNotSupported(l2capinbuf[9], l2capinbuf[10]); // The service is not supported + #ifdef EXTRADEBUG Notify(PSTR("\r\nUUID: "), 0x80); uint16_t uuid; @@ -226,8 +228,6 @@ void SPP::ACLData(uint8_t* l2capinbuf) { Notify(PSTR(" "), 0x80); } #endif - serviceNotSupported(l2capinbuf[9], l2capinbuf[10]); // The service is not supported - } } } else if (l2capinbuf[6] == rfcomm_dcid[0] && l2capinbuf[7] == rfcomm_dcid[1]) { // RFCOMM rfcommChannel = l2capinbuf[8] & 0xF8; From 35ce9c7ab3bc45319e6ec97f0216ff0367ca190d Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Mon, 8 Apr 2013 00:23:52 +0200 Subject: [PATCH 04/21] Fixed indent --- SPP.cpp | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/SPP.cpp b/SPP.cpp index 1f6e269c..14493430 100644 --- a/SPP.cpp +++ b/SPP.cpp @@ -209,24 +209,23 @@ void SPP::ACLData(uint8_t* l2capinbuf) { } } else serviceNotSupported(l2capinbuf[9], l2capinbuf[10]); // The service is not supported - #ifdef EXTRADEBUG - Notify(PSTR("\r\nUUID: "), 0x80); - uint16_t uuid; - if((l2capinbuf[16] << 8 | l2capinbuf[17]) == 0x0000) // Check if it's sending the UUID as a 128-bit UUID - uuid = (l2capinbuf[18] << 8 | l2capinbuf[19]); - else // Short UUID - uuid = (l2capinbuf[16] << 8 | l2capinbuf[17]); - PrintHex (uuid, 0x80); + Notify(PSTR("\r\nUUID: "), 0x80); + uint16_t uuid; + if((l2capinbuf[16] << 8 | l2capinbuf[17]) == 0x0000) // Check if it's sending the UUID as a 128-bit UUID + uuid = (l2capinbuf[18] << 8 | l2capinbuf[19]); + else // Short UUID + uuid = (l2capinbuf[16] << 8 | l2capinbuf[17]); + PrintHex (uuid, 0x80); - Notify(PSTR("\r\nLength: "), 0x80); - uint16_t length = l2capinbuf[11] << 8 | l2capinbuf[12]; - PrintHex (length, 0x80); - Notify(PSTR("\r\nData: "), 0x80); - for (uint8_t i = 0; i < length; i++) { - PrintHex (l2capinbuf[13+i], 0x80); - Notify(PSTR(" "), 0x80); - } + Notify(PSTR("\r\nLength: "), 0x80); + uint16_t length = l2capinbuf[11] << 8 | l2capinbuf[12]; + PrintHex (length, 0x80); + Notify(PSTR("\r\nData: "), 0x80); + for (uint8_t i = 0; i < length; i++) { + PrintHex (l2capinbuf[13+i], 0x80); + Notify(PSTR(" "), 0x80); + } #endif } } else if (l2capinbuf[6] == rfcomm_dcid[0] && l2capinbuf[7] == rfcomm_dcid[1]) { // RFCOMM From b88de728266e8b3fa024b4e2f36b639b3b83af8a Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Mon, 8 Apr 2013 00:25:01 +0200 Subject: [PATCH 05/21] Simplified print commands --- SPP.cpp | 56 ++++++-------------------------------------------------- 1 file changed, 6 insertions(+), 50 deletions(-) diff --git a/SPP.cpp b/SPP.cpp index 14493430..8845ea22 100644 --- a/SPP.cpp +++ b/SPP.cpp @@ -726,61 +726,17 @@ uint8_t SPP::calcFcs(uint8_t *data) { /* Serial commands */ void SPP::print(const String &str) { - if (!connected) - return; - uint8_t stringLength = str.length(); // This will be used to store the characters that still needs to be sent - uint8_t length; // This is the length of the string we are sending - uint8_t offset = 0; // This is used to keep track of where we are in the string + uint8_t length = str.length(); // Get the length of the string + uint8_t buf[length]; - l2capoutbuf[0] = rfcommChannelConnection | 0 | 0 | extendAddress; // RFCOMM Address - l2capoutbuf[1] = RFCOMM_UIH; // RFCOMM Control - - while (stringLength) { // We will run this while loop until this variable is 0 - if (stringLength > (sizeof (l2capoutbuf) - 4)) // Check if the string is larger that the outgoing buffer - length = sizeof (l2capoutbuf) - 4; - else - length = stringLength; + for(uint8_t i = 0; i < length; i++) + buf[i] = str[i]; - l2capoutbuf[2] = length << 1 | 1; // Length - uint8_t i = 0; - for (; i < length; i++) - l2capoutbuf[i + 3] = str[i + offset]; - l2capoutbuf[i + 3] = calcFcs(l2capoutbuf); // Calculate checksum - - RFCOMM_Command(l2capoutbuf, length + 4); - - stringLength -= length; - offset += length; // Increment the offset - } + print(buf,length); } void SPP::print(const char* str) { - if (!connected) - return; - uint8_t stringLength = strlen(str); // This will be used to store the characters that still needs to be sent - uint8_t length; // This is the length of the string we are sending - uint8_t offset = 0; // This is used to keep track of where we are in the string - - l2capoutbuf[0] = rfcommChannelConnection | 0 | 0 | extendAddress; // RFCOMM Address - l2capoutbuf[1] = RFCOMM_UIH; // RFCOMM Control - - while (stringLength) { // We will run this while loop until this variable is 0 - if (stringLength > (sizeof (l2capoutbuf) - 4)) // Check if the string is larger that the outgoing buffer - length = sizeof (l2capoutbuf) - 4; - else - length = stringLength; - - l2capoutbuf[2] = length << 1 | 1; // Length - uint8_t i = 0; - for (; i < length; i++) - l2capoutbuf[i + 3] = str[i + offset]; - l2capoutbuf[i + 3] = calcFcs(l2capoutbuf); // Calculate checksum - - RFCOMM_Command(l2capoutbuf, length + 4); - - stringLength -= length; - offset += length; // Increment the offset - } + print((uint8_t*) str, strlen(str)); } void SPP::print(uint8_t* array, uint8_t stringLength) { From 9af835e8fe93f89ed1e861e6bd4dc6b9f401640b Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Mon, 15 Apr 2013 18:36:47 +0200 Subject: [PATCH 06/21] Battery level no longer return battery level in percentage --- examples/Xbox/XBOXRECV/XBOXRECV.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/Xbox/XBOXRECV/XBOXRECV.ino b/examples/Xbox/XBOXRECV/XBOXRECV.ino index b6ae3b23..e49dee37 100644 --- a/examples/Xbox/XBOXRECV/XBOXRECV.ino +++ b/examples/Xbox/XBOXRECV/XBOXRECV.ino @@ -89,8 +89,8 @@ void loop() { if(Xbox.getButtonClick(i,XBOX)) { Xbox.setLedMode(i,ROTATING); Serial.print(F("Xbox (Battery: ")); - Serial.print(Xbox.getBatteryLevel(i)); - Serial.println(F("%)")); + Serial.print(Xbox.getBatteryLevel(i)); // The battery level in the range 0-3 + Serial.println(F(")")); } if(Xbox.getButtonClick(i,SYNC)) Serial.println(F("Sync")); From 78cb524bcf8e78b65205b0c2d80f73748a998712 Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Mon, 15 Apr 2013 18:37:24 +0200 Subject: [PATCH 07/21] Fixed bug in getButtonPress --- XBOXRECV.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/XBOXRECV.cpp b/XBOXRECV.cpp index 572cde76..7686c184 100644 --- a/XBOXRECV.cpp +++ b/XBOXRECV.cpp @@ -386,7 +386,7 @@ uint8_t XBOXRECV::getButtonPress(uint8_t controller, Button b) { return (uint8_t)(ButtonState[controller] >> 8); else if (b == R2) return (uint8_t)ButtonState[controller]; - return (ButtonState[controller] & ((uint32_t)pgm_read_word(&XBOXBUTTONS[(uint8_t)b]) << 16)); + return (bool)(ButtonState[controller] & ((uint32_t)pgm_read_word(&XBOXBUTTONS[(uint8_t)b]) << 16)); } bool XBOXRECV::getButtonClick(uint8_t controller, Button b) { From 48b3ed93f5fbf1842ca16351858d1249f77af929 Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Fri, 26 Apr 2013 23:50:39 +0200 Subject: [PATCH 08/21] Removed all direct calls to serial.print --- BTD.cpp | 12 ++++++------ PS3BT.cpp | 32 ++++++++++++++++---------------- PS3USB.cpp | 6 +++--- SPP.cpp | 36 ++++++++++++++++++------------------ Wii.cpp | 44 ++++++++++++++++++++++---------------------- XBOXRECV.cpp | 10 +++++----- XBOXUSB.cpp | 4 ++-- message.cpp | 16 ++++++++++++++++ message.h | 2 ++ 9 files changed, 90 insertions(+), 72 deletions(-) diff --git a/BTD.cpp b/BTD.cpp index 9d0ea3ec..fdcaf249 100755 --- a/BTD.cpp +++ b/BTD.cpp @@ -373,7 +373,7 @@ void BTD::HCI_event_task() { if (hcibuf[2]) { // Check that there is more than zero responses #ifdef EXTRADEBUG Notify(PSTR("\r\nNumber of responses: "), 0x80); - Serial.print(hcibuf[2]); + Notify(hcibuf[2], 0x80); #endif for (uint8_t i = 0; i < hcibuf[2]; i++) { if ((hcibuf[4 + 8 * hcibuf[2] + 3 * i] == 0x04 && hcibuf[5 + 8 * hcibuf[2] + 3 * i] == 0x25 && hcibuf[6 + 8 * hcibuf[2] + 3 * i] == 0x00) || (hcibuf[4 + 8 * hcibuf[2] + 3 * i] == 0x08 && hcibuf[5 + 8 * hcibuf[2] + 3 * i] == 0x05 && hcibuf[6 + 8 * hcibuf[2] + 3 * i] == 0x00)) { // See http://bluetooth-pentest.narod.ru/software/bluetooth_class_of_device-service_generator.html and http://wiibrew.org/wiki/Wiimote#SDP_information @@ -460,7 +460,7 @@ void BTD::HCI_event_task() { } else if (btdPin != NULL) { #ifdef DEBUG Notify(PSTR("\r\nBluetooth pin is set too: "), 0x80); - Serial.print(btdPin); + NotifyStr(btdPin, 0x80); #endif hci_pin_code_request_reply(); } else { @@ -590,7 +590,7 @@ void BTD::HCI_task() { if (hci_cmd_complete) { #ifdef DEBUG Notify(PSTR("\r\nThe name is set to: "), 0x80); - Serial.print(btdName); + NotifyStr(btdName, 0x80); #endif hci_state = HCI_CHECK_WII_SERVICE; } @@ -681,7 +681,7 @@ void BTD::HCI_task() { for (uint8_t i = 0; i < 30; i++) { if (remote_name[i] == NULL) break; - Serial.write(remote_name[i]); + Notifyc(remote_name[i], 0x80); } #endif if (strncmp((const char*)remote_name, "Nintendo", 8) == 0) { @@ -1073,9 +1073,9 @@ void BTD::L2CAP_Command(uint16_t handle, uint8_t* data, uint8_t nbytes, uint8_t Notify(PSTR("\r\nError sending L2CAP message: 0x"), 0x80); PrintHex (rcode, 0x80); Notify(PSTR(" - Channel ID: "), 0x80); - Serial.print(channelHigh); + PrintHex (channelHigh, 0x80); Notify(PSTR(" "), 0x80); - Serial.print(channelLow); + PrintHex (channelLow, 0x80); #endif } } diff --git a/PS3BT.cpp b/PS3BT.cpp index d04433d3..ed2c98c9 100644 --- a/PS3BT.cpp +++ b/PS3BT.cpp @@ -258,7 +258,7 @@ void PS3BT::ACLData(uint8_t* ACLData) { #ifdef DEBUG if (pBtd->hci_version < 3) { // Check the HCI Version of the Bluetooth dongle Notify(PSTR("\r\nYour dongle may not support reading the analog buttons, sensors and status\r\nYour HCI Version is: "), 0x80); - Serial.print(pBtd->hci_version); + Notify(pBtd->hci_version, 0x80); Notify(PSTR("\r\nBut should be at least 3\r\nThis means that it doesn't support Bluetooth Version 2.0+EDR"), 0x80); } #endif @@ -271,17 +271,17 @@ void PS3BT::ACLData(uint8_t* ACLData) { if ((l2capinbuf[6] | (l2capinbuf[7] << 8)) == 0x0001) { //l2cap_control - Channel ID for ACL-U if (l2capinbuf[8] == L2CAP_CMD_COMMAND_REJECT) { #ifdef DEBUG - Notify(PSTR("\r\nL2CAP Command Rejected - Reason: "), 0x80); + Notify(PSTR("\r\nL2CAP Command Rejected - Reason: "), 0x80); PrintHex (l2capinbuf[13], 0x80); - Serial.print(" "); + Notify(PSTR(" "), 0x80); PrintHex (l2capinbuf[12], 0x80); - Serial.print(" Data: "); + Notify(PSTR(" Data: "), 0x80); PrintHex (l2capinbuf[17], 0x80); - Serial.print(" "); + Notify(PSTR(" "), 0x80); PrintHex (l2capinbuf[16], 0x80); - Serial.print(" "); + Notify(PSTR(" "), 0x80); PrintHex (l2capinbuf[15], 0x80); - Serial.print(" "); + Notify(PSTR(" "), 0x80); PrintHex (l2capinbuf[14], 0x80); #endif } else if (l2capinbuf[8] == L2CAP_CMD_CONNECTION_REQUEST) { @@ -311,20 +311,20 @@ void PS3BT::ACLData(uint8_t* ACLData) { } else if (l2capinbuf[8] == L2CAP_CMD_CONFIG_RESPONSE) { if ((l2capinbuf[16] | (l2capinbuf[17] << 8)) == 0x0000) { // Success if (l2capinbuf[12] == control_dcid[0] && l2capinbuf[13] == control_dcid[1]) { - //Serial.print("\r\nHID Control Configuration Complete"); + //Notify(PSTR("\r\nHID Control Configuration Complete"), 0x80); l2cap_event_flag |= L2CAP_FLAG_CONFIG_CONTROL_SUCCESS; } else if (l2capinbuf[12] == interrupt_dcid[0] && l2capinbuf[13] == interrupt_dcid[1]) { - //Serial.print("\r\nHID Interrupt Configuration Complete"); + //Notify(PSTR("\r\nHID Interrupt Configuration Complete"), 0x80); l2cap_event_flag |= L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS; } } } else if (l2capinbuf[8] == L2CAP_CMD_CONFIG_REQUEST) { if (l2capinbuf[12] == control_dcid[0] && l2capinbuf[13] == control_dcid[1]) { - //Serial.print("\r\nHID Control Configuration Request"); + //Notify(PSTR("\r\nHID Control Configuration Request"), 0x80); identifier = l2capinbuf[9]; l2cap_event_flag |= L2CAP_FLAG_CONFIG_CONTROL_REQUEST; } else if (l2capinbuf[12] == interrupt_dcid[0] && l2capinbuf[13] == interrupt_dcid[1]) { - //Serial.print("\r\nHID Interrupt Configuration Request"); + //Notify(PSTR("\r\nHID Interrupt Configuration Request"), 0x80); identifier = l2capinbuf[9]; l2cap_event_flag |= L2CAP_FLAG_CONFIG_INTERRUPT_REQUEST; } @@ -346,11 +346,11 @@ void PS3BT::ACLData(uint8_t* ACLData) { } } else if (l2capinbuf[8] == L2CAP_CMD_DISCONNECT_RESPONSE) { if (l2capinbuf[12] == control_scid[0] && l2capinbuf[13] == control_scid[1]) { - //Serial.print("\r\nDisconnect Response: Control Channel"); + //Notify(PSTR("\r\nDisconnect Response: Control Channel"), 0x80); identifier = l2capinbuf[9]; l2cap_event_flag |= L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE; } else if (l2capinbuf[12] == interrupt_scid[0] && l2capinbuf[13] == interrupt_scid[1]) { - //Serial.print("\r\nDisconnect Response: Interrupt Channel"); + //Notify(PSTR("\r\nDisconnect Response: Interrupt Channel"), 0x80); identifier = l2capinbuf[9]; l2cap_event_flag |= L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE; } @@ -362,7 +362,7 @@ void PS3BT::ACLData(uint8_t* ACLData) { } #endif } else if (l2capinbuf[6] == interrupt_dcid[0] && l2capinbuf[7] == interrupt_dcid[1]) { // l2cap_interrupt - //Serial.print("\r\nL2CAP Interrupt"); + //Notify(PSTR("\r\nL2CAP Interrupt"), 0x80); if (PS3Connected || PS3MoveConnected || PS3NavigationConnected) { /* Read Report */ if (l2capinbuf[8] == 0xA1) { // HID_THDR_DATA_INPUT @@ -382,9 +382,9 @@ void PS3BT::ACLData(uint8_t* ACLData) { #ifdef PRINTREPORT // Uncomment "#define PRINTREPORT" to print the report send by the PS3 Controllers for (uint8_t i = 10; i < 58; i++) { PrintHex (l2capinbuf[i], 0x80); - Serial.print(" "); + Notify(PSTR(" "), 0x80); } - Serial.println(); + Notify(PSTR("\r\n"), 0x80); #endif } } diff --git a/PS3USB.cpp b/PS3USB.cpp index 4ac21b60..a14c14c3 100644 --- a/PS3USB.cpp +++ b/PS3USB.cpp @@ -209,7 +209,7 @@ uint8_t PS3USB::Init(uint8_t parent, uint8_t port, bool lowspeed) { Notify(PSTR("\r\nBluetooth Address was set to: "), 0x80); for (int8_t i = 5; i > 0; i--) { PrintHex (my_bdaddr[i], 0x80); - Serial.print(":"); + Notify(PSTR(":"), 0x80); } PrintHex (my_bdaddr[0], 0x80); #endif @@ -299,9 +299,9 @@ void PS3USB::printReport() { //Uncomment "#define PRINTREPORT" to print the repo return; for (uint8_t i = 0; i < PS3_REPORT_BUFFER_SIZE; i++) { PrintHex (readBuf[i], 0x80); - Serial.print(" "); + Notify(PSTR(" "), 0x80); } - Serial.println(); + Notify(PSTR("\r\n"), 0x80); #endif } diff --git a/SPP.cpp b/SPP.cpp index 8845ea22..80b5a8f6 100644 --- a/SPP.cpp +++ b/SPP.cpp @@ -139,20 +139,20 @@ void SPP::ACLData(uint8_t* l2capinbuf) { } else if (l2capinbuf[8] == L2CAP_CMD_CONFIG_RESPONSE) { if ((l2capinbuf[16] | (l2capinbuf[17] << 8)) == 0x0000) { // Success if (l2capinbuf[12] == sdp_dcid[0] && l2capinbuf[13] == sdp_dcid[1]) { - //Serial.print("\r\nSDP Configuration Complete"); + //Notify(PSTR("\r\nSDP Configuration Complete"), 0x80); l2cap_event_flag |= L2CAP_FLAG_CONFIG_SDP_SUCCESS; } else if (l2capinbuf[12] == rfcomm_dcid[0] && l2capinbuf[13] == rfcomm_dcid[1]) { - //Serial.print("\r\nRFCOMM Configuration Complete"); + //Notify(PSTR("\r\nRFCOMM Configuration Complete"), 0x80); l2cap_event_flag |= L2CAP_FLAG_CONFIG_RFCOMM_SUCCESS; } } } else if (l2capinbuf[8] == L2CAP_CMD_CONFIG_REQUEST) { if (l2capinbuf[12] == sdp_dcid[0] && l2capinbuf[13] == sdp_dcid[1]) { - //Serial.print("\r\nSDP Configuration Request"); + //Notify(PSTR("\r\nSDP Configuration Request"), 0x80); identifier = l2capinbuf[9]; l2cap_event_flag |= L2CAP_FLAG_CONFIG_SDP_REQUEST; } else if (l2capinbuf[12] == rfcomm_dcid[0] && l2capinbuf[13] == rfcomm_dcid[1]) { - //Serial.print("\r\nRFCOMM Configuration Request"); + //Notify(PSTR("\r\nRFCOMM Configuration Request"), 0x80); identifier = l2capinbuf[9]; l2cap_event_flag |= L2CAP_FLAG_CONFIG_RFCOMM_REQUEST; } @@ -168,11 +168,11 @@ void SPP::ACLData(uint8_t* l2capinbuf) { } } else if (l2capinbuf[8] == L2CAP_CMD_DISCONNECT_RESPONSE) { if (l2capinbuf[12] == sdp_scid[0] && l2capinbuf[13] == sdp_scid[1]) { - //Serial.print("\r\nDisconnect Response: SDP Channel"); + //Notify(PSTR("\r\nDisconnect Response: SDP Channel"), 0x80); identifier = l2capinbuf[9]; l2cap_event_flag |= L2CAP_FLAG_DISCONNECT_RESPONSE; } else if (l2capinbuf[12] == rfcomm_scid[0] && l2capinbuf[13] == rfcomm_scid[1]) { - //Serial.print("\r\nDisconnect Response: RFCOMM Channel"); + //Notify(PSTR("\r\nDisconnect Response: RFCOMM Channel"), 0x80); identifier = l2capinbuf[9]; l2cap_event_flag |= L2CAP_FLAG_DISCONNECT_RESPONSE; } @@ -240,20 +240,20 @@ void SPP::ACLData(uint8_t* l2capinbuf) { #ifdef EXTRADEBUG Notify(PSTR("\r\nRFCOMM Channel: "), 0x80); - Serial.print(rfcommChannel >> 3, HEX); + PrintHex (rfcommChannel >> 3, 0x80); Notify(PSTR(" Direction: "), 0x80); - Serial.print(rfcommDirection >> 2, HEX); + PrintHex (rfcommDirection >> 2, 0x80); Notify(PSTR(" CommandResponse: "), 0x80); - Serial.print(rfcommCommandResponse >> 1, HEX); + PrintHex (rfcommCommandResponse >> 1, 0x80); Notify(PSTR(" ChannelType: "), 0x80); - Serial.print(rfcommChannelType, HEX); + PrintHex (rfcommChannelType, 0x80); Notify(PSTR(" PF_BIT: "), 0x80); - Serial.print(rfcommPfBit, HEX); + PrintHex (rfcommPfBit, 0x80); #endif if (rfcommChannelType == RFCOMM_DISC) { #ifdef DEBUG Notify(PSTR("\r\nReceived Disconnect RFCOMM Command on channel: "), 0x80); - Serial.print(rfcommChannel >> 3, HEX); + PrintHex (rfcommChannel >> 3, 0x80); #endif connected = false; sendRfcomm(rfcommChannel, rfcommDirection, rfcommCommandResponse, RFCOMM_UA, rfcommPfBit, rfcommbuf, 0x00); // UA Command @@ -270,15 +270,15 @@ void SPP::ACLData(uint8_t* l2capinbuf) { } #ifdef EXTRADEBUG Notify(PSTR("\r\nRFCOMM Data Available: "), 0x80); - Serial.print(rfcommAvailable); + Notify(rfcommAvailable, 0x80); if (offset) { Notify(PSTR(" - Credit: 0x"), 0x80); - Serial.print(l2capinbuf[11], HEX); + PrintHex (l2capinbuf[11], 0x80); } #endif #ifdef PRINTREPORT // Uncomment "#define PRINTREPORT" to print the report send to the Arduino via Bluetooth for (uint8_t i = 0; i < length; i++) - Serial.write(l2capinbuf[i + 11 + offset]); + Notifyc(l2capinbuf[i + 11 + offset], 0x80); #endif } else if (rfcommChannelType == RFCOMM_UIH && l2capinbuf[11] == BT_RFCOMM_RPN_CMD) { // UIH Remote Port Negotiation Command #ifdef DEBUG @@ -688,7 +688,7 @@ void SPP::sendRfcomm(uint8_t channel, uint8_t direction, uint8_t CR, uint8_t cha #ifdef EXTRADEBUG Notify(PSTR(" - RFCOMM Data: "), 0x80); for (i = 0; i < length + 4; i++) { - Serial.print(l2capoutbuf[i], HEX); + PrintHex (l2capoutbuf[i], 0x80); Notify(PSTR(" "), 0x80); } #endif @@ -704,7 +704,7 @@ void SPP::sendRfcommCredit(uint8_t channel, uint8_t direction, uint8_t CR, uint8 #ifdef EXTRADEBUG Notify(PSTR(" - RFCOMM Credit Data: "), 0x80); for (uint8_t i = 0; i < 5; i++) { - Serial.print(l2capoutbuf[i], HEX); + PrintHex (l2capoutbuf[i], 0x80); Notify(PSTR(" "), 0x80); } #endif @@ -929,7 +929,7 @@ uint8_t SPP::read() { sendRfcommCredit(rfcommChannelConnection, rfcommDirection, 0, RFCOMM_UIH, 0x10, sizeof (rfcommDataBuffer)); // Send more credit #ifdef EXTRADEBUG Notify(PSTR("\r\nSent "), 0x80); - Serial.print(sizeof (rfcommDataBuffer)); + Notify((uint8_t)sizeof (rfcommDataBuffer), 0x80); Notify(PSTR(" more credit"), 0x80); #endif } diff --git a/Wii.cpp b/Wii.cpp index 01571d8e..15bc58ed 100755 --- a/Wii.cpp +++ b/Wii.cpp @@ -161,13 +161,13 @@ void WII::ACLData(uint8_t* l2capinbuf) { } else if (l2capinbuf[8] == L2CAP_CMD_CONNECTION_RESPONSE) { if (((l2capinbuf[16] | (l2capinbuf[17] << 8)) == 0x0000) && ((l2capinbuf[18] | (l2capinbuf[19] << 8)) == SUCCESSFUL)) { // Success if (l2capinbuf[14] == control_dcid[0] && l2capinbuf[15] == control_dcid[1]) { // Success - //Serial.print("\r\nHID Control Connection Complete"); + //Notify(PSTR("\r\nHID Control Connection Complete"), 0x80); identifier = l2capinbuf[9]; control_scid[0] = l2capinbuf[12]; control_scid[1] = l2capinbuf[13]; l2cap_event_flag |= L2CAP_FLAG_CONTROL_CONNECTED; } else if (l2capinbuf[14] == interrupt_dcid[0] && l2capinbuf[15] == interrupt_dcid[1]) { - //Serial.print("\r\nHID Interrupt Connection Complete"); + //Notify(PSTR("\r\nHID Interrupt Connection Complete"), 0x80); identifier = l2capinbuf[9]; interrupt_scid[0] = l2capinbuf[12]; interrupt_scid[1] = l2capinbuf[13]; @@ -201,21 +201,21 @@ void WII::ACLData(uint8_t* l2capinbuf) { } else if (l2capinbuf[8] == L2CAP_CMD_CONFIG_RESPONSE) { if ((l2capinbuf[16] | (l2capinbuf[17] << 8)) == 0x0000) { // Success if (l2capinbuf[12] == control_dcid[0] && l2capinbuf[13] == control_dcid[1]) { - //Serial.print("\r\nHID Control Configuration Complete"); + //Notify(PSTR("\r\nHID Control Configuration Complete"), 0x80); identifier = l2capinbuf[9]; l2cap_event_flag |= L2CAP_FLAG_CONFIG_CONTROL_SUCCESS; } else if (l2capinbuf[12] == interrupt_dcid[0] && l2capinbuf[13] == interrupt_dcid[1]) { - //Serial.print("\r\nHID Interrupt Configuration Complete"); + //Notify(PSTR("\r\nHID Interrupt Configuration Complete"), 0x80); identifier = l2capinbuf[9]; l2cap_event_flag |= L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS; } } } else if (l2capinbuf[8] == L2CAP_CMD_CONFIG_REQUEST) { if (l2capinbuf[12] == control_dcid[0] && l2capinbuf[13] == control_dcid[1]) { - //Serial.print("\r\nHID Control Configuration Request"); + //Notify(PSTR("\r\nHID Control Configuration Request"), 0x80); pBtd->l2cap_config_response(hci_handle, l2capinbuf[9], control_scid); } else if (l2capinbuf[12] == interrupt_dcid[0] && l2capinbuf[13] == interrupt_dcid[1]) { - //Serial.print("\r\nHID Interrupt Configuration Request"); + //Notify(PSTR("\r\nHID Interrupt Configuration Request"), 0x80); pBtd->l2cap_config_response(hci_handle, l2capinbuf[9], interrupt_scid); } } else if (l2capinbuf[8] == L2CAP_CMD_DISCONNECT_REQUEST) { @@ -236,11 +236,11 @@ void WII::ACLData(uint8_t* l2capinbuf) { } } else if (l2capinbuf[8] == L2CAP_CMD_DISCONNECT_RESPONSE) { if (l2capinbuf[12] == control_scid[0] && l2capinbuf[13] == control_scid[1]) { - //Serial.print("\r\nDisconnect Response: Control Channel"); + //Notify(PSTR("\r\nDisconnect Response: Control Channel"), 0x80); identifier = l2capinbuf[9]; l2cap_event_flag |= L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE; } else if (l2capinbuf[12] == interrupt_scid[0] && l2capinbuf[13] == interrupt_scid[1]) { - //Serial.print("\r\nDisconnect Response: Interrupt Channel"); + //Notify(PSTR("\r\nDisconnect Response: Interrupt Channel"), 0x80); identifier = l2capinbuf[9]; l2cap_event_flag |= L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE; } @@ -253,7 +253,7 @@ void WII::ACLData(uint8_t* l2capinbuf) { } #endif } else if (l2capinbuf[6] == interrupt_dcid[0] && l2capinbuf[7] == interrupt_dcid[1]) { // l2cap_interrupt - //Serial.print("\r\nL2CAP Interrupt"); + //Notify(PSTR("\r\nL2CAP Interrupt"), 0x80); if (wiimoteConnected) { if (l2capinbuf[8] == 0xA1) { // HID_THDR_DATA_INPUT if ((l2capinbuf[9] >= 0x20 && l2capinbuf[9] <= 0x22) || (l2capinbuf[9] >= 0x30 && l2capinbuf[9] <= 0x37) || l2capinbuf[9] == 0x3e || l2capinbuf[9] == 0x3f) { // These reports include the buttons @@ -481,19 +481,19 @@ void WII::ACLData(uint8_t* l2capinbuf) { timer = micros(); /* // Uncomment these lines to tune the gyro scale variabels - Serial.print("\r\ngyroYaw: "); - Serial.print(gyroYaw); - Serial.print("\tgyroRoll: "); - Serial.print(gyroRoll); - Serial.print("\tgyroPitch: "); - Serial.print(gyroPitch); - */ + Notify(PSTR("\r\ngyroYaw: "), 0x80); + Notify(gyroYaw, 0x80); + Notify(PSTR("\tgyroRoll: "), 0x80); + Notify(gyroRoll, 0x80); + Notify(PSTR("\tgyroPitch: "), 0x80); + Notify(gyroPitch, 0x80); + */ /* - Serial.print("\twiimoteRoll: "); - Serial.print(wiimoteRoll); - Serial.print("\twiimotePitch: "); - Serial.print(wiimotePitch); - */ + Notify(PSTR("\twiimoteRoll: "), 0x80); + Notify(wiimoteRoll, 0x80); + Notify(PSTR("\twiimotePitch: "), 0x80); + Notify(wiimotePitch, 0x80); + */ } else { if ((micros() - timer) > 1000000) { // Loop for 1 sec before resetting the values #ifdef DEBUG @@ -567,7 +567,7 @@ void WII::ACLData(uint8_t* l2capinbuf) { #ifdef DEBUG default: Notify(PSTR("\r\nUnknown Report type: "), 0x80); - Serial.print(l2capinbuf[9], HEX); + PrintHex (l2capinbuf[9], 0x80); break; #endif } diff --git a/XBOXRECV.cpp b/XBOXRECV.cpp index 7686c184..9231ef88 100644 --- a/XBOXRECV.cpp +++ b/XBOXRECV.cpp @@ -278,7 +278,7 @@ uint8_t XBOXRECV::Poll() { if (bufferSize > 0) { // The number of received bytes #ifdef EXTRADEBUG Notify(PSTR("Bytes Received: "), 0x80); - Serial.print(bufferSize); + PrintHex (bufferSize, 0x80); Notify(PSTR("\r\n"), 0x80); #endif readReport(i); @@ -298,7 +298,7 @@ void XBOXRECV::readReport(uint8_t controller) { Xbox360Connected[controller] = readBuf[1]; #ifdef DEBUG Notify(PSTR("Controller "), 0x80); - Serial.print(controller); + Notify(controller, 0x80); #endif if (Xbox360Connected[controller]) { #ifdef DEBUG @@ -371,13 +371,13 @@ void XBOXRECV::printReport(uint8_t controller, uint8_t nBytes) { //Uncomment "#d if (readBuf == NULL) return; Notify(PSTR("Controller "), 0x80); - Serial.print(controller); + Notify(controller, 0x80); Notify(PSTR(": "), 0x80); for (uint8_t i = 0; i < nBytes; i++) { PrintHex (readBuf[i], 0x80); - Serial.print(" "); + Notify(PSTR(" "), 0x80); } - Serial.println(); + Notify(PSTR("\r\n"), 0x80); #endif } diff --git a/XBOXUSB.cpp b/XBOXUSB.cpp index 33f9bc5b..5c14844a 100644 --- a/XBOXUSB.cpp +++ b/XBOXUSB.cpp @@ -260,9 +260,9 @@ void XBOXUSB::printReport() { //Uncomment "#define PRINTREPORT" to print the rep return; for (uint8_t i = 0; i < XBOX_REPORT_BUFFER_SIZE; i++) { PrintHex (readBuf[i], 0x80); - Serial.print(" "); + Notify(PSTR(" "), 0x80); } - Serial.println(); + Notify(PSTR("\r\n"), 0x80); #endif } diff --git a/message.cpp b/message.cpp index 9a5395ea..ecbc0d40 100644 --- a/message.cpp +++ b/message.cpp @@ -48,6 +48,22 @@ void NotifyStr(char const * msg, int lvl) { while (c = *msg++) Notifyc(c, lvl); } +void Notify(uint8_t b, int lvl) { + if (UsbDEBUGlvl < lvl) return; +#if defined(ARDUINO) && ARDUINO >=100 + Serial.print(b); +#else + Serial.print(b, DEC); +#endif + Serial.flush(); +} + +void Notify(double d, int lvl) { + if (UsbDEBUGlvl < lvl) return; + Serial.print(d); + Serial.flush(); +} + void NotifyFailGetDevDescr(void) { Notify(PSTR("\r\ngetDevDescr"), 0x80); } diff --git a/message.h b/message.h index 02a503ac..8d62513e 100644 --- a/message.h +++ b/message.h @@ -24,6 +24,8 @@ extern int UsbDEBUGlvl; #include "printhex.h" +void Notify(uint8_t b, int lvl); +void Notify(double d, int lvl); void Notify(char const * msg, int lvl); void NotifyStr(char const * msg, int lvl); #ifdef DEBUG From 554487c8491bf82c4840677dd44767b390c708ea Mon Sep 17 00:00:00 2001 From: Oleg Mazurov Date: Tue, 30 Apr 2013 14:06:36 -0600 Subject: [PATCH 09/21] Leonardo Compatibility patch by weizenspreu --- examples/HID/USBHIDBootKbd/USBHIDBootKbd.ino | 4 ++-- examples/HID/USBHIDBootMouse/USBHIDBootMouse.pde | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/HID/USBHIDBootKbd/USBHIDBootKbd.ino b/examples/HID/USBHIDBootKbd/USBHIDBootKbd.ino index 71e7430a..8421f6ee 100644 --- a/examples/HID/USBHIDBootKbd/USBHIDBootKbd.ino +++ b/examples/HID/USBHIDBootKbd/USBHIDBootKbd.ino @@ -68,7 +68,7 @@ void KbdRptParser::OnKeyPressed(uint8_t key) USB Usb; //USBHub Hub(&Usb); -HIDBoot Keyboard(&Usb); +HIDBoot HidKeyboard(&Usb); uint32_t next_time; @@ -86,7 +86,7 @@ void setup() next_time = millis() + 5000; - Keyboard.SetReportParser(0, (HIDReportParser*)&Prs); + HidKeyboard.SetReportParser(0, (HIDReportParser*)&Prs); } void loop() diff --git a/examples/HID/USBHIDBootMouse/USBHIDBootMouse.pde b/examples/HID/USBHIDBootMouse/USBHIDBootMouse.pde index eb3500f4..2227a29b 100644 --- a/examples/HID/USBHIDBootMouse/USBHIDBootMouse.pde +++ b/examples/HID/USBHIDBootMouse/USBHIDBootMouse.pde @@ -58,7 +58,7 @@ void MouseRptParser::OnMiddleButtonDown (MOUSEINFO *mi) USB Usb; USBHub Hub(&Usb); -HIDBoot Mouse(&Usb); +HIDBoot HidMouse(&Usb); uint32_t next_time; @@ -76,7 +76,7 @@ void setup() next_time = millis() + 5000; - Mouse.SetReportParser(0,(HIDReportParser*)&Prs); + HidMouse.SetReportParser(0,(HIDReportParser*)&Prs); } void loop() From b3bd3ee059572e737d830236b43e5be7ea1a0c3d Mon Sep 17 00:00:00 2001 From: Kenneth Newwood Date: Thu, 2 May 2013 13:34:29 +0200 Subject: [PATCH 10/21] Make KeyboardReportParser::handleLockingKeys() virtual to override keyboard LED handling. --- hidboot.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hidboot.h b/hidboot.h index 6e1fa4a6..e68da991 100644 --- a/hidboot.h +++ b/hidboot.h @@ -159,7 +159,7 @@ public: virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf); protected: - uint8_t HandleLockingKeys(HID* hid, uint8_t key); + virtual uint8_t HandleLockingKeys(HID* hid, uint8_t key); virtual void OnKeyDown(uint8_t mod, uint8_t key) { }; From 305eeb08205e9ec8b8d366124cb61216b6cd0a89 Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Mon, 6 May 2013 23:32:00 +0200 Subject: [PATCH 11/21] Added pair function --- Wii.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Wii.h b/Wii.h index c35b31ac..753947ba 100755 --- a/Wii.h +++ b/Wii.h @@ -131,6 +131,11 @@ public: /**@}*/ /** @name Wii Controller functions */ + /** Call this to start the paring sequence with a controller */ + void pair(void) { + if(pBtd) + pBtd->pairWithWiimote(); + } /** * Used to read the joystick of the Nunchuck. * @param a Either ::HatX or ::HatY. From e6a557870bb20d6d30cbde2d12c7537d0fb0c97a Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Tue, 7 May 2013 00:06:49 +0200 Subject: [PATCH 12/21] The SPP Class now inherit from the Arduino Stream library --- SPP.cpp | 200 ++++-------------- SPP.h | 246 ++++------------------- examples/Bluetooth/PS3SPP/PS3SPP.ino | 6 +- examples/Bluetooth/SPP/SPP.ino | 4 +- examples/Bluetooth/SPPMulti/SPPMulti.ino | 6 +- 5 files changed, 83 insertions(+), 379 deletions(-) diff --git a/SPP.cpp b/SPP.cpp index 80b5a8f6..a3b4401d 100644 --- a/SPP.cpp +++ b/SPP.cpp @@ -67,6 +67,7 @@ void SPP::Reset() { l2cap_sdp_state = L2CAP_SDP_WAIT; l2cap_rfcomm_state = L2CAP_RFCOMM_WAIT; l2cap_event_flag = 0; + sppIndex = 0; } void SPP::disconnect() { @@ -381,6 +382,7 @@ void SPP::ACLData(uint8_t* l2capinbuf) { waitForLastCommand = false; creditSent = false; connected = true; // The RFCOMM channel is now established + sppIndex = 0; } #ifdef DEBUG else if (rfcommChannelType != RFCOMM_DISC) { @@ -413,7 +415,9 @@ void SPP::Run() { creditSent = false; waitForLastCommand = false; connected = true; // The RFCOMM channel is now established + sppIndex = 0; } + send(); // Send all bytes currently in the buffer } void SPP::SDP_task() { @@ -725,22 +729,21 @@ uint8_t SPP::calcFcs(uint8_t *data) { } /* Serial commands */ -void SPP::print(const String &str) { - uint8_t length = str.length(); // Get the length of the string - uint8_t buf[length]; - - for(uint8_t i = 0; i < length; i++) - buf[i] = str[i]; - - print(buf,length); +size_t SPP::write(uint8_t data) { + return write(&data,1); } -void SPP::print(const char* str) { - print((uint8_t*) str, strlen(str)); +size_t SPP::write(const uint8_t* data, size_t size) { + for(uint8_t i = 0; i < size; i++) { + if(sppIndex >= sizeof(sppOutputBuffer)/sizeof(sppOutputBuffer[0])) + return i; // Don't send any more if buffer is already full + sppOutputBuffer[sppIndex++] = data[i]; // All the bytes are put into a buffer and then send using the send() function + } + return size; } -void SPP::print(uint8_t* array, uint8_t stringLength) { - if (!connected) +void SPP::send() { + if (!connected || !sppIndex) return; uint8_t length; // This is the length of the string we are sending uint8_t offset = 0; // This is used to keep track of where we are in the string @@ -748,177 +751,42 @@ void SPP::print(uint8_t* array, uint8_t stringLength) { l2capoutbuf[0] = rfcommChannelConnection | 0 | 0 | extendAddress; // RFCOMM Address l2capoutbuf[1] = RFCOMM_UIH; // RFCOMM Control - while (stringLength) { // We will run this while loop until this variable is 0 - if (stringLength > (sizeof (l2capoutbuf) - 4)) // Check if the string is larger that the outgoing buffer + while (sppIndex) { // We will run this while loop until this variable is 0 + if (sppIndex > (sizeof (l2capoutbuf) - 4)) // Check if the string is larger than the outgoing buffer length = sizeof (l2capoutbuf) - 4; else - length = stringLength; + length = sppIndex; l2capoutbuf[2] = length << 1 | 1; // Length uint8_t i = 0; for (; i < length; i++) - l2capoutbuf[i + 3] = array[i + offset]; + l2capoutbuf[i + 3] = sppOutputBuffer[i + offset]; l2capoutbuf[i + 3] = calcFcs(l2capoutbuf); // Calculate checksum RFCOMM_Command(l2capoutbuf, length + 4); - stringLength -= length; + sppIndex -= length; offset += length; // Increment the offset } } -void SPP::println(const String &str) { - String output = str + "\r\n"; - print(output); +int SPP::available(void) { + return rfcommAvailable; +}; + +void SPP::flush(void) { + rfcommAvailable = 0; } -void SPP::println(const char* str) { - char output[strlen(str) + 3]; - strcpy(output, str); - strcat(output, "\r\n"); - print(output); -} - -void SPP::println(uint8_t data) { - uint8_t buf[3] = {data, '\r', '\n'}; - print(buf, 3); -} - -void SPP::println(uint8_t* array, uint8_t length) { - uint8_t buf[length + 2]; - memcpy(buf, array, length); - buf[length] = '\r'; - buf[length + 1] = '\n'; - print(buf, length + 2); -} - -void SPP::printFlashString(const __FlashStringHelper *ifsh, bool newline) { - const char PROGMEM *p = (const char PROGMEM *)ifsh; - uint8_t size = 0; - while (1) { // Calculate the size of the string - uint8_t c = pgm_read_byte(p + size); - if (c == 0) - break; - size++; - } - uint8_t buf[size + 2]; // Add two extra in case it needs to print a newline and carriage return - - for (uint8_t i = 0; i < size; i++) - buf[i] = pgm_read_byte(p++); - - if (newline) { - buf[size] = '\r'; - buf[size + 1] = '\n'; - print(buf, size + 2); - } else - print(buf, size); -} - -void SPP::println(void) { - uint8_t buf[2] = {'\r', '\n'}; - print(buf, 2); -} - -/* These must be used to print numbers */ -void SPP::printNumber(uint32_t n) { - char output[11]; - intToString(n, output); - print(output); -} - -void SPP::printNumberln(uint32_t n) { - char output[13]; - intToString(n, output); - strcat(output, "\r\n"); - print(output); -} - -void SPP::printNumber(int32_t n) { - char output[12]; - intToString(n, output); - print(output); -} - -void SPP::printNumberln(int32_t n) { - char output[14]; - intToString(n, output); - strcat(output, "\r\n"); - print(output); -} - -void SPP::intToString(int32_t input, char* output) { - if (input < 0) { - char buf[11]; - intToString((uint32_t)(input*-1), buf); - strcpy(output, "-"); - strcat(output, buf); - } else - intToString((uint32_t)input, output); -} - -void SPP::intToString(uint32_t input, char* output) { - uint32_t temp = input; - uint8_t digits = 0; - while (temp) { - temp /= 10; - digits++; - } - if (digits == 0) - strcpy(output, "0"); - else { - for (uint8_t i = 1; i <= digits; i++) { - output[digits - i] = input % 10 + '0'; // Get number and convert to ASCII Character - input /= 10; - } - output[digits] = '\0'; // Add null character - } -} - -void SPP::printNumber(double n, uint8_t digits) { - char output[13 + digits]; - doubleToString(n, output, digits); - print(output); -} - -void SPP::printNumberln(double n, uint8_t digits) { - char output[15 + digits]; - doubleToString(n, output, digits); - strcat(output, "\r\n"); - print(output); -} - -void SPP::doubleToString(double input, char* output, uint8_t digits) { - char buffer[13 + digits]; - if (input < 0) { - strcpy(output, "-"); - input = -input; - } else - strcpy(output, ""); - - // Round correctly - double rounding = 0.5; - for (uint8_t i = 0; i < digits; i++) - rounding /= 10.0; - input += rounding; - - uint32_t intpart = (uint32_t)input; - intToString(intpart, buffer); // Convert to string - strcat(output, buffer); - strcat(output, "."); - double fractpart = (input - (double)intpart); - fractpart *= pow(10, digits); - for (uint8_t i = 1; i < digits; i++) { // Put zeros in front of number - if (fractpart < pow(10, digits - i)) { - strcat(output, "0"); - } - } - intToString((uint32_t)fractpart, buffer); // Convert to string - strcat(output, buffer); -} - -uint8_t SPP::read() { +int SPP::peek(void) { if (rfcommAvailable == 0) // Don't read if there is nothing in the buffer - return 0; + return -1; + return rfcommDataBuffer[0]; +} + +int SPP::read(void) { + if (rfcommAvailable == 0) // Don't read if there is nothing in the buffer + return -1; uint8_t output = rfcommDataBuffer[0]; for (uint8_t i = 1; i < rfcommAvailable; i++) rfcommDataBuffer[i - 1] = rfcommDataBuffer[i]; // Shift the buffer one left diff --git a/SPP.h b/SPP.h index 92cb51b0..20ab8f0a 100644 --- a/SPP.h +++ b/SPP.h @@ -90,7 +90,7 @@ */ /** This BluetoothService class implements the Serial Port Protocol (SPP). */ -class SPP : public BluetoothService { +class SPP : public BluetoothService, public Stream { public: /** * Constructor for the SPP class. @@ -118,217 +118,51 @@ public: bool connected; /** @name Serial port profile (SPP) Print functions */ - /** - * Used to send Arduino String data type. - * @param str String to send. - */ - void print(const String &str); - /** - * Same as print(const String &str), but will include newline and carriage return. - * @param str String to send. - */ - void println(const String &str); - - /** - * Used to send standard strings. - * @param str String to send. - */ - void print(const char* str); - /** - * Same as print(const char* str), but will include newline and carriage return. - * @param str String to send. - */ - void println(const char* str); - - /** - * Used to send single bytes. - * @param data Data to send. - */ - void print(uint8_t data) { - print(&data, 1); - }; - /** - * Same as print(uint8_t data), but will include newline and carriage return. - * @param data Data to send. - */ - void println(uint8_t data); - - /** - * Used to send arrays. - * @param array Array to send. - * @param length Number of bytes to send. - */ - void print(uint8_t* array, uint8_t length); - /** - * Same as print(uint8_t* array, uint8_t length), but will include newline and carriage return. - * @param array Array to send. - * @param length Number of bytes to send. - */ - void println(uint8_t* array, uint8_t length); - - /** - * Used to print strings stored in flash. - * Use "SerialBT.print(F("String"));" to print a string stored in flash. - * @param ifsh String to send - see: http://playground.arduino.cc/Learning/Memory. - */ - void print(const __FlashStringHelper *ifsh) { - printFlashString(ifsh, false); - }; - - /** - * Same as print(const __FlashStringHelper *ifsh), but will include newline and carriage return. - * @param ifsh String to send - see: http://playground.arduino.cc/Learning/Memory. - */ - void println(const __FlashStringHelper *ifsh) { - printFlashString(ifsh, true); - }; - /** - * Helper function to print a string stored in flash. - * @param ifsh String stored in flash you want to print. - * @param newline Set this to true to include newline and carriage return. - */ - void printFlashString(const __FlashStringHelper *ifsh, bool newline); - - - /** Use this to print newline and carriage return. */ - void println(void); - - /** - * Used to print unsigned integers. - * @param n Unsigned integer to send. - */ - void printNumber(uint8_t n) { - printNumber((uint32_t) n); - }; - - /** - * Same as printNumber(uint8_t n), but will include newline and carriage return. - * @param n Unsigned integer to send. - */ - void printNumberln(uint8_t n) { - printNumberln((uint32_t) n); - }; - - /** - * Used to print signed integers. - * @param n Signed integer to send. - */ - void printNumber(int8_t n) { - printNumber((int32_t) n); - }; - - /** - * Same as printNumber(int8_t n), but will include newline and carriage return. - * @param n Signed integer to send. - */ - void printNumberln(int8_t n) { - printNumberln((int32_t) n); - }; - - /** - * Used to print unsigned integers. - * @param n Unsigned integer to send. - */ - void printNumber(uint16_t n) { - printNumber((uint32_t) n); - }; - - /** - * Same as printNumber(uint16_t n), but will include newline and carriage return. - * @param n Unsigned integer to send. - */ - void printNumberln(uint16_t n) { - printNumberln((uint32_t) n); - }; - - /** - * Used to print signed integers. - * @param n Signed integer to send. - */ - void printNumber(int16_t n) { - printNumber((int32_t) n); - }; - - /** - * Same as printNumber(int16_t n), but will include newline and carriage return. - * @param n Signed integer to send. - */ - void printNumberln(int16_t n) { - printNumberln((int32_t) n); - }; - - /** - * Used to print unsigned integers. - * @param n Unsigned integer to send. - */ - void printNumber(uint32_t n); - /** - * Same as printNumber(uint32_t n), but will include newline and carriage return. - * @param n Unsigned integer to send. - */ - void printNumberln(uint32_t n); - - /** - * Used to print signed integers. - * @param n Signed integer to send. - */ - void printNumber(int32_t n); - /** - * Same as printNumber(int32_t n), but will include newline and carriage return. - * @param n Signed integer to send. - */ - void printNumberln(int32_t n); - - /** - * Helper function to convert from an unsigned integer to a string. - * @param input Unsigned integer to convert. - * @param output Output buffer. - */ - void intToString(int32_t input, char* output); - /** - * Helper function to convert from a signed integer to a string. - * @param input Signed integer to convert. - * @param output Output buffer. - */ - void intToString(uint32_t input, char* output); - - /** - * Used to print floating-point numbers. - * @param n Floating-point number to print. - * @param digits Number of digits to send. If argument is omitted, then 2 digits will be used. - */ - void printNumber(double n, uint8_t digits = 2); - /** - * Same as printNumber(double n, uint8_t digits), but will include newline and carriage return. - * @param n Floating-point number to print. - * @param digits Number of digits to send. If argument is omitted, then 2 digits will be used. - */ - void printNumberln(double n, uint8_t digits = 2); - /** - * Helper function to convert from a double to a string. - * @param input Floating-point number to convert. - * @param output Output buffer. - * @param digits Number of digits to convert. If argument is omitted, then 2 digits will be used. - */ - void doubleToString(double input, char* output, uint8_t digits = 2); - /** * Get number of bytes waiting to be read. * @return Return the number of bytes ready to be read. */ - uint8_t available() { - return rfcommAvailable; - }; + virtual int available(void); + /** Discard all the bytes in the buffer. */ + virtual void flush(void); + /** + * Used to read the next value in the buffer without advancing to the next one. + * @return Return the byte. Will return -1 if no byte is available. + */ + virtual int peek(void); /** * Used to read the buffer. - * @return Return the byte. Will return 0 if no byte is available. + * @return Return the byte. Will return -1 if no byte is available. */ - uint8_t read(); - - /** Discard all the bytes in the buffer. */ - void flush() { - rfcommAvailable = 0; - }; + virtual int read(void); + /** + * Writes the byte to send to a buffer. The message is send when either send(const uint8_t* data, size_t size) or after Usb.Task() is called. + * @param data The byte to write. + * @return Return the number of bytes written. + */ + virtual size_t write(uint8_t data); + /** + * Writes the bytes to send to a buffer. The message is send when either send(const uint8_t* data, size_t size) or after Usb.Task() is called. + * @param data The data array to send. + * @param size Size of the data. + * @return Return the number of bytes written. + */ + virtual size_t write(const uint8_t* data, size_t size); + /** Pull in write(const char *str) from Print */ + using Print::write; + /** + * This will send all the bytes in the buffer. + * This is called whenever Usb.Task() is called, + * but can also be called via this function. + */ + void send(void); + /** + * Used to provide Boolean tests for the class. + * @return Return true if SPP communication is connected. + */ + operator bool() { + return connected; + } /**@}*/ private: @@ -369,6 +203,8 @@ private: bool creditSent; uint8_t rfcommDataBuffer[100]; // Create a 100 sized buffer for incoming data + uint8_t sppOutputBuffer[100]; // Create a 100 sized buffer for outgoing SPP data + uint8_t sppIndex; uint8_t rfcommAvailable; bool firstMessage; // Used to see if it's the first SDP request received diff --git a/examples/Bluetooth/PS3SPP/PS3SPP.ino b/examples/Bluetooth/PS3SPP/PS3SPP.ino index d07b51dd..098b4278 100644 --- a/examples/Bluetooth/PS3SPP/PS3SPP.ino +++ b/examples/Bluetooth/PS3SPP/PS3SPP.ino @@ -21,7 +21,7 @@ PS3BT PS3(&Btd); // This will just create the instance //PS3BT PS3(&Btd,0x00,0x15,0x83,0x3D,0x0A,0x57); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch boolean firstMessage = true; -String output = ""; // We will store the data in this string so we doesn't overflow the dongle +String output = ""; // We will store the data in this string void setup() { Serial.begin(115200); // This wil lprint the debugging from the libraries @@ -33,7 +33,7 @@ void setup() { output.reserve(200); // Reserve 200 bytes for the output string } void loop() { - Usb.Task(); + Usb.Task(); // The SPP data is actually not send until Usb.Task() is called if(SerialBT.connected) { if(firstMessage) { @@ -41,7 +41,7 @@ void loop() { SerialBT.println(F("Hello from Arduino")); // Send welcome message } if(Serial.available()) - SerialBT.print(Serial.read()); + SerialBT.write(Serial.read()); if(SerialBT.available()) Serial.write(SerialBT.read()); } diff --git a/examples/Bluetooth/SPP/SPP.ino b/examples/Bluetooth/SPP/SPP.ino index e3d13ec0..d488a7a4 100644 --- a/examples/Bluetooth/SPP/SPP.ino +++ b/examples/Bluetooth/SPP/SPP.ino @@ -22,14 +22,14 @@ void setup() { Serial.print(F("\r\nSPP Bluetooth Library Started")); } void loop() { - Usb.Task(); + Usb.Task(); // The SPP data is actually not send until Usb.Task() is called if(SerialBT.connected) { if(firstMessage) { firstMessage = false; SerialBT.println(F("Hello from Arduino")); // Send welcome message } if(Serial.available()) - SerialBT.print(Serial.read()); + SerialBT.write(Serial.read()); if(SerialBT.available()) Serial.write(SerialBT.read()); } diff --git a/examples/Bluetooth/SPPMulti/SPPMulti.ino b/examples/Bluetooth/SPPMulti/SPPMulti.ino index 4cf6f66f..00a4d7b3 100644 --- a/examples/Bluetooth/SPPMulti/SPPMulti.ino +++ b/examples/Bluetooth/SPPMulti/SPPMulti.ino @@ -34,7 +34,7 @@ void setup() { Serial.print(F("\r\nSPP Bluetooth Library Started")); } void loop() { - Usb.Task(); + Usb.Task(); // The SPP data is actually not send until Usb.Task() is called for(uint8_t i=0;iconnected) { if(firstMessage[i]) { @@ -44,7 +44,7 @@ void loop() { if(SerialBT[i]->available()) Serial.write(SerialBT[i]->read()); } - else + else firstMessage[i] = true; } if(Serial.available()) { @@ -61,7 +61,7 @@ void loop() { if(SerialBT[id]->connected) { // Check if a device is actually connected for(uint8_t i2 = 0; i2 < i-1; i2++) // Don't include the first character buffer[i2] = buffer[i2+1]; - SerialBT[id]->println(buffer,i-1); // Send the data + SerialBT[id]->write(buffer,i-1); // Send the data } } } From 1dc75ae89f53af1df1746c96c9b37a8e5bb2100f Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Tue, 7 May 2013 00:37:43 +0200 Subject: [PATCH 13/21] Call send if the buffer would be full --- SPP.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SPP.cpp b/SPP.cpp index a3b4401d..461d9bda 100644 --- a/SPP.cpp +++ b/SPP.cpp @@ -736,7 +736,7 @@ size_t SPP::write(uint8_t data) { size_t SPP::write(const uint8_t* data, size_t size) { for(uint8_t i = 0; i < size; i++) { if(sppIndex >= sizeof(sppOutputBuffer)/sizeof(sppOutputBuffer[0])) - return i; // Don't send any more if buffer is already full + send(); // Send the current data in the buffer sppOutputBuffer[sppIndex++] = data[i]; // All the bytes are put into a buffer and then send using the send() function } return size; From 0e2ae2c8b7c8f3403d53705ccdd7903e7be1030c Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Tue, 7 May 2013 01:01:55 +0200 Subject: [PATCH 14/21] Fixed some comments --- SPP.h | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/SPP.h b/SPP.h index 20ab8f0a..7a4f90e0 100644 --- a/SPP.h +++ b/SPP.h @@ -100,6 +100,16 @@ public: */ SPP(BTD *p, const char* name = "Arduino", const char* pin = "1234"); + /** + * Used to provide Boolean tests for the class. + * @return Return true if SPP communication is connected. + */ + operator bool() { + return connected; + } + /** Variable used to indicate if the connection is established. */ + bool connected; + /** @name BluetoothService implementation */ /** * Used to pass acldata to the services. @@ -114,9 +124,6 @@ public: virtual void disconnect(); /**@}*/ - /** Variable used to indicate if the connection is established. */ - bool connected; - /** @name Serial port profile (SPP) Print functions */ /** * Get number of bytes waiting to be read. @@ -127,22 +134,22 @@ public: virtual void flush(void); /** * Used to read the next value in the buffer without advancing to the next one. - * @return Return the byte. Will return -1 if no byte is available. + * @return Return the byte. Will return -1 if no bytes are available. */ virtual int peek(void); /** * Used to read the buffer. - * @return Return the byte. Will return -1 if no byte is available. + * @return Return the byte. Will return -1 if no bytes are available. */ virtual int read(void); /** - * Writes the byte to send to a buffer. The message is send when either send(const uint8_t* data, size_t size) or after Usb.Task() is called. + * Writes the byte to send to a buffer. The message is send when either send() or after Usb.Task() is called. * @param data The byte to write. * @return Return the number of bytes written. */ virtual size_t write(uint8_t data); /** - * Writes the bytes to send to a buffer. The message is send when either send(const uint8_t* data, size_t size) or after Usb.Task() is called. + * Writes the bytes to send to a buffer. The message is send when either send() or after Usb.Task() is called. * @param data The data array to send. * @param size Size of the data. * @return Return the number of bytes written. @@ -156,13 +163,6 @@ public: * but can also be called via this function. */ void send(void); - /** - * Used to provide Boolean tests for the class. - * @return Return true if SPP communication is connected. - */ - operator bool() { - return connected; - } /**@}*/ private: From 3557dac88d2530cf7f8c10ca875b92f871230762 Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Tue, 7 May 2013 15:50:38 +0200 Subject: [PATCH 15/21] Updated comment --- examples/Bluetooth/PS3SPP/PS3SPP.ino | 2 +- examples/Bluetooth/SPP/SPP.ino | 3 ++- examples/Bluetooth/SPPMulti/SPPMulti.ino | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/examples/Bluetooth/PS3SPP/PS3SPP.ino b/examples/Bluetooth/PS3SPP/PS3SPP.ino index 098b4278..ed02f739 100644 --- a/examples/Bluetooth/PS3SPP/PS3SPP.ino +++ b/examples/Bluetooth/PS3SPP/PS3SPP.ino @@ -33,7 +33,7 @@ void setup() { output.reserve(200); // Reserve 200 bytes for the output string } void loop() { - Usb.Task(); // The SPP data is actually not send until Usb.Task() is called + Usb.Task(); // The SPP data is actually not send until this is called, one could call SerialBT.send() directly as well if(SerialBT.connected) { if(firstMessage) { diff --git a/examples/Bluetooth/SPP/SPP.ino b/examples/Bluetooth/SPP/SPP.ino index d488a7a4..c7732955 100644 --- a/examples/Bluetooth/SPP/SPP.ino +++ b/examples/Bluetooth/SPP/SPP.ino @@ -22,7 +22,8 @@ void setup() { Serial.print(F("\r\nSPP Bluetooth Library Started")); } void loop() { - Usb.Task(); // The SPP data is actually not send until Usb.Task() is called + Usb.Task(); // The SPP data is actually not send until this is called, one could call SerialBT.send() directly as well + if(SerialBT.connected) { if(firstMessage) { firstMessage = false; diff --git a/examples/Bluetooth/SPPMulti/SPPMulti.ino b/examples/Bluetooth/SPPMulti/SPPMulti.ino index 00a4d7b3..173156e5 100644 --- a/examples/Bluetooth/SPPMulti/SPPMulti.ino +++ b/examples/Bluetooth/SPPMulti/SPPMulti.ino @@ -34,7 +34,8 @@ void setup() { Serial.print(F("\r\nSPP Bluetooth Library Started")); } void loop() { - Usb.Task(); // The SPP data is actually not send until Usb.Task() is called + Usb.Task(); // The SPP data is actually not send until this is called, one could call SerialBT.send() directly as well + for(uint8_t i=0;iconnected) { if(firstMessage[i]) { From 9d58b9d09cd7fb55fce08362fdd46245c141aaaa Mon Sep 17 00:00:00 2001 From: Oleg Mazurov Date: Sun, 12 May 2013 12:42:06 -0600 Subject: [PATCH 16/21] added gearfreak's NAK limit fix --- hidboot.h | 1 + 1 file changed, 1 insertion(+) diff --git a/hidboot.h b/hidboot.h index e68da991..b9af1d8a 100644 --- a/hidboot.h +++ b/hidboot.h @@ -438,6 +438,7 @@ void HIDBoot::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t epInfo[index].epAddr = (pep->bEndpointAddress & 0x0F); epInfo[index].maxPktSize = (uint8_t) pep->wMaxPacketSize; epInfo[index].epAttribs = 0; + epInfo[index].bmNakPower = USB_NAK_NOWAIT; bNumEP++; } From 91d719557762b196e5cd8dccd87ebf9038535142 Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Tue, 14 May 2013 00:47:05 +0200 Subject: [PATCH 17/21] Check if Bluetooth address is set before writing to PS3 controller --- BTD.cpp | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/BTD.cpp b/BTD.cpp index fdcaf249..5a0edb8c 100755 --- a/BTD.cpp +++ b/BTD.cpp @@ -145,40 +145,45 @@ uint8_t BTD::Init(uint8_t parent, uint8_t port, bool lowspeed) { if (rcode) goto FailSetConfDescr; - if (PID == PS3_PID || PID == PS3NAVIGATION_PID) { #ifdef DEBUG + if (PID == PS3_PID || PID == PS3NAVIGATION_PID) { if (PID == PS3_PID) Notify(PSTR("\r\nDualshock 3 Controller Connected"), 0x80); - else // must be a navigation controller + else // It must be a navigation controller Notify(PSTR("\r\nNavigation Controller Connected"), 0x80); -#endif - /* Set internal Bluetooth address */ - setBdaddr(my_bdaddr); - } else { // It must be a Motion controller -#ifdef DEBUG + } else // It must be a Motion controller Notify(PSTR("\r\nMotion Controller Connected"), 0x80); #endif - setMoveBdaddr(my_bdaddr); - } + + if (my_bdaddr[0] == 0x00 && my_bdaddr[1] == 0x00 && my_bdaddr[2] == 0x00 && my_bdaddr[3] == 0x00 && my_bdaddr[4] == 0x00 && my_bdaddr[5] == 0x00) { #ifdef DEBUG - Notify(PSTR("\r\nBluetooth Address was set to: "), 0x80); - for (int8_t i = 5; i > 0; i--) { - PrintHex (my_bdaddr[i], 0x80); - Notify(PSTR(":"), 0x80); - } - PrintHex (my_bdaddr[0], 0x80); + Notify(PSTR("\r\nPlease plug in the dongle before trying to pair with the PS3 Controller\n\rOr set the Bluetooth address in the constructor of the PS3BT class"), 0x80); #endif + } else { + if (PID == PS3_PID || PID == PS3NAVIGATION_PID) + setBdaddr(my_bdaddr); // Set internal Bluetooth address + else + setMoveBdaddr(my_bdaddr); // Set internal Bluetooth address +#ifdef DEBUG + Notify(PSTR("\r\nBluetooth Address was set to: "), 0x80); + for (int8_t i = 5; i > 0; i--) { + PrintHex (my_bdaddr[i], 0x80); + Notify(PSTR(":"), 0x80); + } + PrintHex (my_bdaddr[0], 0x80); +#endif + } + rcode = pUsb->setConf(bAddress, epInfo[ BTD_CONTROL_PIPE ].epAddr, 0); // Reset configuration value pUsb->setAddr(bAddress, 0, 0); // Reset address Release(); // Release device - return USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED; // return + return USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED; // Return } else { num_of_conf = ((USB_DEVICE_DESCRIPTOR*)buf)->bNumConfigurations; - // check if attached device is a Bluetooth dongle and fill endpoint data structure - // first interface in the configuration must have Bluetooth assigned Class/Subclass/Protocol - // and 3 endpoints - interrupt-IN, bulk-IN, bulk-OUT, - // not necessarily in this order + // Check if attached device is a Bluetooth dongle and fill endpoint data structure + // First interface in the configuration must have Bluetooth assigned Class/Subclass/Protocol + // And 3 endpoints - interrupt-IN, bulk-IN, bulk-OUT, not necessarily in this order for (uint8_t i = 0; i < num_of_conf; i++) { ConfigDescParser confDescrParser(this); rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser); @@ -251,7 +256,7 @@ void BTD::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto //ErrorMessage(PSTR("Iface Num"),iface); //ErrorMessage(PSTR("Alt.Set"),alt); - if (alt) // wrong interface - by BT spec, no alt setting + if (alt) // Wrong interface - by BT spec, no alt setting return; bConfNum = conf; @@ -261,7 +266,7 @@ void BTD::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto index = BTD_EVENT_PIPE; else { - if ((pep->bmAttributes & 0x02) == 2) // bulk endpoint found + if ((pep->bmAttributes & 0x02) == 2) // Bulk endpoint found index = ((pep->bEndpointAddress & 0x80) == 0x80) ? BTD_DATAIN_PIPE : BTD_DATAOUT_PIPE; else return; From 3d145984000a7ff9771be04daf1fdcf3a21f3cbb Mon Sep 17 00:00:00 2001 From: Oleg Mazurov Date: Mon, 13 May 2013 17:47:32 -0600 Subject: [PATCH 18/21] Added more contributors --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d190c7c0..c645b61d 100644 --- a/README.md +++ b/README.md @@ -19,9 +19,12 @@ For more information about the hardware see the [Hardware Manual](http://www.cir # Developed By * Oleg Mazurov, Circuits@Home - - * Developer of the USB Core, HID, FTDI, ADK, ACM, and PL2303 libraries +* Alexei Glushchenko Circuits@Home - + * Developers of the USB Core, HID, FTDI, ADK, ACM, and PL2303 libraries * Kristian Lauszus, TKJ Electronics - * Developer of the BTD, SPP, PS3, Wii, and Xbox libraries +* Andrew Kroll - + * Major contributor to mass storage code # How to include the library From 4342ee3c4e55e96e1b767e425b0cda48cc463148 Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Tue, 14 May 2013 14:45:21 +0200 Subject: [PATCH 19/21] Use __ instead of in readme --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c645b61d..f1095b3f 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,12 @@ For more information about the hardware see the [Hardware Manual](http://www.cir # Developed By -* Oleg Mazurov, Circuits@Home - -* Alexei Glushchenko Circuits@Home - +* __Oleg Mazurov, Circuits@Home__ - +* __Alexei Glushchenko, Circuits@Home__ - * Developers of the USB Core, HID, FTDI, ADK, ACM, and PL2303 libraries -* Kristian Lauszus, TKJ Electronics - +* __Kristian Lauszus, TKJ Electronics__ - * Developer of the BTD, SPP, PS3, Wii, and Xbox libraries -* Andrew Kroll - +* __Andrew Kroll__ - * Major contributor to mass storage code # How to include the library From 1a362c531052956733eb97463845f994a9e64cbf Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Thu, 16 May 2013 19:29:59 +0200 Subject: [PATCH 20/21] Shift one so it the same as setLedOn etc. --- PS3BT.cpp | 2 +- PS3USB.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/PS3BT.cpp b/PS3BT.cpp index ed2c98c9..a1ba45cb 100644 --- a/PS3BT.cpp +++ b/PS3BT.cpp @@ -597,7 +597,7 @@ void PS3BT::setRumbleOn(uint8_t rightDuration, uint8_t rightPower, uint8_t leftD } void PS3BT::setLedRaw(uint8_t value) { - HIDBuffer[11] = value; + HIDBuffer[11] = value << 1; HID_Command(HIDBuffer, HID_BUFFERSIZE); } void PS3BT::setLedOff(LED a) { diff --git a/PS3USB.cpp b/PS3USB.cpp index a14c14c3..ce7f22bf 100644 --- a/PS3USB.cpp +++ b/PS3USB.cpp @@ -445,7 +445,7 @@ void PS3USB::setRumbleOn(uint8_t rightDuration, uint8_t rightPower, uint8_t left } void PS3USB::setLedRaw(uint8_t value) { - writeBuf[9] = value; + writeBuf[9] = value << 1; PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE); } void PS3USB::setLedOff(LED a) { From 216fce8d951e7fe685f37c03577a930ac1a0da2b Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Thu, 16 May 2013 22:41:52 +0200 Subject: [PATCH 21/21] Controller is default 0 This will ensure compatible with the other controllers --- XBOXRECV.cpp | 18 ++++---- XBOXRECV.h | 66 +++++++++++++------------- examples/Xbox/XBOXRECV/XBOXRECV.ino | 72 ++++++++++++++--------------- 3 files changed, 78 insertions(+), 78 deletions(-) diff --git a/XBOXRECV.cpp b/XBOXRECV.cpp index 9231ef88..da56e7ec 100644 --- a/XBOXRECV.cpp +++ b/XBOXRECV.cpp @@ -325,7 +325,7 @@ void XBOXRECV::readReport(uint8_t controller) { case 3: led = LED4; break; } - setLedOn(controller, led); + setLedOn(led, controller); } #ifdef DEBUG else @@ -381,7 +381,7 @@ void XBOXRECV::printReport(uint8_t controller, uint8_t nBytes) { //Uncomment "#d #endif } -uint8_t XBOXRECV::getButtonPress(uint8_t controller, Button b) { +uint8_t XBOXRECV::getButtonPress(Button b, uint8_t controller) { if (b == L2) // These are analog buttons return (uint8_t)(ButtonState[controller] >> 8); else if (b == R2) @@ -389,7 +389,7 @@ uint8_t XBOXRECV::getButtonPress(uint8_t controller, Button b) { return (bool)(ButtonState[controller] & ((uint32_t)pgm_read_word(&XBOXBUTTONS[(uint8_t)b]) << 16)); } -bool XBOXRECV::getButtonClick(uint8_t controller, Button b) { +bool XBOXRECV::getButtonClick(Button b, uint8_t controller) { if (b == L2) { if (L2Clicked[controller]) { L2Clicked[controller] = false; @@ -409,7 +409,7 @@ bool XBOXRECV::getButtonClick(uint8_t controller, Button b) { return click; } -int16_t XBOXRECV::getAnalogHat(uint8_t controller, AnalogHat a) { +int16_t XBOXRECV::getAnalogHat(AnalogHat a, uint8_t controller) { return hatValue[controller][a]; } @@ -462,7 +462,7 @@ void XBOXRECV::XboxCommand(uint8_t controller, uint8_t* data, uint16_t nbytes) { #endif } -void XBOXRECV::setLedRaw(uint8_t controller, uint8_t value) { +void XBOXRECV::setLedRaw(uint8_t value, uint8_t controller) { writeBuf[0] = 0x00; writeBuf[1] = 0x00; writeBuf[2] = 0x08; @@ -471,16 +471,16 @@ void XBOXRECV::setLedRaw(uint8_t controller, uint8_t value) { XboxCommand(controller, writeBuf, 4); } -void XBOXRECV::setLedOn(uint8_t controller, LED led) { +void XBOXRECV::setLedOn(LED led, uint8_t controller) { if (led != ALL) // All LEDs can't be on a the same time setLedRaw(controller, (pgm_read_byte(&XBOXLEDS[(uint8_t)led])) + 4); } -void XBOXRECV::setLedBlink(uint8_t controller, LED led) { +void XBOXRECV::setLedBlink(LED led, uint8_t controller) { setLedRaw(controller, pgm_read_byte(&XBOXLEDS[(uint8_t)led])); } -void XBOXRECV::setLedMode(uint8_t controller, LEDMode ledMode) { // This function is used to do some speciel LED stuff the controller supports +void XBOXRECV::setLedMode(LEDMode ledMode, uint8_t controller) { // This function is used to do some speciel LED stuff the controller supports setLedRaw(controller, (uint8_t)ledMode); } @@ -510,7 +510,7 @@ void XBOXRECV::checkStatus() { } } -void XBOXRECV::setRumbleOn(uint8_t controller, uint8_t lValue, uint8_t rValue) { +void XBOXRECV::setRumbleOn(uint8_t lValue, uint8_t rValue, uint8_t controller) { writeBuf[0] = 0x00; writeBuf[1] = 0x01; writeBuf[2] = 0x0f; diff --git a/XBOXRECV.h b/XBOXRECV.h index 20744f01..9ae12aca 100644 --- a/XBOXRECV.h +++ b/XBOXRECV.h @@ -113,93 +113,93 @@ public: * * So you instance if you need to increase a variable once you would use getButtonClick(uint8_t controller, Button b), * but if you need to drive a robot forward you would use getButtonPress(uint8_t controller, Button b). - * @param controller The controller to read from. * @param b ::Button to read. + * @param controller The controller to read from. Default to 0. * @return getButtonClick(uint8_t controller, Button b) will return a bool, but getButtonPress(uint8_t controller, Button b) * will return a byte if reading ::L2 or ::R2. */ - uint8_t getButtonPress(uint8_t controller, Button b); - bool getButtonClick(uint8_t controller, Button b); + uint8_t getButtonPress(Button b, uint8_t controller = 0); + bool getButtonClick(Button b, uint8_t controller = 0); /**@}*/ /** @name Xbox Controller functions */ /** - * Return the analog value from the joysticks on the controller. - * @param controller The controller to read from. + * Return the analog value from the joysticks on the controller. * @param a Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY. + * @param controller The controller to read from. Default to 0. * @return Returns a signed 16-bit integer. */ - int16_t getAnalogHat(uint8_t controller, AnalogHat a); + int16_t getAnalogHat(AnalogHat a, uint8_t controller = 0); /** * Turn rumble off and all the LEDs on the specific controller. - * @param controller The controller to write to. + * @param controller The controller to write to. Default to 0. */ - void setAllOff(uint8_t controller) { - setRumbleOn(controller, 0, 0); + void setAllOff(uint8_t controller = 0) { + setRumbleOn(0, 0, controller); setLedOff(controller); }; /** * Turn rumble off the specific controller. - * @param controller The controller to write to. + * @param controller The controller to write to. Default to 0. */ - void setRumbleOff(uint8_t controller) { - setRumbleOn(controller, 0, 0); + void setRumbleOff(uint8_t controller = 0) { + setRumbleOn(0, 0, controller); }; /** - * Turn rumble on. - * @param controller The controller to write to. + * Turn rumble on. * @param lValue Left motor (big weight) inside the controller. * @param rValue Right motor (small weight) inside the controller. + * @param controller The controller to write to. Default to 0. */ - void setRumbleOn(uint8_t controller, uint8_t lValue, uint8_t rValue); + void setRumbleOn(uint8_t lValue, uint8_t rValue, uint8_t controller = 0); /** * Set LED value. Without using the ::LED or ::LEDMode enum. - * @param controller The controller to write to. * @param value See: * setLedOff(uint8_t controller), setLedOn(uint8_t controller, LED l), * setLedBlink(uint8_t controller, LED l), and setLedMode(uint8_t controller, LEDMode lm). + * @param controller The controller to write to. Default to 0. */ - void setLedRaw(uint8_t controller, uint8_t value); + void setLedRaw(uint8_t value, uint8_t controller = 0); /** * Turn all LEDs off the specific controller. - * @param controller The controller to write to. + * @param controller The controller to write to. Default to 0. */ - void setLedOff(uint8_t controller) { - setLedRaw(controller, 0); + void setLedOff(uint8_t controller = 0) { + setLedRaw(0, controller); }; /** - * Turn on a LED by using the ::LED enum. - * @param controller The controller to write to. + * Turn on a LED by using the ::LED enum. * @param l ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller. + * @param controller The controller to write to. Default to 0. */ - void setLedOn(uint8_t controller, LED l); + void setLedOn(LED l, uint8_t controller = 0); /** - * Turn on a LED by using the ::LED enum. - * @param controller The controller to write to. + * Turn on a LED by using the ::LED enum. * @param l ::ALL, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller. + * @param controller The controller to write to. Default to 0. */ - void setLedBlink(uint8_t controller, LED l); + void setLedBlink(LED l, uint8_t controller = 0); /** - * Used to set special LED modes supported by the Xbox controller. - * @param controller The controller to write to. + * Used to set special LED modes supported by the Xbox controller. * @param lm See ::LEDMode. + * @param controller The controller to write to. Default to 0. */ - void setLedMode(uint8_t controller, LEDMode lm); + void setLedMode(LEDMode lm, uint8_t controller = 0); /** * Used to get the battery level from the controller. - * @param controller The controller to read from. + * @param controller The controller to read from. Default to 0. * @return Returns the battery level as an integer in the range of 0-3. */ - uint8_t getBatteryLevel(uint8_t controller); + uint8_t getBatteryLevel(uint8_t controller = 0); /** * Used to check if a button has changed. - * @param controller The controller to read from. + * @param controller The controller to read from. Default to 0. * @return True if a button has changed. */ - bool buttonChanged(uint8_t controller); + bool buttonChanged(uint8_t controller = 0); /**@}*/ /** True if a wireless receiver is connected. */ diff --git a/examples/Xbox/XBOXRECV/XBOXRECV.ino b/examples/Xbox/XBOXRECV/XBOXRECV.ino index e49dee37..b508e837 100644 --- a/examples/Xbox/XBOXRECV/XBOXRECV.ino +++ b/examples/Xbox/XBOXRECV/XBOXRECV.ino @@ -22,86 +22,86 @@ void loop() { if(Xbox.XboxReceiverConnected) { for(uint8_t i=0;i<4;i++) { if(Xbox.Xbox360Connected[i]) { - if(Xbox.getButtonPress(i,L2) || Xbox.getButtonPress(i,R2)) { + if(Xbox.getButtonPress(L2,i) || Xbox.getButtonPress(R2,i)) { Serial.print("L2: "); - Serial.print(Xbox.getButtonPress(i,L2)); + Serial.print(Xbox.getButtonPress(L2,i)); Serial.print("\tR2: "); - Serial.println(Xbox.getButtonPress(i,R2)); - Xbox.setRumbleOn(i,Xbox.getButtonPress(i,L2),Xbox.getButtonPress(i,R2)); + Serial.println(Xbox.getButtonPress(R2,i)); + Xbox.setRumbleOn(Xbox.getButtonPress(L2,i),Xbox.getButtonPress(R2,i)); } - if(Xbox.getAnalogHat(i,LeftHatX) > 7500 || Xbox.getAnalogHat(i,LeftHatX) < -7500 || Xbox.getAnalogHat(i,LeftHatY) > 7500 || Xbox.getAnalogHat(i,LeftHatY) < -7500 || Xbox.getAnalogHat(i,RightHatX) > 7500 || Xbox.getAnalogHat(i,RightHatX) < -7500 || Xbox.getAnalogHat(i,RightHatY) > 7500 || Xbox.getAnalogHat(i,RightHatY) < -7500) { - if(Xbox.getAnalogHat(i,LeftHatX) > 7500 || Xbox.getAnalogHat(i,LeftHatX) < -7500) { + if(Xbox.getAnalogHat(LeftHatX,i) > 7500 || Xbox.getAnalogHat(LeftHatX,i) < -7500 || Xbox.getAnalogHat(LeftHatY,i) > 7500 || Xbox.getAnalogHat(LeftHatY,i) < -7500 || Xbox.getAnalogHat(RightHatX,i) > 7500 || Xbox.getAnalogHat(RightHatX,i) < -7500 || Xbox.getAnalogHat(RightHatY,i) > 7500 || Xbox.getAnalogHat(RightHatY,i) < -7500) { + if(Xbox.getAnalogHat(LeftHatX,i) > 7500 || Xbox.getAnalogHat(LeftHatX,i) < -7500) { Serial.print(F("LeftHatX: ")); - Serial.print(Xbox.getAnalogHat(i,LeftHatX)); + Serial.print(Xbox.getAnalogHat(LeftHatX,i)); Serial.print("\t"); } - if(Xbox.getAnalogHat(i,LeftHatY) > 7500 || Xbox.getAnalogHat(i,LeftHatY) < -7500) { + if(Xbox.getAnalogHat(LeftHatY,i) > 7500 || Xbox.getAnalogHat(LeftHatY,i) < -7500) { Serial.print(F("LeftHatY: ")); - Serial.print(Xbox.getAnalogHat(i,LeftHatY)); + Serial.print(Xbox.getAnalogHat(LeftHatY,i)); Serial.print("\t"); } - if(Xbox.getAnalogHat(i,RightHatX) > 7500 || Xbox.getAnalogHat(i,RightHatX) < -7500) { + if(Xbox.getAnalogHat(RightHatX,i) > 7500 || Xbox.getAnalogHat(RightHatX,i) < -7500) { Serial.print(F("RightHatX: ")); - Serial.print(Xbox.getAnalogHat(i,RightHatX)); + Serial.print(Xbox.getAnalogHat(RightHatX,i)); Serial.print("\t"); } - if(Xbox.getAnalogHat(i,RightHatY) > 7500 || Xbox.getAnalogHat(i,RightHatY) < -7500) { + if(Xbox.getAnalogHat(RightHatY,i) > 7500 || Xbox.getAnalogHat(RightHatY,i) < -7500) { Serial.print(F("RightHatY: ")); - Serial.print(Xbox.getAnalogHat(i,RightHatY)); + Serial.print(Xbox.getAnalogHat(RightHatY,i)); } Serial.println(); } - if(Xbox.getButtonClick(i,UP)) { - Xbox.setLedOn(i,LED1); + if(Xbox.getButtonClick(UP,i)) { + Xbox.setLedOn(LED1,i); Serial.println(F("Up")); } - if(Xbox.getButtonClick(i,DOWN)) { - Xbox.setLedOn(i,LED4); + if(Xbox.getButtonClick(DOWN,i)) { + Xbox.setLedOn(LED4,i); Serial.println(F("Down")); } - if(Xbox.getButtonClick(i,LEFT)) { - Xbox.setLedOn(i,LED3); + if(Xbox.getButtonClick(LEFT,i)) { + Xbox.setLedOn(LED3,i); Serial.println(F("Left")); } - if(Xbox.getButtonClick(i,RIGHT)) { - Xbox.setLedOn(i,LED2); + if(Xbox.getButtonClick(RIGHT,i)) { + Xbox.setLedOn(LED2,i); Serial.println(F("Right")); } - if(Xbox.getButtonClick(i,START)) { - Xbox.setLedMode(i,ALTERNATING); + if(Xbox.getButtonClick(START,i)) { + Xbox.setLedMode(ALTERNATING,i); Serial.println(F("Start")); } - if(Xbox.getButtonClick(i,BACK)) { - Xbox.setLedBlink(i,ALL); + if(Xbox.getButtonClick(BACK,i)) { + Xbox.setLedBlink(ALL,i); Serial.println(F("Back")); } - if(Xbox.getButtonClick(i,L3)) + if(Xbox.getButtonClick(L3,i)) Serial.println(F("L3")); - if(Xbox.getButtonClick(i,R3)) + if(Xbox.getButtonClick(R3,i)) Serial.println(F("R3")); - if(Xbox.getButtonClick(i,L1)) + if(Xbox.getButtonClick(L1,i)) Serial.println(F("L1")); - if(Xbox.getButtonClick(i,R1)) + if(Xbox.getButtonClick(R1,i)) Serial.println(F("R1")); - if(Xbox.getButtonClick(i,XBOX)) { - Xbox.setLedMode(i,ROTATING); + if(Xbox.getButtonClick(XBOX,i)) { + Xbox.setLedMode(ROTATING,i); Serial.print(F("Xbox (Battery: ")); Serial.print(Xbox.getBatteryLevel(i)); // The battery level in the range 0-3 Serial.println(F(")")); } - if(Xbox.getButtonClick(i,SYNC)) + if(Xbox.getButtonClick(SYNC,i)) Serial.println(F("Sync")); - if(Xbox.getButtonClick(i,A)) + if(Xbox.getButtonClick(A,i)) Serial.println(F("A")); - if(Xbox.getButtonClick(i,B)) + if(Xbox.getButtonClick(B,i)) Serial.println(F("B")); - if(Xbox.getButtonClick(i,X)) + if(Xbox.getButtonClick(X,i)) Serial.println(F("X")); - if(Xbox.getButtonClick(i,Y)) + if(Xbox.getButtonClick(Y,i)) Serial.println(F("Y")); } }