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:
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 #ifndef _spp_h_
kotakku 0:b1ce54272580 19 #define _spp_h_
kotakku 0:b1ce54272580 20
kotakku 0:b1ce54272580 21 #include "BTD.h"
kotakku 0:b1ce54272580 22
kotakku 0:b1ce54272580 23 /* Used for SDP */
kotakku 0:b1ce54272580 24 #define SDP_SERVICE_SEARCH_ATTRIBUTE_REQUEST_PDU 0x06 // See the RFCOMM specs
kotakku 0:b1ce54272580 25 #define SDP_SERVICE_SEARCH_ATTRIBUTE_RESPONSE_PDU 0x07 // See the RFCOMM specs
kotakku 0:b1ce54272580 26 #define SERIALPORT_UUID 0x1101 // See http://www.bluetooth.org/Technical/AssignedNumbers/service_discovery.htm
kotakku 0:b1ce54272580 27 #define L2CAP_UUID 0x0100
kotakku 0:b1ce54272580 28
kotakku 0:b1ce54272580 29 /* Used for RFCOMM */
kotakku 0:b1ce54272580 30 #define RFCOMM_SABM 0x2F
kotakku 0:b1ce54272580 31 #define RFCOMM_UA 0x63
kotakku 0:b1ce54272580 32 #define RFCOMM_UIH 0xEF
kotakku 0:b1ce54272580 33 //#define RFCOMM_DM 0x0F
kotakku 0:b1ce54272580 34 #define RFCOMM_DISC 0x43
kotakku 0:b1ce54272580 35
kotakku 0:b1ce54272580 36 #define extendAddress 0x01 // Always 1
kotakku 0:b1ce54272580 37
kotakku 0:b1ce54272580 38 // Multiplexer message types
kotakku 0:b1ce54272580 39 #define BT_RFCOMM_PN_CMD 0x83
kotakku 0:b1ce54272580 40 #define BT_RFCOMM_PN_RSP 0x81
kotakku 0:b1ce54272580 41 #define BT_RFCOMM_MSC_CMD 0xE3
kotakku 0:b1ce54272580 42 #define BT_RFCOMM_MSC_RSP 0xE1
kotakku 0:b1ce54272580 43 #define BT_RFCOMM_RPN_CMD 0x93
kotakku 0:b1ce54272580 44 #define BT_RFCOMM_RPN_RSP 0x91
kotakku 0:b1ce54272580 45 /*
kotakku 0:b1ce54272580 46 #define BT_RFCOMM_TEST_CMD 0x23
kotakku 0:b1ce54272580 47 #define BT_RFCOMM_TEST_RSP 0x21
kotakku 0:b1ce54272580 48 #define BT_RFCOMM_FCON_CMD 0xA3
kotakku 0:b1ce54272580 49 #define BT_RFCOMM_FCON_RSP 0xA1
kotakku 0:b1ce54272580 50 #define BT_RFCOMM_FCOFF_CMD 0x63
kotakku 0:b1ce54272580 51 #define BT_RFCOMM_FCOFF_RSP 0x61
kotakku 0:b1ce54272580 52 #define BT_RFCOMM_RLS_CMD 0x53
kotakku 0:b1ce54272580 53 #define BT_RFCOMM_RLS_RSP 0x51
kotakku 0:b1ce54272580 54 #define BT_RFCOMM_NSC_RSP 0x11
kotakku 0:b1ce54272580 55 */
kotakku 0:b1ce54272580 56
kotakku 0:b1ce54272580 57 /**
kotakku 0:b1ce54272580 58 * This BluetoothService class implements the Serial Port Protocol (SPP).
kotakku 0:b1ce54272580 59 * It inherits the Arduino Stream class. This allows it to use all the standard Arduino print and stream functions.
kotakku 0:b1ce54272580 60 */
kotakku 0:b1ce54272580 61 class SPP : public BluetoothService, public Stream {
kotakku 0:b1ce54272580 62 public:
kotakku 0:b1ce54272580 63 /**
kotakku 0:b1ce54272580 64 * Constructor for the SPP class.
kotakku 0:b1ce54272580 65 * @param p Pointer to BTD class instance.
kotakku 0:b1ce54272580 66 * @param name Set the name to BTD#btdName. If argument is omitted, then "Arduino" will be used.
kotakku 0:b1ce54272580 67 * @param pin Write the pin to BTD#btdPin. If argument is omitted, then "0000" will be used.
kotakku 0:b1ce54272580 68 */
kotakku 0:b1ce54272580 69 SPP(BTD *p, const char *name = "Arduino", const char *pin = "0000");
kotakku 0:b1ce54272580 70
kotakku 0:b1ce54272580 71 /** @name BluetoothService implementation */
kotakku 0:b1ce54272580 72 /** Used this to disconnect the virtual serial port. */
kotakku 0:b1ce54272580 73 void disconnect();
kotakku 0:b1ce54272580 74 /**@}*/
kotakku 0:b1ce54272580 75
kotakku 0:b1ce54272580 76 /**
kotakku 0:b1ce54272580 77 * Used to provide Boolean tests for the class.
kotakku 0:b1ce54272580 78 * @return Return true if SPP communication is connected.
kotakku 0:b1ce54272580 79 */
kotakku 0:b1ce54272580 80 operator bool() {
kotakku 0:b1ce54272580 81 return connected;
kotakku 0:b1ce54272580 82 }
kotakku 0:b1ce54272580 83 /** Variable used to indicate if the connection is established. */
kotakku 0:b1ce54272580 84 bool connected;
kotakku 0:b1ce54272580 85
kotakku 0:b1ce54272580 86 /** @name Serial port profile (SPP) Print functions */
kotakku 0:b1ce54272580 87 /**
kotakku 0:b1ce54272580 88 * Get number of bytes waiting to be read.
kotakku 0:b1ce54272580 89 * @return Return the number of bytes ready to be read.
kotakku 0:b1ce54272580 90 */
kotakku 0:b1ce54272580 91 int available(void);
kotakku 0:b1ce54272580 92
kotakku 0:b1ce54272580 93 /** Send out all bytes in the buffer. */
kotakku 0:b1ce54272580 94 void flush(void) {
kotakku 0:b1ce54272580 95 send();
kotakku 0:b1ce54272580 96 };
kotakku 0:b1ce54272580 97 /**
kotakku 0:b1ce54272580 98 * Used to read the next value in the buffer without advancing to the next one.
kotakku 0:b1ce54272580 99 * @return Return the byte. Will return -1 if no bytes are available.
kotakku 0:b1ce54272580 100 */
kotakku 0:b1ce54272580 101 int peek(void);
kotakku 0:b1ce54272580 102 /**
kotakku 0:b1ce54272580 103 * Used to read the buffer.
kotakku 0:b1ce54272580 104 * @return Return the byte. Will return -1 if no bytes are available.
kotakku 0:b1ce54272580 105 */
kotakku 0:b1ce54272580 106 int read(void);
kotakku 0:b1ce54272580 107
kotakku 0:b1ce54272580 108 #if defined(ARDUINO) && ARDUINO >=100
kotakku 0:b1ce54272580 109 /**
kotakku 0:b1ce54272580 110 * Writes the byte to send to a buffer. The message is send when either send() or after Usb.Task() is called.
kotakku 0:b1ce54272580 111 * @param data The byte to write.
kotakku 0:b1ce54272580 112 * @return Return the number of bytes written.
kotakku 0:b1ce54272580 113 */
kotakku 0:b1ce54272580 114 size_t write(uint8_t data);
kotakku 0:b1ce54272580 115 /**
kotakku 0:b1ce54272580 116 * Writes the bytes to send to a buffer. The message is send when either send() or after Usb.Task() is called.
kotakku 0:b1ce54272580 117 * @param data The data array to send.
kotakku 0:b1ce54272580 118 * @param size Size of the data.
kotakku 0:b1ce54272580 119 * @return Return the number of bytes written.
kotakku 0:b1ce54272580 120 */
kotakku 0:b1ce54272580 121 size_t write(const uint8_t* data, size_t size);
kotakku 0:b1ce54272580 122 /** Pull in write(const char *str) from Print */
kotakku 0:b1ce54272580 123 /*
kotakku 0:b1ce54272580 124 #if !defined(RBL_NRF51822)
kotakku 0:b1ce54272580 125 using Print::write;
kotakku 0:b1ce54272580 126 #endif*/
kotakku 0:b1ce54272580 127
kotakku 0:b1ce54272580 128 #else
kotakku 0:b1ce54272580 129 /**
kotakku 0:b1ce54272580 130 * Writes the byte to send to a buffer. The message is send when either send() or after Usb.Task() is called.
kotakku 0:b1ce54272580 131 * @param data The byte to write.
kotakku 0:b1ce54272580 132 */
kotakku 0:b1ce54272580 133 void write(uint8_t data);
kotakku 0:b1ce54272580 134 /**
kotakku 0:b1ce54272580 135 * Writes the bytes to send to a buffer. The message is send when either send() or after Usb.Task() is called.
kotakku 0:b1ce54272580 136 * @param data The data array to send.
kotakku 0:b1ce54272580 137 * @param size Size of the data.
kotakku 0:b1ce54272580 138 */
kotakku 0:b1ce54272580 139 void write(const uint8_t* data, size_t size);
kotakku 0:b1ce54272580 140 #endif
kotakku 0:b1ce54272580 141
kotakku 0:b1ce54272580 142 /** Discard all the bytes in the buffer. */
kotakku 0:b1ce54272580 143 void discard(void);
kotakku 0:b1ce54272580 144 /**
kotakku 0:b1ce54272580 145 * This will send all the bytes in the buffer.
kotakku 0:b1ce54272580 146 * This is called whenever Usb.Task() is called,
kotakku 0:b1ce54272580 147 * but can also be called via this function.
kotakku 0:b1ce54272580 148 */
kotakku 0:b1ce54272580 149 void send(void);
kotakku 0:b1ce54272580 150 /**@}*/
kotakku 0:b1ce54272580 151
kotakku 0:b1ce54272580 152 protected:
kotakku 0:b1ce54272580 153 /** @name BluetoothService implementation */
kotakku 0:b1ce54272580 154 /**
kotakku 0:b1ce54272580 155 * Used to pass acldata to the services.
kotakku 0:b1ce54272580 156 * @param ACLData Incoming acldata.
kotakku 0:b1ce54272580 157 */
kotakku 0:b1ce54272580 158 void ACLData(uint8_t* ACLData);
kotakku 0:b1ce54272580 159 /** Used to establish the connection automatically. */
kotakku 0:b1ce54272580 160 void Run();
kotakku 0:b1ce54272580 161 /** Use this to reset the service. */
kotakku 0:b1ce54272580 162 void Reset();
kotakku 0:b1ce54272580 163 /**
kotakku 0:b1ce54272580 164 * Called when a device is successfully initialized.
kotakku 0:b1ce54272580 165 * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
kotakku 0:b1ce54272580 166 * This is useful for instance if you want to set the LEDs in a specific way.
kotakku 0:b1ce54272580 167 */
kotakku 0:b1ce54272580 168 void onInit();
kotakku 0:b1ce54272580 169 /**@}*/
kotakku 0:b1ce54272580 170
kotakku 0:b1ce54272580 171 private:
kotakku 0:b1ce54272580 172 /* Set true when a channel is created */
kotakku 0:b1ce54272580 173 bool SDPConnected;
kotakku 0:b1ce54272580 174 bool RFCOMMConnected;
kotakku 0:b1ce54272580 175
kotakku 0:b1ce54272580 176 /* Variables used by L2CAP state machines */
kotakku 0:b1ce54272580 177 uint8_t l2cap_sdp_state;
kotakku 0:b1ce54272580 178 uint8_t l2cap_rfcomm_state;
kotakku 0:b1ce54272580 179
kotakku 0:b1ce54272580 180 uint8_t l2capoutbuf[BULK_MAXPKTSIZE]; // General purpose buffer for l2cap out data
kotakku 0:b1ce54272580 181 uint8_t rfcommbuf[10]; // Buffer for RFCOMM Commands
kotakku 0:b1ce54272580 182
kotakku 0:b1ce54272580 183 /* L2CAP Channels */
kotakku 0:b1ce54272580 184 uint8_t sdp_scid[2]; // L2CAP source CID for SDP
kotakku 0:b1ce54272580 185 uint8_t sdp_dcid[2]; // 0x0050
kotakku 0:b1ce54272580 186 uint8_t rfcomm_scid[2]; // L2CAP source CID for RFCOMM
kotakku 0:b1ce54272580 187 uint8_t rfcomm_dcid[2]; // 0x0051
kotakku 0:b1ce54272580 188
kotakku 0:b1ce54272580 189 /* RFCOMM Variables */
kotakku 0:b1ce54272580 190 uint8_t rfcommChannel;
kotakku 0:b1ce54272580 191 uint8_t rfcommChannelConnection; // This is the channel the SPP channel will be running at
kotakku 0:b1ce54272580 192 uint8_t rfcommDirection;
kotakku 0:b1ce54272580 193 uint8_t rfcommCommandResponse;
kotakku 0:b1ce54272580 194 uint8_t rfcommChannelType;
kotakku 0:b1ce54272580 195 uint8_t rfcommPfBit;
kotakku 0:b1ce54272580 196
kotakku 0:b1ce54272580 197 uint32_t timer;
kotakku 0:b1ce54272580 198 bool waitForLastCommand;
kotakku 0:b1ce54272580 199 bool creditSent;
kotakku 0:b1ce54272580 200
kotakku 0:b1ce54272580 201 uint8_t rfcommDataBuffer[100]; // Create a 100 sized buffer for incoming data
kotakku 0:b1ce54272580 202 uint8_t sppOutputBuffer[100]; // Create a 100 sized buffer for outgoing SPP data
kotakku 0:b1ce54272580 203 uint8_t sppIndex;
kotakku 0:b1ce54272580 204 uint8_t rfcommAvailable;
kotakku 0:b1ce54272580 205
kotakku 0:b1ce54272580 206 bool firstMessage; // Used to see if it's the first SDP request received
kotakku 0:b1ce54272580 207 uint8_t bytesRead; // Counter to see when it's time to send more credit
kotakku 0:b1ce54272580 208
kotakku 0:b1ce54272580 209 /* State machines */
kotakku 0:b1ce54272580 210 void SDP_task(); // SDP state machine
kotakku 0:b1ce54272580 211 void RFCOMM_task(); // RFCOMM state machine
kotakku 0:b1ce54272580 212
kotakku 0:b1ce54272580 213 /* SDP Commands */
kotakku 0:b1ce54272580 214 void SDP_Command(uint8_t *data, uint8_t nbytes);
kotakku 0:b1ce54272580 215 void serviceNotSupported(uint8_t transactionIDHigh, uint8_t transactionIDLow);
kotakku 0:b1ce54272580 216 void serialPortResponse1(uint8_t transactionIDHigh, uint8_t transactionIDLow);
kotakku 0:b1ce54272580 217 void serialPortResponse2(uint8_t transactionIDHigh, uint8_t transactionIDLow);
kotakku 0:b1ce54272580 218 void l2capResponse1(uint8_t transactionIDHigh, uint8_t transactionIDLow);
kotakku 0:b1ce54272580 219 void l2capResponse2(uint8_t transactionIDHigh, uint8_t transactionIDLow);
kotakku 0:b1ce54272580 220
kotakku 0:b1ce54272580 221 /* RFCOMM Commands */
kotakku 0:b1ce54272580 222 void RFCOMM_Command(uint8_t *data, uint8_t nbytes);
kotakku 0:b1ce54272580 223 void sendRfcomm(uint8_t channel, uint8_t direction, uint8_t CR, uint8_t channelType, uint8_t pfBit, uint8_t *data, uint8_t length);
kotakku 0:b1ce54272580 224 void sendRfcommCredit(uint8_t channel, uint8_t direction, uint8_t CR, uint8_t channelType, uint8_t pfBit, uint8_t credit);
kotakku 0:b1ce54272580 225 uint8_t calcFcs(uint8_t *data);
kotakku 0:b1ce54272580 226 bool checkFcs(uint8_t *data, uint8_t fcs);
kotakku 0:b1ce54272580 227 uint8_t crc(uint8_t *data);
kotakku 0:b1ce54272580 228 };
kotakku 0:b1ce54272580 229 #endif
kotakku 0:b1ce54272580 230