From 73427c03848c21cfd6651bc2148ebb26db4aaeb2 Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Thu, 24 May 2012 14:39:01 +0200 Subject: [PATCH 1/3] Combined constructors --- PS3BT.cpp | 19 ------------------- PS3BT.h | 5 ++--- 2 files changed, 2 insertions(+), 22 deletions(-) diff --git a/PS3BT.cpp b/PS3BT.cpp index c80e58ab..827e2867 100644 --- a/PS3BT.cpp +++ b/PS3BT.cpp @@ -62,25 +62,6 @@ bPollEnable(false) // don't start polling before dongle is connected my_bdaddr[0] = btadr0; } -PS3BT::PS3BT(USB *p): -pUsb(p), // pointer to USB class instance - mandatory -bAddress(0), // device address - mandatory -bNumEP(1), // if config descriptor needs to be parsed -qNextPollTime(0), -bPollEnable(false) // don't start polling before dongle is connected -{ - for(uint8_t i=0; iRegisterDeviceClass(this); //set devConfig[] entry -} - uint8_t PS3BT::Init(uint8_t parent, uint8_t port, bool lowspeed) { uint8_t buf[sizeof(USB_DEVICE_DESCRIPTOR)]; diff --git a/PS3BT.h b/PS3BT.h index b2983b7c..3f326454 100644 --- a/PS3BT.h +++ b/PS3BT.h @@ -316,9 +316,8 @@ enum Rumble class PS3BT : public USBDeviceConfig, public UsbConfigXtracter { -public: - PS3BT(USB *pUsb, uint8_t btadr5, uint8_t btadr4, uint8_t btadr3, uint8_t btadr2, uint8_t btadr1, uint8_t btadr0); - PS3BT(USB *pUsb); +public: + PS3BT(USB *pUsb, uint8_t btadr5=0, uint8_t btadr4=0, uint8_t btadr3=0, uint8_t btadr2=0, uint8_t btadr1=0, uint8_t btadr0=0); // USBDeviceConfig implementation virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed); From 6b93de24ce858507cd0576987b66cb33069fd67b Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Fri, 25 May 2012 18:29:58 +0200 Subject: [PATCH 2/3] Created separate flags for read bdaddr and read local version Sometimes it didn't read the bluetooth address correctly, but still just went on with the state machine. The start number og reset loops also had to be increased. --- PS3BT.cpp | 22 ++++++++++++---------- PS3BT.h | 6 +++++- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/PS3BT.cpp b/PS3BT.cpp index 827e2867..e9b21f57 100644 --- a/PS3BT.cpp +++ b/PS3BT.cpp @@ -264,7 +264,7 @@ uint8_t PS3BT::Init(uint8_t parent, uint8_t port, bool lowspeed) interrupt_dcid[0] = 0x41;//0x0041 interrupt_dcid[1] = 0x00; - hci_num_reset_loops = 10; // only loop 10 times before trying to send the hci reset command + hci_num_reset_loops = 100; // only loop 100 times before trying to send the hci reset command hci_state = HCI_INIT_STATE; hci_counter = 0; @@ -382,8 +382,8 @@ uint8_t PS3BT::Poll() if (qNextPollTime <= millis()) { // Don't poll if shorter than polling interval HCI_event_task(); // poll the HCI event pipe ACL_event_task(); // start polling the ACL input pipe too, though discard data until connected - } - qNextPollTime = millis() + pollInterval; // Poll time + qNextPollTime = millis() + pollInterval; // Set new poll time + } return 0; } void PS3BT::setBdaddr(uint8_t* BDADDR) @@ -608,15 +608,17 @@ void PS3BT::HCI_event_task() switch (hcibuf[0]) //switch on event type { case EV_COMMAND_COMPLETE: - if (!hcibuf[5]) { // check if command succeeded - hci_event_flag |= HCI_FLAG_CMD_COMPLETE; // set command complete flag - if((hcibuf[3] == 0x01) && (hcibuf[4] == 0x10)) // parameters from read local version information + hci_event_flag |= HCI_FLAG_CMD_COMPLETE; // set command complete flag + if (!hcibuf[5]) { // check if command succeeded + if((hcibuf[3] == 0x01) && (hcibuf[4] == 0x10)) { // parameters from read local version information hci_version = hcibuf[6]; // Check if it supports 2.0+EDR - see http://www.bluetooth.org/Technical/AssignedNumbers/hci.htm - + hci_event_flag |= HCI_FLAG_READ_VERSION; + } else if((hcibuf[3] == 0x09) && (hcibuf[4] == 0x10)) { // parameters from read local bluetooth address for (uint8_t i = 0; i < 6; i++) my_bdaddr[i] = hcibuf[6 + i]; - } + hci_event_flag |= HCI_FLAG_READ_BDADDR; + } } break; @@ -750,7 +752,7 @@ void PS3BT::HCI_task() break; case HCI_BDADDR_STATE: - if (hci_cmd_complete) + if (hci_read_bdaddr_complete) { #ifdef DEBUG Notify(PSTR("\r\nLocal Bluetooth Address: ")); @@ -767,7 +769,7 @@ void PS3BT::HCI_task() break; case HCI_LOCAL_VERSION_STATE: - if (hci_cmd_complete) + if (hci_read_version_complete) { #ifdef DEBUG if(hci_version < 3) { diff --git a/PS3BT.h b/PS3BT.h index 3f326454..27a2decd 100644 --- a/PS3BT.h +++ b/PS3BT.h @@ -77,6 +77,8 @@ #define HCI_FLAG_DISCONN_COMPLETE 0x04 #define HCI_FLAG_REMOTE_NAME_COMPLETE 0x08 #define HCI_FLAG_INCOMING_REQUEST 0x10 +#define HCI_FLAG_READ_BDADDR 0x20 +#define HCI_FLAG_READ_VERSION 0x40 /*Macros for HCI event flag tests */ #define hci_cmd_complete (hci_event_flag & HCI_FLAG_CMD_COMPLETE) @@ -84,6 +86,8 @@ #define hci_disconnect_complete (hci_event_flag & HCI_FLAG_DISCONN_COMPLETE) #define hci_remote_name_complete (hci_event_flag & HCI_FLAG_REMOTE_NAME_COMPLETE) #define hci_incoming_connect_request (hci_event_flag & HCI_FLAG_INCOMING_REQUEST) +#define hci_read_bdaddr_complete (hci_event_flag & HCI_FLAG_READ_BDADDR) +#define hci_read_version_complete (hci_event_flag & HCI_FLAG_READ_VERSION) /* HCI Events managed */ #define EV_COMMAND_COMPLETE 0x0E @@ -362,7 +366,7 @@ public: bool PS3NavigationBTConnected;// Variable used to indicate if the navigation controller is successfully connected bool buttonChanged;//Indicate if a button has been changed bool buttonPressed;//Indicate if a button has been pressed - bool buttonReleased;//Indicate if a button has been pressed + bool buttonReleased;//Indicate if a button has been released protected: /* mandatory members */ From b66a8f214ca2b8b8c46f3a3aa80fc1a06ae02285 Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Fri, 25 May 2012 19:59:00 +0200 Subject: [PATCH 3/3] Added small delay in example sketch Also fixed qNextPollTime --- PS3BT.cpp | 4 ++-- examples/PS3BT/PS3BT.ino | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/PS3BT.cpp b/PS3BT.cpp index e9b21f57..9166d7eb 100644 --- a/PS3BT.cpp +++ b/PS3BT.cpp @@ -380,9 +380,9 @@ uint8_t PS3BT::Poll() if (!bPollEnable) return 0; if (qNextPollTime <= millis()) { // Don't poll if shorter than polling interval - HCI_event_task(); // poll the HCI event pipe - ACL_event_task(); // start polling the ACL input pipe too, though discard data until connected qNextPollTime = millis() + pollInterval; // Set new poll time + HCI_event_task(); // poll the HCI event pipe + ACL_event_task(); // start polling the ACL input pipe too, though discard data until connected } return 0; } diff --git a/examples/PS3BT/PS3BT.ino b/examples/PS3BT/PS3BT.ino index 2b6fbd95..df667a4e 100644 --- a/examples/PS3BT/PS3BT.ino +++ b/examples/PS3BT/PS3BT.ino @@ -198,4 +198,5 @@ void loop() Serial.println(templow); } } + delay(1); }