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
robo_ichinoseki_a 1:da31140f2a1c 1 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
robo_ichinoseki_a 1:da31140f2a1c 2
robo_ichinoseki_a 1:da31140f2a1c 3 This program is free software; you can redistribute it and/or modify
robo_ichinoseki_a 1:da31140f2a1c 4 it under the terms of the GNU General Public License as published by
robo_ichinoseki_a 1:da31140f2a1c 5 the Free Software Foundation; either version 2 of the License, or
robo_ichinoseki_a 1:da31140f2a1c 6 (at your option) any later version.
robo_ichinoseki_a 1:da31140f2a1c 7
robo_ichinoseki_a 1:da31140f2a1c 8 This program is distributed in the hope that it will be useful,
robo_ichinoseki_a 1:da31140f2a1c 9 but WITHOUT ANY WARRANTY; without even the implied warranty of
robo_ichinoseki_a 1:da31140f2a1c 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
robo_ichinoseki_a 1:da31140f2a1c 11 GNU General Public License for more details.
robo_ichinoseki_a 1:da31140f2a1c 12
robo_ichinoseki_a 1:da31140f2a1c 13 You should have received a copy of the GNU General Public License
robo_ichinoseki_a 1:da31140f2a1c 14 along with this program; if not, write to the Free Software
robo_ichinoseki_a 1:da31140f2a1c 15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
robo_ichinoseki_a 1:da31140f2a1c 16
robo_ichinoseki_a 1:da31140f2a1c 17 Contact information
robo_ichinoseki_a 1:da31140f2a1c 18 -------------------
robo_ichinoseki_a 1:da31140f2a1c 19
robo_ichinoseki_a 1:da31140f2a1c 20 Circuits At Home, LTD
robo_ichinoseki_a 1:da31140f2a1c 21 Web : http://www.circuitsathome.com
robo_ichinoseki_a 1:da31140f2a1c 22 e-mail : support@circuitsathome.com
robo_ichinoseki_a 1:da31140f2a1c 23 */
robo_ichinoseki_a 1:da31140f2a1c 24
robo_ichinoseki_a 1:da31140f2a1c 25 #include <stdio.h>
robo_ichinoseki_a 1:da31140f2a1c 26
robo_ichinoseki_a 1:da31140f2a1c 27 #if !defined(_usb_h_) || defined(__PRINTHEX_H__)
robo_ichinoseki_a 1:da31140f2a1c 28 #error "Never include printhex.h directly; include Usb.h instead"
robo_ichinoseki_a 1:da31140f2a1c 29 #else
robo_ichinoseki_a 1:da31140f2a1c 30 #define __PRINTHEX_H__
robo_ichinoseki_a 1:da31140f2a1c 31
robo_ichinoseki_a 1:da31140f2a1c 32 void E_Notifyc(char c, int lvl);
robo_ichinoseki_a 1:da31140f2a1c 33
robo_ichinoseki_a 1:da31140f2a1c 34 template <class T>
robo_ichinoseki_a 1:da31140f2a1c 35 void PrintHex(T val, int lvl) {
robo_ichinoseki_a 1:da31140f2a1c 36 int num_nibbles = sizeof (T) * 2;
robo_ichinoseki_a 1:da31140f2a1c 37
robo_ichinoseki_a 1:da31140f2a1c 38 do {
robo_ichinoseki_a 1:da31140f2a1c 39 char v = 48 + (((val >> (num_nibbles - 1) * 4)) & 0x0f);
robo_ichinoseki_a 1:da31140f2a1c 40 if(v > 57) v += 7;
robo_ichinoseki_a 1:da31140f2a1c 41 E_Notifyc(v, lvl);
robo_ichinoseki_a 1:da31140f2a1c 42 } while(--num_nibbles);
robo_ichinoseki_a 1:da31140f2a1c 43 }
kotakku 0:b1ce54272580 44
robo_ichinoseki_a 1:da31140f2a1c 45 template <class T>
robo_ichinoseki_a 1:da31140f2a1c 46 void PrintBin(T val, int lvl) {
robo_ichinoseki_a 1:da31140f2a1c 47 for(T mask = (((T)1) << ((sizeof (T) << 3) - 1)); mask; mask >>= 1)
robo_ichinoseki_a 1:da31140f2a1c 48 if(val & mask)
robo_ichinoseki_a 1:da31140f2a1c 49 E_Notifyc('1', lvl);
robo_ichinoseki_a 1:da31140f2a1c 50 else
robo_ichinoseki_a 1:da31140f2a1c 51 E_Notifyc('0', lvl);
robo_ichinoseki_a 1:da31140f2a1c 52 }
robo_ichinoseki_a 1:da31140f2a1c 53
robo_ichinoseki_a 1:da31140f2a1c 54 template <class T>
robo_ichinoseki_a 1:da31140f2a1c 55 void SerialPrintHex(T val) {
robo_ichinoseki_a 1:da31140f2a1c 56 int num_nibbles = sizeof (T) * 2;
robo_ichinoseki_a 1:da31140f2a1c 57
robo_ichinoseki_a 1:da31140f2a1c 58 do {
robo_ichinoseki_a 1:da31140f2a1c 59 char v = 48 + (((val >> (num_nibbles - 1) * 4)) & 0x0f);
robo_ichinoseki_a 1:da31140f2a1c 60 if(v > 57) v += 7;
robo_ichinoseki_a 1:da31140f2a1c 61 //print(v);
robo_ichinoseki_a 1:da31140f2a1c 62 } while(--num_nibbles);
robo_ichinoseki_a 1:da31140f2a1c 63 }
robo_ichinoseki_a 1:da31140f2a1c 64
robo_ichinoseki_a 1:da31140f2a1c 65 template <class T>
robo_ichinoseki_a 1:da31140f2a1c 66 void PrintHex2(Print *prn, T val) {
robo_ichinoseki_a 1:da31140f2a1c 67 T mask = (((T)1) << (((sizeof (T) << 1) - 1) << 2));
robo_ichinoseki_a 1:da31140f2a1c 68
robo_ichinoseki_a 1:da31140f2a1c 69 while(mask > 1) {
robo_ichinoseki_a 1:da31140f2a1c 70 if(val < mask)
robo_ichinoseki_a 1:da31140f2a1c 71 prn->print("0");
robo_ichinoseki_a 1:da31140f2a1c 72
robo_ichinoseki_a 1:da31140f2a1c 73 mask >>= 4;
robo_ichinoseki_a 1:da31140f2a1c 74 }
robo_ichinoseki_a 1:da31140f2a1c 75 prn->print((T)val, HEX);
robo_ichinoseki_a 1:da31140f2a1c 76 }
robo_ichinoseki_a 1:da31140f2a1c 77
robo_ichinoseki_a 1:da31140f2a1c 78 template <class T> void D_PrintHex(T val __attribute__((unused)), int lvl __attribute__((unused))) {
robo_ichinoseki_a 1:da31140f2a1c 79 #ifdef DEBUG_USB_HOST
robo_ichinoseki_a 1:da31140f2a1c 80 PrintHex<T > (val, lvl);
robo_ichinoseki_a 1:da31140f2a1c 81 #endif
robo_ichinoseki_a 1:da31140f2a1c 82 }
robo_ichinoseki_a 1:da31140f2a1c 83
robo_ichinoseki_a 1:da31140f2a1c 84 template <class T>
robo_ichinoseki_a 1:da31140f2a1c 85 void D_PrintBin(T val, int lvl) {
robo_ichinoseki_a 1:da31140f2a1c 86 #ifdef DEBUG_USB_HOST
robo_ichinoseki_a 1:da31140f2a1c 87 PrintBin<T > (val, lvl);
robo_ichinoseki_a 1:da31140f2a1c 88 #endif
robo_ichinoseki_a 1:da31140f2a1c 89 }
robo_ichinoseki_a 1:da31140f2a1c 90
robo_ichinoseki_a 1:da31140f2a1c 91
robo_ichinoseki_a 1:da31140f2a1c 92
robo_ichinoseki_a 1:da31140f2a1c 93 #endif // __PRINTHEX_H__
robo_ichinoseki_a 1:da31140f2a1c 94