mirror of
https://github.com/felis/USB_Host_Shield_2.0.git
synced 2024-03-22 11:31:26 +01:00
Merge pull request #23 from TKJElectronics/master
Check microcontroller type before defining Mega ADK
This commit is contained in:
commit
19bf7e007d
7 changed files with 647 additions and 3 deletions
|
@ -1,4 +1,4 @@
|
||||||
The PS3BT.cpp, PS3BT.h, PS3USB.cpp, and PS3USB.h were developed by Kristian Lauszus
|
The PS3BT.cpp, PS3BT.h, PS3USB.cpp, PS3USB.h, XBOXUSB.cpp, and XBOXUSB.h were developed by Kristian Lauszus
|
||||||
|
|
||||||
For more information regarding the PS3 protocol etc. visit my blog at: http://blog.tkjelectronics.dk/ or send me an email at kristianl at tkjelectronics dot dk.
|
For more information regarding the PS3 protocol etc. visit my blog at: http://blog.tkjelectronics.dk/ or send me an email at kristianl at tkjelectronics dot dk.
|
||||||
You could also visit the official wiki: https://github.com/TKJElectronics/USB_Host_Shield_2.0/wiki for information.
|
You could also visit the official wiki: https://github.com/TKJElectronics/USB_Host_Shield_2.0/wiki for information.
|
||||||
|
@ -19,4 +19,8 @@ http://www.copenhagengamecollective.org/unimove/
|
||||||
https://github.com/thp/psmoveapi
|
https://github.com/thp/psmoveapi
|
||||||
http://code.google.com/p/moveonpc/
|
http://code.google.com/p/moveonpc/
|
||||||
|
|
||||||
|
All the information regarding the Xbox 360 controller protocol are form these two sites:
|
||||||
|
http://tattiebogle.net/index.php/ProjectRoot/Xbox360Controller/UsbInfo
|
||||||
|
http://pingus.seul.org/~grumbel/xboxdrv/
|
||||||
|
|
||||||
And at last I would like to thank Oleg from http://www.circuitsathome.com/ for making such an awesome shield!
|
And at last I would like to thank Oleg from http://www.circuitsathome.com/ for making such an awesome shield!
|
|
@ -24,7 +24,7 @@ const uint8_t PS3BT::BTD_EVENT_PIPE = 1;
|
||||||
const uint8_t PS3BT::BTD_DATAIN_PIPE = 2;
|
const uint8_t PS3BT::BTD_DATAIN_PIPE = 2;
|
||||||
const uint8_t PS3BT::BTD_DATAOUT_PIPE = 3;
|
const uint8_t PS3BT::BTD_DATAOUT_PIPE = 3;
|
||||||
|
|
||||||
prog_char OUTPUT_REPORT_BUFFER[] PROGMEM = {
|
uint8_t OUTPUT_REPORT_BUFFER[] PROGMEM = {
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00,
|
0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00,
|
0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
0xff, 0x27, 0x10, 0x00, 0x32,
|
0xff, 0x27, 0x10, 0x00, 0x32,
|
||||||
|
|
342
XBOXUSB.cpp
Normal file
342
XBOXUSB.cpp
Normal file
|
@ -0,0 +1,342 @@
|
||||||
|
/* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
|
||||||
|
|
||||||
|
This software may be distributed and modified under the terms of the GNU
|
||||||
|
General Public License version 2 (GPL2) as published by the Free Software
|
||||||
|
Foundation and appearing in the file GPL2.TXT included in the packaging of
|
||||||
|
this file. Please note that GPL2 Section 2[b] requires that all works based
|
||||||
|
on this software must also be made publicly available under the terms of
|
||||||
|
the GPL2 ("Copyleft").
|
||||||
|
|
||||||
|
Contact information
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
Kristian Lauszus, TKJ Electronics
|
||||||
|
Web : http://www.tkjelectronics.com
|
||||||
|
e-mail : kristianl@tkjelectronics.com
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "XBOXUSB.h"
|
||||||
|
#define DEBUG // Uncomment to print data for debugging
|
||||||
|
//#define EXTRADEBUG // Uncomment to get even more debugging data
|
||||||
|
//#define PRINTREPORT // Uncomment to print the report send by the Xbox 360 Controller
|
||||||
|
|
||||||
|
XBOXUSB::XBOXUSB(USB *p):
|
||||||
|
pUsb(p), // pointer to USB class instance - mandatory
|
||||||
|
bAddress(0), // device address - mandatory
|
||||||
|
bPollEnable(false) { // don't start polling before dongle is connected
|
||||||
|
for(uint8_t i=0; i<XBOX_MAX_ENDPOINTS; i++) {
|
||||||
|
epInfo[i].epAddr = 0;
|
||||||
|
epInfo[i].maxPktSize = (i) ? 0 : 8;
|
||||||
|
epInfo[i].epAttribs = 0;
|
||||||
|
epInfo[i].bmNakPower = (i) ? USB_NAK_NOWAIT : USB_NAK_MAX_POWER;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pUsb) // register in USB subsystem
|
||||||
|
pUsb->RegisterDeviceClass(this); //set devConfig[] entry
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t XBOXUSB::Init(uint8_t parent, uint8_t port, bool lowspeed) {
|
||||||
|
uint8_t buf[sizeof(USB_DEVICE_DESCRIPTOR)];
|
||||||
|
uint8_t rcode;
|
||||||
|
UsbDevice *p = NULL;
|
||||||
|
EpInfo *oldep_ptr = NULL;
|
||||||
|
uint16_t VID;
|
||||||
|
|
||||||
|
// get memory address of USB device address pool
|
||||||
|
AddressPool &addrPool = pUsb->GetAddressPool();
|
||||||
|
#ifdef EXTRADEBUG
|
||||||
|
Notify(PSTR("\r\nXBOXUSB Init"));
|
||||||
|
#endif
|
||||||
|
// check if address has already been assigned to an instance
|
||||||
|
if (bAddress)
|
||||||
|
{
|
||||||
|
#ifdef DEBUG
|
||||||
|
Notify(PSTR("\r\nAddress in use"));
|
||||||
|
#endif
|
||||||
|
return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get pointer to pseudo device with address 0 assigned
|
||||||
|
p = addrPool.GetUsbDevicePtr(0);
|
||||||
|
|
||||||
|
if (!p)
|
||||||
|
{
|
||||||
|
#ifdef DEBUG
|
||||||
|
Notify(PSTR("\r\nAddress not found"));
|
||||||
|
#endif
|
||||||
|
return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!p->epinfo)
|
||||||
|
{
|
||||||
|
#ifdef DEBUG
|
||||||
|
Notify(PSTR("\r\nepinfo is null"));
|
||||||
|
#endif
|
||||||
|
return USB_ERROR_EPINFO_IS_NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save old pointer to EP_RECORD of address 0
|
||||||
|
oldep_ptr = p->epinfo;
|
||||||
|
|
||||||
|
// Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
|
||||||
|
p->epinfo = epInfo;
|
||||||
|
|
||||||
|
p->lowspeed = lowspeed;
|
||||||
|
|
||||||
|
// Get device descriptor
|
||||||
|
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;
|
||||||
|
|
||||||
|
if(rcode)
|
||||||
|
goto FailGetDevDescr;
|
||||||
|
|
||||||
|
// Allocate new address according to device class
|
||||||
|
bAddress = addrPool.AllocAddress(parent, false, port);
|
||||||
|
|
||||||
|
if (!bAddress)
|
||||||
|
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;
|
||||||
|
|
||||||
|
// Assign new address to the device
|
||||||
|
rcode = pUsb->setAddr( 0, 0, bAddress );
|
||||||
|
if (rcode)
|
||||||
|
{
|
||||||
|
p->lowspeed = false;
|
||||||
|
addrPool.FreeAddress(bAddress);
|
||||||
|
bAddress = 0;
|
||||||
|
#ifdef DEBUG
|
||||||
|
Notify(PSTR("\r\nsetAddr: "));
|
||||||
|
#endif
|
||||||
|
PrintHex<uint8_t>(rcode);
|
||||||
|
return rcode;
|
||||||
|
}
|
||||||
|
#ifdef EXTRADEBUG
|
||||||
|
Notify(PSTR("\r\nAddr: "));
|
||||||
|
PrintHex<uint8_t>(bAddress);
|
||||||
|
#endif
|
||||||
|
p->lowspeed = false;
|
||||||
|
|
||||||
|
//get pointer to assigned address record
|
||||||
|
p = addrPool.GetUsbDevicePtr(bAddress);
|
||||||
|
if (!p)
|
||||||
|
return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
|
||||||
|
|
||||||
|
p->lowspeed = lowspeed;
|
||||||
|
|
||||||
|
// Assign epInfo to epinfo pointer - only EP0 is known
|
||||||
|
rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo);
|
||||||
|
if (rcode)
|
||||||
|
goto FailSetDevTblEntry;
|
||||||
|
|
||||||
|
VID = ((USB_DEVICE_DESCRIPTOR*)buf)->idVendor;
|
||||||
|
|
||||||
|
if(VID == XBOX_VID) { // We just check if it's a xbox controller using the Vendor ID
|
||||||
|
/* The application will work in reduced host mode, so we can save program and data
|
||||||
|
memory space. After verifying the VID we will use known values for the
|
||||||
|
configuration values for device, interface, endpoints and HID for the XBOX360 Controllers */
|
||||||
|
|
||||||
|
/* Initialize data structures for endpoints of device */
|
||||||
|
epInfo[ XBOX_INPUT_PIPE ].epAddr = 0x01; // XBOX 360 report endpoint
|
||||||
|
epInfo[ XBOX_INPUT_PIPE ].epAttribs = EP_INTERRUPT;
|
||||||
|
epInfo[ XBOX_INPUT_PIPE ].bmNakPower = USB_NAK_NOWAIT; // Only poll once for interrupt endpoints
|
||||||
|
epInfo[ XBOX_INPUT_PIPE ].maxPktSize = EP_MAXPKTSIZE;
|
||||||
|
epInfo[ XBOX_INPUT_PIPE ].bmSndToggle = bmSNDTOG0;
|
||||||
|
epInfo[ XBOX_INPUT_PIPE ].bmRcvToggle = bmRCVTOG0;
|
||||||
|
epInfo[ XBOX_OUTPUT_PIPE ].epAddr = 0x02; // XBOX 360 output endpoint
|
||||||
|
epInfo[ XBOX_OUTPUT_PIPE ].epAttribs = EP_INTERRUPT;
|
||||||
|
epInfo[ XBOX_OUTPUT_PIPE ].bmNakPower = USB_NAK_NOWAIT; // Only poll once for interrupt endpoints
|
||||||
|
epInfo[ XBOX_OUTPUT_PIPE ].maxPktSize = EP_MAXPKTSIZE;
|
||||||
|
epInfo[ XBOX_OUTPUT_PIPE ].bmSndToggle = bmSNDTOG0;
|
||||||
|
epInfo[ XBOX_OUTPUT_PIPE ].bmRcvToggle = bmRCVTOG0;
|
||||||
|
|
||||||
|
rcode = pUsb->setEpInfoEntry(bAddress, 3, epInfo);
|
||||||
|
if( rcode )
|
||||||
|
goto FailSetDevTblEntry;
|
||||||
|
|
||||||
|
delay(200);//Give time for address change
|
||||||
|
|
||||||
|
rcode = pUsb->setConf(bAddress, epInfo[ XBOX_CONTROL_PIPE ].epAddr, 1);
|
||||||
|
if( rcode )
|
||||||
|
goto FailSetConf;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
Notify(PSTR("\r\nXbox 360 Controller Connected"));
|
||||||
|
#endif
|
||||||
|
setLedMode(ROTATING);
|
||||||
|
Xbox360Connected = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
goto FailUnknownDevice;
|
||||||
|
|
||||||
|
bPollEnable = true;
|
||||||
|
Notify(PSTR("\r\n"));
|
||||||
|
return 0; // successful configuration
|
||||||
|
|
||||||
|
/* diagnostic messages */
|
||||||
|
FailGetDevDescr:
|
||||||
|
#ifdef DEBUG
|
||||||
|
Notify(PSTR("\r\ngetDevDescr:"));
|
||||||
|
#endif
|
||||||
|
goto Fail;
|
||||||
|
FailSetDevTblEntry:
|
||||||
|
#ifdef DEBUG
|
||||||
|
Notify(PSTR("\r\nsetDevTblEn:"));
|
||||||
|
#endif
|
||||||
|
goto Fail;
|
||||||
|
FailSetConf:
|
||||||
|
#ifdef DEBUG
|
||||||
|
Notify(PSTR("\r\nsetConf:"));
|
||||||
|
#endif
|
||||||
|
goto Fail;
|
||||||
|
FailUnknownDevice:
|
||||||
|
#ifdef DEBUG
|
||||||
|
Notify(PSTR("\r\nUnknown Device Connected - VID: "));
|
||||||
|
PrintHex<uint16_t>(VID);
|
||||||
|
Notify(PSTR(" PID: "));
|
||||||
|
PrintHex<uint16_t>(((USB_DEVICE_DESCRIPTOR*)buf)->idProduct);
|
||||||
|
#endif
|
||||||
|
goto Fail;
|
||||||
|
Fail:
|
||||||
|
#ifdef DEBUG
|
||||||
|
Notify(PSTR("\r\nXbox 360 Init Failed, error code: "));
|
||||||
|
Serial.print(rcode);
|
||||||
|
#endif
|
||||||
|
Release();
|
||||||
|
return rcode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Performs a cleanup after failed Init() attempt */
|
||||||
|
uint8_t XBOXUSB::Release() {
|
||||||
|
Xbox360Connected = false;
|
||||||
|
pUsb->GetAddressPool().FreeAddress(bAddress);
|
||||||
|
bAddress = 0;
|
||||||
|
bPollEnable = false;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
uint8_t XBOXUSB::Poll() {
|
||||||
|
if (!bPollEnable)
|
||||||
|
return 0;
|
||||||
|
uint16_t BUFFER_SIZE = EP_MAXPKTSIZE;
|
||||||
|
pUsb->inTransfer(bAddress, epInfo[ XBOX_INPUT_PIPE ].epAddr, &BUFFER_SIZE, readBuf); // input on endpoint 1
|
||||||
|
readReport();
|
||||||
|
#ifdef PRINTREPORT
|
||||||
|
printReport(); // Uncomment "#define PRINTREPORT" to print the report send by the Xbox 360 Controller
|
||||||
|
#endif
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void XBOXUSB::readReport() {
|
||||||
|
if (readBuf == NULL)
|
||||||
|
return;
|
||||||
|
if(readBuf[0] != 0x00 || readBuf[1] != 0x14) { // Check if it's the correct report - the controller also sends different status reports
|
||||||
|
/*for(int i = 0; i < EP_MAXPKTSIZE; i++)
|
||||||
|
readBuf[i] = 0;*/
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ButtonState = (uint32_t)(readBuf[2] | ((uint16_t)readBuf[3] << 8) | ((uint32_t)readBuf[4] << 16) | ((uint32_t)readBuf[5] << 24));
|
||||||
|
|
||||||
|
//Notify(PSTR("\r\nButtonState");
|
||||||
|
//PrintHex<uint32_t>(ButtonState);
|
||||||
|
|
||||||
|
if(ButtonState != OldButtonState) {
|
||||||
|
buttonChanged = true;
|
||||||
|
if(ButtonState != 0x00) {
|
||||||
|
buttonPressed = true;
|
||||||
|
buttonReleased = false;
|
||||||
|
} else {
|
||||||
|
buttonPressed = false;
|
||||||
|
buttonReleased = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
buttonChanged = false;
|
||||||
|
buttonPressed = false;
|
||||||
|
buttonReleased = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
OldButtonState = ButtonState;
|
||||||
|
}
|
||||||
|
|
||||||
|
void XBOXUSB::printReport() { //Uncomment "#define PRINTREPORT" to print the report send by the Xbox 360 Controller
|
||||||
|
if (readBuf == NULL)
|
||||||
|
return;
|
||||||
|
for(uint8_t i = 0; i < XBOX_REPORT_BUFFER_SIZE;i++) {
|
||||||
|
PrintHex<uint8_t>(readBuf[i]);
|
||||||
|
Serial.print(" ");
|
||||||
|
}
|
||||||
|
Serial.println("");
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t XBOXUSB::getButton(Button b) {
|
||||||
|
if (readBuf == NULL)
|
||||||
|
return false;
|
||||||
|
if(b == L2 || b == R2) { // These are analog buttons
|
||||||
|
return (uint8_t)(readBuf[(uint8_t)b]);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if ((readBuf[(uint16_t)b >> 8] & ((uint8_t)b & 0xff)) > 0)
|
||||||
|
return 1;
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int16_t XBOXUSB::getAnalogHat(AnalogHat a) {
|
||||||
|
if (readBuf == NULL)
|
||||||
|
return 0;
|
||||||
|
return (int16_t)(readBuf[(uint8_t)a+1] << 8 | readBuf[(uint8_t)a]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Playstation Sixaxis Dualshock and Navigation Controller commands */
|
||||||
|
void XBOXUSB::XboxCommand(uint8_t* data, uint16_t nbytes) {
|
||||||
|
//bmRequest = Host to device (0x00) | Class (0x20) | Interface (0x01) = 0x21, bRequest = Set Report (0x09), Report ID (0x00), Report Type (Output 0x02), interface (0x00), datalength, datalength, data)
|
||||||
|
pUsb->ctrlReq(bAddress,epInfo[XBOX_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0x00, 0x02, 0x00, nbytes, nbytes, data, NULL);
|
||||||
|
}
|
||||||
|
void XBOXUSB::setLedOn(LED l) {
|
||||||
|
if(l == ALL) // All LEDs can't be on a the same time
|
||||||
|
return;
|
||||||
|
writeBuf[0] = 0x01;
|
||||||
|
writeBuf[1] = 0x03;
|
||||||
|
writeBuf[2] = (uint8_t)l;
|
||||||
|
writeBuf[2] += 4;
|
||||||
|
|
||||||
|
XboxCommand(writeBuf, 3);
|
||||||
|
}
|
||||||
|
void XBOXUSB::setLedOff() {
|
||||||
|
writeBuf[0] = 0x01;
|
||||||
|
writeBuf[1] = 0x03;
|
||||||
|
writeBuf[2] = 0x00;
|
||||||
|
|
||||||
|
XboxCommand(writeBuf, 3);
|
||||||
|
}
|
||||||
|
void XBOXUSB::setLedBlink(LED l) {
|
||||||
|
writeBuf[0] = 0x01;
|
||||||
|
writeBuf[1] = 0x03;
|
||||||
|
writeBuf[2] = (uint8_t)l;
|
||||||
|
|
||||||
|
XboxCommand(writeBuf, 3);
|
||||||
|
}
|
||||||
|
void XBOXUSB::setLedMode(LEDMode lm) { // This function is used to do some speciel LED stuff the controller supports
|
||||||
|
writeBuf[0] = 0x01;
|
||||||
|
writeBuf[1] = 0x03;
|
||||||
|
writeBuf[2] = (uint8_t)lm;
|
||||||
|
|
||||||
|
XboxCommand(writeBuf, 3);
|
||||||
|
}
|
||||||
|
void XBOXUSB::setRumbleOn(uint8_t lValue, uint8_t rValue) {
|
||||||
|
writeBuf[0] = 0x00;
|
||||||
|
writeBuf[1] = 0x08;
|
||||||
|
writeBuf[2] = 0x00;
|
||||||
|
writeBuf[3] = lValue; // big weight
|
||||||
|
writeBuf[4] = rValue; // small weight
|
||||||
|
writeBuf[5] = 0x00;
|
||||||
|
writeBuf[6] = 0x00;
|
||||||
|
writeBuf[7] = 0x00;
|
||||||
|
|
||||||
|
XboxCommand(writeBuf, 8);
|
||||||
|
}
|
149
XBOXUSB.h
Normal file
149
XBOXUSB.h
Normal file
|
@ -0,0 +1,149 @@
|
||||||
|
/* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
|
||||||
|
|
||||||
|
This software may be distributed and modified under the terms of the GNU
|
||||||
|
General Public License version 2 (GPL2) as published by the Free Software
|
||||||
|
Foundation and appearing in the file GPL2.TXT included in the packaging of
|
||||||
|
this file. Please note that GPL2 Section 2[b] requires that all works based
|
||||||
|
on this software must also be made publicly available under the terms of
|
||||||
|
the GPL2 ("Copyleft").
|
||||||
|
|
||||||
|
Contact information
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
Kristian Lauszus, TKJ Electronics
|
||||||
|
Web : http://www.tkjelectronics.com
|
||||||
|
e-mail : kristianl@tkjelectronics.com
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _xboxusb_h_
|
||||||
|
#define _xboxusb_h_
|
||||||
|
|
||||||
|
#if defined(ARDUINO) && ARDUINO >= 100
|
||||||
|
#include "Arduino.h"
|
||||||
|
#else
|
||||||
|
#include "WProgram.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "Usb.h"
|
||||||
|
|
||||||
|
/* Data Xbox 360 taken from descriptors */
|
||||||
|
#define EP_MAXPKTSIZE 32 // max size for data via USB
|
||||||
|
|
||||||
|
/* Endpoint types */
|
||||||
|
#define EP_INTERRUPT 0x03
|
||||||
|
|
||||||
|
/* Names we give to the 3 Xbox360 pipes */
|
||||||
|
#define XBOX_CONTROL_PIPE 0
|
||||||
|
#define XBOX_INPUT_PIPE 1
|
||||||
|
#define XBOX_OUTPUT_PIPE 2
|
||||||
|
|
||||||
|
//PID and VID of the different devices
|
||||||
|
#define XBOX_VID 0x045E // Microsoft Corporation
|
||||||
|
|
||||||
|
#define XBOX_REPORT_BUFFER_SIZE 14 // Size of the input report buffer
|
||||||
|
|
||||||
|
// 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 HID_REQUEST_SET_REPORT 0x09
|
||||||
|
|
||||||
|
#define XBOX_MAX_ENDPOINTS 3
|
||||||
|
|
||||||
|
enum LED {
|
||||||
|
ALL = 0x01, // Used to blink all LEDs
|
||||||
|
LED1 = 0x02,
|
||||||
|
LED2 = 0x03,
|
||||||
|
LED3 = 0x04,
|
||||||
|
LED4 = 0x05,
|
||||||
|
};
|
||||||
|
enum LEDMode {
|
||||||
|
ROTATING = 0x0A,
|
||||||
|
FASTBLINK = 0x0B,
|
||||||
|
SLOWBLINK = 0x0C,
|
||||||
|
ALTERNATING = 0x0D,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum Button {
|
||||||
|
// byte location | bit location
|
||||||
|
UP = (2 << 8) | 0x01,
|
||||||
|
DOWN = (2 << 8) | 0x02,
|
||||||
|
LEFT = (2 << 8) | 0x04,
|
||||||
|
RIGHT = (2 << 8) | 0x08,
|
||||||
|
|
||||||
|
START = (2 << 8) | 0x10,
|
||||||
|
BACK = (2 << 8) | 0x20,
|
||||||
|
L3 = (2 << 8) | 0x40,
|
||||||
|
R3 = (2 << 8) | 0x80,
|
||||||
|
|
||||||
|
L1 = (3 << 8) | 0x01,
|
||||||
|
R1 = (3 << 8) | 0x02,
|
||||||
|
XBOX = (3 << 8) | 0x04,
|
||||||
|
|
||||||
|
A = (3 << 8) | 0x10,
|
||||||
|
B = (3 << 8) | 0x20,
|
||||||
|
X = (3 << 8) | 0x40,
|
||||||
|
Y = (3 << 8) | 0x80,
|
||||||
|
|
||||||
|
// These buttons are analog
|
||||||
|
L2 = 4,
|
||||||
|
R2 = 5,
|
||||||
|
};
|
||||||
|
enum AnalogHat
|
||||||
|
{
|
||||||
|
LeftHatX = 6,
|
||||||
|
LeftHatY = 8,
|
||||||
|
RightHatX = 10,
|
||||||
|
RightHatY = 12,
|
||||||
|
};
|
||||||
|
|
||||||
|
class XBOXUSB : public USBDeviceConfig
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
XBOXUSB(USB *pUsb);
|
||||||
|
|
||||||
|
// USBDeviceConfig implementation
|
||||||
|
virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
|
||||||
|
virtual uint8_t Release();
|
||||||
|
virtual uint8_t Poll();
|
||||||
|
virtual uint8_t GetAddress() { return bAddress; };
|
||||||
|
virtual bool isReady() { return bPollEnable; };
|
||||||
|
|
||||||
|
/* XBOX Controller Readings */
|
||||||
|
uint8_t getButton(Button b);
|
||||||
|
int16_t getAnalogHat(AnalogHat a);
|
||||||
|
|
||||||
|
/* Commands for Dualshock 3 and Navigation controller */
|
||||||
|
void setAllOff() { setRumbleOn(0,0); setLedOff(); };
|
||||||
|
void setRumbleOff() { setRumbleOn(0,0); };
|
||||||
|
void setRumbleOn(uint8_t lValue, uint8_t rValue);
|
||||||
|
void setLedOff();
|
||||||
|
void setLedOn(LED l);
|
||||||
|
void setLedBlink(LED l);
|
||||||
|
void setLedMode(LEDMode lm);
|
||||||
|
|
||||||
|
bool Xbox360Connected;// Variable used to indicate if the XBOX 360 controller is successfully connected
|
||||||
|
bool buttonChanged;//Indicate if a button has been changed
|
||||||
|
bool buttonPressed;//Indicate if a button has been pressed
|
||||||
|
bool buttonReleased;//Indicate if a button has been released
|
||||||
|
|
||||||
|
protected:
|
||||||
|
/* mandatory members */
|
||||||
|
USB *pUsb;
|
||||||
|
uint8_t bAddress; // device address
|
||||||
|
EpInfo epInfo[XBOX_MAX_ENDPOINTS]; //endpoint info structure
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool bPollEnable;
|
||||||
|
|
||||||
|
uint32_t ButtonState;
|
||||||
|
uint32_t OldButtonState;
|
||||||
|
|
||||||
|
uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
|
||||||
|
uint8_t writeBuf[EP_MAXPKTSIZE]; // General purpose buffer for output data
|
||||||
|
|
||||||
|
void readReport(); // read incoming data
|
||||||
|
void printReport(); // print incoming date - Uncomment for debugging
|
||||||
|
|
||||||
|
/* Private commands */
|
||||||
|
void XboxCommand(uint8_t* data, uint16_t nbytes);
|
||||||
|
};
|
||||||
|
#endif
|
|
@ -20,8 +20,10 @@ e-mail : support@circuitsathome.com
|
||||||
#ifndef _avrpins_h_
|
#ifndef _avrpins_h_
|
||||||
#define _avrpins_h_
|
#define _avrpins_h_
|
||||||
|
|
||||||
|
#if defined(__AVR_ATmega1280__) || (__AVR_ATmega2560__)
|
||||||
/* Uncomment the following if you have Arduino Mega ADK board with MAX3421e built-in */
|
/* Uncomment the following if you have Arduino Mega ADK board with MAX3421e built-in */
|
||||||
//#define BOARD_MEGA_ADK
|
//#define BOARD_MEGA_ADK
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Uncomment the following if you are using a Teensy 2.0 */
|
/* Uncomment the following if you are using a Teensy 2.0 */
|
||||||
//#define BOARD_TEENSY
|
//#define BOARD_TEENSY
|
||||||
|
|
109
examples/XBOX360USB/XBOX360USB.ino
Normal file
109
examples/XBOX360USB/XBOX360USB.ino
Normal file
|
@ -0,0 +1,109 @@
|
||||||
|
/*
|
||||||
|
Example sketch for the Xbox 360 USB library - developed by Kristian Lauszus
|
||||||
|
For more information visit my blog: http://blog.tkjelectronics.dk/ or
|
||||||
|
send me an e-mail: kristianl@tkjelectronics.com
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <XBOXUSB.h>
|
||||||
|
USB Usb;
|
||||||
|
XBOXUSB Xbox(&Usb);
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(115200);
|
||||||
|
|
||||||
|
if (Usb.Init() == -1) {
|
||||||
|
Serial.print(F("\r\nOSC did not start"));
|
||||||
|
while(1); //halt
|
||||||
|
}
|
||||||
|
Serial.print(F("\r\nXBOX USB Library Started"));
|
||||||
|
}
|
||||||
|
void loop() {
|
||||||
|
Usb.Task();
|
||||||
|
if(Xbox.Xbox360Connected) {
|
||||||
|
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) {
|
||||||
|
Serial.print(F("LeftHatX: "));
|
||||||
|
Serial.print(Xbox.getAnalogHat(LeftHatX));
|
||||||
|
Serial.print("\t");
|
||||||
|
}
|
||||||
|
if(Xbox.getAnalogHat(LeftHatY) > 7500 || Xbox.getAnalogHat(LeftHatY) < -7500) {
|
||||||
|
Serial.print(F("LeftHatY: "));
|
||||||
|
Serial.print(Xbox.getAnalogHat(LeftHatY));
|
||||||
|
Serial.print("\t");
|
||||||
|
}
|
||||||
|
if(Xbox.getAnalogHat(RightHatX) > 7500 || Xbox.getAnalogHat(RightHatX) < -7500) {
|
||||||
|
Serial.print(F("RightHatX: "));
|
||||||
|
Serial.print(Xbox.getAnalogHat(RightHatX));
|
||||||
|
Serial.print("\t");
|
||||||
|
}
|
||||||
|
if(Xbox.getAnalogHat(RightHatY) > 7500 || Xbox.getAnalogHat(RightHatY) < -7500) {
|
||||||
|
Serial.print(F("RightHatY: "));
|
||||||
|
Serial.print(Xbox.getAnalogHat(RightHatY));
|
||||||
|
}
|
||||||
|
Serial.println("");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(Xbox.buttonPressed) {
|
||||||
|
Serial.print(F("Xbox 360 Controller"));
|
||||||
|
if(Xbox.getButton(UP)) {
|
||||||
|
Xbox.setLedOn(LED1);
|
||||||
|
Serial.print(F(" - UP"));
|
||||||
|
}
|
||||||
|
if(Xbox.getButton(DOWN)) {
|
||||||
|
Xbox.setLedOn(LED4);
|
||||||
|
Serial.print(F(" - DOWN"));
|
||||||
|
}
|
||||||
|
if(Xbox.getButton(LEFT)) {
|
||||||
|
Xbox.setLedOn(LED3);
|
||||||
|
Serial.print(F(" - LEFT"));
|
||||||
|
}
|
||||||
|
if(Xbox.getButton(RIGHT)) {
|
||||||
|
Xbox.setLedOn(LED2);
|
||||||
|
Serial.print(F(" - RIGHT"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(Xbox.getButton(START)) {
|
||||||
|
Xbox.setLedMode(ALTERNATING);
|
||||||
|
Serial.print(F(" - START"));
|
||||||
|
}
|
||||||
|
if(Xbox.getButton(BACK)) {
|
||||||
|
Xbox.setLedBlink(ALL);
|
||||||
|
Serial.print(F(" - BACK"));
|
||||||
|
}
|
||||||
|
if(Xbox.getButton(L3))
|
||||||
|
Serial.print(F(" - L3"));
|
||||||
|
if(Xbox.getButton(R3))
|
||||||
|
Serial.print(F(" - R3"));
|
||||||
|
|
||||||
|
if(Xbox.getButton(L1))
|
||||||
|
Serial.print(F(" - L1"));
|
||||||
|
if(Xbox.getButton(R1))
|
||||||
|
Serial.print(F(" - R1"));
|
||||||
|
if(Xbox.getButton(XBOX)) {
|
||||||
|
Xbox.setLedMode(ROTATING);
|
||||||
|
Serial.print(F(" - XBOX"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(Xbox.getButton(A))
|
||||||
|
Serial.print(F(" - A"));
|
||||||
|
if(Xbox.getButton(B))
|
||||||
|
Serial.print(F(" - B"));
|
||||||
|
if(Xbox.getButton(X))
|
||||||
|
Serial.print(F(" - X"));
|
||||||
|
if(Xbox.getButton(Y))
|
||||||
|
Serial.print(F(" - Y"));
|
||||||
|
|
||||||
|
if(Xbox.getButton(L2)) {
|
||||||
|
Serial.print(F(" - L2:"));
|
||||||
|
Serial.print(Xbox.getButton(L2));
|
||||||
|
}
|
||||||
|
if(Xbox.getButton(R2)) {
|
||||||
|
Serial.print(F(" - R2:"));
|
||||||
|
Serial.print(Xbox.getButton(R2));
|
||||||
|
}
|
||||||
|
Xbox.setRumbleOn(Xbox.getButton(L2),Xbox.getButton(R2));
|
||||||
|
Serial.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delay(1);
|
||||||
|
}
|
38
keywords.txt
38
keywords.txt
|
@ -146,3 +146,41 @@ Bluetooth LITERAL1
|
||||||
|
|
||||||
RumbleHigh LITERAL1
|
RumbleHigh LITERAL1
|
||||||
RumbleLow LITERAL1
|
RumbleLow LITERAL1
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
# Syntax Coloring Map For Xbox 360 USB Library
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
# Datatypes (KEYWORD1)
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
XBOXUSB KEYWORD1
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
# Methods and Functions (KEYWORD2)
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
setLedBlink KEYWORD2
|
||||||
|
setLedMode KEYWORD2
|
||||||
|
Xbox360Connected KEYWORD2
|
||||||
|
|
||||||
|
####################################################
|
||||||
|
# Constants and enums (LITERAL1)
|
||||||
|
####################################################
|
||||||
|
|
||||||
|
ALL LITERAL1
|
||||||
|
|
||||||
|
ROTATING LITERAL1
|
||||||
|
FASTBLINK LITERAL1
|
||||||
|
SLOWBLINK LITERAL1
|
||||||
|
ALTERNATING LITERAL1
|
||||||
|
|
||||||
|
BACK LITERAL1
|
||||||
|
|
||||||
|
XBOX LITERAL1
|
||||||
|
|
||||||
|
A LITERAL1
|
||||||
|
B LITERAL1
|
||||||
|
X LITERAL1
|
||||||
|
Y LITERAL1
|
Loading…
Reference in a new issue