Library to use Arduino USB host shield on mbed
ArduinoのUSB Host Shield 2.0をmbedで使えるようにしたライブラリです。
大体のコードがArduinoからそのまま移植可能です。
Arduino UNOやMega用のホストシールド以外にもミニサイズのホストシールドでも使用可能です
シールドについて
3.3VのI/O用にシールドの改造が必要になりますがネット上に記事がたくさんあるのでそちらを参考にしてください
接続例
使い方
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/PS4Parser.cpp
- Committer:
- robo_ichinoseki_a
- Date:
- 2020-05-02
- Revision:
- 1:da31140f2a1c
- Parent:
- 0:b1ce54272580
File content as of revision 1:da31140f2a1c:
/* Copyright (C) 2014 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 */ #include "PS4Parser.h" enum DPADEnum { DPAD_UP = 0x0, DPAD_UP_RIGHT = 0x1, DPAD_RIGHT = 0x2, DPAD_RIGHT_DOWN = 0x3, DPAD_DOWN = 0x4, DPAD_DOWN_LEFT = 0x5, DPAD_LEFT = 0x6, DPAD_LEFT_UP = 0x7, DPAD_OFF = 0x8, }; // To enable serial debugging see "settings.h" //#define PRINTREPORT // Uncomment to print the report send by the PS4 Controller bool PS4Parser::checkDpad(ButtonEnum b) { switch (b) { case UP: return ps4Data.btn.dpad == DPAD_LEFT_UP || ps4Data.btn.dpad == DPAD_UP || ps4Data.btn.dpad == DPAD_UP_RIGHT; case RIGHT: return ps4Data.btn.dpad == DPAD_UP_RIGHT || ps4Data.btn.dpad == DPAD_RIGHT || ps4Data.btn.dpad == DPAD_RIGHT_DOWN; case DOWN: return ps4Data.btn.dpad == DPAD_RIGHT_DOWN || ps4Data.btn.dpad == DPAD_DOWN || ps4Data.btn.dpad == DPAD_DOWN_LEFT; case LEFT: return ps4Data.btn.dpad == DPAD_DOWN_LEFT || ps4Data.btn.dpad == DPAD_LEFT || ps4Data.btn.dpad == DPAD_LEFT_UP; default: return false; } } bool PS4Parser::getButtonPress(ButtonEnum b) { if (b <= LEFT) // Dpad return checkDpad(b); else return ps4Data.btn.val & (1UL << pgm_read_byte(&PS4_BUTTONS[(uint8_t)b])); } bool PS4Parser::getButtonClick(ButtonEnum b) { uint32_t mask = 1UL << pgm_read_byte(&PS4_BUTTONS[(uint8_t)b]); bool click = buttonClickState.val & mask; buttonClickState.val &= ~mask; // Clear "click" event return click; } uint8_t PS4Parser::getAnalogButton(ButtonEnum b) { if (b == L2) // These are the only analog buttons on the controller return ps4Data.trigger[0]; else if (b == R2) return ps4Data.trigger[1]; return 0; } uint8_t PS4Parser::getAnalogHat(AnalogHatEnum a) { return ps4Data.hatValue[(uint8_t)a]; } void PS4Parser::Parse(uint8_t len, uint8_t *buf) { if (len > 1 && buf) { #ifdef PRINTREPORT Notify(PSTR("\r\n"), 0x80); for (uint8_t i = 0; i < len; i++) { D_PrintHex<uint8_t > (buf[i], 0x80); Notify(PSTR(" "), 0x80); } #endif if (buf[0] == 0x01) // Check report ID memcpy(&ps4Data, buf + 1, min((uint8_t)(len - 1), MFK_CASTUINT8T sizeof(ps4Data))); else if (buf[0] == 0x11) { // This report is send via Bluetooth, it has an offset of 2 compared to the USB data if (len < 4) { #ifdef DEBUG_USB_HOST Notify(PSTR("\r\nReport is too short: "), 0x80); D_PrintHex<uint8_t > (len, 0x80); #endif return; } memcpy(&ps4Data, buf + 3, min((uint8_t)(len - 3), MFK_CASTUINT8T sizeof(ps4Data))); } else { #ifdef DEBUG_USB_HOST Notify(PSTR("\r\nUnknown report id: "), 0x80); D_PrintHex<uint8_t > (buf[0], 0x80); #endif return; } if (ps4Data.btn.val != oldButtonState.val) { // Check if anything has changed buttonClickState.val = ps4Data.btn.val & ~oldButtonState.val; // Update click state variable oldButtonState.val = ps4Data.btn.val; // The DPAD buttons does not set the different bits, but set a value corresponding to the buttons pressed, we will simply set the bits ourself uint8_t newDpad = 0; if (checkDpad(UP)) newDpad |= 1 << UP; if (checkDpad(RIGHT)) newDpad |= 1 << RIGHT; if (checkDpad(DOWN)) newDpad |= 1 << DOWN; if (checkDpad(LEFT)) newDpad |= 1 << LEFT; if (newDpad != oldDpad) { buttonClickState.dpad = newDpad & ~oldDpad; // Override values oldDpad = newDpad; } } } if (ps4Output.reportChanged) sendOutputReport(&ps4Output); // Send output report } void PS4Parser::Reset() { uint8_t i; for (i = 0; i < sizeof(ps4Data.hatValue); i++) ps4Data.hatValue[i] = 127; // Center value ps4Data.btn.val = 0; oldButtonState.val = 0; for (i = 0; i < sizeof(ps4Data.trigger); i++) ps4Data.trigger[i] = 0; for (i = 0; i < sizeof(ps4Data.xy)/sizeof(ps4Data.xy[0]); i++) { for (uint8_t j = 0; j < sizeof(ps4Data.xy[0].finger)/sizeof(ps4Data.xy[0].finger[0]); j++) ps4Data.xy[i].finger[j].touching = 1; // The bit is cleared if the finger is touching the touchpad } ps4Data.btn.dpad = DPAD_OFF; oldButtonState.dpad = DPAD_OFF; buttonClickState.dpad = 0; oldDpad = 0; ps4Output.bigRumble = ps4Output.smallRumble = 0; ps4Output.r = ps4Output.g = ps4Output.b = 0; ps4Output.flashOn = ps4Output.flashOff = 0; ps4Output.reportChanged = false; };