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/PSBuzz.h@1:da31140f2a1c, 2020-05-02 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
kotakku | 0:b1ce54272580 | 1 | /* Copyright (C) 2014 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 _psbuzz_h_ |
kotakku | 0:b1ce54272580 | 19 | #define _psbuzz_h_ |
kotakku | 0:b1ce54272580 | 20 | |
kotakku | 0:b1ce54272580 | 21 | #include "hiduniversal.h" |
kotakku | 0:b1ce54272580 | 22 | #include "controllerEnums.h" |
kotakku | 0:b1ce54272580 | 23 | |
kotakku | 0:b1ce54272580 | 24 | #define PSBUZZ_VID 0x054C // Sony Corporation |
kotakku | 0:b1ce54272580 | 25 | #define PSBUZZ_PID 0x1000 // PS Buzz Controller |
kotakku | 0:b1ce54272580 | 26 | |
kotakku | 0:b1ce54272580 | 27 | /** Struct used to easily read the different buttons on the controllers */ |
kotakku | 0:b1ce54272580 | 28 | union PSBUZZButtons { |
kotakku | 0:b1ce54272580 | 29 | struct { |
kotakku | 0:b1ce54272580 | 30 | uint8_t red : 1; |
kotakku | 0:b1ce54272580 | 31 | uint8_t yellow : 1; |
kotakku | 0:b1ce54272580 | 32 | uint8_t green : 1; |
kotakku | 0:b1ce54272580 | 33 | uint8_t orange : 1; |
kotakku | 0:b1ce54272580 | 34 | uint8_t blue : 1; |
kotakku | 0:b1ce54272580 | 35 | } __attribute__((packed)) btn[4]; |
kotakku | 0:b1ce54272580 | 36 | uint32_t val : 20; |
kotakku | 0:b1ce54272580 | 37 | } __attribute__((packed)); |
kotakku | 0:b1ce54272580 | 38 | |
kotakku | 0:b1ce54272580 | 39 | /** |
kotakku | 0:b1ce54272580 | 40 | * This class implements support for the PS Buzz controllers via USB. |
kotakku | 0:b1ce54272580 | 41 | * It uses the HIDUniversal class for all the USB communication. |
kotakku | 0:b1ce54272580 | 42 | */ |
kotakku | 0:b1ce54272580 | 43 | class PSBuzz : public HIDUniversal { |
kotakku | 0:b1ce54272580 | 44 | public: |
kotakku | 0:b1ce54272580 | 45 | /** |
kotakku | 0:b1ce54272580 | 46 | * Constructor for the PSBuzz class. |
kotakku | 0:b1ce54272580 | 47 | * @param p Pointer to the USB class instance. |
kotakku | 0:b1ce54272580 | 48 | */ |
kotakku | 0:b1ce54272580 | 49 | PSBuzz(USB *p) : |
kotakku | 0:b1ce54272580 | 50 | HIDUniversal(p) { |
kotakku | 0:b1ce54272580 | 51 | Reset(); |
kotakku | 0:b1ce54272580 | 52 | }; |
kotakku | 0:b1ce54272580 | 53 | |
kotakku | 0:b1ce54272580 | 54 | /** |
kotakku | 0:b1ce54272580 | 55 | * Used to check if a PS Buzz controller is connected. |
kotakku | 0:b1ce54272580 | 56 | * @return Returns true if it is connected. |
kotakku | 0:b1ce54272580 | 57 | */ |
kotakku | 0:b1ce54272580 | 58 | bool connected() { |
kotakku | 0:b1ce54272580 | 59 | return HIDUniversal::isReady() && HIDUniversal::VID == PSBUZZ_VID && HIDUniversal::PID == PSBUZZ_PID; |
kotakku | 0:b1ce54272580 | 60 | }; |
kotakku | 0:b1ce54272580 | 61 | |
kotakku | 0:b1ce54272580 | 62 | /** |
kotakku | 0:b1ce54272580 | 63 | * Used to call your own function when the device is successfully initialized. |
kotakku | 0:b1ce54272580 | 64 | * @param funcOnInit Function to call. |
kotakku | 0:b1ce54272580 | 65 | */ |
kotakku | 0:b1ce54272580 | 66 | void attachOnInit(void (*funcOnInit)(void)) { |
kotakku | 0:b1ce54272580 | 67 | pFuncOnInit = funcOnInit; |
kotakku | 0:b1ce54272580 | 68 | }; |
kotakku | 0:b1ce54272580 | 69 | |
kotakku | 0:b1ce54272580 | 70 | /** @name PS Buzzer Controller functions */ |
kotakku | 0:b1ce54272580 | 71 | /** |
kotakku | 0:b1ce54272580 | 72 | * getButtonPress(ButtonEnum b) will return true as long as the button is held down. |
kotakku | 0:b1ce54272580 | 73 | * |
kotakku | 0:b1ce54272580 | 74 | * While getButtonClick(ButtonEnum b) will only return it once. |
kotakku | 0:b1ce54272580 | 75 | * |
kotakku | 0:b1ce54272580 | 76 | * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b), |
kotakku | 0:b1ce54272580 | 77 | * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b). |
kotakku | 0:b1ce54272580 | 78 | * @param b ::ButtonEnum to read. |
kotakku | 0:b1ce54272580 | 79 | * @param controller The controller to read from. Default to 0. |
kotakku | 0:b1ce54272580 | 80 | * @return getButtonPress(ButtonEnum b) will return a true as long as a button is held down, while getButtonClick(ButtonEnum b) will return true once for each button press. |
kotakku | 0:b1ce54272580 | 81 | */ |
kotakku | 0:b1ce54272580 | 82 | bool getButtonPress(ButtonEnum b, uint8_t controller = 0); |
kotakku | 0:b1ce54272580 | 83 | bool getButtonClick(ButtonEnum b, uint8_t controller = 0); |
kotakku | 0:b1ce54272580 | 84 | /**@}*/ |
kotakku | 0:b1ce54272580 | 85 | /** @name PS Buzzer Controller functions */ |
kotakku | 0:b1ce54272580 | 86 | /** |
kotakku | 0:b1ce54272580 | 87 | * Set LED value without using ::LEDEnum. |
kotakku | 0:b1ce54272580 | 88 | * @param value See: ::LEDEnum. |
kotakku | 0:b1ce54272580 | 89 | */ |
kotakku | 0:b1ce54272580 | 90 | /** |
kotakku | 0:b1ce54272580 | 91 | * Set LED values directly. |
kotakku | 0:b1ce54272580 | 92 | * @param value Used to set whenever the LED should be on or off |
kotakku | 0:b1ce54272580 | 93 | * @param controller The controller to control. Defaults to 0. |
kotakku | 0:b1ce54272580 | 94 | */ |
kotakku | 0:b1ce54272580 | 95 | void setLedRaw(bool value, uint8_t controller = 0); |
kotakku | 0:b1ce54272580 | 96 | |
kotakku | 0:b1ce54272580 | 97 | /** Turn all LEDs off. */ |
kotakku | 0:b1ce54272580 | 98 | void setLedOffAll() { |
kotakku | 0:b1ce54272580 | 99 | for (uint8_t i = 1; i < 4; i++) // Skip first as it will be set in setLedRaw |
kotakku | 0:b1ce54272580 | 100 | ledState[i] = false; // Just an easy way to set all four off at the same time |
kotakku | 0:b1ce54272580 | 101 | setLedRaw(false); // Turn the LED off, on all four controllers |
kotakku | 0:b1ce54272580 | 102 | }; |
kotakku | 0:b1ce54272580 | 103 | |
kotakku | 0:b1ce54272580 | 104 | /** |
kotakku | 0:b1ce54272580 | 105 | * Turn the LED off on a specific controller. |
kotakku | 0:b1ce54272580 | 106 | * @param controller The controller to turn off. Defaults to 0. |
kotakku | 0:b1ce54272580 | 107 | */ |
kotakku | 0:b1ce54272580 | 108 | void setLedOff(uint8_t controller = 0) { |
kotakku | 0:b1ce54272580 | 109 | setLedRaw(false, controller); |
kotakku | 0:b1ce54272580 | 110 | }; |
kotakku | 0:b1ce54272580 | 111 | |
kotakku | 0:b1ce54272580 | 112 | |
kotakku | 0:b1ce54272580 | 113 | /** Turn all LEDs on. */ |
kotakku | 0:b1ce54272580 | 114 | void setLedOnAll() { |
kotakku | 0:b1ce54272580 | 115 | for (uint8_t i = 1; i < 4; i++) // Skip first as it will be set in setLedRaw |
kotakku | 0:b1ce54272580 | 116 | ledState[i] = true; // Just an easy way to set all four off at the same time |
kotakku | 0:b1ce54272580 | 117 | setLedRaw(true); // Turn the LED on, on all four controllers |
kotakku | 0:b1ce54272580 | 118 | }; |
kotakku | 0:b1ce54272580 | 119 | |
kotakku | 0:b1ce54272580 | 120 | /** |
kotakku | 0:b1ce54272580 | 121 | * Turn the LED on on a specific controller. |
kotakku | 0:b1ce54272580 | 122 | * @param controller The controller to turn off. Defaults to 0. |
kotakku | 0:b1ce54272580 | 123 | */ |
kotakku | 0:b1ce54272580 | 124 | void setLedOn(uint8_t controller = 0) { |
kotakku | 0:b1ce54272580 | 125 | setLedRaw(true, controller); |
kotakku | 0:b1ce54272580 | 126 | }; |
kotakku | 0:b1ce54272580 | 127 | |
kotakku | 0:b1ce54272580 | 128 | /** |
kotakku | 0:b1ce54272580 | 129 | * Toggle the LED on a specific controller. |
kotakku | 0:b1ce54272580 | 130 | * @param controller The controller to turn off. Defaults to 0. |
kotakku | 0:b1ce54272580 | 131 | */ |
kotakku | 0:b1ce54272580 | 132 | void setLedToggle(uint8_t controller = 0) { |
kotakku | 0:b1ce54272580 | 133 | setLedRaw(!ledState[controller], controller); |
kotakku | 0:b1ce54272580 | 134 | }; |
kotakku | 0:b1ce54272580 | 135 | /**@}*/ |
kotakku | 0:b1ce54272580 | 136 | |
kotakku | 0:b1ce54272580 | 137 | protected: |
kotakku | 0:b1ce54272580 | 138 | /** @name HIDUniversal implementation */ |
kotakku | 0:b1ce54272580 | 139 | /** |
kotakku | 0:b1ce54272580 | 140 | * Used to parse USB HID data. |
kotakku | 0:b1ce54272580 | 141 | * @param hid Pointer to the HID class. |
kotakku | 0:b1ce54272580 | 142 | * @param is_rpt_id Only used for Hubs. |
kotakku | 0:b1ce54272580 | 143 | * @param len The length of the incoming data. |
kotakku | 0:b1ce54272580 | 144 | * @param buf Pointer to the data buffer. |
kotakku | 0:b1ce54272580 | 145 | */ |
kotakku | 0:b1ce54272580 | 146 | void ParseHIDData(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf); |
kotakku | 0:b1ce54272580 | 147 | |
kotakku | 0:b1ce54272580 | 148 | /** |
kotakku | 0:b1ce54272580 | 149 | * Called when a device is successfully initialized. |
kotakku | 0:b1ce54272580 | 150 | * Use attachOnInit(void (*funcOnInit)(void)) to call your own function. |
kotakku | 0:b1ce54272580 | 151 | * This is useful for instance if you want to set the LEDs in a specific way. |
kotakku | 0:b1ce54272580 | 152 | */ |
kotakku | 0:b1ce54272580 | 153 | uint8_t OnInitSuccessful(); |
kotakku | 0:b1ce54272580 | 154 | /**@}*/ |
kotakku | 0:b1ce54272580 | 155 | |
kotakku | 0:b1ce54272580 | 156 | /** Used to reset the different buffers to their default values */ |
kotakku | 0:b1ce54272580 | 157 | void Reset() { |
kotakku | 0:b1ce54272580 | 158 | psbuzzButtons.val = 0; |
kotakku | 0:b1ce54272580 | 159 | oldButtonState.val = 0; |
kotakku | 0:b1ce54272580 | 160 | buttonClickState.val = 0; |
kotakku | 0:b1ce54272580 | 161 | for (uint8_t i = 0; i < sizeof(ledState); i++) |
kotakku | 0:b1ce54272580 | 162 | ledState[i] = 0; |
kotakku | 0:b1ce54272580 | 163 | }; |
kotakku | 0:b1ce54272580 | 164 | |
kotakku | 0:b1ce54272580 | 165 | /** @name USBDeviceConfig implementation */ |
kotakku | 0:b1ce54272580 | 166 | /** |
kotakku | 0:b1ce54272580 | 167 | * Used by the USB core to check what this driver support. |
kotakku | 0:b1ce54272580 | 168 | * @param vid The device's VID. |
kotakku | 0:b1ce54272580 | 169 | * @param pid The device's PID. |
kotakku | 0:b1ce54272580 | 170 | * @return Returns true if the device's VID and PID matches this driver. |
kotakku | 0:b1ce54272580 | 171 | */ |
kotakku | 0:b1ce54272580 | 172 | virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) { |
kotakku | 0:b1ce54272580 | 173 | return (vid == PSBUZZ_VID && pid == PSBUZZ_PID); |
kotakku | 0:b1ce54272580 | 174 | }; |
kotakku | 0:b1ce54272580 | 175 | /**@}*/ |
kotakku | 0:b1ce54272580 | 176 | |
kotakku | 0:b1ce54272580 | 177 | private: |
kotakku | 0:b1ce54272580 | 178 | void (*pFuncOnInit)(void); // Pointer to function called in onInit() |
kotakku | 0:b1ce54272580 | 179 | |
kotakku | 0:b1ce54272580 | 180 | void PSBuzz_Command(uint8_t *data, uint16_t nbytes); |
kotakku | 0:b1ce54272580 | 181 | |
kotakku | 0:b1ce54272580 | 182 | PSBUZZButtons psbuzzButtons, oldButtonState, buttonClickState; |
kotakku | 0:b1ce54272580 | 183 | bool ledState[4]; |
kotakku | 0:b1ce54272580 | 184 | }; |
kotakku | 0:b1ce54272580 | 185 | #endif |
kotakku | 0:b1ce54272580 | 186 |