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) 2013 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 _controllerenums_h
kotakku 0:b1ce54272580 19 #define _controllerenums_h
kotakku 0:b1ce54272580 20
kotakku 0:b1ce54272580 21 #if defined(ESP32)
kotakku 0:b1ce54272580 22 #undef PS
kotakku 0:b1ce54272580 23 #endif
kotakku 0:b1ce54272580 24
kotakku 0:b1ce54272580 25 /**
kotakku 0:b1ce54272580 26 * This header file is used to store different enums for the controllers,
kotakku 0:b1ce54272580 27 * This is necessary so all the different libraries can be used at once.
kotakku 0:b1ce54272580 28 */
kotakku 0:b1ce54272580 29
kotakku 0:b1ce54272580 30 /** Enum used to turn on the LEDs on the different controllers. */
kotakku 0:b1ce54272580 31 enum LEDEnum {
kotakku 0:b1ce54272580 32 OFF = 0,
kotakku 0:b1ce54272580 33 #ifndef RBL_NRF51822
kotakku 0:b1ce54272580 34 CONTROLLER_LED1 = 1,
kotakku 0:b1ce54272580 35 CONTROLLER_LED2 = 2,
kotakku 0:b1ce54272580 36 CONTROLLER_LED3 = 3,
kotakku 0:b1ce54272580 37 CONTROLLER_LED4 = 4,
kotakku 0:b1ce54272580 38 #endif
kotakku 0:b1ce54272580 39 CONTROLLER_LED5 = 5,
kotakku 0:b1ce54272580 40 CONTROLLER_LED6 = 6,
kotakku 0:b1ce54272580 41 CONTROLLER_LED7 = 7,
kotakku 0:b1ce54272580 42 CONTROLLER_LED8 = 8,
kotakku 0:b1ce54272580 43 CONTROLLER_LED9 = 9,
kotakku 0:b1ce54272580 44 CONTROLLER_LED10 = 10,
kotakku 0:b1ce54272580 45 /** Used to blink all LEDs on the Xbox controller */
kotakku 0:b1ce54272580 46 ALL = 5,
kotakku 0:b1ce54272580 47 };
kotakku 0:b1ce54272580 48
kotakku 0:b1ce54272580 49 /** Used to set the colors of the Move and PS4 controller. */
kotakku 0:b1ce54272580 50 enum ColorsEnum {
kotakku 0:b1ce54272580 51 /** r = 255, g = 0, b = 0 */
kotakku 0:b1ce54272580 52 Red = 0xFF0000,
kotakku 0:b1ce54272580 53 /** r = 0, g = 255, b = 0 */
kotakku 0:b1ce54272580 54 Green = 0xFF00,
kotakku 0:b1ce54272580 55 /** r = 0, g = 0, b = 255 */
kotakku 0:b1ce54272580 56 Blue = 0xFF,
kotakku 0:b1ce54272580 57
kotakku 0:b1ce54272580 58 /** r = 255, g = 235, b = 4 */
kotakku 0:b1ce54272580 59 Yellow = 0xFFEB04,
kotakku 0:b1ce54272580 60 /** r = 0, g = 255, b = 255 */
kotakku 0:b1ce54272580 61 Lightblue = 0xFFFF,
kotakku 0:b1ce54272580 62 /** r = 255, g = 0, b = 255 */
kotakku 0:b1ce54272580 63 Purple = 0xFF00FF,
kotakku 0:b1ce54272580 64 Purble = 0xFF00FF,
kotakku 0:b1ce54272580 65
kotakku 0:b1ce54272580 66 /** r = 255, g = 255, b = 255 */
kotakku 0:b1ce54272580 67 White = 0xFFFFFF,
kotakku 0:b1ce54272580 68 /** r = 0, g = 0, b = 0 */
kotakku 0:b1ce54272580 69 Off = 0x00,
kotakku 0:b1ce54272580 70 };
kotakku 0:b1ce54272580 71
kotakku 0:b1ce54272580 72 enum RumbleEnum {
kotakku 0:b1ce54272580 73 RumbleHigh = 0x10,
kotakku 0:b1ce54272580 74 RumbleLow = 0x20,
kotakku 0:b1ce54272580 75 };
kotakku 0:b1ce54272580 76
kotakku 0:b1ce54272580 77 /** This enum is used to read all the different buttons on the different controllers */
kotakku 0:b1ce54272580 78 enum ButtonEnum {
kotakku 0:b1ce54272580 79 /**@{*/
kotakku 0:b1ce54272580 80 /** These buttons are available on all the the controllers */
kotakku 0:b1ce54272580 81 UP = 0,
kotakku 0:b1ce54272580 82 RIGHT = 1,
kotakku 0:b1ce54272580 83 DOWN = 2,
kotakku 0:b1ce54272580 84 LEFT = 3,
kotakku 0:b1ce54272580 85 /**@}*/
kotakku 0:b1ce54272580 86
kotakku 0:b1ce54272580 87 /**@{*/
kotakku 0:b1ce54272580 88 /** Wii buttons */
kotakku 0:b1ce54272580 89 PLUS = 5,
kotakku 0:b1ce54272580 90 TWO = 6,
kotakku 0:b1ce54272580 91 ONE = 7,
kotakku 0:b1ce54272580 92 MINUS = 8,
kotakku 0:b1ce54272580 93 HOME = 9,
kotakku 0:b1ce54272580 94 Z = 10,
kotakku 0:b1ce54272580 95 C = 11,
kotakku 0:b1ce54272580 96 B = 12,
kotakku 0:b1ce54272580 97 A = 13,
kotakku 0:b1ce54272580 98 /**@}*/
kotakku 0:b1ce54272580 99
kotakku 0:b1ce54272580 100 /**@{*/
kotakku 0:b1ce54272580 101 /** These are only available on the Wii U Pro Controller */
kotakku 0:b1ce54272580 102 L = 16,
kotakku 0:b1ce54272580 103 R = 17,
kotakku 0:b1ce54272580 104 ZL = 18,
kotakku 0:b1ce54272580 105 ZR = 19,
kotakku 0:b1ce54272580 106 /**@}*/
kotakku 0:b1ce54272580 107
kotakku 0:b1ce54272580 108 /**@{*/
kotakku 0:b1ce54272580 109 /** PS3 controllers buttons */
kotakku 0:b1ce54272580 110 SELECT = 4,
kotakku 0:b1ce54272580 111 START = 5,
kotakku 0:b1ce54272580 112 L3 = 6,
kotakku 0:b1ce54272580 113 R3 = 7,
kotakku 0:b1ce54272580 114
kotakku 0:b1ce54272580 115 L2 = 8,
kotakku 0:b1ce54272580 116 R2 = 9,
kotakku 0:b1ce54272580 117 L1 = 10,
kotakku 0:b1ce54272580 118 R1 = 11,
kotakku 0:b1ce54272580 119 TRIANGLE = 12,
kotakku 0:b1ce54272580 120 CIRCLE = 13,
kotakku 0:b1ce54272580 121 CROSS = 14,
kotakku 0:b1ce54272580 122 SQUARE = 15,
kotakku 0:b1ce54272580 123
kotakku 0:b1ce54272580 124 PS = 16,
kotakku 0:b1ce54272580 125
kotakku 0:b1ce54272580 126 MOVE = 17, // Covers 12 bits - we only need to read the top 8
kotakku 0:b1ce54272580 127 T = 18, // Covers 12 bits - we only need to read the top 8
kotakku 0:b1ce54272580 128 /**@}*/
kotakku 0:b1ce54272580 129
kotakku 0:b1ce54272580 130 /** PS4 controllers buttons - SHARE and OPTIONS are present instead of SELECT and START */
kotakku 0:b1ce54272580 131 SHARE = 4,
kotakku 0:b1ce54272580 132 OPTIONS = 5,
kotakku 0:b1ce54272580 133 TOUCHPAD = 17,
kotakku 0:b1ce54272580 134 /**@}*/
kotakku 0:b1ce54272580 135
kotakku 0:b1ce54272580 136 /**@{*/
kotakku 0:b1ce54272580 137 /** Xbox buttons */
kotakku 0:b1ce54272580 138 BACK = 4,
kotakku 0:b1ce54272580 139 X = 14,
kotakku 0:b1ce54272580 140 Y = 15,
kotakku 0:b1ce54272580 141 XBOX = 16,
kotakku 0:b1ce54272580 142 SYNC = 17,
kotakku 0:b1ce54272580 143 BLACK = 8, // Available on the original Xbox controller
kotakku 0:b1ce54272580 144 WHITE = 9, // Available on the original Xbox controller
kotakku 0:b1ce54272580 145 /**@}*/
kotakku 0:b1ce54272580 146
kotakku 0:b1ce54272580 147 /** PS Buzz controllers */
kotakku 0:b1ce54272580 148 RED = 0,
kotakku 0:b1ce54272580 149 YELLOW = 1,
kotakku 0:b1ce54272580 150 GREEN = 2,
kotakku 0:b1ce54272580 151 ORANGE = 3,
kotakku 0:b1ce54272580 152 BLUE = 4,
kotakku 0:b1ce54272580 153 /**@}*/
kotakku 0:b1ce54272580 154 };
kotakku 0:b1ce54272580 155
kotakku 0:b1ce54272580 156 /** Joysticks on the PS3 and Xbox controllers. */
kotakku 0:b1ce54272580 157 enum AnalogHatEnum {
kotakku 0:b1ce54272580 158 /** Left joystick x-axis */
kotakku 0:b1ce54272580 159 LeftHatX = 0,
kotakku 0:b1ce54272580 160 /** Left joystick y-axis */
kotakku 0:b1ce54272580 161 LeftHatY = 1,
kotakku 0:b1ce54272580 162 /** Right joystick x-axis */
kotakku 0:b1ce54272580 163 RightHatX = 2,
kotakku 0:b1ce54272580 164 /** Right joystick y-axis */
kotakku 0:b1ce54272580 165 RightHatY = 3,
kotakku 0:b1ce54272580 166 };
kotakku 0:b1ce54272580 167
kotakku 0:b1ce54272580 168 /**
kotakku 0:b1ce54272580 169 * Sensors inside the Sixaxis Dualshock 3, Move controller and PS4 controller.
kotakku 0:b1ce54272580 170 * <B>Note:</B> that the location is shifted 9 when it's connected via USB on the PS3 controller.
kotakku 0:b1ce54272580 171 */
kotakku 0:b1ce54272580 172 enum SensorEnum {
kotakku 0:b1ce54272580 173 /** Accelerometer values */
kotakku 0:b1ce54272580 174 aX = 50, aY = 52, aZ = 54,
kotakku 0:b1ce54272580 175 /** Gyro z-axis */
kotakku 0:b1ce54272580 176 gZ = 56,
kotakku 0:b1ce54272580 177 gX, gY, // These are not available on the PS3 controller
kotakku 0:b1ce54272580 178
kotakku 0:b1ce54272580 179 /** Accelerometer x-axis */
kotakku 0:b1ce54272580 180 aXmove = 28,
kotakku 0:b1ce54272580 181 /** Accelerometer z-axis */
kotakku 0:b1ce54272580 182 aZmove = 30,
kotakku 0:b1ce54272580 183 /** Accelerometer y-axis */
kotakku 0:b1ce54272580 184 aYmove = 32,
kotakku 0:b1ce54272580 185
kotakku 0:b1ce54272580 186 /** Gyro x-axis */
kotakku 0:b1ce54272580 187 gXmove = 40,
kotakku 0:b1ce54272580 188 /** Gyro z-axis */
kotakku 0:b1ce54272580 189 gZmove = 42,
kotakku 0:b1ce54272580 190 /** Gyro y-axis */
kotakku 0:b1ce54272580 191 gYmove = 44,
kotakku 0:b1ce54272580 192
kotakku 0:b1ce54272580 193 /** Temperature sensor */
kotakku 0:b1ce54272580 194 tempMove = 46,
kotakku 0:b1ce54272580 195
kotakku 0:b1ce54272580 196 /** Magnetometer x-axis */
kotakku 0:b1ce54272580 197 mXmove = 47,
kotakku 0:b1ce54272580 198 /** Magnetometer z-axis */
kotakku 0:b1ce54272580 199 mZmove = 49,
kotakku 0:b1ce54272580 200 /** Magnetometer y-axis */
kotakku 0:b1ce54272580 201 mYmove = 50,
kotakku 0:b1ce54272580 202 };
kotakku 0:b1ce54272580 203
kotakku 0:b1ce54272580 204 /** Used to get the angle calculated using the PS3 controller and PS4 controller. */
kotakku 0:b1ce54272580 205 enum AngleEnum {
kotakku 0:b1ce54272580 206 Pitch = 0x01,
kotakku 0:b1ce54272580 207 Roll = 0x02,
kotakku 0:b1ce54272580 208 };
kotakku 0:b1ce54272580 209
kotakku 0:b1ce54272580 210 #endif
kotakku 0:b1ce54272580 211