Library to use Arduino USB host shield on mbed

Dependents:   USBHOST_PS5

ArduinoのUSB Host Shield 2.0をmbedで使えるようにしたライブラリです。
大体のコードがArduinoからそのまま移植可能です。

Arduino UNOやMega用のホストシールド以外にもミニサイズのホストシールドでも使用可能です https://os.mbed.com/media/uploads/kotakku/dffgfddswa.png

シールドについて

3.3VのI/O用にシールドの改造が必要になりますがネット上に記事がたくさんあるのでそちらを参考にしてください

接続例

https://os.mbed.com/media/uploads/kotakku/esgsvfvhjrekldkcjxvb.png

使い方

Arduinoのコードと違うのはUSBのインスタンスの宣言部分のみです。
ピンを自分で指定できるようにしたので使いやすくなりました。

仕様

  • Arduinoのmillis関数、micros関数の移植のために内部でTimerクラスを使用しています。

main.cpp

#include "mbed.h"
#include <PS3BT.h>
#include <usbhub.h>

Serial pc(USBTX, USBRX, 115200);

//Nucleo f303k8用
USB Usb(A6, A5, A4, A3, A2); // mosi, miso, sclk, ssel, intr
BTD Btd(&Usb);
PS3BT PS3(&Btd);

int main()
{
    bool printAngle = false;

    if (Usb.Init() == -1)
    {
        pc.printf("\r\nOSC did not start");
        while (1); // Halt
    }
    pc.printf("\r\nPS3 USB Library Started");

    while (1)
    {
        Usb.Task();
        
        if (PS3.PS3Connected || PS3.PS3NavigationConnected) {
            if (PS3.getAnalogHat(LeftHatX) > 137 || PS3.getAnalogHat(LeftHatX) < 117 || PS3.getAnalogHat(LeftHatY) > 137 || PS3.getAnalogHat(LeftHatY) < 117 || PS3.getAnalogHat(RightHatX) > 137 || PS3.getAnalogHat(RightHatX) < 117 || PS3.getAnalogHat(RightHatY) > 137 || PS3.getAnalogHat(RightHatY) < 117)
            {
                pc.printf("\r\nLeftHatX: %d", PS3.getAnalogHat(LeftHatX));
                pc.printf("\tLeftHatY: %d", PS3.getAnalogHat(LeftHatY));
                if (PS3.PS3Connected)
                { // The Navigation controller only have one joystick
                    pc.printf("\tRightHatX: %d", PS3.getAnalogHat(RightHatX));
                    pc.printf("\tRightHatY: %d", PS3.getAnalogHat(RightHatY));
                }
            }
            // Analog button values can be read from almost all buttons
            if (PS3.getAnalogButton(L2) || PS3.getAnalogButton(R2))
            {
                pc.printf("\r\nL2: %d", PS3.getAnalogButton(L2));
                if (!PS3.PS3NavigationConnected)
                {
                    pc.printf("\tR2: %d", PS3.getAnalogButton(R2));
                }
            }
            if (PS3.getButtonClick(PS))
            {
                PS3.disconnect();
                pc.printf("\r\nPS");
            }
    
            if (PS3.getButtonClick(TRIANGLE))
                pc.printf("\r\nTriangle");
            if (PS3.getButtonClick(CIRCLE))
                pc.printf("\r\nCircle");
            if (PS3.getButtonClick(CROSS))
                pc.printf("\r\nCross");
            if (PS3.getButtonClick(SQUARE))
                pc.printf("\r\nSquare");
    
            if (PS3.getButtonClick(UP))
            {
                pc.printf("\r\nUp");
                PS3.setLedOff();
                PS3.setLedOn(CONTROLLER_LED4);
            }
            if (PS3.getButtonClick(RIGHT))
            {
                pc.printf("\r\nRight");
                PS3.setLedOff();
                PS3.setLedOn(CONTROLLER_LED1);
            }
            if (PS3.getButtonClick(DOWN))
            {
                pc.printf("\r\nDown");
                PS3.setLedOff();
                PS3.setLedOn(CONTROLLER_LED2);
            }
            if (PS3.getButtonClick(LEFT))
            {
                pc.printf("\r\nLeft");
                PS3.setLedOff();
                PS3.setLedOn(CONTROLLER_LED3);
            }
    
            if (PS3.getButtonClick(L1))
                pc.printf("\r\nL1");
            if (PS3.getButtonClick(L3))
                pc.printf("\r\nL3");
            if (PS3.getButtonClick(R1))
                pc.printf("\r\nR1");
            if (PS3.getButtonClick(R3))
                pc.printf("\r\nR3");
    
            if (PS3.getButtonClick(SELECT))
            {
                pc.printf("\r\nSelect - ");
                PS3.printStatusString();
            }
            if (PS3.getButtonClick(START))
            {
                pc.printf("\r\nStart");
                printAngle = !printAngle;
            }
            if (printAngle)
            {
                pc.printf("\r\nPitch: %.3lf", PS3.getAngle(Pitch));
                pc.printf("\tRoll: %.3lf", PS3.getAngle(Roll));
            }
        }
        else
        {
            pc.printf("not connect\n");
        }
    }
}
Committer:
robo_ichinoseki_a
Date:
Sat May 02 05:56:48 2020 +0000
Revision:
1:da31140f2a1c
Parent:
0:b1ce54272580
update

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 "stdio.h"
kotakku 0:b1ce54272580 19 #include "PS3USB.h"
kotakku 0:b1ce54272580 20 // To enable serial debugging see "settings.h"
kotakku 0:b1ce54272580 21 //#define EXTRADEBUG // Uncomment to get even more debugging data
kotakku 0:b1ce54272580 22 //#define PRINTREPORT // Uncomment to print the report send by the PS3 Controllers
kotakku 0:b1ce54272580 23
kotakku 0:b1ce54272580 24 PS3USB::PS3USB(USB *p, uint8_t btadr5, uint8_t btadr4, uint8_t btadr3, uint8_t btadr2, uint8_t btadr1, uint8_t btadr0) :
kotakku 0:b1ce54272580 25 pUsb(p), // pointer to USB class instance - mandatory
kotakku 0:b1ce54272580 26 bAddress(0), // device address - mandatory
kotakku 0:b1ce54272580 27 bPollEnable(false) // don't start polling before dongle is connected
kotakku 0:b1ce54272580 28 {
kotakku 0:b1ce54272580 29 for(uint8_t i = 0; i < PS3_MAX_ENDPOINTS; i++) {
kotakku 0:b1ce54272580 30 epInfo[i].epAddr = 0;
kotakku 0:b1ce54272580 31 epInfo[i].maxPktSize = (i) ? 0 : 8;
kotakku 0:b1ce54272580 32 epInfo[i].bmSndToggle = 0;
kotakku 0:b1ce54272580 33 epInfo[i].bmRcvToggle = 0;
kotakku 0:b1ce54272580 34 epInfo[i].bmNakPower = (i) ? USB_NAK_NOWAIT : USB_NAK_MAX_POWER;
kotakku 0:b1ce54272580 35 }
kotakku 0:b1ce54272580 36
kotakku 0:b1ce54272580 37 if(pUsb) // register in USB subsystem
kotakku 0:b1ce54272580 38 pUsb->RegisterDeviceClass(this); //set devConfig[] entry
kotakku 0:b1ce54272580 39
kotakku 0:b1ce54272580 40 my_bdaddr[5] = btadr5; // Change to your dongle's Bluetooth address instead
kotakku 0:b1ce54272580 41 my_bdaddr[4] = btadr4;
kotakku 0:b1ce54272580 42 my_bdaddr[3] = btadr3;
kotakku 0:b1ce54272580 43 my_bdaddr[2] = btadr2;
kotakku 0:b1ce54272580 44 my_bdaddr[1] = btadr1;
kotakku 0:b1ce54272580 45 my_bdaddr[0] = btadr0;
kotakku 0:b1ce54272580 46
robo_ichinoseki_a 1:da31140f2a1c 47 //printf("Constractor\n");
kotakku 0:b1ce54272580 48 }
kotakku 0:b1ce54272580 49
kotakku 0:b1ce54272580 50 uint8_t PS3USB::Init(uint8_t parent, uint8_t port, bool lowspeed) {
kotakku 0:b1ce54272580 51 uint8_t buf[sizeof (USB_DEVICE_DESCRIPTOR)];
kotakku 0:b1ce54272580 52 USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf);
kotakku 0:b1ce54272580 53 uint8_t rcode;
kotakku 0:b1ce54272580 54 UsbDevice *p = NULL;
kotakku 0:b1ce54272580 55 EpInfo *oldep_ptr = NULL;
kotakku 0:b1ce54272580 56 uint16_t PID;
kotakku 0:b1ce54272580 57 uint16_t VID;
kotakku 0:b1ce54272580 58
kotakku 0:b1ce54272580 59 // get memory address of USB device address pool
kotakku 0:b1ce54272580 60 AddressPool &addrPool = pUsb->GetAddressPool();
kotakku 0:b1ce54272580 61 printf("\r\nPS3USB Init");
kotakku 0:b1ce54272580 62 #ifdef EXTRADEBUG
kotakku 0:b1ce54272580 63 Notify(PSTR("\r\nPS3USB Init"), 0x80);
kotakku 0:b1ce54272580 64 std:printf("\r\nPS3USB Init");
kotakku 0:b1ce54272580 65 #endif
kotakku 0:b1ce54272580 66 // check if address has already been assigned to an instance
kotakku 0:b1ce54272580 67 if(bAddress) {
kotakku 0:b1ce54272580 68 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 69 Notify(PSTR("\r\nAddress in use"), 0x80);
kotakku 0:b1ce54272580 70 #endif
kotakku 0:b1ce54272580 71 return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE;
kotakku 0:b1ce54272580 72 }
kotakku 0:b1ce54272580 73
kotakku 0:b1ce54272580 74 // Get pointer to pseudo device with address 0 assigned
kotakku 0:b1ce54272580 75 p = addrPool.GetUsbDevicePtr(0);
kotakku 0:b1ce54272580 76
kotakku 0:b1ce54272580 77 if(!p) {
kotakku 0:b1ce54272580 78 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 79 Notify(PSTR("\r\nAddress not found"), 0x80);
kotakku 0:b1ce54272580 80 #endif
kotakku 0:b1ce54272580 81 return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
kotakku 0:b1ce54272580 82 }
kotakku 0:b1ce54272580 83
kotakku 0:b1ce54272580 84 if(!p->epinfo) {
kotakku 0:b1ce54272580 85 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 86 Notify(PSTR("\r\nepinfo is null"), 0x80);
kotakku 0:b1ce54272580 87 #endif
kotakku 0:b1ce54272580 88 return USB_ERROR_EPINFO_IS_NULL;
kotakku 0:b1ce54272580 89 }
kotakku 0:b1ce54272580 90
kotakku 0:b1ce54272580 91 // Save old pointer to EP_RECORD of address 0
kotakku 0:b1ce54272580 92 oldep_ptr = p->epinfo;
kotakku 0:b1ce54272580 93
kotakku 0:b1ce54272580 94 // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
kotakku 0:b1ce54272580 95 p->epinfo = epInfo;
kotakku 0:b1ce54272580 96
kotakku 0:b1ce54272580 97 p->lowspeed = lowspeed;
kotakku 0:b1ce54272580 98
kotakku 0:b1ce54272580 99 // Get device descriptor
kotakku 0:b1ce54272580 100 rcode = pUsb->getDevDescr(0, 0, sizeof (USB_DEVICE_DESCRIPTOR), (uint8_t*)buf); // Get device descriptor - addr, ep, nbytes, data
kotakku 0:b1ce54272580 101 // Restore p->epinfo
kotakku 0:b1ce54272580 102 p->epinfo = oldep_ptr;
kotakku 0:b1ce54272580 103
kotakku 0:b1ce54272580 104 if(rcode)
kotakku 0:b1ce54272580 105 goto FailGetDevDescr;
kotakku 0:b1ce54272580 106
kotakku 0:b1ce54272580 107 VID = udd->idVendor;
kotakku 0:b1ce54272580 108 PID = udd->idProduct;
kotakku 0:b1ce54272580 109
kotakku 0:b1ce54272580 110 if(VID != PS3_VID || (PID != PS3_PID && PID != PS3NAVIGATION_PID && PID != PS3MOVE_PID))
kotakku 0:b1ce54272580 111 goto FailUnknownDevice;
kotakku 0:b1ce54272580 112
kotakku 0:b1ce54272580 113 // Allocate new address according to device class
kotakku 0:b1ce54272580 114 bAddress = addrPool.AllocAddress(parent, false, port);
kotakku 0:b1ce54272580 115
kotakku 0:b1ce54272580 116 if(!bAddress)
kotakku 0:b1ce54272580 117 return USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL;
kotakku 0:b1ce54272580 118
kotakku 0:b1ce54272580 119 // Extract Max Packet Size from device descriptor
kotakku 0:b1ce54272580 120 epInfo[0].maxPktSize = udd->bMaxPacketSize0;
kotakku 0:b1ce54272580 121
kotakku 0:b1ce54272580 122 // Assign new address to the device
kotakku 0:b1ce54272580 123 rcode = pUsb->setAddr(0, 0, bAddress);
kotakku 0:b1ce54272580 124 if(rcode) {
kotakku 0:b1ce54272580 125 p->lowspeed = false;
kotakku 0:b1ce54272580 126 addrPool.FreeAddress(bAddress);
kotakku 0:b1ce54272580 127 bAddress = 0;
kotakku 0:b1ce54272580 128 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 129 Notify(PSTR("\r\nsetAddr: "), 0x80);
kotakku 0:b1ce54272580 130 D_PrintHex<uint8_t > (rcode, 0x80);
kotakku 0:b1ce54272580 131 #endif
kotakku 0:b1ce54272580 132 return rcode;
kotakku 0:b1ce54272580 133 }
kotakku 0:b1ce54272580 134 #ifdef EXTRADEBUG
kotakku 0:b1ce54272580 135 Notify(PSTR("\r\nAddr: "), 0x80);
kotakku 0:b1ce54272580 136 D_PrintHex<uint8_t > (bAddress, 0x80);
kotakku 0:b1ce54272580 137 #endif
kotakku 0:b1ce54272580 138 //delay(300); // Spec says you should wait at least 200ms
kotakku 0:b1ce54272580 139
kotakku 0:b1ce54272580 140 p->lowspeed = false;
kotakku 0:b1ce54272580 141
kotakku 0:b1ce54272580 142 //get pointer to assigned address record
kotakku 0:b1ce54272580 143 p = addrPool.GetUsbDevicePtr(bAddress);
kotakku 0:b1ce54272580 144 if(!p)
kotakku 0:b1ce54272580 145 return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
kotakku 0:b1ce54272580 146
kotakku 0:b1ce54272580 147 p->lowspeed = lowspeed;
kotakku 0:b1ce54272580 148
kotakku 0:b1ce54272580 149 // Assign epInfo to epinfo pointer - only EP0 is known
kotakku 0:b1ce54272580 150 rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo);
kotakku 0:b1ce54272580 151 if(rcode)
kotakku 0:b1ce54272580 152 goto FailSetDevTblEntry;
kotakku 0:b1ce54272580 153
kotakku 0:b1ce54272580 154
kotakku 0:b1ce54272580 155 /* The application will work in reduced host mode, so we can save program and data
kotakku 0:b1ce54272580 156 memory space. After verifying the PID and VID we will use known values for the
kotakku 0:b1ce54272580 157 configuration values for device, interface, endpoints and HID for the PS3 Controllers */
kotakku 0:b1ce54272580 158
kotakku 0:b1ce54272580 159 /* Initialize data structures for endpoints of device */
kotakku 0:b1ce54272580 160 epInfo[ PS3_OUTPUT_PIPE ].epAddr = 0x02; // PS3 output endpoint
kotakku 0:b1ce54272580 161 epInfo[ PS3_OUTPUT_PIPE ].epAttribs = USB_TRANSFER_TYPE_INTERRUPT;
kotakku 0:b1ce54272580 162 epInfo[ PS3_OUTPUT_PIPE ].bmNakPower = USB_NAK_NOWAIT; // Only poll once for interrupt endpoints
kotakku 0:b1ce54272580 163 epInfo[ PS3_OUTPUT_PIPE ].maxPktSize = EP_MAXPKTSIZE;
kotakku 0:b1ce54272580 164 epInfo[ PS3_OUTPUT_PIPE ].bmSndToggle = 0;
kotakku 0:b1ce54272580 165 epInfo[ PS3_OUTPUT_PIPE ].bmRcvToggle = 0;
kotakku 0:b1ce54272580 166 epInfo[ PS3_INPUT_PIPE ].epAddr = 0x01; // PS3 report endpoint
kotakku 0:b1ce54272580 167 epInfo[ PS3_INPUT_PIPE ].epAttribs = USB_TRANSFER_TYPE_INTERRUPT;
kotakku 0:b1ce54272580 168 epInfo[ PS3_INPUT_PIPE ].bmNakPower = USB_NAK_NOWAIT; // Only poll once for interrupt endpoints
kotakku 0:b1ce54272580 169 epInfo[ PS3_INPUT_PIPE ].maxPktSize = EP_MAXPKTSIZE;
kotakku 0:b1ce54272580 170 epInfo[ PS3_INPUT_PIPE ].bmSndToggle = 0;
kotakku 0:b1ce54272580 171 epInfo[ PS3_INPUT_PIPE ].bmRcvToggle = 0;
kotakku 0:b1ce54272580 172
kotakku 0:b1ce54272580 173 rcode = pUsb->setEpInfoEntry(bAddress, 3, epInfo);
kotakku 0:b1ce54272580 174 if(rcode)
kotakku 0:b1ce54272580 175 goto FailSetDevTblEntry;
kotakku 0:b1ce54272580 176
kotakku 0:b1ce54272580 177 delay(200); //Give time for address change
kotakku 0:b1ce54272580 178
kotakku 0:b1ce54272580 179 rcode = pUsb->setConf(bAddress, epInfo[ PS3_CONTROL_PIPE ].epAddr, 1);
kotakku 0:b1ce54272580 180 if(rcode)
kotakku 0:b1ce54272580 181 goto FailSetConfDescr;
kotakku 0:b1ce54272580 182
kotakku 0:b1ce54272580 183 if(PID == PS3_PID || PID == PS3NAVIGATION_PID) {
kotakku 0:b1ce54272580 184 if(PID == PS3_PID) {
kotakku 0:b1ce54272580 185 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 186 Notify(PSTR("\r\nDualshock 3 Controller Connected"), 0x80);
kotakku 0:b1ce54272580 187 #endif
kotakku 0:b1ce54272580 188 PS3Connected = true;
kotakku 0:b1ce54272580 189 } else { // must be a navigation controller
kotakku 0:b1ce54272580 190 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 191 Notify(PSTR("\r\nNavigation Controller Connected"), 0x80);
kotakku 0:b1ce54272580 192 #endif
kotakku 0:b1ce54272580 193 PS3NavigationConnected = true;
kotakku 0:b1ce54272580 194 }
kotakku 0:b1ce54272580 195 enable_sixaxis(); // The PS3 controller needs a special command before it starts sending data
kotakku 0:b1ce54272580 196
kotakku 0:b1ce54272580 197 // Needed for PS3 Dualshock and Navigation commands to work
kotakku 0:b1ce54272580 198 for(uint8_t i = 0; i < PS3_REPORT_BUFFER_SIZE; i++)
kotakku 0:b1ce54272580 199 writeBuf[i] = pgm_read_byte(&PS3_REPORT_BUFFER[i]);
kotakku 0:b1ce54272580 200
kotakku 0:b1ce54272580 201 for(uint8_t i = 6; i < 10; i++)
kotakku 0:b1ce54272580 202 readBuf[i] = 0x7F; // Set the analog joystick values to center position
kotakku 0:b1ce54272580 203 } else { // must be a Motion controller
kotakku 0:b1ce54272580 204 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 205 Notify(PSTR("\r\nMotion Controller Connected"), 0x80);
kotakku 0:b1ce54272580 206 #endif
kotakku 0:b1ce54272580 207 PS3MoveConnected = true;
kotakku 0:b1ce54272580 208 writeBuf[0] = 0x02; // Set report ID, this is needed for Move commands to work
kotakku 0:b1ce54272580 209 }
kotakku 0:b1ce54272580 210 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) {
kotakku 0:b1ce54272580 211 if(PS3MoveConnected)
kotakku 0:b1ce54272580 212 setMoveBdaddr(my_bdaddr); // Set internal Bluetooth address
kotakku 0:b1ce54272580 213 else
kotakku 0:b1ce54272580 214 setBdaddr(my_bdaddr); // Set internal Bluetooth address
kotakku 0:b1ce54272580 215
kotakku 0:b1ce54272580 216 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 217 Notify(PSTR("\r\nBluetooth Address was set to: "), 0x80);
kotakku 0:b1ce54272580 218 for(int8_t i = 5; i > 0; i--) {
kotakku 0:b1ce54272580 219 D_PrintHex<uint8_t > (my_bdaddr[i], 0x80);
kotakku 0:b1ce54272580 220 Notify(PSTR(":"), 0x80);
kotakku 0:b1ce54272580 221 }
kotakku 0:b1ce54272580 222 D_PrintHex<uint8_t > (my_bdaddr[0], 0x80);
kotakku 0:b1ce54272580 223 #endif
kotakku 0:b1ce54272580 224 }
kotakku 0:b1ce54272580 225 onInit();
kotakku 0:b1ce54272580 226
kotakku 0:b1ce54272580 227 bPollEnable = true;
kotakku 0:b1ce54272580 228 Notify(PSTR("\r\n"), 0x80);
kotakku 0:b1ce54272580 229 timer = (uint32_t)millis();
kotakku 0:b1ce54272580 230 return 0; // Successful configuration
kotakku 0:b1ce54272580 231
kotakku 0:b1ce54272580 232 /* Diagnostic messages */
kotakku 0:b1ce54272580 233 FailGetDevDescr:
kotakku 0:b1ce54272580 234 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 235 NotifyFailGetDevDescr();
kotakku 0:b1ce54272580 236 goto Fail;
kotakku 0:b1ce54272580 237 #endif
kotakku 0:b1ce54272580 238
kotakku 0:b1ce54272580 239 FailSetDevTblEntry:
kotakku 0:b1ce54272580 240 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 241 NotifyFailSetDevTblEntry();
kotakku 0:b1ce54272580 242 goto Fail;
kotakku 0:b1ce54272580 243 #endif
kotakku 0:b1ce54272580 244
kotakku 0:b1ce54272580 245 FailSetConfDescr:
kotakku 0:b1ce54272580 246 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 247 NotifyFailSetConfDescr();
kotakku 0:b1ce54272580 248 #endif
kotakku 0:b1ce54272580 249 goto Fail;
kotakku 0:b1ce54272580 250
kotakku 0:b1ce54272580 251 FailUnknownDevice:
kotakku 0:b1ce54272580 252 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 253 NotifyFailUnknownDevice(VID, PID);
kotakku 0:b1ce54272580 254 #endif
kotakku 0:b1ce54272580 255 rcode = USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
kotakku 0:b1ce54272580 256
kotakku 0:b1ce54272580 257 Fail:
kotakku 0:b1ce54272580 258 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 259 Notify(PSTR("\r\nPS3 Init Failed, error code: "), 0x80);
kotakku 0:b1ce54272580 260 NotifyFail(rcode);
kotakku 0:b1ce54272580 261 #endif
kotakku 0:b1ce54272580 262 Release();
kotakku 0:b1ce54272580 263 return rcode;
kotakku 0:b1ce54272580 264 }
kotakku 0:b1ce54272580 265
kotakku 0:b1ce54272580 266 /* Performs a cleanup after failed Init() attempt */
kotakku 0:b1ce54272580 267 uint8_t PS3USB::Release() {
kotakku 0:b1ce54272580 268 PS3Connected = false;
kotakku 0:b1ce54272580 269 PS3MoveConnected = false;
kotakku 0:b1ce54272580 270 PS3NavigationConnected = false;
kotakku 0:b1ce54272580 271 pUsb->GetAddressPool().FreeAddress(bAddress);
kotakku 0:b1ce54272580 272 bAddress = 0;
kotakku 0:b1ce54272580 273 bPollEnable = false;
kotakku 0:b1ce54272580 274 return 0;
kotakku 0:b1ce54272580 275 }
kotakku 0:b1ce54272580 276
kotakku 0:b1ce54272580 277 uint8_t PS3USB::Poll() {
kotakku 0:b1ce54272580 278 if(!bPollEnable)
kotakku 0:b1ce54272580 279 return 0;
kotakku 0:b1ce54272580 280
kotakku 0:b1ce54272580 281 if(PS3Connected || PS3NavigationConnected) {
kotakku 0:b1ce54272580 282 uint16_t BUFFER_SIZE = EP_MAXPKTSIZE;
kotakku 0:b1ce54272580 283 pUsb->inTransfer(bAddress, epInfo[ PS3_INPUT_PIPE ].epAddr, &BUFFER_SIZE, readBuf); // input on endpoint 1
kotakku 0:b1ce54272580 284 if((int32_t)((uint32_t)millis() - timer) > 100) { // Loop 100ms before processing data
kotakku 0:b1ce54272580 285 readReport();
kotakku 0:b1ce54272580 286 #ifdef PRINTREPORT
kotakku 0:b1ce54272580 287 printReport(); // Uncomment "#define PRINTREPORT" to print the report send by the PS3 Controllers
kotakku 0:b1ce54272580 288 #endif
kotakku 0:b1ce54272580 289 }
kotakku 0:b1ce54272580 290 } 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
kotakku 0:b1ce54272580 291 if((int32_t)((uint32_t)millis() - timer) > 4000) { // Send at least every 4th second
kotakku 0:b1ce54272580 292 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
kotakku 0:b1ce54272580 293 timer = (uint32_t)millis();
kotakku 0:b1ce54272580 294 }
kotakku 0:b1ce54272580 295 }
kotakku 0:b1ce54272580 296 return 0;
kotakku 0:b1ce54272580 297 }
kotakku 0:b1ce54272580 298
kotakku 0:b1ce54272580 299 void PS3USB::readReport() {
kotakku 0:b1ce54272580 300 ButtonState = (uint32_t)(readBuf[2] | ((uint16_t)readBuf[3] << 8) | ((uint32_t)readBuf[4] << 16));
kotakku 0:b1ce54272580 301
kotakku 0:b1ce54272580 302 //Notify(PSTR("\r\nButtonState", 0x80);
kotakku 0:b1ce54272580 303 //PrintHex<uint32_t>(ButtonState, 0x80);
kotakku 0:b1ce54272580 304
kotakku 0:b1ce54272580 305 if(ButtonState != OldButtonState) {
kotakku 0:b1ce54272580 306 ButtonClickState = ButtonState & ~OldButtonState; // Update click state variable
kotakku 0:b1ce54272580 307 OldButtonState = ButtonState;
kotakku 0:b1ce54272580 308 }
kotakku 0:b1ce54272580 309 }
kotakku 0:b1ce54272580 310
kotakku 0:b1ce54272580 311 void PS3USB::printReport() { // Uncomment "#define PRINTREPORT" to print the report send by the PS3 Controllers
kotakku 0:b1ce54272580 312 #ifdef PRINTREPORT
kotakku 0:b1ce54272580 313 for(uint8_t i = 0; i < PS3_REPORT_BUFFER_SIZE; i++) {
kotakku 0:b1ce54272580 314 D_PrintHex<uint8_t > (readBuf[i], 0x80);
kotakku 0:b1ce54272580 315 Notify(PSTR(" "), 0x80);
kotakku 0:b1ce54272580 316 }
kotakku 0:b1ce54272580 317 Notify(PSTR("\r\n"), 0x80);
kotakku 0:b1ce54272580 318 #endif
kotakku 0:b1ce54272580 319 }
kotakku 0:b1ce54272580 320
kotakku 0:b1ce54272580 321 bool PS3USB::getButtonPress(ButtonEnum b) {
kotakku 0:b1ce54272580 322 return (ButtonState & pgm_read_dword(&PS3_BUTTONS[(uint8_t)b]));
kotakku 0:b1ce54272580 323 }
kotakku 0:b1ce54272580 324
kotakku 0:b1ce54272580 325 bool PS3USB::getButtonClick(ButtonEnum b) {
kotakku 0:b1ce54272580 326 uint32_t button = pgm_read_dword(&PS3_BUTTONS[(uint8_t)b]);
kotakku 0:b1ce54272580 327 bool click = (ButtonClickState & button);
kotakku 0:b1ce54272580 328 ButtonClickState &= ~button; // Clear "click" event
kotakku 0:b1ce54272580 329 return click;
kotakku 0:b1ce54272580 330 }
kotakku 0:b1ce54272580 331
kotakku 0:b1ce54272580 332 uint8_t PS3USB::getAnalogButton(ButtonEnum a) {
kotakku 0:b1ce54272580 333 return (uint8_t)(readBuf[(pgm_read_byte(&PS3_ANALOG_BUTTONS[(uint8_t)a])) - 9]);
kotakku 0:b1ce54272580 334 }
kotakku 0:b1ce54272580 335
kotakku 0:b1ce54272580 336 uint8_t PS3USB::getAnalogHat(AnalogHatEnum a) {
kotakku 0:b1ce54272580 337 return (uint8_t)(readBuf[((uint8_t)a + 6)]);
kotakku 0:b1ce54272580 338 }
kotakku 0:b1ce54272580 339
kotakku 0:b1ce54272580 340 uint16_t PS3USB::getSensor(SensorEnum a) {
kotakku 0:b1ce54272580 341 return ((readBuf[((uint16_t)a) - 9] << 8) | readBuf[((uint16_t)a + 1) - 9]);
kotakku 0:b1ce54272580 342 }
kotakku 0:b1ce54272580 343
kotakku 0:b1ce54272580 344 float PS3USB::getAngle(AngleEnum a) {
kotakku 0:b1ce54272580 345 if(PS3Connected) {
kotakku 0:b1ce54272580 346 float accXval, accYval, accZval;
kotakku 0:b1ce54272580 347
kotakku 0:b1ce54272580 348 // Data for the Kionix KXPC4 used in the DualShock 3
kotakku 0:b1ce54272580 349 const float zeroG = 511.5f; // 1.65/3.3*1023 (1,65V)
kotakku 0:b1ce54272580 350 accXval = -((float)getSensor(aX) - zeroG);
kotakku 0:b1ce54272580 351 accYval = -((float)getSensor(aY) - zeroG);
kotakku 0:b1ce54272580 352 accZval = -((float)getSensor(aZ) - zeroG);
kotakku 0:b1ce54272580 353
kotakku 0:b1ce54272580 354 // Convert to 360 degrees resolution
kotakku 0:b1ce54272580 355 // atan2 outputs the value of -π to π (radians)
kotakku 0:b1ce54272580 356 // We are then converting it to 0 to 2π and then to degrees
kotakku 0:b1ce54272580 357 if(a == Pitch)
kotakku 0:b1ce54272580 358 return (atan2f(accYval, accZval) + PI) * RAD_TO_DEG;
kotakku 0:b1ce54272580 359 else
kotakku 0:b1ce54272580 360 return (atan2f(accXval, accZval) + PI) * RAD_TO_DEG;
kotakku 0:b1ce54272580 361 } else
kotakku 0:b1ce54272580 362 return 0;
kotakku 0:b1ce54272580 363 }
kotakku 0:b1ce54272580 364
kotakku 0:b1ce54272580 365 bool PS3USB::getStatus(StatusEnum c) {
kotakku 0:b1ce54272580 366 return (readBuf[((uint16_t)c >> 8) - 9] == ((uint8_t)c & 0xff));
kotakku 0:b1ce54272580 367 }
kotakku 0:b1ce54272580 368
kotakku 0:b1ce54272580 369 void PS3USB::printStatusString() {
kotakku 0:b1ce54272580 370 char statusOutput[102]; // Max string length plus null character
kotakku 0:b1ce54272580 371 if(PS3Connected || PS3NavigationConnected) {
kotakku 0:b1ce54272580 372 strcpy_P(statusOutput, PSTR("\r\nConnectionStatus: "));
kotakku 0:b1ce54272580 373
kotakku 0:b1ce54272580 374 if(getStatus(Plugged)) strcat_P(statusOutput, PSTR("Plugged"));
kotakku 0:b1ce54272580 375 else if(getStatus(Unplugged)) strcat_P(statusOutput, PSTR("Unplugged"));
kotakku 0:b1ce54272580 376 else strcat_P(statusOutput, PSTR("Error"));
kotakku 0:b1ce54272580 377
kotakku 0:b1ce54272580 378 strcat_P(statusOutput, PSTR(" - PowerRating: "));
kotakku 0:b1ce54272580 379
kotakku 0:b1ce54272580 380 if(getStatus(Charging)) strcat_P(statusOutput, PSTR("Charging"));
kotakku 0:b1ce54272580 381 else if(getStatus(NotCharging)) strcat_P(statusOutput, PSTR("Not Charging"));
kotakku 0:b1ce54272580 382 else if(getStatus(Shutdown)) strcat_P(statusOutput, PSTR("Shutdown"));
kotakku 0:b1ce54272580 383 else if(getStatus(Dying)) strcat_P(statusOutput, PSTR("Dying"));
kotakku 0:b1ce54272580 384 else if(getStatus(Low)) strcat_P(statusOutput, PSTR("Low"));
kotakku 0:b1ce54272580 385 else if(getStatus(High)) strcat_P(statusOutput, PSTR("High"));
kotakku 0:b1ce54272580 386 else if(getStatus(Full)) strcat_P(statusOutput, PSTR("Full"));
kotakku 0:b1ce54272580 387 else strcat_P(statusOutput, PSTR("Error"));
kotakku 0:b1ce54272580 388
kotakku 0:b1ce54272580 389 strcat_P(statusOutput, PSTR(" - WirelessStatus: "));
kotakku 0:b1ce54272580 390
kotakku 0:b1ce54272580 391 if(getStatus(CableRumble)) strcat_P(statusOutput, PSTR("Cable - Rumble is on"));
kotakku 0:b1ce54272580 392 else if(getStatus(Cable)) strcat_P(statusOutput, PSTR("Cable - Rumble is off"));
kotakku 0:b1ce54272580 393 else if(getStatus(BluetoothRumble)) strcat_P(statusOutput, PSTR("Bluetooth - Rumble is on"));
kotakku 0:b1ce54272580 394 else if(getStatus(Bluetooth)) strcat_P(statusOutput, PSTR("Bluetooth - Rumble is off"));
kotakku 0:b1ce54272580 395 else strcat_P(statusOutput, PSTR("Error"));
kotakku 0:b1ce54272580 396 } else
kotakku 0:b1ce54272580 397 strcpy_P(statusOutput, PSTR("\r\nError"));
kotakku 0:b1ce54272580 398
kotakku 0:b1ce54272580 399 //////USB_HOST_SERIAL.write(statusOutput);
kotakku 0:b1ce54272580 400 }
kotakku 0:b1ce54272580 401
kotakku 0:b1ce54272580 402 /* Playstation Sixaxis Dualshock and Navigation Controller commands */
kotakku 0:b1ce54272580 403 void PS3USB::PS3_Command(uint8_t *data, uint16_t nbytes) {
kotakku 0:b1ce54272580 404 // bmRequest = Host to device (0x00) | Class (0x20) | Interface (0x01) = 0x21, bRequest = Set Report (0x09), Report ID (0x01), Report Type (Output 0x02), interface (0x00), datalength, datalength, data)
kotakku 0:b1ce54272580 405 pUsb->ctrlReq(bAddress, epInfo[PS3_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0x01, 0x02, 0x00, nbytes, nbytes, data, NULL);
kotakku 0:b1ce54272580 406 }
kotakku 0:b1ce54272580 407
kotakku 0:b1ce54272580 408 void PS3USB::setAllOff() {
kotakku 0:b1ce54272580 409 for(uint8_t i = 0; i < PS3_REPORT_BUFFER_SIZE; i++)
kotakku 0:b1ce54272580 410 writeBuf[i] = pgm_read_byte(&PS3_REPORT_BUFFER[i]); // Reset buffer
kotakku 0:b1ce54272580 411
kotakku 0:b1ce54272580 412 PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE);
kotakku 0:b1ce54272580 413 }
kotakku 0:b1ce54272580 414
kotakku 0:b1ce54272580 415 void PS3USB::setRumbleOff() {
kotakku 0:b1ce54272580 416 uint8_t rumbleBuf[EP_MAXPKTSIZE];
kotakku 0:b1ce54272580 417 memcpy(rumbleBuf, writeBuf, EP_MAXPKTSIZE);
kotakku 0:b1ce54272580 418 rumbleBuf[1] = 0x00;
kotakku 0:b1ce54272580 419 rumbleBuf[2] = 0x00; // Low mode off
kotakku 0:b1ce54272580 420 rumbleBuf[3] = 0x00;
kotakku 0:b1ce54272580 421 rumbleBuf[4] = 0x00; // High mode off
kotakku 0:b1ce54272580 422 PS3_Command(rumbleBuf, PS3_REPORT_BUFFER_SIZE);
kotakku 0:b1ce54272580 423 }
kotakku 0:b1ce54272580 424
kotakku 0:b1ce54272580 425 void PS3USB::setRumbleOn(RumbleEnum mode) {
kotakku 0:b1ce54272580 426 if((mode & 0x30) > 0x00) {
kotakku 0:b1ce54272580 427 uint8_t power[2] = {0xff, 0x00}; // Defaults to RumbleLow
kotakku 0:b1ce54272580 428 if(mode == RumbleHigh) {
kotakku 0:b1ce54272580 429 power[0] = 0x00;
kotakku 0:b1ce54272580 430 power[1] = 0xff;
kotakku 0:b1ce54272580 431 }
kotakku 0:b1ce54272580 432 setRumbleOn(0xfe, power[0], 0xfe, power[1]);
kotakku 0:b1ce54272580 433 }
kotakku 0:b1ce54272580 434 }
kotakku 0:b1ce54272580 435
kotakku 0:b1ce54272580 436 void PS3USB::setRumbleOn(uint8_t rightDuration, uint8_t rightPower, uint8_t leftDuration, uint8_t leftPower) {
kotakku 0:b1ce54272580 437 uint8_t rumbleBuf[EP_MAXPKTSIZE];
kotakku 0:b1ce54272580 438 memcpy(rumbleBuf, writeBuf, EP_MAXPKTSIZE);
kotakku 0:b1ce54272580 439 rumbleBuf[1] = rightDuration;
kotakku 0:b1ce54272580 440 rumbleBuf[2] = rightPower;
kotakku 0:b1ce54272580 441 rumbleBuf[3] = leftDuration;
kotakku 0:b1ce54272580 442 rumbleBuf[4] = leftPower;
kotakku 0:b1ce54272580 443 PS3_Command(rumbleBuf, PS3_REPORT_BUFFER_SIZE);
kotakku 0:b1ce54272580 444 }
kotakku 0:b1ce54272580 445
kotakku 0:b1ce54272580 446 void PS3USB::setLedRaw(uint8_t value) {
kotakku 0:b1ce54272580 447 writeBuf[9] = value << 1;
kotakku 0:b1ce54272580 448 PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE);
kotakku 0:b1ce54272580 449 }
kotakku 0:b1ce54272580 450
kotakku 0:b1ce54272580 451 void PS3USB::setLedOff(LEDEnum a) {
kotakku 0:b1ce54272580 452 writeBuf[9] &= ~((uint8_t)((pgm_read_byte(&PS3_LEDS[(uint8_t)a]) & 0x0f) << 1));
kotakku 0:b1ce54272580 453 PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE);
kotakku 0:b1ce54272580 454 }
kotakku 0:b1ce54272580 455
kotakku 0:b1ce54272580 456 void PS3USB::setLedOn(LEDEnum a) {
kotakku 0:b1ce54272580 457 if(a == OFF)
kotakku 0:b1ce54272580 458 setLedRaw(0);
kotakku 0:b1ce54272580 459 else {
kotakku 0:b1ce54272580 460 writeBuf[9] |= (uint8_t)((pgm_read_byte(&PS3_LEDS[(uint8_t)a]) & 0x0f) << 1);
kotakku 0:b1ce54272580 461 PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE);
kotakku 0:b1ce54272580 462 }
kotakku 0:b1ce54272580 463 }
kotakku 0:b1ce54272580 464
kotakku 0:b1ce54272580 465 void PS3USB::setLedToggle(LEDEnum a) {
kotakku 0:b1ce54272580 466 writeBuf[9] ^= (uint8_t)((pgm_read_byte(&PS3_LEDS[(uint8_t)a]) & 0x0f) << 1);
kotakku 0:b1ce54272580 467 PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE);
kotakku 0:b1ce54272580 468 }
kotakku 0:b1ce54272580 469
kotakku 0:b1ce54272580 470 void PS3USB::setBdaddr(uint8_t *bdaddr) {
kotakku 0:b1ce54272580 471 /* Set the internal Bluetooth address */
kotakku 0:b1ce54272580 472 uint8_t buf[8];
kotakku 0:b1ce54272580 473 buf[0] = 0x01;
kotakku 0:b1ce54272580 474 buf[1] = 0x00;
kotakku 0:b1ce54272580 475
kotakku 0:b1ce54272580 476 for(uint8_t i = 0; i < 6; i++)
kotakku 0:b1ce54272580 477 buf[i + 2] = bdaddr[5 - i]; // Copy into buffer, has to be written reversed, so it is MSB first
kotakku 0:b1ce54272580 478
kotakku 0:b1ce54272580 479 // 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
kotakku 0:b1ce54272580 480 pUsb->ctrlReq(bAddress, epInfo[PS3_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0xF5, 0x03, 0x00, 8, 8, buf, NULL);
kotakku 0:b1ce54272580 481 }
kotakku 0:b1ce54272580 482
kotakku 0:b1ce54272580 483 void PS3USB::getBdaddr(uint8_t *bdaddr) {
kotakku 0:b1ce54272580 484 uint8_t buf[8];
kotakku 0:b1ce54272580 485
kotakku 0:b1ce54272580 486 // 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
kotakku 0:b1ce54272580 487 pUsb->ctrlReq(bAddress, epInfo[PS3_CONTROL_PIPE].epAddr, bmREQ_HID_IN, HID_REQUEST_GET_REPORT, 0xF5, 0x03, 0x00, 8, 8, buf, NULL);
kotakku 0:b1ce54272580 488
kotakku 0:b1ce54272580 489 for(uint8_t i = 0; i < 6; i++)
kotakku 0:b1ce54272580 490 bdaddr[5 - i] = buf[i + 2]; // Copy into buffer reversed, so it is LSB first
kotakku 0:b1ce54272580 491 }
kotakku 0:b1ce54272580 492
kotakku 0:b1ce54272580 493 void PS3USB::enable_sixaxis() { // Command used to enable the Dualshock 3 and Navigation controller to send data via USB
kotakku 0:b1ce54272580 494 uint8_t cmd_buf[4];
kotakku 0:b1ce54272580 495 cmd_buf[0] = 0x42; // Special PS3 Controller enable commands
kotakku 0:b1ce54272580 496 cmd_buf[1] = 0x0c;
kotakku 0:b1ce54272580 497 cmd_buf[2] = 0x00;
kotakku 0:b1ce54272580 498 cmd_buf[3] = 0x00;
kotakku 0:b1ce54272580 499
kotakku 0:b1ce54272580 500 // bmRequest = Host to device (0x00) | Class (0x20) | Interface (0x01) = 0x21, bRequest = Set Report (0x09), Report ID (0xF4), Report Type (Feature 0x03), interface (0x00), datalength, datalength, data)
kotakku 0:b1ce54272580 501 pUsb->ctrlReq(bAddress, epInfo[PS3_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0xF4, 0x03, 0x00, 4, 4, cmd_buf, NULL);
kotakku 0:b1ce54272580 502 }
kotakku 0:b1ce54272580 503
kotakku 0:b1ce54272580 504 /* Playstation Move Controller commands */
kotakku 0:b1ce54272580 505 void PS3USB::Move_Command(uint8_t *data, uint16_t nbytes) {
kotakku 0:b1ce54272580 506 pUsb->outTransfer(bAddress, epInfo[ PS3_OUTPUT_PIPE ].epAddr, nbytes, data);
kotakku 0:b1ce54272580 507 }
kotakku 0:b1ce54272580 508
kotakku 0:b1ce54272580 509 void PS3USB::moveSetBulb(uint8_t r, uint8_t g, uint8_t b) { // Use this to set the Color using RGB values
kotakku 0:b1ce54272580 510 // Set the Bulb's values into the write buffer
kotakku 0:b1ce54272580 511 writeBuf[2] = r;
kotakku 0:b1ce54272580 512 writeBuf[3] = g;
kotakku 0:b1ce54272580 513 writeBuf[4] = b;
kotakku 0:b1ce54272580 514
kotakku 0:b1ce54272580 515 Move_Command(writeBuf, MOVE_REPORT_BUFFER_SIZE);
kotakku 0:b1ce54272580 516 }
kotakku 0:b1ce54272580 517
kotakku 0:b1ce54272580 518 void PS3USB::moveSetBulb(ColorsEnum color) { // Use this to set the Color using the predefined colors in "enums.h"
kotakku 0:b1ce54272580 519 moveSetBulb((uint8_t)(color >> 16), (uint8_t)(color >> 8), (uint8_t)(color));
kotakku 0:b1ce54272580 520 }
kotakku 0:b1ce54272580 521
kotakku 0:b1ce54272580 522 void PS3USB::moveSetRumble(uint8_t rumble) {
kotakku 0:b1ce54272580 523 #ifdef DEBUG_USB_HOST
kotakku 0:b1ce54272580 524 if(rumble < 64 && rumble != 0) // The rumble value has to at least 64, or approximately 25% (64/255*100)
kotakku 0:b1ce54272580 525 Notify(PSTR("\r\nThe rumble value has to at least 64, or approximately 25%"), 0x80);
kotakku 0:b1ce54272580 526 #endif
kotakku 0:b1ce54272580 527 writeBuf[6] = rumble; // Set the rumble value into the write buffer
kotakku 0:b1ce54272580 528
kotakku 0:b1ce54272580 529 Move_Command(writeBuf, MOVE_REPORT_BUFFER_SIZE);
kotakku 0:b1ce54272580 530 }
kotakku 0:b1ce54272580 531
kotakku 0:b1ce54272580 532 void PS3USB::setMoveBdaddr(uint8_t *bdaddr) {
kotakku 0:b1ce54272580 533 /* Set the internal Bluetooth address */
kotakku 0:b1ce54272580 534 uint8_t buf[11];
kotakku 0:b1ce54272580 535 buf[0] = 0x05;
kotakku 0:b1ce54272580 536 buf[7] = 0x10;
kotakku 0:b1ce54272580 537 buf[8] = 0x01;
kotakku 0:b1ce54272580 538 buf[9] = 0x02;
kotakku 0:b1ce54272580 539 buf[10] = 0x12;
kotakku 0:b1ce54272580 540
kotakku 0:b1ce54272580 541 for(uint8_t i = 0; i < 6; i++)
kotakku 0:b1ce54272580 542 buf[i + 1] = bdaddr[i];
kotakku 0:b1ce54272580 543
kotakku 0:b1ce54272580 544 // 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
kotakku 0:b1ce54272580 545 pUsb->ctrlReq(bAddress, epInfo[PS3_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0x05, 0x03, 0x00, 11, 11, buf, NULL);
kotakku 0:b1ce54272580 546 }
kotakku 0:b1ce54272580 547
kotakku 0:b1ce54272580 548 void PS3USB::getMoveBdaddr(uint8_t *bdaddr) {
kotakku 0:b1ce54272580 549 uint8_t buf[16];
kotakku 0:b1ce54272580 550
kotakku 0:b1ce54272580 551 // 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
kotakku 0:b1ce54272580 552 pUsb->ctrlReq(bAddress, epInfo[PS3_CONTROL_PIPE].epAddr, bmREQ_HID_IN, HID_REQUEST_GET_REPORT, 0x04, 0x03, 0x00, 16, 16, buf, NULL);
kotakku 0:b1ce54272580 553
kotakku 0:b1ce54272580 554 for(uint8_t i = 0; i < 6; i++)
kotakku 0:b1ce54272580 555 bdaddr[i] = buf[10 + i];
kotakku 0:b1ce54272580 556 }
kotakku 0:b1ce54272580 557
kotakku 0:b1ce54272580 558 void PS3USB::getMoveCalibration(uint8_t *data) {
kotakku 0:b1ce54272580 559 uint8_t buf[49];
kotakku 0:b1ce54272580 560
kotakku 0:b1ce54272580 561 for(uint8_t i = 0; i < 3; i++) {
kotakku 0:b1ce54272580 562 // 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
kotakku 0:b1ce54272580 563 pUsb->ctrlReq(bAddress, epInfo[PS3_CONTROL_PIPE].epAddr, bmREQ_HID_IN, HID_REQUEST_GET_REPORT, 0x10, 0x03, 0x00, 49, 49, buf, NULL);
kotakku 0:b1ce54272580 564
kotakku 0:b1ce54272580 565 for(uint8_t j = 0; j < 49; j++)
kotakku 0:b1ce54272580 566 data[49 * i + j] = buf[j];
kotakku 0:b1ce54272580 567 }
kotakku 0:b1ce54272580 568 }
kotakku 0:b1ce54272580 569
kotakku 0:b1ce54272580 570 void PS3USB::onInit() {
kotakku 0:b1ce54272580 571 if(pFuncOnInit)
kotakku 0:b1ce54272580 572 pFuncOnInit(); // Call the user function
kotakku 0:b1ce54272580 573 else {
kotakku 0:b1ce54272580 574 if(PS3MoveConnected)
kotakku 0:b1ce54272580 575 moveSetBulb(Red);
kotakku 0:b1ce54272580 576 else // Dualshock 3 or Navigation controller
kotakku 0:b1ce54272580 577 setLedOn(static_cast<LEDEnum>(CONTROLLER_LED1));
kotakku 0:b1ce54272580 578 }
kotakku 0:b1ce54272580 579 }
kotakku 0:b1ce54272580 580