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 Copyright (C) 2015 guruthree
kotakku 0:b1ce54272580 3
kotakku 0:b1ce54272580 4 This software may be distributed and modified under the terms of the GNU
kotakku 0:b1ce54272580 5 General Public License version 2 (GPL2) as published by the Free Software
kotakku 0:b1ce54272580 6 Foundation and appearing in the file GPL2.TXT included in the packaging of
kotakku 0:b1ce54272580 7 this file. Please note that GPL2 Section 2[b] requires that all works based
kotakku 0:b1ce54272580 8 on this software must also be made publicly available under the terms of
kotakku 0:b1ce54272580 9 the GPL2 ("Copyleft").
kotakku 0:b1ce54272580 10
kotakku 0:b1ce54272580 11 Contact information
kotakku 0:b1ce54272580 12 -------------------
kotakku 0:b1ce54272580 13
kotakku 0:b1ce54272580 14 Kristian Lauszus, TKJ Electronics
kotakku 0:b1ce54272580 15 Web : http://www.tkjelectronics.com
kotakku 0:b1ce54272580 16 e-mail : kristianl@tkjelectronics.com
kotakku 0:b1ce54272580 17
kotakku 0:b1ce54272580 18 guruthree
kotakku 0:b1ce54272580 19 Web : https://github.com/guruthree/
kotakku 0:b1ce54272580 20 */
kotakku 0:b1ce54272580 21
kotakku 0:b1ce54272580 22
kotakku 0:b1ce54272580 23 #ifndef _xboxone_h_
kotakku 0:b1ce54272580 24 #define _xboxone_h_
kotakku 0:b1ce54272580 25
kotakku 0:b1ce54272580 26 #include "Usb.h"
kotakku 0:b1ce54272580 27 #include "xboxEnums.h"
kotakku 0:b1ce54272580 28
kotakku 0:b1ce54272580 29 /* Xbox One data taken from descriptors */
kotakku 0:b1ce54272580 30 #define XBOX_ONE_EP_MAXPKTSIZE 64 // Max size for data via USB
kotakku 0:b1ce54272580 31
kotakku 0:b1ce54272580 32 /* Names we give to the 3 XboxONE pipes */
kotakku 0:b1ce54272580 33 #define XBOX_ONE_CONTROL_PIPE 0
kotakku 0:b1ce54272580 34 #define XBOX_ONE_OUTPUT_PIPE 1
kotakku 0:b1ce54272580 35 #define XBOX_ONE_INPUT_PIPE 2
kotakku 0:b1ce54272580 36
kotakku 0:b1ce54272580 37 #define XBOX_ONE_MAX_ENDPOINTS 3
kotakku 0:b1ce54272580 38
kotakku 0:b1ce54272580 39 // PID and VID of the different versions of the controller - see: https://github.com/torvalds/linux/blob/master/drivers/input/joystick/xpad.c
kotakku 0:b1ce54272580 40
kotakku 0:b1ce54272580 41 // Official controllers
kotakku 0:b1ce54272580 42 #define XBOX_VID1 0x045E // Microsoft Corporation
kotakku 0:b1ce54272580 43 #define XBOX_ONE_PID1 0x02D1 // Microsoft X-Box One pad
kotakku 0:b1ce54272580 44 #define XBOX_ONE_PID2 0x02DD // Microsoft X-Box One pad (Firmware 2015)
kotakku 0:b1ce54272580 45 #define XBOX_ONE_PID3 0x02E3 // Microsoft X-Box One Elite pad
kotakku 0:b1ce54272580 46 #define XBOX_ONE_PID4 0x02EA // Microsoft X-Box One S pad
kotakku 0:b1ce54272580 47 #define XBOX_ONE_PID13 0x0B0A // Microsoft X-Box One Adaptive Controller
kotakku 0:b1ce54272580 48
kotakku 0:b1ce54272580 49 // Unofficial controllers
kotakku 0:b1ce54272580 50 #define XBOX_VID2 0x0738 // Mad Catz
kotakku 0:b1ce54272580 51 #define XBOX_VID3 0x0E6F // Afterglow
kotakku 0:b1ce54272580 52 #define XBOX_VID4 0x0F0D // HORIPAD ONE
kotakku 0:b1ce54272580 53 #define XBOX_VID5 0x1532 // Razer
kotakku 0:b1ce54272580 54 #define XBOX_VID6 0x24C6 // PowerA
kotakku 0:b1ce54272580 55
kotakku 0:b1ce54272580 56 #define XBOX_ONE_PID5 0x4A01 // Mad Catz FightStick TE 2 - might have different mapping for triggers?
kotakku 0:b1ce54272580 57 #define XBOX_ONE_PID6 0x0139 // Afterglow Prismatic Wired Controller
kotakku 0:b1ce54272580 58 #define XBOX_ONE_PID7 0x0146 // Rock Candy Wired Controller for Xbox One
kotakku 0:b1ce54272580 59 #define XBOX_ONE_PID8 0x0067 // HORIPAD ONE
kotakku 0:b1ce54272580 60 #define XBOX_ONE_PID9 0x0A03 // Razer Wildcat
kotakku 0:b1ce54272580 61 #define XBOX_ONE_PID10 0x541A // PowerA Xbox One Mini Wired Controller
kotakku 0:b1ce54272580 62 #define XBOX_ONE_PID11 0x542A // Xbox ONE spectra
kotakku 0:b1ce54272580 63 #define XBOX_ONE_PID12 0x543A // PowerA Xbox One wired controller
kotakku 0:b1ce54272580 64
kotakku 0:b1ce54272580 65 /** This class implements support for a Xbox ONE controller connected via USB. */
kotakku 0:b1ce54272580 66 class XBOXONE : public USBDeviceConfig, public UsbConfigXtracter {
kotakku 0:b1ce54272580 67 public:
kotakku 0:b1ce54272580 68 /**
kotakku 0:b1ce54272580 69 * Constructor for the XBOXONE class.
kotakku 0:b1ce54272580 70 * @param pUsb Pointer to USB class instance.
kotakku 0:b1ce54272580 71 */
kotakku 0:b1ce54272580 72 XBOXONE(USB *pUsb);
kotakku 0:b1ce54272580 73
kotakku 0:b1ce54272580 74 /** @name USBDeviceConfig implementation */
kotakku 0:b1ce54272580 75 /**
kotakku 0:b1ce54272580 76 * Initialize the Xbox Controller.
kotakku 0:b1ce54272580 77 * @param parent Hub number.
kotakku 0:b1ce54272580 78 * @param port Port number on the hub.
kotakku 0:b1ce54272580 79 * @param lowspeed Speed of the device.
kotakku 0:b1ce54272580 80 * @return 0 on success.
kotakku 0:b1ce54272580 81 */
kotakku 0:b1ce54272580 82 virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
kotakku 0:b1ce54272580 83 /**
kotakku 0:b1ce54272580 84 * Release the USB device.
kotakku 0:b1ce54272580 85 * @return 0 on success.
kotakku 0:b1ce54272580 86 */
kotakku 0:b1ce54272580 87 virtual uint8_t Release();
kotakku 0:b1ce54272580 88 /**
kotakku 0:b1ce54272580 89 * Poll the USB Input endpoins and run the state machines.
kotakku 0:b1ce54272580 90 * @return 0 on success.
kotakku 0:b1ce54272580 91 */
kotakku 0:b1ce54272580 92 virtual uint8_t Poll();
kotakku 0:b1ce54272580 93
kotakku 0:b1ce54272580 94 /**
kotakku 0:b1ce54272580 95 * Get the device address.
kotakku 0:b1ce54272580 96 * @return The device address.
kotakku 0:b1ce54272580 97 */
kotakku 0:b1ce54272580 98 virtual uint8_t GetAddress() {
kotakku 0:b1ce54272580 99 return bAddress;
kotakku 0:b1ce54272580 100 };
kotakku 0:b1ce54272580 101
kotakku 0:b1ce54272580 102 /**
kotakku 0:b1ce54272580 103 * Used to check if the controller has been initialized.
kotakku 0:b1ce54272580 104 * @return True if it's ready.
kotakku 0:b1ce54272580 105 */
kotakku 0:b1ce54272580 106 virtual bool isReady() {
kotakku 0:b1ce54272580 107 return bPollEnable;
kotakku 0:b1ce54272580 108 };
kotakku 0:b1ce54272580 109
kotakku 0:b1ce54272580 110 /**
kotakku 0:b1ce54272580 111 * Read the poll interval taken from the endpoint descriptors.
kotakku 0:b1ce54272580 112 * @return The poll interval in ms.
kotakku 0:b1ce54272580 113 */
kotakku 0:b1ce54272580 114 uint8_t readPollInterval() {
kotakku 0:b1ce54272580 115 return pollInterval;
kotakku 0:b1ce54272580 116 };
kotakku 0:b1ce54272580 117
kotakku 0:b1ce54272580 118 /**
kotakku 0:b1ce54272580 119 * Used by the USB core to check what this driver support.
kotakku 0:b1ce54272580 120 * @param vid The device's VID.
kotakku 0:b1ce54272580 121 * @param pid The device's PID.
kotakku 0:b1ce54272580 122 * @return Returns true if the device's VID and PID matches this driver.
kotakku 0:b1ce54272580 123 */
kotakku 0:b1ce54272580 124 virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
kotakku 0:b1ce54272580 125 return ((vid == XBOX_VID1 || vid == XBOX_VID2 || vid == XBOX_VID3 || vid == XBOX_VID4 || vid == XBOX_VID5 || vid == XBOX_VID6) &&
kotakku 0:b1ce54272580 126 (pid == XBOX_ONE_PID1 || pid == XBOX_ONE_PID2 || pid == XBOX_ONE_PID3 || pid == XBOX_ONE_PID4 ||
kotakku 0:b1ce54272580 127 pid == XBOX_ONE_PID5 || pid == XBOX_ONE_PID6 || pid == XBOX_ONE_PID7 || pid == XBOX_ONE_PID8 ||
kotakku 0:b1ce54272580 128 pid == XBOX_ONE_PID9 || pid == XBOX_ONE_PID10 || pid == XBOX_ONE_PID11 || pid == XBOX_ONE_PID12 || pid == XBOX_ONE_PID13));
kotakku 0:b1ce54272580 129 };
kotakku 0:b1ce54272580 130 /**@}*/
kotakku 0:b1ce54272580 131
kotakku 0:b1ce54272580 132 /** @name Xbox Controller functions */
kotakku 0:b1ce54272580 133 /**
kotakku 0:b1ce54272580 134 * getButtonPress(ButtonEnum b) will return true as long as the button is held down.
kotakku 0:b1ce54272580 135 *
kotakku 0:b1ce54272580 136 * While getButtonClick(ButtonEnum b) will only return it once.
kotakku 0:b1ce54272580 137 *
kotakku 0:b1ce54272580 138 * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b),
kotakku 0:b1ce54272580 139 * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b).
kotakku 0:b1ce54272580 140 * @param b ::ButtonEnum to read.
kotakku 0:b1ce54272580 141 * @return getButtonClick(ButtonEnum b) will return a bool, while getButtonPress(ButtonEnum b) will return a word if reading ::L2 or ::R2.
kotakku 0:b1ce54272580 142 */
kotakku 0:b1ce54272580 143 uint16_t getButtonPress(ButtonEnum b);
kotakku 0:b1ce54272580 144 bool getButtonClick(ButtonEnum b);
kotakku 0:b1ce54272580 145
kotakku 0:b1ce54272580 146 /**
kotakku 0:b1ce54272580 147 * Return the analog value from the joysticks on the controller.
kotakku 0:b1ce54272580 148 * @param a Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY.
kotakku 0:b1ce54272580 149 * @return Returns a signed 16-bit integer.
kotakku 0:b1ce54272580 150 */
kotakku 0:b1ce54272580 151 int16_t getAnalogHat(AnalogHatEnum a);
kotakku 0:b1ce54272580 152
kotakku 0:b1ce54272580 153 /**
kotakku 0:b1ce54272580 154 * Used to call your own function when the controller is successfully initialized.
kotakku 0:b1ce54272580 155 * @param funcOnInit Function to call.
kotakku 0:b1ce54272580 156 */
kotakku 0:b1ce54272580 157 void attachOnInit(void (*funcOnInit)(void)) {
kotakku 0:b1ce54272580 158 pFuncOnInit = funcOnInit;
kotakku 0:b1ce54272580 159 };
kotakku 0:b1ce54272580 160
kotakku 0:b1ce54272580 161 /** Used to set the rumble off. */
kotakku 0:b1ce54272580 162 void setRumbleOff();
kotakku 0:b1ce54272580 163
kotakku 0:b1ce54272580 164 /**
kotakku 0:b1ce54272580 165 * Used to turn on rumble continuously.
kotakku 0:b1ce54272580 166 * @param leftTrigger Left trigger force.
kotakku 0:b1ce54272580 167 * @param rightTrigger Right trigger force.
kotakku 0:b1ce54272580 168 * @param leftMotor Left motor force.
kotakku 0:b1ce54272580 169 * @param rightMotor Right motor force.
kotakku 0:b1ce54272580 170 */
kotakku 0:b1ce54272580 171 void setRumbleOn(uint8_t leftTrigger, uint8_t rightTrigger, uint8_t leftMotor, uint8_t rightMotor);
kotakku 0:b1ce54272580 172 /**@}*/
kotakku 0:b1ce54272580 173
kotakku 0:b1ce54272580 174 /** True if a Xbox ONE controller is connected. */
kotakku 0:b1ce54272580 175 bool XboxOneConnected;
kotakku 0:b1ce54272580 176
kotakku 0:b1ce54272580 177 protected:
kotakku 0:b1ce54272580 178 /** Pointer to USB class instance. */
kotakku 0:b1ce54272580 179 USB *pUsb;
kotakku 0:b1ce54272580 180 /** Device address. */
kotakku 0:b1ce54272580 181 uint8_t bAddress;
kotakku 0:b1ce54272580 182 /** Endpoint info structure. */
kotakku 0:b1ce54272580 183 EpInfo epInfo[XBOX_ONE_MAX_ENDPOINTS];
kotakku 0:b1ce54272580 184
kotakku 0:b1ce54272580 185 /** Configuration number. */
kotakku 0:b1ce54272580 186 uint8_t bConfNum;
kotakku 0:b1ce54272580 187 /** Total number of endpoints in the configuration. */
kotakku 0:b1ce54272580 188 uint8_t bNumEP;
kotakku 0:b1ce54272580 189 /** Next poll time based on poll interval taken from the USB descriptor. */
kotakku 0:b1ce54272580 190 uint32_t qNextPollTime;
kotakku 0:b1ce54272580 191
kotakku 0:b1ce54272580 192 /** @name UsbConfigXtracter implementation */
kotakku 0:b1ce54272580 193 /**
kotakku 0:b1ce54272580 194 * UsbConfigXtracter implementation, used to extract endpoint information.
kotakku 0:b1ce54272580 195 * @param conf Configuration value.
kotakku 0:b1ce54272580 196 * @param iface Interface number.
kotakku 0:b1ce54272580 197 * @param alt Alternate setting.
kotakku 0:b1ce54272580 198 * @param proto Interface Protocol.
kotakku 0:b1ce54272580 199 * @param ep Endpoint Descriptor.
kotakku 0:b1ce54272580 200 */
kotakku 0:b1ce54272580 201 void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep);
kotakku 0:b1ce54272580 202 /**@}*/
kotakku 0:b1ce54272580 203
kotakku 0:b1ce54272580 204 /**
kotakku 0:b1ce54272580 205 * Used to print the USB Endpoint Descriptor.
kotakku 0:b1ce54272580 206 * @param ep_ptr Pointer to USB Endpoint Descriptor.
kotakku 0:b1ce54272580 207 */
kotakku 0:b1ce54272580 208 void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr);
kotakku 0:b1ce54272580 209
kotakku 0:b1ce54272580 210 private:
kotakku 0:b1ce54272580 211 /**
kotakku 0:b1ce54272580 212 * Called when the controller is successfully initialized.
kotakku 0:b1ce54272580 213 * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
kotakku 0:b1ce54272580 214 */
kotakku 0:b1ce54272580 215 void onInit();
kotakku 0:b1ce54272580 216 void (*pFuncOnInit)(void); // Pointer to function called in onInit()
kotakku 0:b1ce54272580 217
kotakku 0:b1ce54272580 218 uint8_t pollInterval;
kotakku 0:b1ce54272580 219 bool bPollEnable;
kotakku 0:b1ce54272580 220
kotakku 0:b1ce54272580 221 /* Variables to store the buttons */
kotakku 0:b1ce54272580 222 uint16_t ButtonState;
kotakku 0:b1ce54272580 223 uint16_t OldButtonState;
kotakku 0:b1ce54272580 224 uint16_t ButtonClickState;
kotakku 0:b1ce54272580 225 int16_t hatValue[4];
kotakku 0:b1ce54272580 226 uint16_t triggerValue[2];
kotakku 0:b1ce54272580 227 uint16_t triggerValueOld[2];
kotakku 0:b1ce54272580 228
kotakku 0:b1ce54272580 229 bool L2Clicked; // These buttons are analog, so we use we use these bools to check if they where clicked or not
kotakku 0:b1ce54272580 230 bool R2Clicked;
kotakku 0:b1ce54272580 231
kotakku 0:b1ce54272580 232 uint8_t readBuf[XBOX_ONE_EP_MAXPKTSIZE]; // General purpose buffer for input data
kotakku 0:b1ce54272580 233 uint8_t cmdCounter;
kotakku 0:b1ce54272580 234
kotakku 0:b1ce54272580 235 void readReport(); // Used to read the incoming data
kotakku 0:b1ce54272580 236
kotakku 0:b1ce54272580 237 /* Private commands */
kotakku 0:b1ce54272580 238 uint8_t XboxCommand(uint8_t* data, uint16_t nbytes);
kotakku 0:b1ce54272580 239 };
kotakku 0:b1ce54272580 240 #endif
kotakku 0:b1ce54272580 241