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");
        }
    }
}

USB_Host/PS3Enums.h

Committer:
robo_ichinoseki_a
Date:
2020-05-02
Revision:
1:da31140f2a1c
Parent:
0:b1ce54272580

File content as of revision 1:da31140f2a1c:

/* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.

 This software may be distributed and modified under the terms of the GNU
 General Public License version 2 (GPL2) as published by the Free Software
 Foundation and appearing in the file GPL2.TXT included in the packaging of
 this file. Please note that GPL2 Section 2[b] requires that all works based
 on this software must also be made publicly available under the terms of
 the GPL2 ("Copyleft").

 Contact information
 -------------------

 Kristian Lauszus, TKJ Electronics
 Web      :  http://www.tkjelectronics.com
 e-mail   :  kristianl@tkjelectronics.com
 */

#ifndef _ps3enums_h
#define _ps3enums_h

#include "controllerEnums.h"

/** Size of the output report buffer for the Dualshock and Navigation controllers */
#define PS3_REPORT_BUFFER_SIZE  48

/** Report buffer for all PS3 commands */
const uint8_t PS3_REPORT_BUFFER[PS3_REPORT_BUFFER_SIZE] PROGMEM = {
        0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00,
        0xff, 0x27, 0x10, 0x00, 0x32,
        0xff, 0x27, 0x10, 0x00, 0x32,
        0xff, 0x27, 0x10, 0x00, 0x32,
        0xff, 0x27, 0x10, 0x00, 0x32,
        0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

/** Size of the output report buffer for the Move Controller */
#define MOVE_REPORT_BUFFER_SIZE 7

/** Used to set the LEDs on the controllers */
const uint8_t PS3_LEDS[] PROGMEM = {
        0x00, // OFF
        0x01, // LED1
        0x02, // LED2
        0x04, // LED3
        0x08, // LED4

        0x09, // LED5
        0x0A, // LED6
        0x0C, // LED7
        0x0D, // LED8
        0x0E, // LED9
        0x0F, // LED10
};

/**
 * Buttons on the controllers.
 * <B>Note:</B> that the location is shifted 9 when it's connected via USB.
 */
const uint32_t PS3_BUTTONS[] PROGMEM = {
        0x10, // UP
        0x20, // RIGHT
        0x40, // DOWN
        0x80, // LEFT

        0x01, // SELECT
        0x08, // START
        0x02, // L3
        0x04, // R3

        0x0100, // L2
        0x0200, // R2
        0x0400, // L1
        0x0800, // R1

        0x1000, // TRIANGLE
        0x2000, // CIRCLE
        0x4000, // CROSS
        0x8000, // SQUARE

        0x010000, // PS
        0x080000, // MOVE - covers 12 bits - we only need to read the top 8
        0x100000, // T - covers 12 bits - we only need to read the top 8
};

/**
 * Analog buttons on the controllers.
 * <B>Note:</B> that the location is shifted 9 when it's connected via USB.
 */
const uint8_t PS3_ANALOG_BUTTONS[] PROGMEM = {
        23, // UP_ANALOG
        24, // RIGHT_ANALOG
        25, // DOWN_ANALOG
        26, // LEFT_ANALOG
        0, 0, 0, 0, // Skip SELECT, L3, R3 and START

        27, // L2_ANALOG
        28, // R2_ANALOG
        29, // L1_ANALOG
        30, // R1_ANALOG
        31, // TRIANGLE_ANALOG
        32, // CIRCLE_ANALOG
        33, // CROSS_ANALOG
        34, // SQUARE_ANALOG
        0, 0, // Skip PS and MOVE

        // Playstation Move Controller
        15, // T_ANALOG - Both at byte 14 (last reading) and byte 15 (current reading)
};

enum StatusEnum {
        // Note that the location is shifted 9 when it's connected via USB
        // Byte location | bit location
        Plugged = (38 << 8) | 0x02,
        Unplugged = (38 << 8) | 0x03,

        Charging = (39 << 8) | 0xEE,
        NotCharging = (39 << 8) | 0xF1,
        Shutdown = (39 << 8) | 0x01,
        Dying = (39 << 8) | 0x02,
        Low = (39 << 8) | 0x03,
        High = (39 << 8) | 0x04,
        Full = (39 << 8) | 0x05,

        MoveCharging = (21 << 8) | 0xEE,
        MoveNotCharging = (21 << 8) | 0xF1,
        MoveShutdown = (21 << 8) | 0x01,
        MoveDying = (21 << 8) | 0x02,
        MoveLow = (21 << 8) | 0x03,
        MoveHigh = (21 << 8) | 0x04,
        MoveFull = (21 << 8) | 0x05,

        CableRumble = (40 << 8) | 0x10, // Operating by USB and rumble is turned on
        Cable = (40 << 8) | 0x12, // Operating by USB and rumble is turned off
        BluetoothRumble = (40 << 8) | 0x14, // Operating by Bluetooth and rumble is turned on
        Bluetooth = (40 << 8) | 0x16, // Operating by Bluetooth and rumble is turned off
};

#endif