Merge branch 'master' into xxxajk

This commit is contained in:
Kristian Lauszus 2013-11-24 15:32:52 +01:00
commit 5b9fb51db0
34 changed files with 1072 additions and 925 deletions

45
BTD.cpp
View file

@ -16,7 +16,7 @@
*/ */
#include "BTD.h" #include "BTD.h"
// To enable serial debugging uncomment "#define DEBUG_USB_HOST" in message.h // To enable serial debugging see "settings.h"
//#define EXTRADEBUG // Uncomment to get even more debugging data //#define EXTRADEBUG // Uncomment to get even more debugging data
const uint8_t BTD::BTD_CONTROL_PIPE = 0; const uint8_t BTD::BTD_CONTROL_PIPE = 0;
@ -402,14 +402,10 @@ void BTD::HCI_event_task() {
break; break;
case EV_COMMAND_STATUS: case EV_COMMAND_STATUS:
if (hcibuf[2]) { // show status on serial if not OK if (hcibuf[2]) { // Show status on serial if not OK
#ifdef DEBUG_USB_HOST #ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nHCI Command Failed: "), 0x80); Notify(PSTR("\r\nHCI Command Failed: "), 0x80);
D_PrintHex<uint8_t > (hcibuf[2], 0x80); D_PrintHex<uint8_t > (hcibuf[2], 0x80);
Notify(PSTR(" "), 0x80);
D_PrintHex<uint8_t > (hcibuf[4], 0x80);
Notify(PSTR(" "), 0x80);
D_PrintHex<uint8_t > (hcibuf[5], 0x80);
#endif #endif
} }
break; break;
@ -465,15 +461,18 @@ void BTD::HCI_event_task() {
case EV_CONNECT_COMPLETE: case EV_CONNECT_COMPLETE:
hci_event_flag |= HCI_FLAG_CONNECT_EVENT; hci_event_flag |= HCI_FLAG_CONNECT_EVENT;
if (!hcibuf[2]) { // check if connected OK if (!hcibuf[2]) { // check if connected OK
#ifdef EXTRADEBUG
Notify(PSTR("\r\nConnection established"), 0x80);
#endif
hci_handle = hcibuf[3] | ((hcibuf[4] & 0x0F) << 8); // store the handle for the ACL connection hci_handle = hcibuf[3] | ((hcibuf[4] & 0x0F) << 8); // store the handle for the ACL connection
hci_event_flag |= HCI_FLAG_CONN_COMPLETE; // set connection complete flag hci_event_flag |= HCI_FLAG_CONN_COMPLETE; // set connection complete flag
} } else {
#ifdef DEBUG_USB_HOST
else {
Notify(PSTR("\r\nConnection Failed"), 0x80);
hci_state = HCI_CHECK_WII_SERVICE; hci_state = HCI_CHECK_WII_SERVICE;
} #ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nConnection Failed: "), 0x80);
D_PrintHex<uint8_t > (hcibuf[2], 0x80);
#endif #endif
}
break; break;
case EV_DISCONNECT_COMPLETE: case EV_DISCONNECT_COMPLETE:
@ -970,16 +969,20 @@ void BTD::hci_inquiry_cancel() {
} }
void BTD::hci_connect() { void BTD::hci_connect() {
hci_connect(disc_bdaddr); // Use last discovered device
}
void BTD::hci_connect(uint8_t *bdaddr) {
hci_event_flag &= ~(HCI_FLAG_CONN_COMPLETE | HCI_FLAG_CONNECT_EVENT); hci_event_flag &= ~(HCI_FLAG_CONN_COMPLETE | HCI_FLAG_CONNECT_EVENT);
hcibuf[0] = 0x05; hcibuf[0] = 0x05;
hcibuf[1] = 0x01 << 2; // HCI OGF = 1 hcibuf[1] = 0x01 << 2; // HCI OGF = 1
hcibuf[2] = 0x0D; // parameter Total Length = 13 hcibuf[2] = 0x0D; // parameter Total Length = 13
hcibuf[3] = disc_bdaddr[0]; // 6 octet bdaddr hcibuf[3] = bdaddr[0]; // 6 octet bdaddr (LSB)
hcibuf[4] = disc_bdaddr[1]; hcibuf[4] = bdaddr[1];
hcibuf[5] = disc_bdaddr[2]; hcibuf[5] = bdaddr[2];
hcibuf[6] = disc_bdaddr[3]; hcibuf[6] = bdaddr[3];
hcibuf[7] = disc_bdaddr[4]; hcibuf[7] = bdaddr[4];
hcibuf[8] = disc_bdaddr[5]; hcibuf[8] = bdaddr[5];
hcibuf[9] = 0x18; // DM1 or DH1 may be used hcibuf[9] = 0x18; // DM1 or DH1 may be used
hcibuf[10] = 0xCC; // DM3, DH3, DM5, DH5 may be used hcibuf[10] = 0xCC; // DM3, DH3, DM5, DH5 may be used
hcibuf[11] = 0x01; // Page repetition mode R1 hcibuf[11] = 0x01; // Page repetition mode R1
@ -1078,7 +1081,7 @@ void BTD::hci_disconnect(uint16_t handle) { // This is called by the different s
} }
void BTD::hci_write_class_of_device() { // See http://bluetooth-pentest.narod.ru/software/bluetooth_class_of_device-service_generator.html void BTD::hci_write_class_of_device() { // See http://bluetooth-pentest.narod.ru/software/bluetooth_class_of_device-service_generator.html
hcibuf[0] = 0x24; // HCI OCF = 3 hcibuf[0] = 0x24; // HCI OCF = 24
hcibuf[1] = 0x03 << 2; // HCI OGF = 3 hcibuf[1] = 0x03 << 2; // HCI OGF = 3
hcibuf[2] = 0x03; // parameter length = 3 hcibuf[2] = 0x03; // parameter length = 3
hcibuf[3] = 0x04; // Robot hcibuf[3] = 0x04; // Robot
@ -1258,9 +1261,9 @@ void BTD::setBdaddr(uint8_t* bdaddr) {
buf[1] = 0x00; buf[1] = 0x00;
for (uint8_t i = 0; i < 6; i++) for (uint8_t i = 0; i < 6; i++)
buf[i + 2] = bdaddr[5 - i]; // Copy into buffer, has to be written reversed buf[i + 2] = bdaddr[5 - i]; // Copy into buffer, has to be written reversed, so it is MSB first
// bmRequest = Host to device (0x00) | Class (0x20) | Interface (0x01) = 0x21, bRequest = Set Report (0x09), Report ID (0xF5), Report Type (Feature 0x03), interface (0x00), datalength, datalength, data) // bmRequest = Host to device (0x00) | Class (0x20) | Interface (0x01) = 0x21, bRequest = Set Report (0x09), Report ID (0xF5), Report Type (Feature 0x03), interface (0x00), datalength, datalength, data
pUsb->ctrlReq(bAddress, epInfo[BTD_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0xF5, 0x03, 0x00, 8, 8, buf, NULL); pUsb->ctrlReq(bAddress, epInfo[BTD_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0xF5, 0x03, 0x00, 8, 8, buf, NULL);
} }
@ -1276,6 +1279,6 @@ void BTD::setMoveBdaddr(uint8_t* bdaddr) {
for (uint8_t i = 0; i < 6; i++) for (uint8_t i = 0; i < 6; i++)
buf[i + 1] = bdaddr[i]; buf[i + 1] = bdaddr[i];
// bmRequest = Host to device (0x00) | Class (0x20) | Interface (0x01) = 0x21, bRequest = Set Report (0x09), Report ID (0x05), Report Type (Feature 0x03), interface (0x00), datalength, datalength, data) // bmRequest = Host to device (0x00) | Class (0x20) | Interface (0x01) = 0x21, bRequest = Set Report (0x09), Report ID (0x05), Report Type (Feature 0x03), interface (0x00), datalength, datalength, data
pUsb->ctrlReq(bAddress, epInfo[BTD_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0x05, 0x03, 0x00, 11, 11, buf, NULL); pUsb->ctrlReq(bAddress, epInfo[BTD_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0x05, 0x03, 0x00, 11, 11, buf, NULL);
} }

15
BTD.h
View file

@ -219,7 +219,13 @@ public:
* @return Returns true if the device's VID and PID matches this driver. * @return Returns true if the device's VID and PID matches this driver.
*/ */
virtual boolean VIDPIDOK(uint16_t vid, uint16_t pid) { virtual boolean VIDPIDOK(uint16_t vid, uint16_t pid) {
return ((vid == PS3_VID || vid == IOGEAR_GBU521_VID) && (pid == PS3_PID || pid == PS3NAVIGATION_PID || pid == PS3MOVE_PID || pid == IOGEAR_GBU521_PID)); if (vid == IOGEAR_GBU521_VID && pid == IOGEAR_GBU521_PID)
return true;
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) { // Check if Bluetooth address is set
if (vid == PS3_VID && (pid == PS3_PID || pid == PS3NAVIGATION_PID || pid == PS3MOVE_PID))
return true;
}
return false;
}; };
/**@}*/ /**@}*/
@ -307,8 +313,13 @@ public:
void hci_inquiry(); void hci_inquiry();
/** Cancel a HCI inquiry. */ /** Cancel a HCI inquiry. */
void hci_inquiry_cancel(); void hci_inquiry_cancel();
/** Connect to a device. */ /** Connect to last device communicated with. */
void hci_connect(); void hci_connect();
/**
* Connect to device.
* @param bdaddr Bluetooth address of the device.
*/
void hci_connect(uint8_t *bdaddr);
/** Used to a set the class of the device. */ /** Used to a set the class of the device. */
void hci_write_class_of_device(); void hci_write_class_of_device();
/**@}*/ /**@}*/

View file

@ -16,7 +16,7 @@
*/ */
#include "PS3BT.h" #include "PS3BT.h"
// To enable serial debugging uncomment "#define DEBUG_USB_HOST" in message.h // To enable serial debugging see "settings.h"
//#define EXTRADEBUG // Uncomment to get even more debugging data //#define EXTRADEBUG // Uncomment to get even more debugging data
//#define PRINTREPORT // Uncomment to print the report send by the PS3 Controllers //#define PRINTREPORT // Uncomment to print the report send by the PS3 Controllers
@ -56,25 +56,19 @@ bool PS3BT::getButtonPress(Button b) {
bool PS3BT::getButtonClick(Button b) { bool PS3BT::getButtonClick(Button b) {
uint32_t button = pgm_read_dword(&BUTTONS[(uint8_t)b]); uint32_t button = pgm_read_dword(&BUTTONS[(uint8_t)b]);
bool click = (ButtonClickState & button); bool click = (ButtonClickState & button);
ButtonClickState &= ~button; // clear "click" event ButtonClickState &= ~button; // Clear "click" event
return click; return click;
} }
uint8_t PS3BT::getAnalogButton(Button a) { uint8_t PS3BT::getAnalogButton(Button a) {
if (l2capinbuf == NULL)
return 0;
return (uint8_t)(l2capinbuf[pgm_read_byte(&ANALOGBUTTONS[(uint8_t)a])]); return (uint8_t)(l2capinbuf[pgm_read_byte(&ANALOGBUTTONS[(uint8_t)a])]);
} }
uint8_t PS3BT::getAnalogHat(AnalogHat a) { uint8_t PS3BT::getAnalogHat(AnalogHat a) {
if (l2capinbuf == NULL)
return 0;
return (uint8_t)(l2capinbuf[(uint8_t)a + 15]); return (uint8_t)(l2capinbuf[(uint8_t)a + 15]);
} }
int16_t PS3BT::getSensor(Sensor a) { int16_t PS3BT::getSensor(Sensor a) {
if (l2capinbuf == NULL)
return 0;
if (PS3Connected) { if (PS3Connected) {
if (a == aX || a == aY || a == aZ || a == gZ) if (a == aX || a == aY || a == aZ || a == gZ)
return ((l2capinbuf[(uint16_t)a] << 8) | l2capinbuf[(uint16_t)a + 1]); return ((l2capinbuf[(uint16_t)a] << 8) | l2capinbuf[(uint16_t)a + 1]);
@ -92,13 +86,11 @@ int16_t PS3BT::getSensor(Sensor a) {
} }
double PS3BT::getAngle(Angle a) { double PS3BT::getAngle(Angle a) {
double accXval; double accXval, accYval, accZval;
double accYval;
double accZval;
if (PS3Connected) { if (PS3Connected) {
// Data for the Kionix KXPC4 used in the DualShock 3 // Data for the Kionix KXPC4 used in the DualShock 3
const double zeroG = 511.5; // 1.65/3.3*1023 (1,65V) const double zeroG = 511.5; // 1.65/3.3*1023 (1.65V)
accXval = -((double)getSensor(aX) - zeroG); accXval = -((double)getSensor(aX) - zeroG);
accYval = -((double)getSensor(aY) - zeroG); accYval = -((double)getSensor(aY) - zeroG);
accZval = -((double)getSensor(aZ) - zeroG); accZval = -((double)getSensor(aZ) - zeroG);
@ -114,13 +106,10 @@ double PS3BT::getAngle(Angle a) {
// Convert to 360 degrees resolution // Convert to 360 degrees resolution
// atan2 outputs the value of -π to π (radians) // atan2 outputs the value of -π to π (radians)
// We are then converting it to 0 to 2π and then to degrees // We are then converting it to 0 to 2π and then to degrees
if (a == Pitch) { if (a == Pitch)
double angle = (atan2(accYval, accZval) + PI) * RAD_TO_DEG; return (atan2(accYval, accZval) + PI) * RAD_TO_DEG;
return angle; else
} else { return (atan2(accXval, accZval) + PI) * RAD_TO_DEG;
double angle = (atan2(accXval, accZval) + PI) * RAD_TO_DEG;
return angle;
}
} }
double PS3BT::get9DOFValues(Sensor a) { // Thanks to Manfred Piendl double PS3BT::get9DOFValues(Sensor a) { // Thanks to Manfred Piendl
@ -168,11 +157,7 @@ String PS3BT::getTemperature() {
} }
bool PS3BT::getStatus(Status c) { bool PS3BT::getStatus(Status c) {
if (l2capinbuf == NULL) return (l2capinbuf[(uint16_t)c >> 8] == ((uint8_t)c & 0xff));
return false;
if (l2capinbuf[(uint16_t)c >> 8] == ((uint8_t)c & 0xff))
return true;
return false;
} }
String PS3BT::getStatusString() { String PS3BT::getStatusString() {
@ -266,8 +251,7 @@ void PS3BT::ACLData(uint8_t* ACLData) {
} }
} }
if (((ACLData[0] | (ACLData[1] << 8)) == (hci_handle | 0x2000))) { //acl_handle_ok if (((ACLData[0] | (ACLData[1] << 8)) == (hci_handle | 0x2000))) { //acl_handle_ok
for (uint8_t i = 0; i < BULK_MAXPKTSIZE; i++) memcpy(l2capinbuf, ACLData, BULK_MAXPKTSIZE);
l2capinbuf[i] = ACLData[i];
if ((l2capinbuf[6] | (l2capinbuf[7] << 8)) == 0x0001) { //l2cap_control - Channel ID for ACL-U if ((l2capinbuf[6] | (l2capinbuf[7] << 8)) == 0x0001) { //l2cap_control - Channel ID for ACL-U
if (l2capinbuf[8] == L2CAP_CMD_COMMAND_REJECT) { if (l2capinbuf[8] == L2CAP_CMD_COMMAND_REJECT) {
#ifdef DEBUG_USB_HOST #ifdef DEBUG_USB_HOST
@ -457,8 +441,7 @@ void PS3BT::L2CAP_task() {
Notify(PSTR("\r\nHID Interrupt Successfully Configured"), 0x80); Notify(PSTR("\r\nHID Interrupt Successfully Configured"), 0x80);
#endif #endif
if (remote_name[0] == 'M') { // First letter in Motion Controller ('M') if (remote_name[0] == 'M') { // First letter in Motion Controller ('M')
for (uint8_t i = 0; i < BULK_MAXPKTSIZE; i++) // Reset l2cap in buffer as it sometimes read it as a button has been pressed memset(l2capinbuf, 0, BULK_MAXPKTSIZE); // Reset l2cap in buffer as it sometimes read it as a button has been pressed
l2capinbuf[i] = 0;
l2cap_state = L2CAP_HID_PS3_LED; l2cap_state = L2CAP_HID_PS3_LED;
} else } else
l2cap_state = L2CAP_HID_ENABLE_SIXAXIS; l2cap_state = L2CAP_HID_ENABLE_SIXAXIS;
@ -497,8 +480,7 @@ void PS3BT::Run() {
switch (l2cap_state) { switch (l2cap_state) {
case L2CAP_HID_ENABLE_SIXAXIS: case L2CAP_HID_ENABLE_SIXAXIS:
if (millis() - timer > 1000) { // loop 1 second before sending the command if (millis() - timer > 1000) { // loop 1 second before sending the command
for (uint8_t i = 0; i < BULK_MAXPKTSIZE; i++) // Reset l2cap in buffer as it sometimes read it as a button has been pressed memset(l2capinbuf, 0, BULK_MAXPKTSIZE); // Reset l2cap in buffer as it sometimes read it as a button has been pressed
l2capinbuf[i] = 0;
for (uint8_t i = 15; i < 19; i++) for (uint8_t i = 15; i < 19; i++)
l2capinbuf[i] = 0x7F; // Set the analog joystick values to center position l2capinbuf[i] = 0x7F; // Set the analog joystick values to center position
enable_sixaxis(); enable_sixaxis();
@ -560,8 +542,12 @@ void PS3BT::HID_Command(uint8_t* data, uint8_t nbytes) {
} }
void PS3BT::setAllOff() { void PS3BT::setAllOff() {
for (uint8_t i = 0; i < PS3_REPORT_BUFFER_SIZE; i++) HIDBuffer[3] = 0x00; // Rumble bytes
HIDBuffer[i + 2] = pgm_read_byte(&PS3_REPORT_BUFFER[i]); // First two bytes reserved for report type and ID HIDBuffer[4] = 0x00;
HIDBuffer[5] = 0x00;
HIDBuffer[6] = 0x00;
HIDBuffer[11] = 0x00; // LED byte
HID_Command(HIDBuffer, HID_BUFFERSIZE); HID_Command(HIDBuffer, HID_BUFFERSIZE);
} }
@ -596,6 +582,7 @@ void PS3BT::setLedRaw(uint8_t value) {
HIDBuffer[11] = value << 1; HIDBuffer[11] = value << 1;
HID_Command(HIDBuffer, HID_BUFFERSIZE); HID_Command(HIDBuffer, HID_BUFFERSIZE);
} }
void PS3BT::setLedOff(LED a) { void PS3BT::setLedOff(LED a) {
HIDBuffer[11] &= ~((uint8_t)((pgm_read_byte(&LEDS[(uint8_t)a]) & 0x0f) << 1)); HIDBuffer[11] &= ~((uint8_t)((pgm_read_byte(&LEDS[(uint8_t)a]) & 0x0f) << 1));
HID_Command(HIDBuffer, HID_BUFFERSIZE); HID_Command(HIDBuffer, HID_BUFFERSIZE);
@ -611,7 +598,7 @@ void PS3BT::setLedToggle(LED a) {
HID_Command(HIDBuffer, HID_BUFFERSIZE); HID_Command(HIDBuffer, HID_BUFFERSIZE);
} }
void PS3BT::enable_sixaxis() { //Command used to enable the Dualshock 3 and Navigation controller to send data via USB void PS3BT::enable_sixaxis() { // Command used to enable the Dualshock 3 and Navigation controller to send data via Bluetooth
uint8_t cmd_buf[6]; uint8_t cmd_buf[6];
cmd_buf[0] = 0x53; // HID BT Set_report (0x50) | Report Type (Feature 0x03) cmd_buf[0] = 0x53; // HID BT Set_report (0x50) | Report Type (Feature 0x03)
cmd_buf[1] = 0xF4; // Report ID cmd_buf[1] = 0xF4; // Report ID
@ -633,7 +620,7 @@ void PS3BT::HIDMove_Command(uint8_t* data, uint8_t nbytes) {
} }
void PS3BT::moveSetBulb(uint8_t r, uint8_t g, uint8_t b) { //Use this to set the Color using RGB values void PS3BT::moveSetBulb(uint8_t r, uint8_t g, uint8_t b) { //Use this to set the Color using RGB values
//set the Bulb's values into the write buffer // Set the Bulb's values into the write buffer
HIDMoveBuffer[3] = r; HIDMoveBuffer[3] = r;
HIDMoveBuffer[4] = g; HIDMoveBuffer[4] = g;
HIDMoveBuffer[5] = b; HIDMoveBuffer[5] = b;
@ -650,7 +637,7 @@ void PS3BT::moveSetRumble(uint8_t rumble) {
if (rumble < 64 && rumble != 0) // The rumble value has to at least 64, or approximately 25% (64/255*100) if (rumble < 64 && rumble != 0) // The rumble value has to at least 64, or approximately 25% (64/255*100)
Notify(PSTR("\r\nThe rumble value has to at least 64, or approximately 25%"), 0x80); Notify(PSTR("\r\nThe rumble value has to at least 64, or approximately 25%"), 0x80);
#endif #endif
//set the rumble value into the write buffer // Set the rumble value into the write buffer
HIDMoveBuffer[7] = rumble; HIDMoveBuffer[7] = rumble;
HIDMove_Command(HIDMoveBuffer, HID_BUFFERSIZE); HIDMove_Command(HIDMoveBuffer, HID_BUFFERSIZE);

View file

@ -176,6 +176,10 @@ public:
* @param value See: ::LED enum. * @param value See: ::LED enum.
*/ */
void setLedRaw(uint8_t value); void setLedRaw(uint8_t value);
/** Turn all LEDs off. */
void setLedOff() {
setLedRaw(0);
}
/** /**
* Turn the specific ::LED off. * Turn the specific ::LED off.
* @param a The ::LED to turn off. * @param a The ::LED to turn off.

View file

@ -16,7 +16,7 @@
*/ */
#include "PS3USB.h" #include "PS3USB.h"
// To enable serial debugging uncomment "#define DEBUG_USB_HOST" in message.h // To enable serial debugging see "settings.h"
//#define EXTRADEBUG // Uncomment to get even more debugging data //#define EXTRADEBUG // Uncomment to get even more debugging data
//#define PRINTREPORT // Uncomment to print the report send by the PS3 Controllers //#define PRINTREPORT // Uncomment to print the report send by the PS3 Controllers
@ -279,8 +279,7 @@ uint8_t PS3USB::Poll() {
#endif #endif
} }
} else if (PS3MoveConnected) { // One can only set the color of the bulb, set the rumble, set and get the bluetooth address and calibrate the magnetometer via USB } else if (PS3MoveConnected) { // One can only set the color of the bulb, set the rumble, set and get the bluetooth address and calibrate the magnetometer via USB
if (millis() - timer > 4000) // Send at least every 4th second if (millis() - timer > 4000) { // Send at least every 4th second
{
Move_Command(writeBuf, MOVE_REPORT_BUFFER_SIZE); // The Bulb and rumble values, has to be written again and again, for it to stay turned on Move_Command(writeBuf, MOVE_REPORT_BUFFER_SIZE); // The Bulb and rumble values, has to be written again and again, for it to stay turned on
timer = millis(); timer = millis();
} }
@ -289,9 +288,6 @@ uint8_t PS3USB::Poll() {
} }
void PS3USB::readReport() { void PS3USB::readReport() {
if (readBuf == NULL)
return;
ButtonState = (uint32_t)(readBuf[2] | ((uint16_t)readBuf[3] << 8) | ((uint32_t)readBuf[4] << 16)); ButtonState = (uint32_t)(readBuf[2] | ((uint16_t)readBuf[3] << 8) | ((uint32_t)readBuf[4] << 16));
//Notify(PSTR("\r\nButtonState", 0x80); //Notify(PSTR("\r\nButtonState", 0x80);
@ -305,8 +301,6 @@ void PS3USB::readReport() {
void PS3USB::printReport() { // Uncomment "#define PRINTREPORT" to print the report send by the PS3 Controllers void PS3USB::printReport() { // Uncomment "#define PRINTREPORT" to print the report send by the PS3 Controllers
#ifdef PRINTREPORT #ifdef PRINTREPORT
if (readBuf == NULL)
return;
for (uint8_t i = 0; i < PS3_REPORT_BUFFER_SIZE; i++) { for (uint8_t i = 0; i < PS3_REPORT_BUFFER_SIZE; i++) {
D_PrintHex<uint8_t > (readBuf[i], 0x80); D_PrintHex<uint8_t > (readBuf[i], 0x80);
Notify(PSTR(" "), 0x80); Notify(PSTR(" "), 0x80);
@ -322,25 +316,19 @@ bool PS3USB::getButtonPress(Button b) {
bool PS3USB::getButtonClick(Button b) { bool PS3USB::getButtonClick(Button b) {
uint32_t button = pgm_read_dword(&BUTTONS[(uint8_t)b]); uint32_t button = pgm_read_dword(&BUTTONS[(uint8_t)b]);
bool click = (ButtonClickState & button); bool click = (ButtonClickState & button);
ButtonClickState &= ~button; // clear "click" event ButtonClickState &= ~button; // Clear "click" event
return click; return click;
} }
uint8_t PS3USB::getAnalogButton(Button a) { uint8_t PS3USB::getAnalogButton(Button a) {
if (readBuf == NULL)
return 0;
return (uint8_t)(readBuf[(pgm_read_byte(&ANALOGBUTTONS[(uint8_t)a])) - 9]); return (uint8_t)(readBuf[(pgm_read_byte(&ANALOGBUTTONS[(uint8_t)a])) - 9]);
} }
uint8_t PS3USB::getAnalogHat(AnalogHat a) { uint8_t PS3USB::getAnalogHat(AnalogHat a) {
if (readBuf == NULL)
return 0;
return (uint8_t)(readBuf[((uint8_t)a + 6)]); return (uint8_t)(readBuf[((uint8_t)a + 6)]);
} }
uint16_t PS3USB::getSensor(Sensor a) { uint16_t PS3USB::getSensor(Sensor a) {
if (readBuf == NULL)
return 0;
return ((readBuf[((uint16_t)a) - 9] << 8) | readBuf[((uint16_t)a + 1) - 9]); return ((readBuf[((uint16_t)a) - 9] << 8) | readBuf[((uint16_t)a + 1) - 9]);
} }
@ -359,23 +347,16 @@ double PS3USB::getAngle(Angle a) {
// Convert to 360 degrees resolution // Convert to 360 degrees resolution
// atan2 outputs the value of -π to π (radians) // atan2 outputs the value of -π to π (radians)
// We are then converting it to 0 to 2π and then to degrees // We are then converting it to 0 to 2π and then to degrees
if (a == Pitch) { if (a == Pitch)
double angle = (atan2(accYval, accZval) + PI) * RAD_TO_DEG; return (atan2(accYval, accZval) + PI) * RAD_TO_DEG;
return angle; else
} else { return (atan2(accXval, accZval) + PI) * RAD_TO_DEG;
double angle = (atan2(accXval, accZval) + PI) * RAD_TO_DEG;
return angle;
}
} else } else
return 0; return 0;
} }
bool PS3USB::getStatus(Status c) { bool PS3USB::getStatus(Status c) {
if (readBuf == NULL) return (readBuf[((uint16_t)c >> 8) - 9] == ((uint8_t)c & 0xff));
return false;
if (readBuf[((uint16_t)c >> 8) - 9] == ((uint8_t)c & 0xff))
return true;
return false;
} }
String PS3USB::getStatusString() { String PS3USB::getStatusString() {
@ -428,9 +409,9 @@ void PS3USB::setAllOff() {
void PS3USB::setRumbleOff() { void PS3USB::setRumbleOff() {
writeBuf[1] = 0x00; writeBuf[1] = 0x00;
writeBuf[2] = 0x00; //low mode off writeBuf[2] = 0x00; // Low mode off
writeBuf[3] = 0x00; writeBuf[3] = 0x00;
writeBuf[4] = 0x00; //high mode off writeBuf[4] = 0x00; // High mode off
PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE); PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE);
} }
@ -474,18 +455,29 @@ void PS3USB::setLedToggle(LED a) {
PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE); PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE);
} }
void PS3USB::setBdaddr(uint8_t* BDADDR) { void PS3USB::setBdaddr(uint8_t *bdaddr) {
/* Set the internal bluetooth address */ /* Set the internal Bluetooth address */
uint8_t buf[8]; uint8_t buf[8];
buf[0] = 0x01; buf[0] = 0x01;
buf[1] = 0x00; buf[1] = 0x00;
for (uint8_t i = 0; i < 6; i++)
buf[i + 2] = BDADDR[5 - i]; //Copy into buffer, has to be written reversed
//bmRequest = Host to device (0x00) | Class (0x20) | Interface (0x01) = 0x21, bRequest = Set Report (0x09), Report ID (0xF5), Report Type (Feature 0x03), interface (0x00), datalength, datalength, data) for (uint8_t i = 0; i < 6; i++)
buf[i + 2] = bdaddr[5 - i]; // Copy into buffer, has to be written reversed, so it is MSB first
// bmRequest = Host to device (0x00) | Class (0x20) | Interface (0x01) = 0x21, bRequest = Set Report (0x09), Report ID (0xF5), Report Type (Feature 0x03), interface (0x00), datalength, datalength, data
pUsb->ctrlReq(bAddress, epInfo[PS3_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0xF5, 0x03, 0x00, 8, 8, buf, NULL); pUsb->ctrlReq(bAddress, epInfo[PS3_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0xF5, 0x03, 0x00, 8, 8, buf, NULL);
} }
void PS3USB::getBdaddr(uint8_t *bdaddr) {
uint8_t buf[8];
// bmRequest = Device to host (0x80) | Class (0x20) | Interface (0x01) = 0xA1, bRequest = Get Report (0x01), Report ID (0xF5), Report Type (Feature 0x03), interface (0x00), datalength, datalength, data
pUsb->ctrlReq(bAddress, epInfo[PS3_CONTROL_PIPE].epAddr, bmREQ_HID_IN, HID_REQUEST_GET_REPORT, 0xF5, 0x03, 0x00, 8, 8, buf, NULL);
for (uint8_t i = 0; i < 6; i++)
bdaddr[5 - i] = buf[i + 2]; // Copy into buffer reversed, so it is LSB first
}
void PS3USB::enable_sixaxis() { // Command used to enable the Dualshock 3 and Navigation controller to send data via USB void PS3USB::enable_sixaxis() { // Command used to enable the Dualshock 3 and Navigation controller to send data via USB
uint8_t cmd_buf[4]; uint8_t cmd_buf[4];
cmd_buf[0] = 0x42; // Special PS3 Controller enable commands cmd_buf[0] = 0x42; // Special PS3 Controller enable commands
@ -503,7 +495,7 @@ void PS3USB::Move_Command(uint8_t* data, uint16_t nbytes) {
} }
void PS3USB::moveSetBulb(uint8_t r, uint8_t g, uint8_t b) { // Use this to set the Color using RGB values void PS3USB::moveSetBulb(uint8_t r, uint8_t g, uint8_t b) { // Use this to set the Color using RGB values
// set the Bulb's values into the write buffer // Set the Bulb's values into the write buffer
writeBuf[2] = r; writeBuf[2] = r;
writeBuf[3] = g; writeBuf[3] = g;
writeBuf[4] = b; writeBuf[4] = b;
@ -520,14 +512,13 @@ void PS3USB::moveSetRumble(uint8_t rumble) {
if (rumble < 64 && rumble != 0) // The rumble value has to at least 64, or approximately 25% (64/255*100) if (rumble < 64 && rumble != 0) // The rumble value has to at least 64, or approximately 25% (64/255*100)
Notify(PSTR("\r\nThe rumble value has to at least 64, or approximately 25%"), 0x80); Notify(PSTR("\r\nThe rumble value has to at least 64, or approximately 25%"), 0x80);
#endif #endif
//set the rumble value into the write buffer writeBuf[6] = rumble; // Set the rumble value into the write buffer
writeBuf[6] = rumble;
Move_Command(writeBuf, MOVE_REPORT_BUFFER_SIZE); Move_Command(writeBuf, MOVE_REPORT_BUFFER_SIZE);
} }
void PS3USB::setMoveBdaddr(uint8_t* BDADDR) { void PS3USB::setMoveBdaddr(uint8_t *bdaddr) {
/* Set the internal bluetooth address */ /* Set the internal Bluetooth address */
uint8_t buf[11]; uint8_t buf[11];
buf[0] = 0x05; buf[0] = 0x05;
buf[7] = 0x10; buf[7] = 0x10;
@ -536,12 +527,34 @@ void PS3USB::setMoveBdaddr(uint8_t* BDADDR) {
buf[10] = 0x12; buf[10] = 0x12;
for (uint8_t i = 0; i < 6; i++) for (uint8_t i = 0; i < 6; i++)
buf[i + 1] = BDADDR[i]; buf[i + 1] = bdaddr[i];
//bmRequest = Host to device (0x00) | Class (0x20) | Interface (0x01) = 0x21, bRequest = Set Report (0x09), Report ID (0x05), Report Type (Feature 0x03), interface (0x00), datalength, datalength, data) // bmRequest = Host to device (0x00) | Class (0x20) | Interface (0x01) = 0x21, bRequest = Set Report (0x09), Report ID (0x05), Report Type (Feature 0x03), interface (0x00), datalength, datalength, data
pUsb->ctrlReq(bAddress, epInfo[PS3_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0x05, 0x03, 0x00, 11, 11, buf, NULL); pUsb->ctrlReq(bAddress, epInfo[PS3_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0x05, 0x03, 0x00, 11, 11, buf, NULL);
} }
void PS3USB::getMoveBdaddr(uint8_t *bdaddr) {
uint8_t buf[16];
// bmRequest = Device to host (0x80) | Class (0x20) | Interface (0x01) = 0xA1, bRequest = Get Report (0x01), Report ID (0x04), Report Type (Feature 0x03), interface (0x00), datalength, datalength, data
pUsb->ctrlReq(bAddress, epInfo[PS3_CONTROL_PIPE].epAddr, bmREQ_HID_IN, HID_REQUEST_GET_REPORT, 0x04, 0x03, 0x00, 16, 16, buf, NULL);
for (uint8_t i = 0; i < 6; i++)
bdaddr[i] = buf[10 + i];
}
void PS3USB::getMoveCalibration(uint8_t *data) {
uint8_t buf[49];
for (uint8_t i = 0; i < 3; i++) {
// bmRequest = Device to host (0x80) | Class (0x20) | Interface (0x01) = 0xA1, bRequest = Get Report (0x01), Report ID (0x10), Report Type (Feature 0x03), interface (0x00), datalength, datalength, data
pUsb->ctrlReq(bAddress, epInfo[PS3_CONTROL_PIPE].epAddr, bmREQ_HID_IN, HID_REQUEST_GET_REPORT, 0x10, 0x03, 0x00, 49, 49, buf, NULL);
for (byte j = 0; j < 49; j++)
data[49 * i + j] = buf[j];
}
}
void PS3USB::onInit() { void PS3USB::onInit() {
if (pFuncOnInit) if (pFuncOnInit)
pFuncOnInit(); // Call the user function pFuncOnInit(); // Call the user function

View file

@ -38,8 +38,11 @@
#define PS3NAVIGATION_PID 0x042F // Navigation controller #define PS3NAVIGATION_PID 0x042F // Navigation controller
#define PS3MOVE_PID 0x03D5 // Motion controller #define PS3MOVE_PID 0x03D5 // Motion controller
// used in control endpoint header for HID Commands // Used in control endpoint header for HID Commands
#define bmREQ_HID_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE #define bmREQ_HID_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
#define bmREQ_HID_IN USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
#define HID_REQUEST_GET_REPORT 0x01
#define HID_REQUEST_SET_REPORT 0x09 #define HID_REQUEST_SET_REPORT 0x09
#define PS3_MAX_ENDPOINTS 3 #define PS3_MAX_ENDPOINTS 3
@ -112,14 +115,34 @@ public:
/** /**
* Used to set the Bluetooth address inside the Dualshock 3 and Navigation controller. * Used to set the Bluetooth address inside the Dualshock 3 and Navigation controller.
* @param BDADDR Your dongles Bluetooth address. * Set using LSB first.
* @param bdaddr Your dongles Bluetooth address.
*/ */
void setBdaddr(uint8_t* BDADDR); void setBdaddr(uint8_t *bdaddr);
/**
* Used to get the Bluetooth address inside the Dualshock 3 and Navigation controller.
* Will return LSB first.
* @param bdaddr Your dongles Bluetooth address.
*/
void getBdaddr(uint8_t *bdaddr);
/** /**
* Used to set the Bluetooth address inside the Move controller. * Used to set the Bluetooth address inside the Move controller.
* @param BDADDR Your dongles Bluetooth address. * Set using LSB first.
* @param bdaddr Your dongles Bluetooth address.
*/ */
void setMoveBdaddr(uint8_t* BDADDR); void setMoveBdaddr(uint8_t *bdaddr);
/**
* Used to get the Bluetooth address inside the Move controller.
* Will return LSB first.
* @param bdaddr Your dongles Bluetooth address.
*/
void getMoveBdaddr(uint8_t *bdaddr);
/**
* Used to get the calibration data inside the Move controller.
* @param bdaddr Buffer to store data in. Must be at least 147 bytes
*/
void getMoveCalibration(uint8_t *data);
/** @name PS3 Controller functions */ /** @name PS3 Controller functions */
/** /**
@ -197,6 +220,10 @@ public:
* @param value See: ::LED enum. * @param value See: ::LED enum.
*/ */
void setLedRaw(uint8_t value); void setLedRaw(uint8_t value);
/** Turn all LEDs off. */
void setLedOff() {
setLedRaw(0);
}
/** /**
* Turn the specific ::LED off. * Turn the specific ::LED off.
* @param a The ::LED to turn off. * @param a The ::LED to turn off.

View file

@ -107,7 +107,7 @@ These libraries consist of the [PS3BT](PS3BT.cpp) and [PS3USB](PS3USB.cpp). Thes
In order to use your Playstation controller via Bluetooth you have to set the Bluetooth address of the dongle internally to your PS3 Controller. This can be achieved by plugging the controller in via USB and letting the library set it automatically. In order to use your Playstation controller via Bluetooth you have to set the Bluetooth address of the dongle internally to your PS3 Controller. This can be achieved by plugging the controller in via USB and letting the library set it automatically.
__Note:__ To obtain the address you have to plug in the Bluetooth dongle before connecting the controller, or alternatively you could set it in code like so: <https://github.com/felis/USB_Host_Shield_2.0/blob/master/examples/Bluetooth/PS3BT/PS3BT.ino#L12>. __Note:__ To obtain the address you have to plug in the Bluetooth dongle before connecting the controller, or alternatively you could set it in code like so: <https://github.com/felis/USB_Host_Shield_2.0/blob/master/examples/Bluetooth/PS3BT/PS3BT.ino#L15>.
For more information about the PS3 protocol see the official wiki: <https://github.com/felis/USB_Host_Shield_2.0/wiki/PS3-Information>. For more information about the PS3 protocol see the official wiki: <https://github.com/felis/USB_Host_Shield_2.0/wiki/PS3-Information>.
@ -184,7 +184,7 @@ After that you can simply create the instance like so:
WII Wii(&Btd); WII Wii(&Btd);
``` ```
Then just press any button any button on the Wiimote and it will connect to the dongle. Then just press any button on the Wiimote and it will then connect to the dongle.
Take a look at the example for more information: <https://github.com/felis/USB_Host_Shield_2.0/blob/master/examples/Bluetooth/Wii/Wii.ino>. Take a look at the example for more information: <https://github.com/felis/USB_Host_Shield_2.0/blob/master/examples/Bluetooth/Wii/Wii.ino>.

42
SPP.cpp
View file

@ -16,7 +16,7 @@
*/ */
#include "SPP.h" #include "SPP.h"
// To enable serial debugging uncomment "#define DEBUG_USB_HOST" in message.h // To enable serial debugging see "settings.h"
//#define EXTRADEBUG // Uncomment to get even more debugging data //#define EXTRADEBUG // Uncomment to get even more debugging data
//#define PRINTREPORT // Uncomment to print the report sent to the Arduino //#define PRINTREPORT // Uncomment to print the report sent to the Arduino
@ -270,12 +270,19 @@ void SPP::ACLData(uint8_t* l2capinbuf) {
/* Read the incoming message */ /* Read the incoming message */
if (rfcommChannelType == RFCOMM_UIH && rfcommChannel == rfcommChannelConnection) { if (rfcommChannelType == RFCOMM_UIH && rfcommChannel == rfcommChannelConnection) {
uint8_t length = l2capinbuf[10] >> 1; // Get length uint8_t length = l2capinbuf[10] >> 1; // Get length
uint8_t offset = l2capinbuf[4] - length - 4; // See if there is credit uint8_t offset = l2capinbuf[4] - length - 4; // Check if there is credit
if (rfcommAvailable + length <= sizeof (rfcommDataBuffer)) { // Don't add data to buffer if it would be full if (checkFcs(&l2capinbuf[8], l2capinbuf[11 + length + offset])) {
for (uint8_t i = 0; i < length; i++) uint8_t i = 0;
rfcommDataBuffer[rfcommAvailable + i] = l2capinbuf[11 + i + offset]; for (; i < length; i++) {
rfcommAvailable += length; if (rfcommAvailable + i >= sizeof (rfcommDataBuffer)) {
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nWarning: Buffer is full!"), 0x80);
#endif
break;
} }
rfcommDataBuffer[rfcommAvailable + i] = l2capinbuf[11 + i + offset];
}
rfcommAvailable += i;
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
Notify(PSTR("\r\nRFCOMM Data Available: "), 0x80); Notify(PSTR("\r\nRFCOMM Data Available: "), 0x80);
Notify(rfcommAvailable, 0x80); Notify(rfcommAvailable, 0x80);
@ -283,6 +290,11 @@ void SPP::ACLData(uint8_t* l2capinbuf) {
Notify(PSTR(" - Credit: 0x"), 0x80); Notify(PSTR(" - Credit: 0x"), 0x80);
D_PrintHex<uint8_t > (l2capinbuf[11], 0x80); D_PrintHex<uint8_t > (l2capinbuf[11], 0x80);
} }
#endif
}
#ifdef DEBUG_USB_HOST
else
Notify(PSTR("\r\nError in FCS checksum!"), 0x80);
#endif #endif
#ifdef PRINTREPORT // Uncomment "#define PRINTREPORT" to print the report send to the Arduino via Bluetooth #ifdef PRINTREPORT // Uncomment "#define PRINTREPORT" to print the report send to the Arduino via Bluetooth
for (uint8_t i = 0; i < length; i++) for (uint8_t i = 0; i < length; i++)
@ -723,16 +735,24 @@ void SPP::sendRfcommCredit(uint8_t channel, uint8_t direction, uint8_t CR, uint8
} }
/* CRC on 2 bytes */ /* CRC on 2 bytes */
uint8_t SPP::__crc(uint8_t* data) { uint8_t SPP::crc(uint8_t *data) {
return (pgm_read_byte(&rfcomm_crc_table[pgm_read_byte(&rfcomm_crc_table[0xff ^ data[0]]) ^ data[1]])); return (pgm_read_byte(&rfcomm_crc_table[pgm_read_byte(&rfcomm_crc_table[0xFF ^ data[0]]) ^ data[1]]));
} }
/* Calculate FCS - we never actually check if the host sends correct FCS to the Arduino */ /* Calculate FCS */
uint8_t SPP::calcFcs(uint8_t *data) { uint8_t SPP::calcFcs(uint8_t *data) {
if ((data[1] & 0xEF) == RFCOMM_UIH) if ((data[1] & 0xEF) == RFCOMM_UIH)
return (0xff - __crc(data)); // FCS on 2 bytes return (0xFF - crc(data)); // FCS on 2 bytes
else else
return (0xff - pgm_read_byte(&rfcomm_crc_table[__crc(data) ^ data[2]])); // FCS on 3 bytes return (0xFF - pgm_read_byte(&rfcomm_crc_table[crc(data) ^ data[2]])); // FCS on 3 bytes
}
/* Check FCS */
bool SPP::checkFcs(uint8_t *data, uint8_t fcs) {
uint8_t temp = crc(data);
if ((data[1] & 0xEF) != RFCOMM_UIH)
temp = pgm_read_byte(&rfcomm_crc_table[temp ^ data[2]]); // FCS on 3 bytes
return (pgm_read_byte(&rfcomm_crc_table[temp ^ fcs]) == 0xCF);
} }
/* Serial commands */ /* Serial commands */

3
SPP.h
View file

@ -227,6 +227,7 @@ private:
void sendRfcomm(uint8_t channel, uint8_t direction, uint8_t CR, uint8_t channelType, uint8_t pfBit, uint8_t *data, uint8_t length); void sendRfcomm(uint8_t channel, uint8_t direction, uint8_t CR, uint8_t channelType, uint8_t pfBit, uint8_t *data, uint8_t length);
void sendRfcommCredit(uint8_t channel, uint8_t direction, uint8_t CR, uint8_t channelType, uint8_t pfBit, uint8_t credit); void sendRfcommCredit(uint8_t channel, uint8_t direction, uint8_t CR, uint8_t channelType, uint8_t pfBit, uint8_t credit);
uint8_t calcFcs(uint8_t *data); uint8_t calcFcs(uint8_t *data);
uint8_t __crc(uint8_t* data); bool checkFcs(uint8_t *data, uint8_t fcs);
uint8_t crc(uint8_t *data);
}; };
#endif #endif

View file

@ -567,6 +567,7 @@ uint8_t USB::DefaultAddressing(uint8_t parent, uint8_t port, bool lowspeed) {
uint8_t USB::AttemptConfig(uint8_t driver, uint8_t parent, uint8_t port, bool lowspeed) { uint8_t USB::AttemptConfig(uint8_t driver, uint8_t parent, uint8_t port, bool lowspeed) {
//printf("AttemptConfig: parent = %i, port = %i\r\n", parent, port); //printf("AttemptConfig: parent = %i, port = %i\r\n", parent, port);
uint8_t retries = 0;
again: again:
uint8_t rcode = devConfig[driver]->ConfigureDevice(parent, port, lowspeed); uint8_t rcode = devConfig[driver]->ConfigureDevice(parent, port, lowspeed);
@ -579,15 +580,17 @@ again:
// reset parent port // reset parent port
devConfig[parent]->ResetHubPort(port); devConfig[parent]->ResetHubPort(port);
} }
} else if (rcode == hrJERR) { // Some devices returns this when plugged in - trying to initialize the device again usually works } else if (rcode == hrJERR && retries < 3) { // Some devices returns this when plugged in - trying to initialize the device again usually works
delay(100); delay(100);
retries++;
goto again; goto again;
} else if (rcode) } else if (rcode)
return rcode; return rcode;
rcode = devConfig[driver]->Init(parent, port, lowspeed); rcode = devConfig[driver]->Init(parent, port, lowspeed);
if (rcode == hrJERR) { // Some devices returns this when plugged in - trying to initialize the device again usually works if (rcode == hrJERR && retries < 3) { // Some devices returns this when plugged in - trying to initialize the device again usually works
delay(100); delay(100);
retries++;
goto again; goto again;
} }
if (rcode) { if (rcode) {

View file

@ -18,7 +18,7 @@
*/ */
#include "Wii.h" #include "Wii.h"
// To enable serial debugging uncomment "#define DEBUG_USB_HOST" in message.h // To enable serial debugging see "settings.h"
//#define EXTRADEBUG // Uncomment to get even more debugging data //#define EXTRADEBUG // Uncomment to get even more debugging data
//#define PRINTREPORT // Uncomment to print the report send by the Wii controllers //#define PRINTREPORT // Uncomment to print the report send by the Wii controllers
@ -473,11 +473,11 @@ void WII::ACLData(uint8_t* l2capinbuf) {
pitchGyroSpeed = (double)gyroPitchRaw / ((double)gyroPitchZero / pitchGyroScale); pitchGyroSpeed = (double)gyroPitchRaw / ((double)gyroPitchZero / pitchGyroScale);
/* The onboard gyro has two ranges for slow and fast mode */ /* The onboard gyro has two ranges for slow and fast mode */
if (!(l2capinbuf[18] & 0x02)) // Check if fast more is used if (!(l2capinbuf[18] & 0x02)) // Check if fast mode is used
yawGyroSpeed *= 4.545; yawGyroSpeed *= 4.545;
if (!(l2capinbuf[18] & 0x01)) // Check if fast more is used if (!(l2capinbuf[18] & 0x01)) // Check if fast mode is used
pitchGyroSpeed *= 4.545; pitchGyroSpeed *= 4.545;
if (!(l2capinbuf[19] & 0x02)) // Check if fast more is used if (!(l2capinbuf[19] & 0x02)) // Check if fast mode is used
rollGyroSpeed *= 4.545; rollGyroSpeed *= 4.545;
compPitch = (0.93 * (compPitch + (pitchGyroSpeed * (double)(micros() - timer) / 1000000)))+(0.07 * getWiimotePitch()); // Use a complimentary filter to calculate the angle compPitch = (0.93 * (compPitch + (pitchGyroSpeed * (double)(micros() - timer) / 1000000)))+(0.07 * getWiimotePitch()); // Use a complimentary filter to calculate the angle

6
Wii.h
View file

@ -191,6 +191,10 @@ public:
* @param value See: ::LED enum. * @param value See: ::LED enum.
*/ */
void setLedRaw(uint8_t value); void setLedRaw(uint8_t value);
/** Turn all LEDs off. */
void setLedOff() {
setLedRaw(0);
}
/** /**
* Turn the specific ::LED off. * Turn the specific ::LED off.
* @param a The ::LED to turn off. * @param a The ::LED to turn off.
@ -210,9 +214,7 @@ public:
* This will set the LEDs, so the user can see which connections are active. * This will set the LEDs, so the user can see which connections are active.
* *
* The first ::LED indicate that the Wiimote is connected, * The first ::LED indicate that the Wiimote is connected,
*
* the second ::LED indicate indicate that a Motion Plus is also connected * the second ::LED indicate indicate that a Motion Plus is also connected
*
* the third ::LED will indicate that a Nunchuck controller is also connected. * the third ::LED will indicate that a Nunchuck controller is also connected.
*/ */
void setLedStatus(); void setLedStatus();

View file

@ -1,8 +1,6 @@
Please see <http://wiibrew.org/wiki/Wiimote#IR_Camera> for the complete capabilities of the Wii camera. The IR camera code was written based on the above website and with support from Kristian Lauszus. Please see <http://wiibrew.org/wiki/Wiimote#IR_Camera> for the complete capabilities of the Wii camera. The IR camera code was written based on the above website and with support from Kristian Lauszus.
Must omit the "." in the name of the USB\_Host\_Shiled\_2.0 library folder when inserting into the Arudino library folder. This library is large, if you run into memory problems when uploading to the Arduino, disable serial debugging.
This library is large, if you run into memory problems when uploading to the Arduino, comment out the \#define DEBUG in the BTD.cpp and Wii.cpp files.
To enable the IR camera code, uncomment \#define WIICAMERA in Wii.h. To enable the IR camera code, uncomment \#define WIICAMERA in Wii.h.

View file

@ -16,7 +16,7 @@
*/ */
#include "XBOXOLD.h" #include "XBOXOLD.h"
// To enable serial debugging uncomment "#define DEBUG_USB_HOST" in message.h // To enable serial debugging see "settings.h"
//#define EXTRADEBUG // Uncomment to get even more debugging data //#define EXTRADEBUG // Uncomment to get even more debugging data
//#define PRINTREPORT // Uncomment to print the report send by the Xbox controller //#define PRINTREPORT // Uncomment to print the report send by the Xbox controller
@ -290,15 +290,15 @@ void XBOXOLD::printReport(uint16_t length) { //Uncomment "#define PRINTREPORT" t
} }
uint8_t XBOXOLD::getButtonPress(Button b) { uint8_t XBOXOLD::getButtonPress(Button b) {
uint8_t button = pgm_read_byte(&XBOXOLDBUTTONS[(uint8_t)b]);
if (b == A || b == B || b == X || b == Y || b == BLACK || b == WHITE || b == L1 || b == R1) // A, B, X, Y, BLACK, WHITE, L1, and R1 are analog buttons if (b == A || b == B || b == X || b == Y || b == BLACK || b == WHITE || b == L1 || b == R1) // A, B, X, Y, BLACK, WHITE, L1, and R1 are analog buttons
return buttonValues[pgm_read_byte(&XBOXOLDBUTTONS[(uint8_t)b])]; // Analog buttons return buttonValues[button]; // Analog buttons
return (ButtonState & pgm_read_byte(&XBOXOLDBUTTONS[(uint8_t)b])); // Digital buttons return (ButtonState & button); // Digital buttons
} }
bool XBOXOLD::getButtonClick(Button b) { bool XBOXOLD::getButtonClick(Button b) {
uint8_t button; uint8_t button = pgm_read_byte(&XBOXOLDBUTTONS[(uint8_t)b]);
if (b == A || b == B || b == X || b == Y || b == BLACK || b == WHITE || b == L1 || b == R1) { // A, B, X, Y, BLACK, WHITE, L1, and R1 are analog buttons if (b == A || b == B || b == X || b == Y || b == BLACK || b == WHITE || b == L1 || b == R1) { // A, B, X, Y, BLACK, WHITE, L1, and R1 are analog buttons
button = pgm_read_byte(&XBOXOLDBUTTONS[(uint8_t)b]);
if (buttonClicked[button]) { if (buttonClicked[button]) {
buttonClicked[button] = false; buttonClicked[button] = false;
return true; return true;
@ -306,7 +306,6 @@ bool XBOXOLD::getButtonClick(Button b) {
return false; return false;
} }
button = pgm_read_byte(&XBOXOLDBUTTONS[(uint8_t)b]); // Digital buttons
bool click = (ButtonClickState & button); bool click = (ButtonClickState & button);
ButtonClickState &= ~button; // clear "click" event ButtonClickState &= ~button; // clear "click" event
return click; return click;

View file

@ -18,7 +18,7 @@
*/ */
#include "XBOXRECV.h" #include "XBOXRECV.h"
// To enable serial debugging uncomment "#define DEBUG_USB_HOST" in message.h // To enable serial debugging see "settings.h"
//#define EXTRADEBUG // Uncomment to get even more debugging data //#define EXTRADEBUG // Uncomment to get even more debugging data
//#define PRINTREPORT // Uncomment to print the report send by the Xbox 360 Controller //#define PRINTREPORT // Uncomment to print the report send by the Xbox 360 Controller
@ -37,29 +37,27 @@ bPollEnable(false) { // don't start polling before dongle is connected
pUsb->RegisterDeviceClass(this); //set devConfig[] entry pUsb->RegisterDeviceClass(this); //set devConfig[] entry
} }
uint8_t XBOXRECV::Init(uint8_t parent, uint8_t port, bool lowspeed) { uint8_t XBOXRECV::ConfigureDevice(uint8_t parent, uint8_t port, bool lowspeed) {
uint8_t buf[sizeof (USB_DEVICE_DESCRIPTOR)]; const uint8_t constBufSize = sizeof (USB_DEVICE_DESCRIPTOR);
uint8_t buf[constBufSize];
uint8_t rcode; uint8_t rcode;
UsbDevice *p = NULL; UsbDevice *p = NULL;
EpInfo *oldep_ptr = NULL; EpInfo *oldep_ptr = NULL;
uint16_t PID; uint16_t PID, VID;
uint16_t VID;
// get memory address of USB device address pool AddressPool &addrPool = pUsb->GetAddressPool(); // Get memory address of USB device address pool
AddressPool &addrPool = pUsb->GetAddressPool();
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
Notify(PSTR("\r\nXBOXRECV Init"), 0x80); Notify(PSTR("\r\nXBOXRECV Init"), 0x80);
#endif #endif
// check if address has already been assigned to an instance
if (bAddress) { if (bAddress) { // Check if address has already been assigned to an instance
#ifdef DEBUG_USB_HOST #ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nAddress in use"), 0x80); Notify(PSTR("\r\nAddress in use"), 0x80);
#endif #endif
return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE; return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE;
} }
// Get pointer to pseudo device with address 0 assigned p = addrPool.GetUsbDevicePtr(0); // Get pointer to pseudo device with address 0 assigned
p = addrPool.GetUsbDevicePtr(0);
if (!p) { if (!p) {
#ifdef DEBUG_USB_HOST #ifdef DEBUG_USB_HOST
@ -75,18 +73,13 @@ uint8_t XBOXRECV::Init(uint8_t parent, uint8_t port, bool lowspeed) {
return USB_ERROR_EPINFO_IS_NULL; return USB_ERROR_EPINFO_IS_NULL;
} }
// Save old pointer to EP_RECORD of address 0 oldep_ptr = p->epinfo; // Save old pointer to EP_RECORD of address 0
oldep_ptr = p->epinfo; p->epinfo = epInfo; // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
// Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
p->epinfo = epInfo;
p->lowspeed = lowspeed; p->lowspeed = lowspeed;
// Get device descriptor rcode = pUsb->getDevDescr(0, 0, constBufSize, (uint8_t*)buf); // Get device descriptor - addr, ep, nbytes, data
rcode = pUsb->getDevDescr(0, 0, sizeof (USB_DEVICE_DESCRIPTOR), (uint8_t*)buf); // Get device descriptor - addr, ep, nbytes, data
// Restore p->epinfo p->epinfo = oldep_ptr; // Restore p->epinfo
p->epinfo = oldep_ptr;
if (rcode) if (rcode)
goto FailGetDevDescr; goto FailGetDevDescr;
@ -94,53 +87,100 @@ uint8_t XBOXRECV::Init(uint8_t parent, uint8_t port, bool lowspeed) {
VID = ((USB_DEVICE_DESCRIPTOR*)buf)->idVendor; VID = ((USB_DEVICE_DESCRIPTOR*)buf)->idVendor;
PID = ((USB_DEVICE_DESCRIPTOR*)buf)->idProduct; PID = ((USB_DEVICE_DESCRIPTOR*)buf)->idProduct;
if (VID != XBOX_VID && VID != MADCATZ_VID) // We just check if it's a Xbox receiver using the Vendor ID if ((VID != XBOX_VID && VID != MADCATZ_VID) || (PID != XBOX_WIRELESS_RECEIVER_PID && PID != XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID)) { // Check if it's a Xbox receiver using the Vendor ID and Product ID
goto FailUnknownDevice;
else if (PID != XBOX_WIRELESS_RECEIVER_PID && PID != XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID) { // Check the PID as well
#ifdef DEBUG_USB_HOST #ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nYou'll need a wireless receiver for this libary to work"), 0x80); Notify(PSTR("\r\nYou'll need a wireless receiver for this libary to work"), 0x80);
#endif #endif
goto FailUnknownDevice; goto FailUnknownDevice;
} }
// Allocate new address according to device class bAddress = addrPool.AllocAddress(parent, false, port); // Allocate new address according to device class
bAddress = addrPool.AllocAddress(parent, false, port);
if (!bAddress) if (!bAddress) {
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nOut of address space"), 0x80);
#endif
return USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL; return USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL;
}
// Extract Max Packet Size from device descriptor epInfo[0].maxPktSize = (uint8_t)((USB_DEVICE_DESCRIPTOR*)buf)->bMaxPacketSize0; // Extract Max Packet Size from device descriptor
epInfo[0].maxPktSize = (uint8_t)((USB_DEVICE_DESCRIPTOR*)buf)->bMaxPacketSize0; epInfo[1].epAddr = ((USB_DEVICE_DESCRIPTOR*)buf)->bNumConfigurations; // Steal and abuse from epInfo structure to save memory
// Assign new address to the device delay(20); // Wait a little before resetting device
rcode = pUsb->setAddr(0, 0, bAddress);
return USB_ERROR_CONFIG_REQUIRES_ADDITIONAL_RESET;
/* Diagnostic messages */
FailGetDevDescr:
#ifdef DEBUG_USB_HOST
NotifyFailGetDevDescr(rcode);
#endif
if (rcode != hrJERR)
rcode = USB_ERROR_FailGetDevDescr;
goto Fail;
FailUnknownDevice:
#ifdef DEBUG_USB_HOST
NotifyFailUnknownDevice(VID,PID);
#endif
rcode = USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
Fail:
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nXbox 360 Init Failed, error code: "), 0x80);
NotifyFail(rcode);
#endif
Release();
return rcode;
};
uint8_t XBOXRECV::Init(uint8_t parent, uint8_t port, bool lowspeed) {
uint8_t rcode;
uint8_t num_of_conf = epInfo[1].epAddr; // Number of configurations
epInfo[1].epAddr = 0;
AddressPool &addrPool = pUsb->GetAddressPool();
#ifdef EXTRADEBUG
Notify(PSTR("\r\nBTD Init"), 0x80);
#endif
UsbDevice *p = addrPool.GetUsbDevicePtr(bAddress); // Get pointer to assigned address record
if (!p) {
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nAddress not found"), 0x80);
#endif
return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
}
delay(300); // Assign new address to the device
rcode = pUsb->setAddr(0, 0, bAddress); // Assign new address to the device
if (rcode) { if (rcode) {
p->lowspeed = false;
addrPool.FreeAddress(bAddress);
bAddress = 0;
#ifdef DEBUG_USB_HOST #ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nsetAddr: "), 0x80); Notify(PSTR("\r\nsetAddr: "), 0x80);
D_PrintHex<uint8_t > (rcode, 0x80); D_PrintHex<uint8_t > (rcode, 0x80);
#endif #endif
return rcode; p->lowspeed = false;
goto Fail;
} }
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
Notify(PSTR("\r\nAddr: "), 0x80); Notify(PSTR("\r\nAddr: "), 0x80);
D_PrintHex<uint8_t > (bAddress, 0x80); D_PrintHex<uint8_t > (bAddress, 0x80);
#endif #endif
delay(300); // Spec says you should wait at least 200ms
p->lowspeed = false; p->lowspeed = false;
//get pointer to assigned address record p = addrPool.GetUsbDevicePtr(bAddress); // Get pointer to assigned address record
p = addrPool.GetUsbDevicePtr(bAddress); if (!p) {
if (!p) #ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nAddress not found"), 0x80);
#endif
return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL; return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
}
p->lowspeed = lowspeed; p->lowspeed = lowspeed;
// Assign epInfo to epinfo pointer - only EP0 is known rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo); // Assign epInfo to epinfo pointer - only EP0 is known
rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo);
if (rcode) if (rcode)
goto FailSetDevTblEntry; goto FailSetDevTblEntry;
@ -216,9 +256,10 @@ uint8_t XBOXRECV::Init(uint8_t parent, uint8_t port, bool lowspeed) {
#endif #endif
XboxReceiverConnected = true; XboxReceiverConnected = true;
bPollEnable = true; bPollEnable = true;
return 0; // successful configuration checkStatusTimer = 0; // Reset timer
return 0; // Successful configuration
/* diagnostic messages */ /* Diagnostic messages */
FailGetDevDescr: FailGetDevDescr:
#ifdef DEBUG_USB_HOST #ifdef DEBUG_USB_HOST
NotifyFailGetDevDescr(); NotifyFailGetDevDescr();
@ -237,12 +278,6 @@ FailSetConfDescr:
#endif #endif
goto Fail; goto Fail;
FailUnknownDevice:
#ifdef DEBUG_USB_HOST
NotifyFailUnknownDevice(VID,PID);
#endif
rcode = USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
Fail: Fail:
#ifdef DEBUG_USB_HOST #ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nXbox 360 Init Failed, error code: "), 0x80); Notify(PSTR("\r\nXbox 360 Init Failed, error code: "), 0x80);
@ -266,23 +301,23 @@ uint8_t XBOXRECV::Release() {
uint8_t XBOXRECV::Poll() { uint8_t XBOXRECV::Poll() {
if (!bPollEnable) if (!bPollEnable)
return 0; return 0;
if (!timer || ((millis() - timer) > 3000)) { // Run checkStatus every 3 seconds if (!checkStatusTimer || ((millis() - checkStatusTimer) > 3000)) { // Run checkStatus every 3 seconds
timer = millis(); checkStatusTimer = millis();
checkStatus(); checkStatus();
} }
uint8_t inputPipe; uint8_t inputPipe;
uint16_t bufferSize; uint16_t bufferSize;
for (uint8_t i = 0; i < 4; i++) { for (uint8_t i = 0; i < 4; i++) {
switch (i) { if (i == 0)
case 0: inputPipe = XBOX_INPUT_PIPE_1; inputPipe = XBOX_INPUT_PIPE_1;
break; else if (i == 1)
case 1: inputPipe = XBOX_INPUT_PIPE_2; inputPipe = XBOX_INPUT_PIPE_2;
break; else if (i == 2)
case 2: inputPipe = XBOX_INPUT_PIPE_3; inputPipe = XBOX_INPUT_PIPE_3;
break; else
case 3: inputPipe = XBOX_INPUT_PIPE_4; inputPipe = XBOX_INPUT_PIPE_4;
break;
}
bufferSize = EP_MAXPKTSIZE; // This is the maximum number of bytes we want to receive bufferSize = EP_MAXPKTSIZE; // This is the maximum number of bytes we want to receive
pUsb->inTransfer(bAddress, epInfo[ inputPipe ].epAddr, &bufferSize, readBuf); pUsb->inTransfer(bAddress, epInfo[ inputPipe ].epAddr, &bufferSize, readBuf);
if (bufferSize > 0) { // The number of received bytes if (bufferSize > 0) { // The number of received bytes
@ -442,25 +477,32 @@ uint8_t XBOXRECV::getBatteryLevel(uint8_t controller) {
} }
void XBOXRECV::XboxCommand(uint8_t controller, uint8_t* data, uint16_t nbytes) { void XBOXRECV::XboxCommand(uint8_t controller, uint8_t* data, uint16_t nbytes) {
uint8_t rcode;
uint8_t outputPipe; uint8_t outputPipe;
switch (controller) { if (controller == 0)
case 0: outputPipe = XBOX_OUTPUT_PIPE_1; outputPipe = XBOX_OUTPUT_PIPE_1;
break; else if (controller == 1)
case 1: outputPipe = XBOX_OUTPUT_PIPE_2; outputPipe = XBOX_OUTPUT_PIPE_2;
break; else if (controller == 2)
case 2: outputPipe = XBOX_OUTPUT_PIPE_3; outputPipe = XBOX_OUTPUT_PIPE_3;
break; else
case 3: outputPipe = XBOX_OUTPUT_PIPE_4; outputPipe = XBOX_OUTPUT_PIPE_4;
break;
} uint8_t rcode = pUsb->outTransfer(bAddress, epInfo[ outputPipe ].epAddr, nbytes, data);
rcode = pUsb->outTransfer(bAddress, epInfo[ outputPipe ].epAddr, nbytes, data);
#ifdef EXTRADEBUG #ifdef EXTRADEBUG
if (rcode) if (rcode)
Notify(PSTR("Error sending Xbox message\r\n"), 0x80); Notify(PSTR("Error sending Xbox message\r\n"), 0x80);
#endif #endif
} }
void XBOXRECV::disconnect(uint8_t controller) {
writeBuf[0] = 0x00;
writeBuf[1] = 0x00;
writeBuf[2] = 0x08;
writeBuf[3] = 0xC0;
XboxCommand(controller, writeBuf, 4);
}
void XBOXRECV::setLedRaw(uint8_t value, uint8_t controller) { void XBOXRECV::setLedRaw(uint8_t value, uint8_t controller) {
writeBuf[0] = 0x00; writeBuf[0] = 0x00;
writeBuf[1] = 0x00; writeBuf[1] = 0x00;

View file

@ -64,6 +64,14 @@ public:
XBOXRECV(USB *pUsb); XBOXRECV(USB *pUsb);
/** @name USBDeviceConfig implementation */ /** @name USBDeviceConfig implementation */
/**
* Address assignment and basic initilization is done here.
* @param parent Hub number.
* @param port Port number on the hub.
* @param lowspeed Speed of the device.
* @return 0 on success.
*/
virtual uint8_t ConfigureDevice(uint8_t parent, uint8_t port, bool lowspeed);
/** /**
* Initialize the Xbox wireless receiver. * Initialize the Xbox wireless receiver.
* @param parent Hub number. * @param parent Hub number.
@ -136,6 +144,12 @@ public:
*/ */
int16_t getAnalogHat(AnalogHat a, uint8_t controller = 0); int16_t getAnalogHat(AnalogHat a, uint8_t controller = 0);
/**
* Used to disconnect any of the controllers.
* @param controller The controller to disconnect. Default to 0.
*/
void disconnect(uint8_t controller = 0);
/** /**
* Turn rumble off and all the LEDs on the specific controller. * Turn rumble off and all the LEDs on the specific controller.
* @param controller The controller to write to. Default to 0. * @param controller The controller to write to. Default to 0.
@ -251,7 +265,7 @@ private:
bool L2Clicked[4]; // These buttons are analog, so we use we use these bools to check if they where clicked or not bool L2Clicked[4]; // These buttons are analog, so we use we use these bools to check if they where clicked or not
bool R2Clicked[4]; bool R2Clicked[4];
unsigned long timer; // Timing for checkStatus() signals uint32_t checkStatusTimer; // Timing for checkStatus() signals
uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
uint8_t writeBuf[7]; // General purpose buffer for output data uint8_t writeBuf[7]; // General purpose buffer for output data

View file

@ -16,7 +16,7 @@
*/ */
#include "XBOXUSB.h" #include "XBOXUSB.h"
// To enable serial debugging uncomment "#define DEBUG_USB_HOST" in message.h // To enable serial debugging see "settings.h"
//#define EXTRADEBUG // Uncomment to get even more debugging data //#define EXTRADEBUG // Uncomment to get even more debugging data
//#define PRINTREPORT // Uncomment to print the report send by the Xbox 360 Controller //#define PRINTREPORT // Uncomment to print the report send by the Xbox 360 Controller
@ -282,7 +282,7 @@ uint8_t XBOXUSB::getButtonPress(Button b) {
return (uint8_t)(ButtonState >> 8); return (uint8_t)(ButtonState >> 8);
else if (b == R2) else if (b == R2)
return (uint8_t)ButtonState; return (uint8_t)ButtonState;
return (ButtonState & ((uint32_t)pgm_read_word(&XBOXBUTTONS[(uint8_t)b]) << 16)); return (bool)(ButtonState & ((uint32_t)pgm_read_word(&XBOXBUTTONS[(uint8_t)b]) << 16));
} }
bool XBOXUSB::getButtonClick(Button b) { bool XBOXUSB::getButtonClick(Button b) {

68
adk.cpp
View file

@ -30,7 +30,6 @@ ADK::ADK(USB *p, const char* manufacturer,
const char* serial) : const char* serial) :
/* ADK ID Strings */ /* ADK ID Strings */
manufacturer(manufacturer), manufacturer(manufacturer),
model(model), model(model),
description(description), description(description),
@ -46,12 +45,10 @@ ready(false) {
for (uint8_t i = 0; i < ADK_MAX_ENDPOINTS; i++) { for (uint8_t i = 0; i < ADK_MAX_ENDPOINTS; i++) {
epInfo[i].epAddr = 0; epInfo[i].epAddr = 0;
epInfo[i].maxPktSize = (i) ? 0 : 8; epInfo[i].maxPktSize = (i) ? 0 : 8;
epInfo[i].epAttribs = (0xfc & (USB_NAK_MAX_POWER << 2)); epInfo[i].epAttribs = 0;
epInfo[i].bmNakPower = (i) ? USB_NAK_NOWAIT : USB_NAK_MAX_POWER;
}//for(uint8_t i=0; i<ADK_MAX_ENDPOINTS; i++... }//for(uint8_t i=0; i<ADK_MAX_ENDPOINTS; i++...
//set bulk-IN EP naklimit to 1
epInfo[epDataInIndex].epAttribs = (0xfc & (USB_NAK_NOWAIT << 2));
// register in USB subsystem // register in USB subsystem
if (pUsb) { if (pUsb) {
pUsb->RegisterDeviceClass(this); //set devConfig[] entry pUsb->RegisterDeviceClass(this); //set devConfig[] entry
@ -64,7 +61,6 @@ uint8_t ADK::ConfigureDevice(uint8_t parent, uint8_t port, bool lowspeed) {
/* Connection initialization of an Android phone */ /* Connection initialization of an Android phone */
uint8_t ADK::Init(uint8_t parent, uint8_t port, bool lowspeed) { uint8_t ADK::Init(uint8_t parent, uint8_t port, bool lowspeed) {
uint8_t buf[sizeof (USB_DEVICE_DESCRIPTOR)]; uint8_t buf[sizeof (USB_DEVICE_DESCRIPTOR)];
uint8_t rcode; uint8_t rcode;
uint8_t num_of_conf; // number of configurations uint8_t num_of_conf; // number of configurations
@ -118,6 +114,7 @@ uint8_t ADK::Init(uint8_t parent, uint8_t port, bool lowspeed) {
// Extract Max Packet Size from device descriptor // Extract Max Packet Size from device descriptor
epInfo[0].maxPktSize = (uint8_t)((USB_DEVICE_DESCRIPTOR*)buf)->bMaxPacketSize0; epInfo[0].maxPktSize = (uint8_t)((USB_DEVICE_DESCRIPTOR*)buf)->bMaxPacketSize0;
// Assign new address to the device // Assign new address to the device
rcode = pUsb->setAddr(0, 0, bAddress); rcode = pUsb->setAddr(0, 0, bAddress);
if (rcode) { if (rcode) {
@ -156,10 +153,18 @@ uint8_t ADK::Init(uint8_t parent, uint8_t port, bool lowspeed) {
num_of_conf = ((USB_DEVICE_DESCRIPTOR*)buf)->bNumConfigurations; num_of_conf = ((USB_DEVICE_DESCRIPTOR*)buf)->bNumConfigurations;
//USBTRACE2("\r\nNC:",num_of_conf); //USBTRACE2("\r\nNC:",num_of_conf);
for (uint8_t i = 0; i < num_of_conf; i++) { for (uint8_t i = 0; i < num_of_conf; i++) {
ConfigDescParser < 0, 0, 0, 0 > confDescrParser(this); ConfigDescParser < 0, 0, 0, 0 > confDescrParser(this);
delay(1);
rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser); rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser);
#if defined(XOOM)
//added by Jaylen Scott Vanorden
if (rcode) {
USBTRACE2("\r\nGot 1st bad code for config: ", rcode);
// Try once more
rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser);
}
#endif
if (rcode) { if (rcode) {
goto FailGetConfDescr; goto FailGetConfDescr;
} }
@ -176,27 +181,27 @@ uint8_t ADK::Init(uint8_t parent, uint8_t port, bool lowspeed) {
} }
} }
// Set Configuration Value // Set Configuration Value
rcode = pUsb->setConf(bAddress, 0, bConfNum); rcode = pUsb->setConf(bAddress, 0, bConfNum);
if (rcode) { if (rcode) {
goto FailSetConfDescr; goto FailSetConfDescr;
} }
/* print endpoint structure */ /* print endpoint structure */
// USBTRACE("\r\nEndpoint Structure:"); /*
// USBTRACE("\r\nEP0:"); USBTRACE("\r\nEndpoint Structure:");
// USBTRACE2("\r\nAddr: ", epInfo[0].epAddr ); USBTRACE("\r\nEP0:");
// USBTRACE2("\r\nMax.pkt.size: ", epInfo[0].maxPktSize ); USBTRACE2("\r\nAddr: ", epInfo[0].epAddr);
// USBTRACE2("\r\nAttr: ", epInfo[0].epAttribs ); USBTRACE2("\r\nMax.pkt.size: ", epInfo[0].maxPktSize);
// USBTRACE("\r\nEpout:"); USBTRACE2("\r\nAttr: ", epInfo[0].epAttribs);
// USBTRACE2("\r\nAddr: ", epInfo[epDataOutIndex].epAddr ); USBTRACE("\r\nEpout:");
// USBTRACE2("\r\nMax.pkt.size: ", epInfo[epDataOutIndex].maxPktSize ); USBTRACE2("\r\nAddr: ", epInfo[epDataOutIndex].epAddr);
// USBTRACE2("\r\nAttr: ", epInfo[epDataOutIndex].epAttribs ); USBTRACE2("\r\nMax.pkt.size: ", epInfo[epDataOutIndex].maxPktSize);
// USBTRACE("\r\nEpin:"); USBTRACE2("\r\nAttr: ", epInfo[epDataOutIndex].epAttribs);
// USBTRACE2("\r\nAddr: ", epInfo[epDataInIndex].epAddr ); USBTRACE("\r\nEpin:");
// USBTRACE2("\r\nMax.pkt.size: ", epInfo[epDataInIndex].maxPktSize ); USBTRACE2("\r\nAddr: ", epInfo[epDataInIndex].epAddr);
// USBTRACE2("\r\nAttr: ", epInfo[epDataInIndex].epAttribs ); USBTRACE2("\r\nMax.pkt.size: ", epInfo[epDataInIndex].maxPktSize);
USBTRACE2("\r\nAttr: ", epInfo[epDataInIndex].epAttribs);
*/
USBTRACE("\r\nConfiguration successful"); USBTRACE("\r\nConfiguration successful");
ready = true; ready = true;
@ -206,7 +211,16 @@ uint8_t ADK::Init(uint8_t parent, uint8_t port, bool lowspeed) {
//probe device - get accessory protocol revision //probe device - get accessory protocol revision
{ {
uint16_t adkproto = -1; uint16_t adkproto = -1;
delay(1);
rcode = getProto((uint8_t*) & adkproto); rcode = getProto((uint8_t*) & adkproto);
#if defined(XOOM)
//added by Jaylen Scott Vanorden
if (rcode) {
USBTRACE2("\r\nGot 1st bad code for proto: ", rcode);
// Try once more
rcode = getProto((uint8_t*) & adkproto);
}
#endif
if (rcode) { if (rcode) {
goto FailGetProto; //init fails goto FailGetProto; //init fails
} }
@ -275,7 +289,6 @@ SwAttempt:
//FailOnInit: //FailOnInit:
// USBTRACE("OnInit:"); // USBTRACE("OnInit:");
// goto Fail; // goto Fail;
//
Fail: Fail:
//USBTRACE2("\r\nADK Init Failed, error code: ", rcode); //USBTRACE2("\r\nADK Init Failed, error code: ", rcode);
//NotifyFail(rcode); //NotifyFail(rcode);
@ -296,12 +309,8 @@ void ADK::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto
bConfNum = conf; bConfNum = conf;
uint8_t index; if ((pep->bmAttributes & 0x02) == 2) {
uint8_t index = ((pep->bEndpointAddress & 0x80) == 0x80) ? epDataInIndex : epDataOutIndex;
// if ((pep->bmAttributes & 0x02) == 2) {
index = ((pep->bEndpointAddress & 0x80) == 0x80) ? epDataInIndex : epDataOutIndex;
// }
// Fill in the endpoint info structure // Fill in the endpoint info structure
epInfo[index].epAddr = (pep->bEndpointAddress & 0x0F); epInfo[index].epAddr = (pep->bEndpointAddress & 0x0F);
epInfo[index].maxPktSize = (uint8_t)pep->wMaxPacketSize; epInfo[index].maxPktSize = (uint8_t)pep->wMaxPacketSize;
@ -310,6 +319,7 @@ void ADK::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto
//PrintEndpointDescriptor(pep); //PrintEndpointDescriptor(pep);
} }
}
/* Performs a cleanup after failed Init() attempt */ /* Performs a cleanup after failed Init() attempt */
uint8_t ADK::Release() { uint8_t ADK::Release() {

4
adk.h
View file

@ -26,6 +26,10 @@ e-mail : support@circuitsathome.com
#define ADK_PID 0x2D00 #define ADK_PID 0x2D00
#define ADB_PID 0x2D01 #define ADB_PID 0x2D01
#define XOOM //enables repeating getProto() and getConf() attempts
//necessary for slow devices such as Motorola XOOM
//defined by default, can be commented out to save memory
/* requests */ /* requests */
#define ADK_GETPROTO 51 //check USB accessory protocol version #define ADK_GETPROTO 51 //check USB accessory protocol version

View file

@ -8,7 +8,8 @@
#include <usbhub.h> #include <usbhub.h>
USB Usb; USB Usb;
USBHub Hub1(&Usb); // Some dongles have a hub inside //USBHub Hub1(&Usb); // Some dongles have a hub inside
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
/* You can create the instance of the class in two ways */ /* You can create the instance of the class in two ways */
PS3BT PS3(&Btd); // This will just create the instance PS3BT PS3(&Btd); // This will just create the instance
@ -35,7 +36,7 @@ void loop() {
Serial.print(PS3.getAnalogHat(LeftHatX)); Serial.print(PS3.getAnalogHat(LeftHatX));
Serial.print(F("\tLeftHatY: ")); Serial.print(F("\tLeftHatY: "));
Serial.print(PS3.getAnalogHat(LeftHatY)); Serial.print(PS3.getAnalogHat(LeftHatY));
if(!PS3.PS3NavigationConnected) { if (PS3.PS3Connected) { // The Navigation controller only have one joystick
Serial.print(F("\tRightHatX: ")); Serial.print(F("\tRightHatX: "));
Serial.print(PS3.getAnalogHat(RightHatX)); Serial.print(PS3.getAnalogHat(RightHatX));
Serial.print(F("\tRightHatY: ")); Serial.print(F("\tRightHatY: "));
@ -69,28 +70,28 @@ void loop() {
if (PS3.getButtonClick(UP)) { if (PS3.getButtonClick(UP)) {
Serial.print(F("\r\nUp")); Serial.print(F("\r\nUp"));
if (PS3.PS3Connected) { if (PS3.PS3Connected) {
PS3.setAllOff(); PS3.setLedOff();
PS3.setLedOn(LED4); PS3.setLedOn(LED4);
} }
} }
if (PS3.getButtonClick(RIGHT)) { if (PS3.getButtonClick(RIGHT)) {
Serial.print(F("\r\nRight")); Serial.print(F("\r\nRight"));
if (PS3.PS3Connected) { if (PS3.PS3Connected) {
PS3.setAllOff(); PS3.setLedOff();
PS3.setLedOn(LED1); PS3.setLedOn(LED1);
} }
} }
if (PS3.getButtonClick(DOWN)) { if (PS3.getButtonClick(DOWN)) {
Serial.print(F("\r\nDown")); Serial.print(F("\r\nDown"));
if (PS3.PS3Connected) { if (PS3.PS3Connected) {
PS3.setAllOff(); PS3.setLedOff();
PS3.setLedOn(LED2); PS3.setLedOn(LED2);
} }
} }
if (PS3.getButtonClick(LEFT)) { if (PS3.getButtonClick(LEFT)) {
Serial.print(F("\r\nLeft")); Serial.print(F("\r\nLeft"));
if (PS3.PS3Connected) { if (PS3.PS3Connected) {
PS3.setAllOff(); PS3.setLedOff();
PS3.setLedOn(LED3); PS3.setLedOn(LED3);
} }
} }

View file

@ -9,7 +9,8 @@
#include <usbhub.h> #include <usbhub.h>
USB Usb; USB Usb;
USBHub Hub1(&Usb); // Some dongles have a hub inside //USBHub Hub1(&Usb); // Some dongles have a hub inside
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
PS3BT *PS3[2]; // We will use this pointer to store the two instance, you can easily make it larger if you like, but it will use a lot of RAM! PS3BT *PS3[2]; // We will use this pointer to store the two instance, you can easily make it larger if you like, but it will use a lot of RAM!
const uint8_t length = sizeof(PS3) / sizeof(PS3[0]); // Get the lenght of the array const uint8_t length = sizeof(PS3) / sizeof(PS3[0]); // Get the lenght of the array
@ -40,7 +41,7 @@ void loop() {
Serial.print(PS3[i]->getAnalogHat(LeftHatX)); Serial.print(PS3[i]->getAnalogHat(LeftHatX));
Serial.print(F("\tLeftHatY: ")); Serial.print(F("\tLeftHatY: "));
Serial.print(PS3[i]->getAnalogHat(LeftHatY)); Serial.print(PS3[i]->getAnalogHat(LeftHatY));
if(!PS3[i]->PS3NavigationConnected) { // The Navigation controller only have one joystick if (PS3[i]->PS3Connected) { // The Navigation controller only have one joystick
Serial.print(F("\tRightHatX: ")); Serial.print(F("\tRightHatX: "));
Serial.print(PS3[i]->getAnalogHat(RightHatX)); Serial.print(PS3[i]->getAnalogHat(RightHatX));
Serial.print(F("\tRightHatY: ")); Serial.print(F("\tRightHatY: "));
@ -74,28 +75,28 @@ void loop() {
if (PS3[i]->getButtonClick(UP)) { if (PS3[i]->getButtonClick(UP)) {
Serial.print(F("\r\nUp")); Serial.print(F("\r\nUp"));
if (PS3[i]->PS3Connected) { if (PS3[i]->PS3Connected) {
PS3[i]->setAllOff(); PS3[i]->setLedOff();
PS3[i]->setLedOn(LED4); PS3[i]->setLedOn(LED4);
} }
} }
if (PS3[i]->getButtonClick(RIGHT)) { if (PS3[i]->getButtonClick(RIGHT)) {
Serial.print(F("\r\nRight")); Serial.print(F("\r\nRight"));
if (PS3[i]->PS3Connected) { if (PS3[i]->PS3Connected) {
PS3[i]->setAllOff(); PS3[i]->setLedOff();
PS3[i]->setLedOn(LED1); PS3[i]->setLedOn(LED1);
} }
} }
if (PS3[i]->getButtonClick(DOWN)) { if (PS3[i]->getButtonClick(DOWN)) {
Serial.print(F("\r\nDown")); Serial.print(F("\r\nDown"));
if (PS3[i]->PS3Connected) { if (PS3[i]->PS3Connected) {
PS3[i]->setAllOff(); PS3[i]->setLedOff();
PS3[i]->setLedOn(LED2); PS3[i]->setLedOn(LED2);
} }
} }
if (PS3[i]->getButtonClick(LEFT)) { if (PS3[i]->getButtonClick(LEFT)) {
Serial.print(F("\r\nLeft")); Serial.print(F("\r\nLeft"));
if (PS3[i]->PS3Connected) { if (PS3[i]->PS3Connected) {
PS3[i]->setAllOff(); PS3[i]->setLedOff();
PS3[i]->setLedOn(LED3); PS3[i]->setLedOn(LED3);
} }
} }

View file

@ -14,7 +14,8 @@
#include <usbhub.h> #include <usbhub.h>
USB Usb; USB Usb;
USBHub Hub1(&Usb); // Some dongles have a hub inside //USBHub Hub1(&Usb); // Some dongles have a hub inside
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
/* You can create the instances of the bluetooth services in two ways */ /* You can create the instances of the bluetooth services in two ways */
@ -59,7 +60,7 @@ void loop() {
output += PS3.getAnalogHat(LeftHatX); output += PS3.getAnalogHat(LeftHatX);
output += "\tLeftHatY: "; output += "\tLeftHatY: ";
output += PS3.getAnalogHat(LeftHatY); output += PS3.getAnalogHat(LeftHatY);
if(!PS3.PS3NavigationConnected) { if (PS3.PS3Connected) { // The Navigation controller only have one joystick
output += "\tRightHatX: "; output += "\tRightHatX: ";
output += PS3.getAnalogHat(RightHatX); output += PS3.getAnalogHat(RightHatX);
output += "\tRightHatY: "; output += "\tRightHatY: ";
@ -100,28 +101,28 @@ void loop() {
if (PS3.getButtonClick(UP)) { if (PS3.getButtonClick(UP)) {
output += " - Up"; output += " - Up";
if (PS3.PS3Connected) { if (PS3.PS3Connected) {
PS3.setAllOff(); PS3.setLedOff();
PS3.setLedOn(LED4); PS3.setLedOn(LED4);
} }
} }
if (PS3.getButtonClick(RIGHT)) { if (PS3.getButtonClick(RIGHT)) {
output += " - Right"; output += " - Right";
if (PS3.PS3Connected) { if (PS3.PS3Connected) {
PS3.setAllOff(); PS3.setLedOff();
PS3.setLedOn(LED1); PS3.setLedOn(LED1);
} }
} }
if (PS3.getButtonClick(DOWN)) { if (PS3.getButtonClick(DOWN)) {
output += " - Down"; output += " - Down";
if (PS3.PS3Connected) { if (PS3.PS3Connected) {
PS3.setAllOff(); PS3.setLedOff();
PS3.setLedOn(LED2); PS3.setLedOn(LED2);
} }
} }
if (PS3.getButtonClick(LEFT)) { if (PS3.getButtonClick(LEFT)) {
output += " - Left"; output += " - Left";
if (PS3.PS3Connected) { if (PS3.PS3Connected) {
PS3.setAllOff(); PS3.setLedOff();
PS3.setLedOn(LED3); PS3.setLedOn(LED3);
} }
} }

View file

@ -8,7 +8,8 @@
#include <usbhub.h> #include <usbhub.h>
USB Usb; USB Usb;
USBHub Hub1(&Usb); // Some dongles have a hub inside //USBHub Hub1(&Usb); // Some dongles have a hub inside
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
/* You can create the instance of the class in two ways */ /* You can create the instance of the class in two ways */
SPP SerialBT(&Btd); // This will set the name to the defaults: "Arduino" and the pin to "1234" SPP SerialBT(&Btd); // This will set the name to the defaults: "Arduino" and the pin to "1234"

View file

@ -8,7 +8,8 @@
#include <usbhub.h> #include <usbhub.h>
USB Usb; USB Usb;
USBHub Hub1(&Usb); // Some dongles have a hub inside //USBHub Hub1(&Usb); // Some dongles have a hub inside
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
SPP *SerialBT[2]; // We will use this pointer to store the two instance, you can easily make it larger if you like, but it will use a lot of RAM! SPP *SerialBT[2]; // We will use this pointer to store the two instance, you can easily make it larger if you like, but it will use a lot of RAM!
const uint8_t length = sizeof(SerialBT) / sizeof(SerialBT[0]); // Get the lenght of the array const uint8_t length = sizeof(SerialBT) / sizeof(SerialBT[0]); // Get the lenght of the array

View file

@ -8,7 +8,8 @@
#include <usbhub.h> #include <usbhub.h>
USB Usb; USB Usb;
USBHub Hub1(&Usb); // Some dongles have a hub inside //USBHub Hub1(&Usb); // Some dongles have a hub inside
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
/* You can create the instance of the class in two ways */ /* You can create the instance of the class in two ways */
WII Wii(&Btd, PAIR); // This will start an inquiry and then pair with your Wiimote - you only have to do this once WII Wii(&Btd, PAIR); // This will start an inquiry and then pair with your Wiimote - you only have to do this once
@ -34,22 +35,22 @@ void loop() {
} }
else { else {
if (Wii.getButtonClick(LEFT)) { if (Wii.getButtonClick(LEFT)) {
Wii.setAllOff(); Wii.setLedOff();
Wii.setLedOn(LED1); Wii.setLedOn(LED1);
Serial.print(F("\r\nLeft")); Serial.print(F("\r\nLeft"));
} }
if (Wii.getButtonClick(RIGHT)) { if (Wii.getButtonClick(RIGHT)) {
Wii.setAllOff(); Wii.setLedOff();
Wii.setLedOn(LED3); Wii.setLedOn(LED3);
Serial.print(F("\r\nRight")); Serial.print(F("\r\nRight"));
} }
if (Wii.getButtonClick(DOWN)) { if (Wii.getButtonClick(DOWN)) {
Wii.setAllOff(); Wii.setLedOff();
Wii.setLedOn(LED4); Wii.setLedOn(LED4);
Serial.print(F("\r\nDown")); Serial.print(F("\r\nDown"));
} }
if (Wii.getButtonClick(UP)) { if (Wii.getButtonClick(UP)) {
Wii.setAllOff(); Wii.setLedOff();
Wii.setLedOn(LED2); Wii.setLedOn(LED2);
Serial.print(F("\r\nUp")); Serial.print(F("\r\nUp"));
} }

View file

@ -19,7 +19,8 @@ Otherwise, wire up a IR LED yourself.
#endif #endif
USB Usb; USB Usb;
USBHub Hub1(&Usb); // Some dongles have a hub inside //USBHub Hub1(&Usb); // Some dongles have a hub inside
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
/* You can create the instance of the class in two ways */ /* You can create the instance of the class in two ways */
WII Wii(&Btd, PAIR); // This will start an inquiry and then pair with your Wiimote - you only have to do this once WII Wii(&Btd, PAIR); // This will start an inquiry and then pair with your Wiimote - you only have to do this once

View file

@ -9,7 +9,8 @@
#include <usbhub.h> #include <usbhub.h>
USB Usb; USB Usb;
USBHub Hub1(&Usb); // Some dongles have a hub inside //USBHub Hub1(&Usb); // Some dongles have a hub inside
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
WII *Wii[2]; // We will use this pointer to store the two instance, you can easily make it larger if you like, but it will use a lot of RAM! WII *Wii[2]; // We will use this pointer to store the two instance, you can easily make it larger if you like, but it will use a lot of RAM!
const uint8_t length = sizeof(Wii) / sizeof(Wii[0]); // Get the lenght of the array const uint8_t length = sizeof(Wii) / sizeof(Wii[0]); // Get the lenght of the array
@ -42,22 +43,22 @@ void loop() {
} }
else { else {
if (Wii[i]->getButtonClick(LEFT)) { if (Wii[i]->getButtonClick(LEFT)) {
Wii[i]->setAllOff(); Wii[i]->setLedOff();
Wii[i]->setLedOn(LED1); Wii[i]->setLedOn(LED1);
Serial.print(F("\r\nLeft")); Serial.print(F("\r\nLeft"));
} }
if (Wii[i]->getButtonClick(RIGHT)) { if (Wii[i]->getButtonClick(RIGHT)) {
Wii[i]->setAllOff(); Wii[i]->setLedOff();
Wii[i]->setLedOn(LED3); Wii[i]->setLedOn(LED3);
Serial.print(F("\r\nRight")); Serial.print(F("\r\nRight"));
} }
if (Wii[i]->getButtonClick(DOWN)) { if (Wii[i]->getButtonClick(DOWN)) {
Wii[i]->setAllOff(); Wii[i]->setLedOff();
Wii[i]->setLedOn(LED4); Wii[i]->setLedOn(LED4);
Serial.print(F("\r\nDown")); Serial.print(F("\r\nDown"));
} }
if (Wii[i]->getButtonClick(UP)) { if (Wii[i]->getButtonClick(UP)) {
Wii[i]->setAllOff(); Wii[i]->setLedOff();
Wii[i]->setLedOn(LED2); Wii[i]->setLedOn(LED2);
Serial.print(F("\r\nUp")); Serial.print(F("\r\nUp"));
} }

View file

@ -8,7 +8,8 @@
#include <usbhub.h> #include <usbhub.h>
USB Usb; USB Usb;
USBHub Hub1(&Usb); // Some dongles have a hub inside //USBHub Hub1(&Usb); // Some dongles have a hub inside
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
/* You can create the instance of the class in two ways */ /* You can create the instance of the class in two ways */
WII Wii(&Btd, PAIR); // This will start an inquiry and then pair with your Wiimote - you only have to do this once WII Wii(&Btd, PAIR); // This will start an inquiry and then pair with your Wiimote - you only have to do this once
@ -32,22 +33,22 @@ void loop() {
} }
else { else {
if (Wii.getButtonClick(LEFT)) { if (Wii.getButtonClick(LEFT)) {
Wii.setAllOff(); Wii.setLedOff();
Wii.setLedOn(LED1); Wii.setLedOn(LED1);
Serial.print(F("\r\nLeft")); Serial.print(F("\r\nLeft"));
} }
if (Wii.getButtonClick(RIGHT)) { if (Wii.getButtonClick(RIGHT)) {
Wii.setAllOff(); Wii.setLedOff();
Wii.setLedOn(LED3); Wii.setLedOn(LED3);
Serial.print(F("\r\nRight")); Serial.print(F("\r\nRight"));
} }
if (Wii.getButtonClick(DOWN)) { if (Wii.getButtonClick(DOWN)) {
Wii.setAllOff(); Wii.setLedOff();
Wii.setLedOn(LED4); Wii.setLedOn(LED4);
Serial.print(F("\r\nDown")); Serial.print(F("\r\nDown"));
} }
if (Wii.getButtonClick(UP)) { if (Wii.getButtonClick(UP)) {
Wii.setAllOff(); Wii.setLedOff();
Wii.setLedOn(LED2); Wii.setLedOn(LED2);
Serial.print(F("\r\nUp")); Serial.print(F("\r\nUp"));
} }

View file

@ -32,7 +32,7 @@ void loop() {
Serial.print(PS3.getAnalogHat(LeftHatX)); Serial.print(PS3.getAnalogHat(LeftHatX));
Serial.print(F("\tLeftHatY: ")); Serial.print(F("\tLeftHatY: "));
Serial.print(PS3.getAnalogHat(LeftHatY)); Serial.print(PS3.getAnalogHat(LeftHatY));
if(!PS3.PS3NavigationConnected) { if (PS3.PS3Connected) { // The Navigation controller only have one joystick
Serial.print(F("\tRightHatX: ")); Serial.print(F("\tRightHatX: "));
Serial.print(PS3.getAnalogHat(RightHatX)); Serial.print(PS3.getAnalogHat(RightHatX));
Serial.print(F("\tRightHatY: ")); Serial.print(F("\tRightHatY: "));
@ -62,22 +62,22 @@ void loop() {
if (PS3.getButtonClick(UP)) { if (PS3.getButtonClick(UP)) {
Serial.print(F("\r\nUp")); Serial.print(F("\r\nUp"));
PS3.setAllOff(); PS3.setLedOff();
PS3.setLedOn(LED4); PS3.setLedOn(LED4);
} }
if (PS3.getButtonClick(RIGHT)) { if (PS3.getButtonClick(RIGHT)) {
Serial.print(F("\r\nRight")); Serial.print(F("\r\nRight"));
PS3.setAllOff(); PS3.setLedOff();
PS3.setLedOn(LED1); PS3.setLedOn(LED1);
} }
if (PS3.getButtonClick(DOWN)) { if (PS3.getButtonClick(DOWN)) {
Serial.print(F("\r\nDown")); Serial.print(F("\r\nDown"));
PS3.setAllOff(); PS3.setLedOff();
PS3.setLedOn(LED2); PS3.setLedOn(LED2);
} }
if (PS3.getButtonClick(LEFT)) { if (PS3.getButtonClick(LEFT)) {
Serial.print(F("\r\nLeft")); Serial.print(F("\r\nLeft"));
PS3.setAllOff(); PS3.setLedOff();
PS3.setLedOn(LED3); PS3.setLedOn(LED3);
} }
@ -98,63 +98,43 @@ void loop() {
Serial.print(F("\r\nStart")); Serial.print(F("\r\nStart"));
printAngle = !printAngle; printAngle = !printAngle;
} }
}
if (printAngle) { if (printAngle) {
Serial.print(F("\r\nPitch: ")); Serial.print(F("\r\nPitch: "));
Serial.print(PS3.getAngle(Pitch)); Serial.print(PS3.getAngle(Pitch));
Serial.print(F("\tRoll: ")); Serial.print(F("\tRoll: "));
Serial.print(PS3.getAngle(Roll)); Serial.print(PS3.getAngle(Roll));
} }
}
else if (PS3.PS3MoveConnected) { // One can only set the color of the bulb, set the rumble, set and get the bluetooth address and calibrate the magnetometer via USB else if (PS3.PS3MoveConnected) { // One can only set the color of the bulb, set the rumble, set and get the bluetooth address and calibrate the magnetometer via USB
switch(state) { if (state == 0) {
case 0:
PS3.moveSetRumble(0); PS3.moveSetRumble(0);
PS3.moveSetBulb(Off); PS3.moveSetBulb(Off);
state = 1; } else if (state == 1) {
break;
case 1:
PS3.moveSetRumble(75); PS3.moveSetRumble(75);
PS3.moveSetBulb(Red); PS3.moveSetBulb(Red);
state = 2; } else if (state == 2) {
break;
case 2:
PS3.moveSetRumble(125); PS3.moveSetRumble(125);
PS3.moveSetBulb(Green); PS3.moveSetBulb(Green);
state = 3; } else if (state == 3) {
break;
case 3:
PS3.moveSetRumble(150); PS3.moveSetRumble(150);
PS3.moveSetBulb(Blue); PS3.moveSetBulb(Blue);
state = 4; } else if (state == 4) {
break;
case 4:
PS3.moveSetRumble(175); PS3.moveSetRumble(175);
PS3.moveSetBulb(Yellow); PS3.moveSetBulb(Yellow);
state = 5; } else if (state == 5) {
break;
case 5:
PS3.moveSetRumble(200); PS3.moveSetRumble(200);
PS3.moveSetBulb(Lightblue); PS3.moveSetBulb(Lightblue);
state = 6; } else if (state == 6) {
break;
case 6:
PS3.moveSetRumble(225); PS3.moveSetRumble(225);
PS3.moveSetBulb(Purble); PS3.moveSetBulb(Purble);
state = 7; } else if (state == 7) {
break;
case 7:
PS3.moveSetRumble(250); PS3.moveSetRumble(250);
PS3.moveSetBulb(White); PS3.moveSetBulb(White);
state = 0;
break;
} }
state++;
if (state > 7)
state = 0;
delay(1000); delay(1000);
} }
} }

View file

@ -31,6 +31,7 @@ void loop() {
Xbox.setRumbleOn(Xbox.getButtonPress(BLACK), Xbox.getButtonPress(WHITE)); Xbox.setRumbleOn(Xbox.getButtonPress(BLACK), Xbox.getButtonPress(WHITE));
} else } else
Xbox.setRumbleOn(0, 0); Xbox.setRumbleOn(0, 0);
if (Xbox.getAnalogHat(LeftHatX) > 7500 || Xbox.getAnalogHat(LeftHatX) < -7500 || Xbox.getAnalogHat(LeftHatY) > 7500 || Xbox.getAnalogHat(LeftHatY) < -7500 || Xbox.getAnalogHat(RightHatX) > 7500 || Xbox.getAnalogHat(RightHatX) < -7500 || Xbox.getAnalogHat(RightHatY) > 7500 || Xbox.getAnalogHat(RightHatY) < -7500) { if (Xbox.getAnalogHat(LeftHatX) > 7500 || Xbox.getAnalogHat(LeftHatX) < -7500 || Xbox.getAnalogHat(LeftHatY) > 7500 || Xbox.getAnalogHat(LeftHatY) < -7500 || Xbox.getAnalogHat(RightHatX) > 7500 || Xbox.getAnalogHat(RightHatX) < -7500 || Xbox.getAnalogHat(RightHatY) > 7500 || Xbox.getAnalogHat(RightHatY) < -7500) {
if (Xbox.getAnalogHat(LeftHatX) > 7500 || Xbox.getAnalogHat(LeftHatX) < -7500) { if (Xbox.getAnalogHat(LeftHatX) > 7500 || Xbox.getAnalogHat(LeftHatX) < -7500) {
Serial.print(F("LeftHatX: ")); Serial.print(F("LeftHatX: "));

View file

@ -31,6 +31,7 @@ void loop() {
Serial.println(Xbox.getButtonPress(R2, i)); Serial.println(Xbox.getButtonPress(R2, i));
Xbox.setRumbleOn(Xbox.getButtonPress(L2, i), Xbox.getButtonPress(R2, i), i); Xbox.setRumbleOn(Xbox.getButtonPress(L2, i), Xbox.getButtonPress(R2, i), i);
} }
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 || 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) { if (Xbox.getAnalogHat(LeftHatX, i) > 7500 || Xbox.getAnalogHat(LeftHatX, i) < -7500) {
Serial.print(F("LeftHatX: ")); Serial.print(F("LeftHatX: "));
@ -94,8 +95,10 @@ void loop() {
Serial.print(Xbox.getBatteryLevel(i)); // The battery level in the range 0-3 Serial.print(Xbox.getBatteryLevel(i)); // The battery level in the range 0-3
Serial.println(F(")")); Serial.println(F(")"));
} }
if(Xbox.getButtonClick(SYNC,i)) if (Xbox.getButtonClick(SYNC, i)) {
Serial.println(F("Sync")); Serial.println(F("Sync"));
Xbox.disconnect(i);
}
if (Xbox.getButtonClick(A, i)) if (Xbox.getButtonClick(A, i))
Serial.println(F("A")); Serial.println(F("A"));
@ -108,5 +111,4 @@ void loop() {
} }
} }
} }
delay(1);
} }

View file

@ -29,6 +29,7 @@ void loop() {
Xbox.setRumbleOn(Xbox.getButtonPress(L2), Xbox.getButtonPress(R2)); Xbox.setRumbleOn(Xbox.getButtonPress(L2), Xbox.getButtonPress(R2));
} else } else
Xbox.setRumbleOn(0, 0); Xbox.setRumbleOn(0, 0);
if (Xbox.getAnalogHat(LeftHatX) > 7500 || Xbox.getAnalogHat(LeftHatX) < -7500 || Xbox.getAnalogHat(LeftHatY) > 7500 || Xbox.getAnalogHat(LeftHatY) < -7500 || Xbox.getAnalogHat(RightHatX) > 7500 || Xbox.getAnalogHat(RightHatX) < -7500 || Xbox.getAnalogHat(RightHatY) > 7500 || Xbox.getAnalogHat(RightHatY) < -7500) { if (Xbox.getAnalogHat(LeftHatX) > 7500 || Xbox.getAnalogHat(LeftHatX) < -7500 || Xbox.getAnalogHat(LeftHatY) > 7500 || Xbox.getAnalogHat(LeftHatY) < -7500 || Xbox.getAnalogHat(RightHatX) > 7500 || Xbox.getAnalogHat(RightHatX) < -7500 || Xbox.getAnalogHat(RightHatY) > 7500 || Xbox.getAnalogHat(RightHatY) < -7500) {
if (Xbox.getAnalogHat(LeftHatX) > 7500 || Xbox.getAnalogHat(LeftHatX) < -7500) { if (Xbox.getAnalogHat(LeftHatX) > 7500 || Xbox.getAnalogHat(LeftHatX) < -7500) {
Serial.print(F("LeftHatX: ")); Serial.print(F("LeftHatX: "));

View file

@ -10,10 +10,11 @@ ADK adk(&Usb,"TKJElectronics", // Manufacturer Name
"http://www.tkjelectronics.dk/uploads/ArduinoBlinkLED.apk", // URL (web page to visit if no installed apps support the accessory) "http://www.tkjelectronics.dk/uploads/ArduinoBlinkLED.apk", // URL (web page to visit if no installed apps support the accessory)
"123456789"); // Serial Number (optional) "123456789"); // Serial Number (optional)
#define LED 13 // Pin 13 is occupied by the SCK pin on a normal Arduino (Uno, Duemilanove etc.), so use a different pin #define LED LED_BUILTIN // Use built in LED - note that pin 13 is occupied by the SCK pin on a normal Arduino (Uno, Duemilanove etc.), so use a different pin
void setup() uint32_t timer;
{
void setup() {
Serial.begin(115200); Serial.begin(115200);
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
if (Usb.Init() == -1) { if (Usb.Init() == -1) {
@ -24,20 +25,32 @@ void setup()
Serial.print("\r\nArduino Blink LED Started"); Serial.print("\r\nArduino Blink LED Started");
} }
void loop() void loop() {
{
Usb.Task(); Usb.Task();
if (adk.isReady()) { if (adk.isReady()) {
uint8_t msg[1]; uint8_t msg[1];
uint16_t len = sizeof(msg); uint16_t len = sizeof(msg);
uint8_t rcode = adk.RcvData(&len, msg); uint8_t rcode = adk.RcvData(&len, msg);
if(rcode && rcode != hrNAK) if (rcode && rcode != hrNAK) {
USBTRACE2("Data rcv. :", rcode); Serial.print(F("\r\nData rcv: "));
if(len > 0) { Serial.print(rcode, HEX);
} else if (len > 0) {
Serial.print(F("\r\nData Packet: ")); Serial.print(F("\r\nData Packet: "));
Serial.print(msg[0]); Serial.print(msg[0]);
digitalWrite(LED, msg[0] ? HIGH : LOW); digitalWrite(LED, msg[0] ? HIGH : LOW);
} }
if (millis() - timer >= 1000) { // Send data every 1s
timer = millis();
rcode = adk.SndData(sizeof(timer), (uint8_t*)&timer);
if (rcode && rcode != hrNAK) {
Serial.print(F("\r\nData send: "));
Serial.print(rcode, HEX);
} else if (rcode != hrNAK) {
Serial.print(F("\r\nTimer: "));
Serial.print(timer);
}
}
} }
else else
digitalWrite(LED, LOW); digitalWrite(LED, LOW);

View file

@ -34,7 +34,10 @@ PS3USB KEYWORD1
# Methods and Functions (KEYWORD2) # Methods and Functions (KEYWORD2)
#################################################### ####################################################
setBdaddr KEYWORD2 setBdaddr KEYWORD2
getBdaddr KEYWORD2
setMoveBdaddr KEYWORD2 setMoveBdaddr KEYWORD2
getMoveBdaddr KEYWORD2
getMoveCalibration KEYWORD2
getButtonPress KEYWORD2 getButtonPress KEYWORD2
getButtonClick KEYWORD2 getButtonClick KEYWORD2