From ed36d04e7ccd399799474866b979b70b561fcc47 Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Wed, 11 Dec 2013 09:29:38 +0100 Subject: [PATCH] Move all L2CAP states etc. into BTD.h, so the different drivers does not conflict when used together --- BTD.h | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- BTHID.h | 40 +------------------------ PS3BT.cpp | 40 +++++++------------------ PS3BT.h | 37 ++--------------------- SPP.h | 39 ++---------------------- Wii.cpp | 20 ++++++------- Wii.h | 54 ++++----------------------------- 7 files changed, 118 insertions(+), 201 deletions(-) diff --git a/BTD.h b/BTD.h index 2bf5e7ba..c4293327 100755 --- a/BTD.h +++ b/BTD.h @@ -47,7 +47,7 @@ #define HCI_SET_NAME_STATE 5 #define HCI_CHECK_DEVICE_SERVICE 6 -#define HCI_INQUIRY_STATE 7 // These three states are only used if it should pair and connect to a Wii controller +#define HCI_INQUIRY_STATE 7 // These three states are only used if it should pair and connect to a device #define HCI_CONNECT_DEVICE_STATE 8 #define HCI_CONNECTED_DEVICE_STATE 9 @@ -105,6 +105,89 @@ #define EV_LOOPBACK_COMMAND 0x19 #define EV_PAGE_SCAN_REP_MODE 0x20 +/* Bluetooth states for the different Bluetooth drivers */ +#define L2CAP_WAIT 0 +#define L2CAP_DONE 1 + +/* Used for HID Control channel */ +#define L2CAP_CONTROL_CONNECT_REQUEST 2 +#define L2CAP_CONTROL_CONFIG_REQUEST 3 +#define L2CAP_CONTROL_SUCCESS 4 +#define L2CAP_CONTROL_DISCONNECT 5 + +/* Used for HID Interrupt channel */ +#define L2CAP_INTERRUPT_SETUP 6 +#define L2CAP_INTERRUPT_CONNECT_REQUEST 7 +#define L2CAP_INTERRUPT_CONFIG_REQUEST 8 +#define L2CAP_INTERRUPT_DISCONNECT 9 + +/* Used for SDP channel */ +#define L2CAP_SDP_WAIT 10 +#define L2CAP_SDP_REQUEST 11 +#define L2CAP_SDP_SUCCESS 12 +#define L2CAP_SDP_DONE 13 +#define L2CAP_DISCONNECT_RESPONSE 14 + +/* Used for RFCOMM channel */ +#define L2CAP_RFCOMM_WAIT 15 +#define L2CAP_RFCOMM_REQUEST 16 +#define L2CAP_RFCOMM_SUCCESS 17 +#define L2CAP_RFCOMM_DONE 18 + +/* Bluetooth states used by some drivers */ +#define TURN_ON_LED 19 +#define PS3_ENABLE_SIXAXIS 20 +#define WII_CHECK_MOTION_PLUS_STATE 21 +#define WII_CHECK_EXTENSION_STATE 22 +#define WII_INIT_MOTION_PLUS_STATE 23 + +/* L2CAP event flags */ +#define L2CAP_FLAG_CONNECTION_CONTROL_REQUEST 0x00000001 +#define L2CAP_FLAG_CONFIG_CONTROL_SUCCESS 0x00000002 +#define L2CAP_FLAG_CONTROL_CONNECTED 0x00000004 +#define L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE 0x00000008 + +#define L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST 0x00000010 +#define L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS 0x00000020 +#define L2CAP_FLAG_INTERRUPT_CONNECTED 0x00000040 +#define L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE 0x00000080 + +#define L2CAP_FLAG_CONNECTION_SDP_REQUEST 0x00000100 +#define L2CAP_FLAG_CONFIG_SDP_REQUEST 0x00000200 +#define L2CAP_FLAG_CONFIG_SDP_SUCCESS 0x00000400 +#define L2CAP_FLAG_DISCONNECT_SDP_REQUEST 0x00000800 + +#define L2CAP_FLAG_CONNECTION_RFCOMM_REQUEST 0x00001000 +#define L2CAP_FLAG_CONFIG_RFCOMM_REQUEST 0x00002000 +#define L2CAP_FLAG_CONFIG_RFCOMM_SUCCESS 0x00004000 +#define L2CAP_FLAG_DISCONNECT_RFCOMM_REQUEST 0x00008000 + +#define L2CAP_FLAG_DISCONNECT_RESPONSE 0x00010000 + +/* Macros for L2CAP event flag tests */ + +#define l2cap_connection_request_control_flag (l2cap_event_flag & L2CAP_FLAG_CONNECTION_CONTROL_REQUEST) +#define l2cap_config_success_control_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_CONTROL_SUCCESS) +#define l2cap_connected_control_flag (l2cap_event_flag & L2CAP_FLAG_CONTROL_CONNECTED) +#define l2cap_disconnect_response_control_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE) + +#define l2cap_connection_request_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST) +#define l2cap_config_success_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS) +#define l2cap_connected_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_INTERRUPT_CONNECTED) +#define l2cap_disconnect_response_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE) + +#define l2cap_connection_request_sdp_flag (l2cap_event_flag & L2CAP_FLAG_CONNECTION_SDP_REQUEST) +#define l2cap_config_request_sdp_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_SDP_REQUEST) +#define l2cap_config_success_sdp_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_SDP_SUCCESS) +#define l2cap_disconnect_request_sdp_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_SDP_REQUEST) + +#define l2cap_connection_request_rfcomm_flag (l2cap_event_flag & L2CAP_FLAG_CONNECTION_RFCOMM_REQUEST) +#define l2cap_config_request_rfcomm_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_RFCOMM_REQUEST) +#define l2cap_config_success_rfcomm_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_RFCOMM_SUCCESS) +#define l2cap_disconnect_request_rfcomm_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_RFCOMM_REQUEST) + +#define l2cap_disconnect_response_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_RESPONSE) + /* L2CAP signaling commands */ #define L2CAP_CMD_COMMAND_REJECT 0x01 #define L2CAP_CMD_CONNECTION_REQUEST 0x02 @@ -131,11 +214,11 @@ #define WI_PROTOCOL_BT 0x01 // Bluetooth Programming Interface #define BTD_MAX_ENDPOINTS 4 -#define BTD_NUMSERVICES 4 // Max number of Bluetooth services - if you need more than four simply increase this number +#define BTD_NUMSERVICES 4 // Max number of Bluetooth services - if you need more than 4 simply increase this number #define PAIR 1 -/** All Bluetooth services should include this class. */ +/** All Bluetooth services should inherit this class. */ class BluetoothService { public: /** diff --git a/BTHID.h b/BTHID.h index f7d18b10..b4cb21f2 100644 --- a/BTHID.h +++ b/BTHID.h @@ -21,44 +21,6 @@ #include "BTD.h" #include "hidboot.h" -/* Bluetooth L2CAP states for L2CAP_task() */ -#define L2CAP_WAIT 0 - -// These states are used if the device is the host -#define L2CAP_CONTROL_SUCCESS 1 -#define L2CAP_INTERRUPT_SETUP 2 - -// These states are used if the Arduino is the host -#define L2CAP_CONTROL_CONNECT_REQUEST 3 -#define L2CAP_CONTROL_CONFIG_REQUEST 4 -#define L2CAP_INTERRUPT_CONNECT_REQUEST 5 - -#define L2CAP_INTERRUPT_CONFIG_REQUEST 6 -#define L2CAP_DONE 7 - -#define L2CAP_INTERRUPT_DISCONNECT 8 -#define L2CAP_CONTROL_DISCONNECT 9 - -/* L2CAP event flags */ -#define L2CAP_FLAG_CONTROL_CONNECTED 0x01 -#define L2CAP_FLAG_INTERRUPT_CONNECTED 0x02 -#define L2CAP_FLAG_CONFIG_CONTROL_SUCCESS 0x04 -#define L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS 0x08 -#define L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE 0x10 -#define L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE 0x20 -#define L2CAP_FLAG_CONNECTION_CONTROL_REQUEST 0x40 -#define L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST 0x80 - -/* Macros for L2CAP event flag tests */ -#define l2cap_connected_control_flag (l2cap_event_flag & L2CAP_FLAG_CONTROL_CONNECTED) -#define l2cap_connected_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_INTERRUPT_CONNECTED) -#define l2cap_config_success_control_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_CONTROL_SUCCESS) -#define l2cap_config_success_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS) -#define l2cap_disconnect_response_control_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE) -#define l2cap_disconnect_response_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE) -#define l2cap_connection_request_control_flag (l2cap_event_flag & L2CAP_FLAG_CONNECTION_CONTROL_REQUEST) -#define l2cap_connection_request_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST) - #define KEYBOARD_PARSER_ID 0 #define MOUSE_PARSER_ID 1 #define NUM_PARSERS 2 @@ -149,7 +111,7 @@ private: /* Variables used by high level L2CAP task */ uint8_t l2cap_state; - uint8_t l2cap_event_flag; // l2cap flags of received Bluetooth events + uint32_t l2cap_event_flag; // l2cap flags of received Bluetooth events /* L2CAP Channels */ uint8_t control_scid[2]; // L2CAP source CID for HID_Control diff --git a/PS3BT.cpp b/PS3BT.cpp index 9ed5da75..a56efa73 100644 --- a/PS3BT.cpp +++ b/PS3BT.cpp @@ -305,12 +305,10 @@ void PS3BT::ACLData(uint8_t* ACLData) { } else if (l2capinbuf[8] == L2CAP_CMD_CONFIG_REQUEST) { if (l2capinbuf[12] == control_dcid[0] && l2capinbuf[13] == control_dcid[1]) { //Notify(PSTR("\r\nHID Control Configuration Request"), 0x80); - identifier = l2capinbuf[9]; - l2cap_event_flag |= L2CAP_FLAG_CONFIG_CONTROL_REQUEST; + pBtd->l2cap_config_response(hci_handle, l2capinbuf[9], control_scid); } else if (l2capinbuf[12] == interrupt_dcid[0] && l2capinbuf[13] == interrupt_dcid[1]) { //Notify(PSTR("\r\nHID Interrupt Configuration Request"), 0x80); - identifier = l2capinbuf[9]; - l2cap_event_flag |= L2CAP_FLAG_CONFIG_INTERRUPT_REQUEST; + pBtd->l2cap_config_response(hci_handle, l2capinbuf[9], interrupt_scid); } } else if (l2capinbuf[8] == L2CAP_CMD_DISCONNECT_REQUEST) { if (l2capinbuf[12] == control_dcid[0] && l2capinbuf[13] == control_dcid[1]) { @@ -390,15 +388,6 @@ void PS3BT::L2CAP_task() { identifier++; delay(1); pBtd->l2cap_config_request(hci_handle, identifier, control_scid); - l2cap_state = L2CAP_CONTROL_REQUEST; - } - break; - case L2CAP_CONTROL_REQUEST: - if (l2cap_config_request_control_flag) { -#ifdef DEBUG_USB_HOST - Notify(PSTR("\r\nHID Control Configuration Request"), 0x80); -#endif - pBtd->l2cap_config_response(hci_handle, identifier, control_scid); l2cap_state = L2CAP_CONTROL_SUCCESS; } break; @@ -411,6 +400,7 @@ void PS3BT::L2CAP_task() { l2cap_state = L2CAP_INTERRUPT_SETUP; } break; + case L2CAP_INTERRUPT_SETUP: if (l2cap_connection_request_interrupt_flag) { #ifdef DEBUG_USB_HOST @@ -423,28 +413,20 @@ void PS3BT::L2CAP_task() { delay(1); pBtd->l2cap_config_request(hci_handle, identifier, interrupt_scid); - l2cap_state = L2CAP_INTERRUPT_REQUEST; + l2cap_state = L2CAP_INTERRUPT_CONFIG_REQUEST; } break; - case L2CAP_INTERRUPT_REQUEST: - if (l2cap_config_request_interrupt_flag) { -#ifdef DEBUG_USB_HOST - Notify(PSTR("\r\nHID Interrupt Configuration Request"), 0x80); -#endif - pBtd->l2cap_config_response(hci_handle, identifier, interrupt_scid); - l2cap_state = L2CAP_INTERRUPT_SUCCESS; - } - break; - case L2CAP_INTERRUPT_SUCCESS: + + case L2CAP_INTERRUPT_CONFIG_REQUEST: if (l2cap_config_success_interrupt_flag) { #ifdef DEBUG_USB_HOST Notify(PSTR("\r\nHID Interrupt Successfully Configured"), 0x80); #endif if (remote_name[0] == 'M') { // First letter in Motion Controller ('M') memset(l2capinbuf, 0, BULK_MAXPKTSIZE); // Reset l2cap in buffer as it sometimes read it as a button has been pressed - l2cap_state = L2CAP_HID_PS3_LED; + l2cap_state = TURN_ON_LED; } else - l2cap_state = L2CAP_HID_ENABLE_SIXAXIS; + l2cap_state = PS3_ENABLE_SIXAXIS; timer = millis(); } break; @@ -478,18 +460,18 @@ void PS3BT::L2CAP_task() { void PS3BT::Run() { switch (l2cap_state) { - case L2CAP_HID_ENABLE_SIXAXIS: + case PS3_ENABLE_SIXAXIS: if (millis() - timer > 1000) { // loop 1 second before sending the command memset(l2capinbuf, 0, BULK_MAXPKTSIZE); // Reset l2cap in buffer as it sometimes read it as a button has been pressed for (uint8_t i = 15; i < 19; i++) l2capinbuf[i] = 0x7F; // Set the analog joystick values to center position enable_sixaxis(); - l2cap_state = L2CAP_HID_PS3_LED; + l2cap_state = TURN_ON_LED; timer = millis(); } break; - case L2CAP_HID_PS3_LED: + case TURN_ON_LED: if (millis() - timer > 1000) { // loop 1 second before sending the command if (remote_name[0] == 'P') { // First letter in PLAYSTATION(R)3 Controller ('P') #ifdef DEBUG_USB_HOST diff --git a/PS3BT.h b/PS3BT.h index 6b901402..8d796649 100644 --- a/PS3BT.h +++ b/PS3BT.h @@ -21,40 +21,7 @@ #include "BTD.h" #include "PS3Enums.h" -#define HID_BUFFERSIZE 50 // Size of the buffer for the Playstation Motion Controller - -/* Bluetooth L2CAP states for L2CAP_task() */ -#define L2CAP_WAIT 0 -#define L2CAP_CONTROL_REQUEST 1 -#define L2CAP_CONTROL_SUCCESS 2 -#define L2CAP_INTERRUPT_SETUP 3 -#define L2CAP_INTERRUPT_REQUEST 4 -#define L2CAP_INTERRUPT_SUCCESS 5 -#define L2CAP_HID_ENABLE_SIXAXIS 6 -#define L2CAP_HID_PS3_LED 7 -#define L2CAP_DONE 8 -#define L2CAP_INTERRUPT_DISCONNECT 9 -#define L2CAP_CONTROL_DISCONNECT 10 - -/* L2CAP event flags */ -#define L2CAP_FLAG_CONNECTION_CONTROL_REQUEST 0x01 -#define L2CAP_FLAG_CONFIG_CONTROL_REQUEST 0x02 -#define L2CAP_FLAG_CONFIG_CONTROL_SUCCESS 0x04 -#define L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST 0x08 -#define L2CAP_FLAG_CONFIG_INTERRUPT_REQUEST 0x10 -#define L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS 0x20 -#define L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE 0x40 -#define L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE 0x80 - -/*Macros for L2CAP event flag tests */ -#define l2cap_connection_request_control_flag (l2cap_event_flag & L2CAP_FLAG_CONNECTION_CONTROL_REQUEST) -#define l2cap_config_request_control_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_CONTROL_REQUEST) -#define l2cap_config_success_control_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_CONTROL_SUCCESS) -#define l2cap_connection_request_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST) -#define l2cap_config_request_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_INTERRUPT_REQUEST) -#define l2cap_config_success_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS) -#define l2cap_disconnect_response_control_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE) -#define l2cap_disconnect_response_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE) +#define HID_BUFFERSIZE 50 // Size of the buffer for the Playstation Motion Controller /** * This BluetoothService class implements support for all the official PS3 Controllers: @@ -249,7 +216,7 @@ private: /* variables used by high level L2CAP task */ uint8_t l2cap_state; - uint16_t l2cap_event_flag; // L2CAP flags of received Bluetooth events + uint32_t l2cap_event_flag; // L2CAP flags of received Bluetooth events unsigned long timer; diff --git a/SPP.h b/SPP.h index 201fb77b..47493fd5 100644 --- a/SPP.h +++ b/SPP.h @@ -20,41 +20,6 @@ #include "BTD.h" -/* Bluetooth L2CAP states for SDP_task() */ -#define L2CAP_SDP_WAIT 0 -#define L2CAP_SDP_REQUEST 1 -#define L2CAP_SDP_SUCCESS 2 -#define L2CAP_SDP_DONE 3 -#define L2CAP_DISCONNECT_RESPONSE 4 - -/* Bluetooth L2CAP states for RFCOMM_task() */ -#define L2CAP_RFCOMM_WAIT 0 -#define L2CAP_RFCOMM_REQUEST 1 -#define L2CAP_RFCOMM_SUCCESS 2 -#define L2CAP_RFCOMM_DONE 3 - -/* L2CAP event flags */ -#define L2CAP_FLAG_CONNECTION_SDP_REQUEST 0x001 -#define L2CAP_FLAG_CONNECTION_RFCOMM_REQUEST 0x002 -#define L2CAP_FLAG_CONFIG_SDP_REQUEST 0x004 -#define L2CAP_FLAG_CONFIG_RFCOMM_REQUEST 0x008 -#define L2CAP_FLAG_CONFIG_SDP_SUCCESS 0x010 -#define L2CAP_FLAG_CONFIG_RFCOMM_SUCCESS 0x020 -#define L2CAP_FLAG_DISCONNECT_SDP_REQUEST 0x040 -#define L2CAP_FLAG_DISCONNECT_RFCOMM_REQUEST 0x080 -#define L2CAP_FLAG_DISCONNECT_RESPONSE 0x100 - -/* Macros for L2CAP event flag tests */ -#define l2cap_connection_request_sdp_flag (l2cap_event_flag & L2CAP_FLAG_CONNECTION_SDP_REQUEST) -#define l2cap_connection_request_rfcomm_flag (l2cap_event_flag & L2CAP_FLAG_CONNECTION_RFCOMM_REQUEST) -#define l2cap_config_request_sdp_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_SDP_REQUEST) -#define l2cap_config_request_rfcomm_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_RFCOMM_REQUEST) -#define l2cap_config_success_sdp_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_SDP_SUCCESS) -#define l2cap_config_success_rfcomm_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_RFCOMM_SUCCESS) -#define l2cap_disconnect_request_sdp_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_SDP_REQUEST) -#define l2cap_disconnect_request_rfcomm_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_RFCOMM_REQUEST) -#define l2cap_disconnect_response_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_RESPONSE) - /* Used for SDP */ #define SDP_SERVICE_SEARCH_ATTRIBUTE_REQUEST_PDU 0x06 // See the RFCOMM specs #define SDP_SERVICE_SEARCH_ATTRIBUTE_RESPONSE_PDU 0x07 // See the RFCOMM specs @@ -91,7 +56,7 @@ /** * This BluetoothService class implements the Serial Port Protocol (SPP). - * It inherits the Arduino Stream class. This allows it to use all the standard Arduino print functions. + * It inherits the Arduino Stream class. This allows it to use all the standard Arduino print and stream functions. */ class SPP : public BluetoothService, public Stream { public: @@ -201,7 +166,7 @@ private: /* 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 + uint32_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 diff --git a/Wii.cpp b/Wii.cpp index d55d4e79..73e638d1 100755 --- a/Wii.cpp +++ b/Wii.cpp @@ -375,7 +375,7 @@ void WII::ACLData(uint8_t* l2capinbuf) { Notify(PSTR("\r\nInactive Wii Motion Plus"), 0x80); Notify(PSTR("\r\nPlease unplug the Motion Plus, disconnect the Wiimote and then replug the Motion Plus Extension"), 0x80); #endif - stateCounter = 300; // Skip the rest in "L2CAP_CHECK_MOTION_PLUS_STATE" + stateCounter = 300; // Skip the rest in "WII_CHECK_MOTION_PLUS_STATE" } else if (l2capinbuf[16] == 0x00 && l2capinbuf[17] == 0xA4 && l2capinbuf[18] == 0x20 && l2capinbuf[19] == 0x01 && l2capinbuf[20] == 0x20) { #ifdef DEBUG_USB_HOST Notify(PSTR("\r\nWii U Pro Controller connected"), 0x80); @@ -648,7 +648,7 @@ void WII::L2CAP_task() { pBtd->connectToWii = false; pBtd->pairWithWii = false; stateCounter = 0; - l2cap_state = L2CAP_CHECK_MOTION_PLUS_STATE; + l2cap_state = WII_CHECK_MOTION_PLUS_STATE; } break; @@ -711,7 +711,7 @@ void WII::Run() { } break; - case L2CAP_CHECK_MOTION_PLUS_STATE: + case WII_CHECK_MOTION_PLUS_STATE: #ifdef DEBUG_USB_HOST if (stateCounter == 0) // Only print onnce Notify(PSTR("\r\nChecking if a Motion Plus is connected"), 0x80); @@ -721,7 +721,7 @@ void WII::Run() { checkMotionPresent(); // Check if there is a motion plus connected if (motion_plus_connected_flag) { stateCounter = 0; - l2cap_state = L2CAP_INIT_MOTION_PLUS_STATE; + l2cap_state = WII_INIT_MOTION_PLUS_STATE; timer = micros(); if (unknownExtensionConnected) { @@ -736,11 +736,11 @@ void WII::Run() { Notify(PSTR("\r\nNo Motion Plus was detected"), 0x80); #endif stateCounter = 0; - l2cap_state = L2CAP_CHECK_EXTENSION_STATE; + l2cap_state = WII_CHECK_EXTENSION_STATE; } break; - case L2CAP_CHECK_EXTENSION_STATE: // This is used to check if there is anything plugged in to the extension port + case WII_CHECK_EXTENSION_STATE: // This is used to check if there is anything plugged in to the extension port #ifdef DEBUG_USB_HOST if (stateCounter == 0) // Only print onnce Notify(PSTR("\r\nChecking if there is any extension connected"), 0x80); @@ -760,11 +760,11 @@ void WII::Run() { unknownExtensionConnected = false; } else if (stateCounter == 400) { stateCounter = 0; - l2cap_state = L2CAP_LED_STATE; + l2cap_state = TURN_ON_LED; } break; - case L2CAP_INIT_MOTION_PLUS_STATE: + case WII_INIT_MOTION_PLUS_STATE: stateCounter++; if (stateCounter == 1) initMotionPlus(); @@ -775,11 +775,11 @@ void WII::Run() { else if (stateCounter == 300) { stateCounter = 0; unknownExtensionConnected = false; // The motion plus will send a status report when it's activated, we will set this to false so it doesn't reinitialize the Motion Plus - l2cap_state = L2CAP_LED_STATE; + l2cap_state = TURN_ON_LED; } break; - case L2CAP_LED_STATE: + case TURN_ON_LED: if (nunchuck_connected_flag) nunchuckConnected = true; wiimoteConnected = true; diff --git a/Wii.h b/Wii.h index 397b837f..0d8f7c96 100755 --- a/Wii.h +++ b/Wii.h @@ -26,55 +26,12 @@ /** You will have to uncomment this to use the IR camera */ //#define WIICAMERA -/* Bluetooth L2CAP states for L2CAP_task() */ -#define L2CAP_WAIT 0 - -// These states are used if the Wiimote is the host -#define L2CAP_CONTROL_SUCCESS 1 -#define L2CAP_INTERRUPT_SETUP 2 - -// These states are used if the Arduino is the host -#define L2CAP_CONTROL_CONNECT_REQUEST 3 -#define L2CAP_CONTROL_CONFIG_REQUEST 4 -#define L2CAP_INTERRUPT_CONNECT_REQUEST 5 - -#define L2CAP_INTERRUPT_CONFIG_REQUEST 6 - -#define L2CAP_CHECK_MOTION_PLUS_STATE 7 -#define L2CAP_CHECK_EXTENSION_STATE 8 -#define L2CAP_INIT_MOTION_PLUS_STATE 9 -#define L2CAP_LED_STATE 10 -#define L2CAP_DONE 11 - -#define L2CAP_INTERRUPT_DISCONNECT 12 -#define L2CAP_CONTROL_DISCONNECT 13 - -/* L2CAP event flags */ -#define L2CAP_FLAG_CONTROL_CONNECTED 0x001 -#define L2CAP_FLAG_INTERRUPT_CONNECTED 0x002 -#define L2CAP_FLAG_CONFIG_CONTROL_SUCCESS 0x004 -#define L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS 0x008 -#define L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE 0x040 -#define L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE 0x080 -#define L2CAP_FLAG_CONNECTION_CONTROL_REQUEST 0x100 -#define L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST 0x200 - -/* Macros for L2CAP event flag tests */ -#define l2cap_connected_control_flag (l2cap_event_flag & L2CAP_FLAG_CONTROL_CONNECTED) -#define l2cap_connected_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_INTERRUPT_CONNECTED) -#define l2cap_config_success_control_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_CONTROL_SUCCESS) -#define l2cap_config_success_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS) -#define l2cap_disconnect_response_control_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE) -#define l2cap_disconnect_response_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE) -#define l2cap_connection_request_control_flag (l2cap_event_flag & L2CAP_FLAG_CONNECTION_CONTROL_REQUEST) -#define l2cap_connection_request_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST) - /* Wii event flags */ -#define WII_FLAG_MOTION_PLUS_CONNECTED 0x400 -#define WII_FLAG_NUNCHUCK_CONNECTED 0x800 +#define WII_FLAG_MOTION_PLUS_CONNECTED 0x01 +#define WII_FLAG_NUNCHUCK_CONNECTED 0x02 -#define motion_plus_connected_flag (l2cap_event_flag & WII_FLAG_MOTION_PLUS_CONNECTED) -#define nunchuck_connected_flag (l2cap_event_flag & WII_FLAG_NUNCHUCK_CONNECTED) +#define motion_plus_connected_flag (wii_event_flag & WII_FLAG_MOTION_PLUS_CONNECTED) +#define nunchuck_connected_flag (wii_event_flag & WII_FLAG_NUNCHUCK_CONNECTED) /** Enum used to read the joystick on the Nunchuck. */ enum Hat { @@ -447,7 +404,8 @@ private: /* Variables used by high level L2CAP task */ uint8_t l2cap_state; - uint16_t l2cap_event_flag; // l2cap flags of received Bluetooth events + uint32_t l2cap_event_flag; // L2CAP flags of received Bluetooth events + uint8_t wii_event_flag; // Used for Wii flags uint32_t ButtonState; uint32_t OldButtonState;