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");
        }
    }
}
Download repository: zip gz

Files at revision 1:da31140f2a1c

Name Size Actions
[up]
BTD.cpp 62205 Revisions Annotate
BTD.h 25538 Revisions Annotate
BTHID.cpp 22605 Revisions Annotate
BTHID.h 5440 Revisions Annotate
PS3BT.cpp 33054 Revisions Annotate
PS3BT.h 9714 Revisions Annotate
PS3Enums.h 4248 Revisions Annotate
PS3USB.cpp 23686 Revisions Annotate
PS3USB.h 11653 Revisions Annotate
PS4BT.h 4419 Revisions Annotate
PS4Parser.cpp 6153 Revisions Annotate
PS4Parser.h 14157 Revisions Annotate
PS4USB.h 4889 Revisions Annotate
PSBuzz.cpp 3654 Revisions Annotate
PSBuzz.h 6845 Revisions Annotate
SPP.cpp 47411 Revisions Annotate
SPP.h 8718 Revisions Annotate
UHS2_gpio.cpp 2540 Revisions Annotate
UHS2_gpio.h 1317 Revisions Annotate
Usb.cpp 33185 Revisions Annotate
Usb.h 1461 Revisions Annotate
UsbCore.h 13341 Revisions Annotate
Wii.cpp 73761 Revisions Annotate
Wii.h 17920 Revisions Annotate
XBOXOLD.cpp 12018 Revisions Annotate
XBOXOLD.h 6731 Revisions Annotate
XBOXONE.cpp 17826 Revisions Annotate
XBOXONE.h 9572 Revisions Annotate
XBOXRECV.cpp 23801 Revisions Annotate
XBOXRECV.h 11052 Revisions Annotate
XBOXUSB.cpp 13474 Revisions Annotate
XBOXUSB.h 8569 Revisions Annotate
address.h 10581 Revisions Annotate
adk.cpp 12822 Revisions Annotate
adk.h 4546 Revisions Annotate
avrpins.h 1322 Revisions Annotate
cdc_XR21B1411.cpp 5874 Revisions Annotate
cdc_XR21B1411.h 13303 Revisions Annotate
cdcacm.cpp 11288 Revisions Annotate
cdcacm.h 9667 Revisions Annotate
cdcftdi.cpp 13042 Revisions Annotate
cdcftdi.h 5997 Revisions Annotate
cdcprolific.cpp 6714 Revisions Annotate
cdcprolific.h 6695 Revisions Annotate
confdescparser.h 10656 Revisions Annotate
controllerEnums.h 5412 Revisions Annotate
hexdump.h 2421 Revisions Annotate
hidboot.cpp 7983 Revisions Annotate
hidboot.h 21298 Revisions Annotate
hidcomposite.cpp 12850 Revisions Annotate
hidcomposite.h 3616 Revisions Annotate
hidescriptorparser.cpp 58837 Revisions Annotate
hidescriptorparser.h 6901 Revisions Annotate
hiduniversal.cpp 13309 Revisions Annotate
hiduniversal.h 3699 Revisions Annotate
hidusagestr.h 54647 Revisions Annotate
hidusagetitlearrays.cpp 28776 Revisions Annotate
macros.h 4270 Revisions Annotate
masstorage.cpp 42985 Revisions Annotate
masstorage.h 19788 Revisions Annotate
max3421e.h 6450 Revisions Annotate
max_LCD.cpp 7112 Revisions Annotate
max_LCD.h 3047 Revisions Annotate
message.cpp 3441 Revisions Annotate
message.h 2863 Revisions Annotate
parsetools.cpp 2576 Revisions Annotate
parsetools.h 4296 Revisions Annotate
printhex.h 2620 Revisions Annotate
settings.h 7094 Revisions Annotate
sink_parser.h 1458 Revisions Annotate
usb_ch9.h 9933 Revisions Annotate
usbh_midi.cpp 22964 Revisions Annotate
usbh_midi.h 4094 Revisions Annotate
usbhid.cpp 4899 Revisions Annotate
usbhid.h 7547 Revisions Annotate
usbhost.cpp 8903 Revisions Annotate
usbhost.h 2473 Revisions Annotate
usbhub.cpp 14656 Revisions Annotate
usbhub.h 12064 Revisions Annotate
version_helper.h 5289 Revisions Annotate
xboxEnums.h 1685 Revisions Annotate