一関 Aチーム / ArduinoUsbHostShield
Committer:
kotakku
Date:
Sat Jan 18 15:06:35 2020 +0000
Revision:
0:b1ce54272580
1.0.0 first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kotakku 0:b1ce54272580 1 /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
kotakku 0:b1ce54272580 2
kotakku 0:b1ce54272580 3 This software may be distributed and modified under the terms of the GNU
kotakku 0:b1ce54272580 4 General Public License version 2 (GPL2) as published by the Free Software
kotakku 0:b1ce54272580 5 Foundation and appearing in the file GPL2.TXT included in the packaging of
kotakku 0:b1ce54272580 6 this file. Please note that GPL2 Section 2[b] requires that all works based
kotakku 0:b1ce54272580 7 on this software must also be made publicly available under the terms of
kotakku 0:b1ce54272580 8 the GPL2 ("Copyleft").
kotakku 0:b1ce54272580 9
kotakku 0:b1ce54272580 10 Contact information
kotakku 0:b1ce54272580 11 -------------------
kotakku 0:b1ce54272580 12
kotakku 0:b1ce54272580 13 Kristian Lauszus, TKJ Electronics
kotakku 0:b1ce54272580 14 Web : http://www.tkjelectronics.com
kotakku 0:b1ce54272580 15 e-mail : kristianl@tkjelectronics.com
kotakku 0:b1ce54272580 16 */
kotakku 0:b1ce54272580 17
kotakku 0:b1ce54272580 18 #include "XBOXUSB.h"
kotakku 0:b1ce54272580 19 // To enable serial debugging see "settings.h"
kotakku 0:b1ce54272580 20 //#define EXTRADEBUG // Uncomment to get even more debugging data
kotakku 0:b1ce54272580 21 //#define PRINTREPORT // Uncomment to print the report send by the Xbox 360 Controller
kotakku 0:b1ce54272580 22
kotakku 0:b1ce54272580 23 XBOXUSB::XBOXUSB(USB *p) :
kotakku 0:b1ce54272580 24 pUsb(p), // pointer to USB class instance - mandatory
kotakku 0:b1ce54272580 25 bAddress(0), // device address - mandatory
kotakku 0:b1ce54272580 26 bPollEnable(false) { // don't start polling before dongle is connected
kotakku 0:b1ce54272580 27 for(uint8_t i = 0; i < XBOX_MAX_ENDPOINTS; i++) {
kotakku 0:b1ce54272580 28 epInfo[i].epAddr = 0;
kotakku 0:b1ce54272580 29 epInfo[i].maxPktSize = (i) ? 0 : 8;
kotakku 0:b1ce54272580 30 epInfo[i].bmSndToggle = 0;
kotakku 0:b1ce54272580 31 epInfo[i].bmRcvToggle = 0;
kotakku 0:b1ce54272580 32 epInfo[i].bmNakPower = (i) ? USB_NAK_NOWAIT : USB_NAK_MAX_POWER;
kotakku 0:b1ce54272580 33 }
kotakku 0:b1ce54272580 34
kotakku 0:b1ce54272580 35 if(pUsb) // register in USB subsystem
kotakku 0:b1ce54272580 36 pUsb->RegisterDeviceClass(this); //set devConfig[] entry
kotakku 0:b1ce54272580 37 }
kotakku 0:b1ce54272580 38
kotakku 0:b1ce54272580 39 uint8_t XBOXUSB::Init(uint8_t parent, uint8_t port, bool lowspeed) {
kotakku 0:b1ce54272580 40 uint8_t buf[sizeof (USB_DEVICE_DESCRIPTOR)];
kotakku 0:b1ce54272580 41 USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf);
kotakku 0:b1ce54272580 42 uint8_t rcode;
kotakku 0:b1ce54272580 43 UsbDevice *p = NULL;
kotakku 0:b1ce54272580 44 EpInfo *oldep_ptr = NULL;
kotakku 0:b1ce54272580 45 uint16_t PID;
kotakku 0:b1ce54272580 46 uint16_t VID;
kotakku 0:b1ce54272580 47
kotakku 0:b1ce54272580 48 // get memory address of USB device address pool
kotakku 0:b1ce54272580 49 AddressPool &addrPool = pUsb->GetAddressPool();
kotakku 0:b1ce54272580 50 #ifdef EXTRADEBUG
kotakku 0:b1ce54272580 51 Notify(PSTR("\r\nXBOXUSB Init"), 0x80);
kotakku 0:b1ce54272580 52 #endif
kotakku 0:b1ce54272580 53 // check if address has already been assigned to an instance
kotakku 0:b1ce54272580 54 if(bAddress) {
kotakku 0:b1ce54272580 55 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 56 Notify(PSTR("\r\nAddress in use"), 0x80);
kotakku 0:b1ce54272580 57 #endif
kotakku 0:b1ce54272580 58 return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE;
kotakku 0:b1ce54272580 59 }
kotakku 0:b1ce54272580 60
kotakku 0:b1ce54272580 61 // Get pointer to pseudo device with address 0 assigned
kotakku 0:b1ce54272580 62 p = addrPool.GetUsbDevicePtr(0);
kotakku 0:b1ce54272580 63
kotakku 0:b1ce54272580 64 if(!p) {
kotakku 0:b1ce54272580 65 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 66 Notify(PSTR("\r\nAddress not found"), 0x80);
kotakku 0:b1ce54272580 67 #endif
kotakku 0:b1ce54272580 68 return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
kotakku 0:b1ce54272580 69 }
kotakku 0:b1ce54272580 70
kotakku 0:b1ce54272580 71 if(!p->epinfo) {
kotakku 0:b1ce54272580 72 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 73 Notify(PSTR("\r\nepinfo is null"), 0x80);
kotakku 0:b1ce54272580 74 #endif
kotakku 0:b1ce54272580 75 return USB_ERROR_EPINFO_IS_NULL;
kotakku 0:b1ce54272580 76 }
kotakku 0:b1ce54272580 77
kotakku 0:b1ce54272580 78 // Save old pointer to EP_RECORD of address 0
kotakku 0:b1ce54272580 79 oldep_ptr = p->epinfo;
kotakku 0:b1ce54272580 80
kotakku 0:b1ce54272580 81 // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
kotakku 0:b1ce54272580 82 p->epinfo = epInfo;
kotakku 0:b1ce54272580 83
kotakku 0:b1ce54272580 84 p->lowspeed = lowspeed;
kotakku 0:b1ce54272580 85
kotakku 0:b1ce54272580 86 // Get device descriptor
kotakku 0:b1ce54272580 87 rcode = pUsb->getDevDescr(0, 0, sizeof (USB_DEVICE_DESCRIPTOR), (uint8_t*)buf); // Get device descriptor - addr, ep, nbytes, data
kotakku 0:b1ce54272580 88 // Restore p->epinfo
kotakku 0:b1ce54272580 89 p->epinfo = oldep_ptr;
kotakku 0:b1ce54272580 90
kotakku 0:b1ce54272580 91 if(rcode)
kotakku 0:b1ce54272580 92 goto FailGetDevDescr;
kotakku 0:b1ce54272580 93
kotakku 0:b1ce54272580 94 VID = udd->idVendor;
kotakku 0:b1ce54272580 95 PID = udd->idProduct;
kotakku 0:b1ce54272580 96
kotakku 0:b1ce54272580 97 if(VID != XBOX_VID && VID != MADCATZ_VID && VID != JOYTECH_VID && VID != GAMESTOP_VID) // Check VID
kotakku 0:b1ce54272580 98 goto FailUnknownDevice;
kotakku 0:b1ce54272580 99 if(PID == XBOX_WIRELESS_PID) {
kotakku 0:b1ce54272580 100 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 101 Notify(PSTR("\r\nYou have plugged in a wireless Xbox 360 controller - it doesn't support USB communication"), 0x80);
kotakku 0:b1ce54272580 102 #endif
kotakku 0:b1ce54272580 103 goto FailUnknownDevice;
kotakku 0:b1ce54272580 104 } else if(PID == XBOX_WIRELESS_RECEIVER_PID || PID == XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID) {
kotakku 0:b1ce54272580 105 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 106 Notify(PSTR("\r\nThis library only supports Xbox 360 controllers via USB"), 0x80);
kotakku 0:b1ce54272580 107 #endif
kotakku 0:b1ce54272580 108 goto FailUnknownDevice;
kotakku 0:b1ce54272580 109 } else if(PID != XBOX_WIRED_PID && PID != MADCATZ_WIRED_PID && PID != GAMESTOP_WIRED_PID && PID != AFTERGLOW_WIRED_PID && PID != JOYTECH_WIRED_PID) // Check PID
kotakku 0:b1ce54272580 110 goto FailUnknownDevice;
kotakku 0:b1ce54272580 111
kotakku 0:b1ce54272580 112 // Allocate new address according to device class
kotakku 0:b1ce54272580 113 bAddress = addrPool.AllocAddress(parent, false, port);
kotakku 0:b1ce54272580 114
kotakku 0:b1ce54272580 115 if(!bAddress)
kotakku 0:b1ce54272580 116 return USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL;
kotakku 0:b1ce54272580 117
kotakku 0:b1ce54272580 118 // Extract Max Packet Size from device descriptor
kotakku 0:b1ce54272580 119 epInfo[0].maxPktSize = udd->bMaxPacketSize0;
kotakku 0:b1ce54272580 120
kotakku 0:b1ce54272580 121 // Assign new address to the device
kotakku 0:b1ce54272580 122 rcode = pUsb->setAddr(0, 0, bAddress);
kotakku 0:b1ce54272580 123 if(rcode) {
kotakku 0:b1ce54272580 124 p->lowspeed = false;
kotakku 0:b1ce54272580 125 addrPool.FreeAddress(bAddress);
kotakku 0:b1ce54272580 126 bAddress = 0;
kotakku 0:b1ce54272580 127 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 128 Notify(PSTR("\r\nsetAddr: "), 0x80);
kotakku 0:b1ce54272580 129 D_PrintHex<uint8_t > (rcode, 0x80);
kotakku 0:b1ce54272580 130 #endif
kotakku 0:b1ce54272580 131 return rcode;
kotakku 0:b1ce54272580 132 }
kotakku 0:b1ce54272580 133 #ifdef EXTRADEBUG
kotakku 0:b1ce54272580 134 Notify(PSTR("\r\nAddr: "), 0x80);
kotakku 0:b1ce54272580 135 D_PrintHex<uint8_t > (bAddress, 0x80);
kotakku 0:b1ce54272580 136 #endif
kotakku 0:b1ce54272580 137 //delay(300); // Spec says you should wait at least 200ms
kotakku 0:b1ce54272580 138
kotakku 0:b1ce54272580 139 p->lowspeed = false;
kotakku 0:b1ce54272580 140
kotakku 0:b1ce54272580 141 //get pointer to assigned address record
kotakku 0:b1ce54272580 142 p = addrPool.GetUsbDevicePtr(bAddress);
kotakku 0:b1ce54272580 143 if(!p)
kotakku 0:b1ce54272580 144 return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
kotakku 0:b1ce54272580 145
kotakku 0:b1ce54272580 146 p->lowspeed = lowspeed;
kotakku 0:b1ce54272580 147
kotakku 0:b1ce54272580 148 // Assign epInfo to epinfo pointer - only EP0 is known
kotakku 0:b1ce54272580 149 rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo);
kotakku 0:b1ce54272580 150 if(rcode)
kotakku 0:b1ce54272580 151 goto FailSetDevTblEntry;
kotakku 0:b1ce54272580 152
kotakku 0:b1ce54272580 153 /* The application will work in reduced host mode, so we can save program and data
kotakku 0:b1ce54272580 154 memory space. After verifying the VID we will use known values for the
kotakku 0:b1ce54272580 155 configuration values for device, interface, endpoints and HID for the XBOX360 Controllers */
kotakku 0:b1ce54272580 156
kotakku 0:b1ce54272580 157 /* Initialize data structures for endpoints of device */
kotakku 0:b1ce54272580 158 epInfo[ XBOX_INPUT_PIPE ].epAddr = 0x01; // XBOX 360 report endpoint
kotakku 0:b1ce54272580 159 epInfo[ XBOX_INPUT_PIPE ].epAttribs = USB_TRANSFER_TYPE_INTERRUPT;
kotakku 0:b1ce54272580 160 epInfo[ XBOX_INPUT_PIPE ].bmNakPower = USB_NAK_NOWAIT; // Only poll once for interrupt endpoints
kotakku 0:b1ce54272580 161 epInfo[ XBOX_INPUT_PIPE ].maxPktSize = EP_MAXPKTSIZE;
kotakku 0:b1ce54272580 162 epInfo[ XBOX_INPUT_PIPE ].bmSndToggle = 0;
kotakku 0:b1ce54272580 163 epInfo[ XBOX_INPUT_PIPE ].bmRcvToggle = 0;
kotakku 0:b1ce54272580 164 epInfo[ XBOX_OUTPUT_PIPE ].epAddr = 0x02; // XBOX 360 output endpoint
kotakku 0:b1ce54272580 165 epInfo[ XBOX_OUTPUT_PIPE ].epAttribs = USB_TRANSFER_TYPE_INTERRUPT;
kotakku 0:b1ce54272580 166 epInfo[ XBOX_OUTPUT_PIPE ].bmNakPower = USB_NAK_NOWAIT; // Only poll once for interrupt endpoints
kotakku 0:b1ce54272580 167 epInfo[ XBOX_OUTPUT_PIPE ].maxPktSize = EP_MAXPKTSIZE;
kotakku 0:b1ce54272580 168 epInfo[ XBOX_OUTPUT_PIPE ].bmSndToggle = 0;
kotakku 0:b1ce54272580 169 epInfo[ XBOX_OUTPUT_PIPE ].bmRcvToggle = 0;
kotakku 0:b1ce54272580 170
kotakku 0:b1ce54272580 171 rcode = pUsb->setEpInfoEntry(bAddress, 3, epInfo);
kotakku 0:b1ce54272580 172 if(rcode)
kotakku 0:b1ce54272580 173 goto FailSetDevTblEntry;
kotakku 0:b1ce54272580 174
kotakku 0:b1ce54272580 175 delay(200); // Give time for address change
kotakku 0:b1ce54272580 176
kotakku 0:b1ce54272580 177 rcode = pUsb->setConf(bAddress, epInfo[ XBOX_CONTROL_PIPE ].epAddr, 1);
kotakku 0:b1ce54272580 178 if(rcode)
kotakku 0:b1ce54272580 179 goto FailSetConfDescr;
kotakku 0:b1ce54272580 180
kotakku 0:b1ce54272580 181 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 182 Notify(PSTR("\r\nXbox 360 Controller Connected\r\n"), 0x80);
kotakku 0:b1ce54272580 183 #endif
kotakku 0:b1ce54272580 184 onInit();
kotakku 0:b1ce54272580 185 Xbox360Connected = true;
kotakku 0:b1ce54272580 186 bPollEnable = true;
kotakku 0:b1ce54272580 187 return 0; // Successful configuration
kotakku 0:b1ce54272580 188
kotakku 0:b1ce54272580 189 /* Diagnostic messages */
kotakku 0:b1ce54272580 190 FailGetDevDescr:
kotakku 0:b1ce54272580 191 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 192 NotifyFailGetDevDescr();
kotakku 0:b1ce54272580 193 goto Fail;
kotakku 0:b1ce54272580 194 #endif
kotakku 0:b1ce54272580 195
kotakku 0:b1ce54272580 196 FailSetDevTblEntry:
kotakku 0:b1ce54272580 197 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 198 NotifyFailSetDevTblEntry();
kotakku 0:b1ce54272580 199 goto Fail;
kotakku 0:b1ce54272580 200 #endif
kotakku 0:b1ce54272580 201
kotakku 0:b1ce54272580 202 FailSetConfDescr:
kotakku 0:b1ce54272580 203 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 204 NotifyFailSetConfDescr();
kotakku 0:b1ce54272580 205 #endif
kotakku 0:b1ce54272580 206 goto Fail;
kotakku 0:b1ce54272580 207
kotakku 0:b1ce54272580 208 FailUnknownDevice:
kotakku 0:b1ce54272580 209 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 210 NotifyFailUnknownDevice(VID, PID);
kotakku 0:b1ce54272580 211 #endif
kotakku 0:b1ce54272580 212 rcode = USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
kotakku 0:b1ce54272580 213
kotakku 0:b1ce54272580 214 Fail:
kotakku 0:b1ce54272580 215 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 216 Notify(PSTR("\r\nXbox 360 Init Failed, error code: "), 0x80);
kotakku 0:b1ce54272580 217 NotifyFail(rcode);
kotakku 0:b1ce54272580 218 #endif
kotakku 0:b1ce54272580 219 Release();
kotakku 0:b1ce54272580 220 return rcode;
kotakku 0:b1ce54272580 221 }
kotakku 0:b1ce54272580 222
kotakku 0:b1ce54272580 223 /* Performs a cleanup after failed Init() attempt */
kotakku 0:b1ce54272580 224 uint8_t XBOXUSB::Release() {
kotakku 0:b1ce54272580 225 Xbox360Connected = false;
kotakku 0:b1ce54272580 226 pUsb->GetAddressPool().FreeAddress(bAddress);
kotakku 0:b1ce54272580 227 bAddress = 0;
kotakku 0:b1ce54272580 228 bPollEnable = false;
kotakku 0:b1ce54272580 229 return 0;
kotakku 0:b1ce54272580 230 }
kotakku 0:b1ce54272580 231
kotakku 0:b1ce54272580 232 uint8_t XBOXUSB::Poll() {
kotakku 0:b1ce54272580 233 if(!bPollEnable)
kotakku 0:b1ce54272580 234 return 0;
kotakku 0:b1ce54272580 235 uint16_t BUFFER_SIZE = EP_MAXPKTSIZE;
kotakku 0:b1ce54272580 236 pUsb->inTransfer(bAddress, epInfo[ XBOX_INPUT_PIPE ].epAddr, &BUFFER_SIZE, readBuf); // input on endpoint 1
kotakku 0:b1ce54272580 237 readReport();
kotakku 0:b1ce54272580 238 #ifdef PRINTREPORT
kotakku 0:b1ce54272580 239 printReport(); // Uncomment "#define PRINTREPORT" to print the report send by the Xbox 360 Controller
kotakku 0:b1ce54272580 240 #endif
kotakku 0:b1ce54272580 241 return 0;
kotakku 0:b1ce54272580 242 }
kotakku 0:b1ce54272580 243
kotakku 0:b1ce54272580 244 void XBOXUSB::readReport() {
kotakku 0:b1ce54272580 245 if(readBuf == NULL)
kotakku 0:b1ce54272580 246 return;
kotakku 0:b1ce54272580 247 if(readBuf[0] != 0x00 || readBuf[1] != 0x14) { // Check if it's the correct report - the controller also sends different status reports
kotakku 0:b1ce54272580 248 return;
kotakku 0:b1ce54272580 249 }
kotakku 0:b1ce54272580 250
kotakku 0:b1ce54272580 251 ButtonState = (uint32_t)(readBuf[5] | ((uint16_t)readBuf[4] << 8) | ((uint32_t)readBuf[3] << 16) | ((uint32_t)readBuf[2] << 24));
kotakku 0:b1ce54272580 252
kotakku 0:b1ce54272580 253 hatValue[LeftHatX] = (int16_t)(((uint16_t)readBuf[7] << 8) | readBuf[6]);
kotakku 0:b1ce54272580 254 hatValue[LeftHatY] = (int16_t)(((uint16_t)readBuf[9] << 8) | readBuf[8]);
kotakku 0:b1ce54272580 255 hatValue[RightHatX] = (int16_t)(((uint16_t)readBuf[11] << 8) | readBuf[10]);
kotakku 0:b1ce54272580 256 hatValue[RightHatY] = (int16_t)(((uint16_t)readBuf[13] << 8) | readBuf[12]);
kotakku 0:b1ce54272580 257
kotakku 0:b1ce54272580 258 //Notify(PSTR("\r\nButtonState"), 0x80);
kotakku 0:b1ce54272580 259 //PrintHex<uint32_t>(ButtonState, 0x80);
kotakku 0:b1ce54272580 260
kotakku 0:b1ce54272580 261 if(ButtonState != OldButtonState) {
kotakku 0:b1ce54272580 262 ButtonClickState = (ButtonState >> 16) & ((~OldButtonState) >> 16); // Update click state variable, but don't include the two trigger buttons L2 and R2
kotakku 0:b1ce54272580 263 if(((uint8_t)OldButtonState) == 0 && ((uint8_t)ButtonState) != 0) // The L2 and R2 buttons are special as they are analog buttons
kotakku 0:b1ce54272580 264 R2Clicked = true;
kotakku 0:b1ce54272580 265 if((uint8_t)(OldButtonState >> 8) == 0 && (uint8_t)(ButtonState >> 8) != 0)
kotakku 0:b1ce54272580 266 L2Clicked = true;
kotakku 0:b1ce54272580 267 OldButtonState = ButtonState;
kotakku 0:b1ce54272580 268 }
kotakku 0:b1ce54272580 269 }
kotakku 0:b1ce54272580 270
kotakku 0:b1ce54272580 271 void XBOXUSB::printReport() { //Uncomment "#define PRINTREPORT" to print the report send by the Xbox 360 Controller
kotakku 0:b1ce54272580 272 #ifdef PRINTREPORT
kotakku 0:b1ce54272580 273 if(readBuf == NULL)
kotakku 0:b1ce54272580 274 return;
kotakku 0:b1ce54272580 275 for(uint8_t i = 0; i < XBOX_REPORT_BUFFER_SIZE; i++) {
kotakku 0:b1ce54272580 276 D_PrintHex<uint8_t > (readBuf[i], 0x80);
kotakku 0:b1ce54272580 277 Notify(PSTR(" "), 0x80);
kotakku 0:b1ce54272580 278 }
kotakku 0:b1ce54272580 279 Notify(PSTR("\r\n"), 0x80);
kotakku 0:b1ce54272580 280 #endif
kotakku 0:b1ce54272580 281 }
kotakku 0:b1ce54272580 282
kotakku 0:b1ce54272580 283 uint8_t XBOXUSB::getButtonPress(ButtonEnum b) {
kotakku 0:b1ce54272580 284 if(b == L2) // These are analog buttons
kotakku 0:b1ce54272580 285 return (uint8_t)(ButtonState >> 8);
kotakku 0:b1ce54272580 286 else if(b == R2)
kotakku 0:b1ce54272580 287 return (uint8_t)ButtonState;
kotakku 0:b1ce54272580 288 return (bool)(ButtonState & ((uint32_t)pgm_read_word(&XBOX_BUTTONS[(uint8_t)b]) << 16));
kotakku 0:b1ce54272580 289 }
kotakku 0:b1ce54272580 290
kotakku 0:b1ce54272580 291 bool XBOXUSB::getButtonClick(ButtonEnum b) {
kotakku 0:b1ce54272580 292 if(b == L2) {
kotakku 0:b1ce54272580 293 if(L2Clicked) {
kotakku 0:b1ce54272580 294 L2Clicked = false;
kotakku 0:b1ce54272580 295 return true;
kotakku 0:b1ce54272580 296 }
kotakku 0:b1ce54272580 297 return false;
kotakku 0:b1ce54272580 298 } else if(b == R2) {
kotakku 0:b1ce54272580 299 if(R2Clicked) {
kotakku 0:b1ce54272580 300 R2Clicked = false;
kotakku 0:b1ce54272580 301 return true;
kotakku 0:b1ce54272580 302 }
kotakku 0:b1ce54272580 303 return false;
kotakku 0:b1ce54272580 304 }
kotakku 0:b1ce54272580 305 uint16_t button = pgm_read_word(&XBOX_BUTTONS[(uint8_t)b]);
kotakku 0:b1ce54272580 306 bool click = (ButtonClickState & button);
kotakku 0:b1ce54272580 307 ButtonClickState &= ~button; // clear "click" event
kotakku 0:b1ce54272580 308 return click;
kotakku 0:b1ce54272580 309 }
kotakku 0:b1ce54272580 310
kotakku 0:b1ce54272580 311 int16_t XBOXUSB::getAnalogHat(AnalogHatEnum a) {
kotakku 0:b1ce54272580 312 return hatValue[a];
kotakku 0:b1ce54272580 313 }
kotakku 0:b1ce54272580 314
kotakku 0:b1ce54272580 315 /* Xbox Controller commands */
kotakku 0:b1ce54272580 316 void XBOXUSB::XboxCommand(uint8_t* data, uint16_t nbytes) {
kotakku 0:b1ce54272580 317 //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)
kotakku 0:b1ce54272580 318 pUsb->ctrlReq(bAddress, epInfo[XBOX_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0x00, 0x02, 0x00, nbytes, nbytes, data, NULL);
kotakku 0:b1ce54272580 319 }
kotakku 0:b1ce54272580 320
kotakku 0:b1ce54272580 321 void XBOXUSB::setLedRaw(uint8_t value) {
kotakku 0:b1ce54272580 322 writeBuf[0] = 0x01;
kotakku 0:b1ce54272580 323 writeBuf[1] = 0x03;
kotakku 0:b1ce54272580 324 writeBuf[2] = value;
kotakku 0:b1ce54272580 325
kotakku 0:b1ce54272580 326 XboxCommand(writeBuf, 3);
kotakku 0:b1ce54272580 327 }
kotakku 0:b1ce54272580 328
kotakku 0:b1ce54272580 329 void XBOXUSB::setLedOn(LEDEnum led) {
kotakku 0:b1ce54272580 330 if(led == OFF)
kotakku 0:b1ce54272580 331 setLedRaw(0);
kotakku 0:b1ce54272580 332 else if(led != ALL) // All LEDs can't be on a the same time
kotakku 0:b1ce54272580 333 setLedRaw(pgm_read_byte(&XBOX_LEDS[(uint8_t)led]) + 4);
kotakku 0:b1ce54272580 334 }
kotakku 0:b1ce54272580 335
kotakku 0:b1ce54272580 336 void XBOXUSB::setLedBlink(LEDEnum led) {
kotakku 0:b1ce54272580 337 setLedRaw(pgm_read_byte(&XBOX_LEDS[(uint8_t)led]));
kotakku 0:b1ce54272580 338 }
kotakku 0:b1ce54272580 339
kotakku 0:b1ce54272580 340 void XBOXUSB::setLedMode(LEDModeEnum ledMode) { // This function is used to do some special LED stuff the controller supports
kotakku 0:b1ce54272580 341 setLedRaw((uint8_t)ledMode);
kotakku 0:b1ce54272580 342 }
kotakku 0:b1ce54272580 343
kotakku 0:b1ce54272580 344 void XBOXUSB::setRumbleOn(uint8_t lValue, uint8_t rValue) {
kotakku 0:b1ce54272580 345 writeBuf[0] = 0x00;
kotakku 0:b1ce54272580 346 writeBuf[1] = 0x08;
kotakku 0:b1ce54272580 347 writeBuf[2] = 0x00;
kotakku 0:b1ce54272580 348 writeBuf[3] = lValue; // big weight
kotakku 0:b1ce54272580 349 writeBuf[4] = rValue; // small weight
kotakku 0:b1ce54272580 350 writeBuf[5] = 0x00;
kotakku 0:b1ce54272580 351 writeBuf[6] = 0x00;
kotakku 0:b1ce54272580 352 writeBuf[7] = 0x00;
kotakku 0:b1ce54272580 353
kotakku 0:b1ce54272580 354 XboxCommand(writeBuf, 8);
kotakku 0:b1ce54272580 355 }
kotakku 0:b1ce54272580 356
kotakku 0:b1ce54272580 357 void XBOXUSB::onInit() {
kotakku 0:b1ce54272580 358 if(pFuncOnInit)
kotakku 0:b1ce54272580 359 pFuncOnInit(); // Call the user function
kotakku 0:b1ce54272580 360 else
kotakku 0:b1ce54272580 361 setLedOn(static_cast<LEDEnum>(CONTROLLER_LED1));
kotakku 0:b1ce54272580 362 }
kotakku 0:b1ce54272580 363