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) 2011 Circuits At Home, LTD. 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 Circuits At Home, LTD
kotakku 0:b1ce54272580 14 Web : http://www.circuitsathome.com
kotakku 0:b1ce54272580 15 e-mail : support@circuitsathome.com
kotakku 0:b1ce54272580 16 */
kotakku 0:b1ce54272580 17 #if !defined(__HIDDESCRIPTORPARSER_H__)
kotakku 0:b1ce54272580 18 #define __HIDDESCRIPTORPARSER_H__
kotakku 0:b1ce54272580 19
kotakku 0:b1ce54272580 20 #include "usbhid.h"
kotakku 0:b1ce54272580 21
kotakku 0:b1ce54272580 22 class ReportDescParserBase : public USBReadParser {
kotakku 0:b1ce54272580 23 public:
kotakku 0:b1ce54272580 24 typedef void (*UsagePageFunc)(uint16_t usage);
kotakku 0:b1ce54272580 25
kotakku 0:b1ce54272580 26 static void PrintGenericDesktopPageUsage(uint16_t usage);
kotakku 0:b1ce54272580 27 static void PrintSimulationControlsPageUsage(uint16_t usage);
kotakku 0:b1ce54272580 28 static void PrintVRControlsPageUsage(uint16_t usage);
kotakku 0:b1ce54272580 29 static void PrintSportsControlsPageUsage(uint16_t usage);
kotakku 0:b1ce54272580 30 static void PrintGameControlsPageUsage(uint16_t usage);
kotakku 0:b1ce54272580 31 static void PrintGenericDeviceControlsPageUsage(uint16_t usage);
kotakku 0:b1ce54272580 32 static void PrintLEDPageUsage(uint16_t usage);
kotakku 0:b1ce54272580 33 static void PrintButtonPageUsage(uint16_t usage);
kotakku 0:b1ce54272580 34 static void PrintOrdinalPageUsage(uint16_t usage);
kotakku 0:b1ce54272580 35 static void PrintTelephonyPageUsage(uint16_t usage);
kotakku 0:b1ce54272580 36 static void PrintConsumerPageUsage(uint16_t usage);
kotakku 0:b1ce54272580 37 static void PrintDigitizerPageUsage(uint16_t usage);
kotakku 0:b1ce54272580 38 static void PrintAlphanumDisplayPageUsage(uint16_t usage);
kotakku 0:b1ce54272580 39 static void PrintMedicalInstrumentPageUsage(uint16_t usage);
kotakku 0:b1ce54272580 40
kotakku 0:b1ce54272580 41 static void PrintValue(uint8_t *p, uint8_t len);
kotakku 0:b1ce54272580 42 static void PrintByteValue(uint8_t data);
kotakku 0:b1ce54272580 43
kotakku 0:b1ce54272580 44 static void PrintItemTitle(uint8_t prefix);
kotakku 0:b1ce54272580 45
kotakku 0:b1ce54272580 46 static const char * const usagePageTitles0[];
kotakku 0:b1ce54272580 47 static const char * const usagePageTitles1[];
kotakku 0:b1ce54272580 48 static const char * const genDesktopTitles0[];
kotakku 0:b1ce54272580 49 static const char * const genDesktopTitles1[];
kotakku 0:b1ce54272580 50 static const char * const genDesktopTitles2[];
kotakku 0:b1ce54272580 51 static const char * const genDesktopTitles3[];
kotakku 0:b1ce54272580 52 static const char * const genDesktopTitles4[];
kotakku 0:b1ce54272580 53 static const char * const simuTitles0[];
kotakku 0:b1ce54272580 54 static const char * const simuTitles1[];
kotakku 0:b1ce54272580 55 static const char * const simuTitles2[];
kotakku 0:b1ce54272580 56 static const char * const vrTitles0[];
kotakku 0:b1ce54272580 57 static const char * const vrTitles1[];
kotakku 0:b1ce54272580 58 static const char * const sportsCtrlTitles0[];
kotakku 0:b1ce54272580 59 static const char * const sportsCtrlTitles1[];
kotakku 0:b1ce54272580 60 static const char * const sportsCtrlTitles2[];
kotakku 0:b1ce54272580 61 static const char * const gameTitles0[];
kotakku 0:b1ce54272580 62 static const char * const gameTitles1[];
kotakku 0:b1ce54272580 63 static const char * const genDevCtrlTitles[];
kotakku 0:b1ce54272580 64 static const char * const ledTitles[];
kotakku 0:b1ce54272580 65 static const char * const telTitles0[];
kotakku 0:b1ce54272580 66 static const char * const telTitles1[];
kotakku 0:b1ce54272580 67 static const char * const telTitles2[];
kotakku 0:b1ce54272580 68 static const char * const telTitles3[];
kotakku 0:b1ce54272580 69 static const char * const telTitles4[];
kotakku 0:b1ce54272580 70 static const char * const telTitles5[];
kotakku 0:b1ce54272580 71 static const char * const consTitles0[];
kotakku 0:b1ce54272580 72 static const char * const consTitles1[];
kotakku 0:b1ce54272580 73 static const char * const consTitles2[];
kotakku 0:b1ce54272580 74 static const char * const consTitles3[];
kotakku 0:b1ce54272580 75 static const char * const consTitles4[];
kotakku 0:b1ce54272580 76 static const char * const consTitles5[];
kotakku 0:b1ce54272580 77 static const char * const consTitles6[];
kotakku 0:b1ce54272580 78 static const char * const consTitles7[];
kotakku 0:b1ce54272580 79 static const char * const consTitles8[];
kotakku 0:b1ce54272580 80 static const char * const consTitles9[];
kotakku 0:b1ce54272580 81 static const char * const consTitlesA[];
kotakku 0:b1ce54272580 82 static const char * const consTitlesB[];
kotakku 0:b1ce54272580 83 static const char * const consTitlesC[];
kotakku 0:b1ce54272580 84 static const char * const consTitlesD[];
kotakku 0:b1ce54272580 85 static const char * const consTitlesE[];
kotakku 0:b1ce54272580 86 static const char * const digitTitles0[];
kotakku 0:b1ce54272580 87 static const char * const digitTitles1[];
kotakku 0:b1ce54272580 88 static const char * const digitTitles2[];
kotakku 0:b1ce54272580 89 static const char * const aplphanumTitles0[];
kotakku 0:b1ce54272580 90 static const char * const aplphanumTitles1[];
kotakku 0:b1ce54272580 91 static const char * const aplphanumTitles2[];
kotakku 0:b1ce54272580 92 static const char * const medInstrTitles0[];
kotakku 0:b1ce54272580 93 static const char * const medInstrTitles1[];
kotakku 0:b1ce54272580 94 static const char * const medInstrTitles2[];
kotakku 0:b1ce54272580 95 static const char * const medInstrTitles3[];
kotakku 0:b1ce54272580 96 static const char * const medInstrTitles4[];
kotakku 0:b1ce54272580 97
kotakku 0:b1ce54272580 98 protected:
kotakku 0:b1ce54272580 99 static UsagePageFunc usagePageFunctions[];
kotakku 0:b1ce54272580 100
kotakku 0:b1ce54272580 101 MultiValueBuffer theBuffer;
kotakku 0:b1ce54272580 102 MultiByteValueParser valParser;
kotakku 0:b1ce54272580 103 ByteSkipper theSkipper;
kotakku 0:b1ce54272580 104 uint8_t varBuffer[sizeof (USB_CONFIGURATION_DESCRIPTOR)];
kotakku 0:b1ce54272580 105
kotakku 0:b1ce54272580 106 uint8_t itemParseState; // Item parser state variable
kotakku 0:b1ce54272580 107 uint8_t itemSize; // Item size
kotakku 0:b1ce54272580 108 uint8_t itemPrefix; // Item prefix (first byte)
kotakku 0:b1ce54272580 109 uint8_t rptSize; // Report Size
kotakku 0:b1ce54272580 110 uint8_t rptCount; // Report Count
kotakku 0:b1ce54272580 111
kotakku 0:b1ce54272580 112 uint16_t totalSize; // Report size in bits
kotakku 0:b1ce54272580 113
kotakku 0:b1ce54272580 114 // Method should be defined here if virtual.
kotakku 0:b1ce54272580 115 virtual uint8_t ParseItem(uint8_t **pp, uint16_t *pcntdn);
kotakku 0:b1ce54272580 116
kotakku 0:b1ce54272580 117 UsagePageFunc pfUsage;
kotakku 0:b1ce54272580 118
kotakku 0:b1ce54272580 119 static void PrintUsagePage(uint16_t page);
kotakku 0:b1ce54272580 120 void SetUsagePage(uint16_t page);
kotakku 0:b1ce54272580 121
kotakku 0:b1ce54272580 122 public:
kotakku 0:b1ce54272580 123
kotakku 0:b1ce54272580 124 ReportDescParserBase() :
kotakku 0:b1ce54272580 125 itemParseState(0),
kotakku 0:b1ce54272580 126 itemSize(0),
kotakku 0:b1ce54272580 127 itemPrefix(0),
kotakku 0:b1ce54272580 128 rptSize(0),
kotakku 0:b1ce54272580 129 rptCount(0),
kotakku 0:b1ce54272580 130 pfUsage(NULL) {
kotakku 0:b1ce54272580 131 theBuffer.pValue = varBuffer;
kotakku 0:b1ce54272580 132 valParser.Initialize(&theBuffer);
kotakku 0:b1ce54272580 133 theSkipper.Initialize(&theBuffer);
kotakku 0:b1ce54272580 134 };
kotakku 0:b1ce54272580 135
kotakku 0:b1ce54272580 136 void Parse(const uint16_t len, const uint8_t *pbuf, const uint16_t &offset);
kotakku 0:b1ce54272580 137
kotakku 0:b1ce54272580 138 enum {
kotakku 0:b1ce54272580 139 enErrorSuccess = 0
kotakku 0:b1ce54272580 140 , enErrorIncomplete // value or record is partialy read in buffer
kotakku 0:b1ce54272580 141 , enErrorBufferTooSmall
kotakku 0:b1ce54272580 142 };
kotakku 0:b1ce54272580 143 };
kotakku 0:b1ce54272580 144
kotakku 0:b1ce54272580 145 class ReportDescParser : public ReportDescParserBase {
kotakku 0:b1ce54272580 146 };
kotakku 0:b1ce54272580 147
kotakku 0:b1ce54272580 148 class ReportDescParser2 : public ReportDescParserBase {
kotakku 0:b1ce54272580 149 uint8_t rptId; // Report ID
kotakku 0:b1ce54272580 150 uint8_t useMin; // Usage Minimum
kotakku 0:b1ce54272580 151 uint8_t useMax; // Usage Maximum
kotakku 0:b1ce54272580 152 uint8_t fieldCount; // Number of field being currently processed
kotakku 0:b1ce54272580 153
kotakku 0:b1ce54272580 154 void OnInputItem(uint8_t itm); // Method which is called every time Input item is found
kotakku 0:b1ce54272580 155
kotakku 0:b1ce54272580 156 uint8_t *pBuf; // Report buffer pointer
kotakku 0:b1ce54272580 157 uint8_t bLen; // Report length
kotakku 0:b1ce54272580 158
kotakku 0:b1ce54272580 159 protected:
kotakku 0:b1ce54272580 160 // Method should be defined here if virtual.
kotakku 0:b1ce54272580 161 virtual uint8_t ParseItem(uint8_t **pp, uint16_t *pcntdn);
kotakku 0:b1ce54272580 162
kotakku 0:b1ce54272580 163 public:
kotakku 0:b1ce54272580 164
kotakku 0:b1ce54272580 165 ReportDescParser2(uint16_t len, uint8_t *pbuf) :
kotakku 0:b1ce54272580 166 ReportDescParserBase(), rptId(0), useMin(0), useMax(0), fieldCount(0), pBuf(pbuf), bLen(len) {
kotakku 0:b1ce54272580 167 };
kotakku 0:b1ce54272580 168 };
kotakku 0:b1ce54272580 169
kotakku 0:b1ce54272580 170 class UniversalReportParser : public HIDReportParser {
kotakku 0:b1ce54272580 171 public:
kotakku 0:b1ce54272580 172 // Method should be defined here if virtual.
kotakku 0:b1ce54272580 173 virtual void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
kotakku 0:b1ce54272580 174 };
kotakku 0:b1ce54272580 175
kotakku 0:b1ce54272580 176 #endif // __HIDDESCRIPTORPARSER_H__
kotakku 0:b1ce54272580 177