Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: UsbHostMAX3421E_Hello
PS4USB.h@0:84353c479782, 2020-07-12 (annotated)
- Committer:
- hudakz
- Date:
- Sun Jul 12 20:39:26 2020 +0000
- Revision:
- 0:84353c479782
- Child:
- 1:2263e77400e9
MAX3421E-based USB Host Shield Library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:84353c479782 | 1 | /* Copyright (C) 2014 Kristian Lauszus, TKJ Electronics. All rights reserved. |
hudakz | 0:84353c479782 | 2 | |
hudakz | 0:84353c479782 | 3 | This software may be distributed and modified under the terms of the GNU |
hudakz | 0:84353c479782 | 4 | General Public License version 2 (GPL2) as published by the Free Software |
hudakz | 0:84353c479782 | 5 | Foundation and appearing in the file GPL2.TXT included in the packaging of |
hudakz | 0:84353c479782 | 6 | this file. Please note that GPL2 Section 2[b] requires that all works based |
hudakz | 0:84353c479782 | 7 | on this software must also be made publicly available under the terms of |
hudakz | 0:84353c479782 | 8 | the GPL2 ("Copyleft"). |
hudakz | 0:84353c479782 | 9 | |
hudakz | 0:84353c479782 | 10 | Contact information |
hudakz | 0:84353c479782 | 11 | ------------------- |
hudakz | 0:84353c479782 | 12 | |
hudakz | 0:84353c479782 | 13 | Kristian Lauszus, TKJ Electronics |
hudakz | 0:84353c479782 | 14 | Web : http://www.tkjelectronics.com |
hudakz | 0:84353c479782 | 15 | e-mail : kristianl@tkjelectronics.com |
hudakz | 0:84353c479782 | 16 | */ |
hudakz | 0:84353c479782 | 17 | |
hudakz | 0:84353c479782 | 18 | #ifndef _ps4usb_h_ |
hudakz | 0:84353c479782 | 19 | #define _ps4usb_h_ |
hudakz | 0:84353c479782 | 20 | |
hudakz | 0:84353c479782 | 21 | #include "hiduniversal.h" |
hudakz | 0:84353c479782 | 22 | #include "PS4Parser.h" |
hudakz | 0:84353c479782 | 23 | |
hudakz | 0:84353c479782 | 24 | #define PS4_VID 0x054C // Sony Corporation |
hudakz | 0:84353c479782 | 25 | #define PS4_PID 0x05C4 // PS4 Controller |
hudakz | 0:84353c479782 | 26 | #define PS4_PID_SLIM 0x09CC // PS4 Slim Controller |
hudakz | 0:84353c479782 | 27 | |
hudakz | 0:84353c479782 | 28 | /** |
hudakz | 0:84353c479782 | 29 | * This class implements support for the PS4 controller via USB. |
hudakz | 0:84353c479782 | 30 | * It uses the HIDUniversal class for all the USB communication. |
hudakz | 0:84353c479782 | 31 | */ |
hudakz | 0:84353c479782 | 32 | class PS4USB : public HIDUniversal, public PS4Parser { |
hudakz | 0:84353c479782 | 33 | public: |
hudakz | 0:84353c479782 | 34 | /** |
hudakz | 0:84353c479782 | 35 | * Constructor for the PS4USB class. |
hudakz | 0:84353c479782 | 36 | * @param p Pointer to the USB class instance. |
hudakz | 0:84353c479782 | 37 | */ |
hudakz | 0:84353c479782 | 38 | PS4USB(USB *p) : |
hudakz | 0:84353c479782 | 39 | HIDUniversal(p) { |
hudakz | 0:84353c479782 | 40 | PS4Parser::Reset(); |
hudakz | 0:84353c479782 | 41 | }; |
hudakz | 0:84353c479782 | 42 | |
hudakz | 0:84353c479782 | 43 | /** |
hudakz | 0:84353c479782 | 44 | * Used to check if a PS4 controller is connected. |
hudakz | 0:84353c479782 | 45 | * @return Returns true if it is connected. |
hudakz | 0:84353c479782 | 46 | */ |
hudakz | 0:84353c479782 | 47 | bool connected() { |
hudakz | 0:84353c479782 | 48 | return HIDUniversal::isReady() && HIDUniversal::VID == PS4_VID && (HIDUniversal::PID == PS4_PID || HIDUniversal::PID == PS4_PID_SLIM); |
hudakz | 0:84353c479782 | 49 | }; |
hudakz | 0:84353c479782 | 50 | |
hudakz | 0:84353c479782 | 51 | /** |
hudakz | 0:84353c479782 | 52 | * Used to call your own function when the device is successfully initialized. |
hudakz | 0:84353c479782 | 53 | * @param funcOnInit Function to call. |
hudakz | 0:84353c479782 | 54 | */ |
hudakz | 0:84353c479782 | 55 | void attachOnInit(void (*funcOnInit)(void)) { |
hudakz | 0:84353c479782 | 56 | pFuncOnInit = funcOnInit; |
hudakz | 0:84353c479782 | 57 | }; |
hudakz | 0:84353c479782 | 58 | |
hudakz | 0:84353c479782 | 59 | protected: |
hudakz | 0:84353c479782 | 60 | /** @name HIDUniversal implementation */ |
hudakz | 0:84353c479782 | 61 | /** |
hudakz | 0:84353c479782 | 62 | * Used to parse USB HID data. |
hudakz | 0:84353c479782 | 63 | * @param hid Pointer to the HID class. |
hudakz | 0:84353c479782 | 64 | * @param is_rpt_id Only used for Hubs. |
hudakz | 0:84353c479782 | 65 | * @param len The length of the incoming data. |
hudakz | 0:84353c479782 | 66 | * @param buf Pointer to the data buffer. |
hudakz | 0:84353c479782 | 67 | */ |
hudakz | 0:84353c479782 | 68 | virtual void ParseHIDData(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) { |
hudakz | 0:84353c479782 | 69 | if (HIDUniversal::VID == PS4_VID && (HIDUniversal::PID == PS4_PID || HIDUniversal::PID == PS4_PID_SLIM)) |
hudakz | 0:84353c479782 | 70 | PS4Parser::Parse(len, buf); |
hudakz | 0:84353c479782 | 71 | }; |
hudakz | 0:84353c479782 | 72 | |
hudakz | 0:84353c479782 | 73 | /** |
hudakz | 0:84353c479782 | 74 | * Called when a device is successfully initialized. |
hudakz | 0:84353c479782 | 75 | * Use attachOnInit(void (*funcOnInit)(void)) to call your own function. |
hudakz | 0:84353c479782 | 76 | * This is useful for instance if you want to set the LEDs in a specific way. |
hudakz | 0:84353c479782 | 77 | */ |
hudakz | 0:84353c479782 | 78 | virtual uint8_t OnInitSuccessful() { |
hudakz | 0:84353c479782 | 79 | if (HIDUniversal::VID == PS4_VID && (HIDUniversal::PID == PS4_PID || HIDUniversal::PID == PS4_PID_SLIM)) { |
hudakz | 0:84353c479782 | 80 | PS4Parser::Reset(); |
hudakz | 0:84353c479782 | 81 | if (pFuncOnInit) |
hudakz | 0:84353c479782 | 82 | pFuncOnInit(); // Call the user function |
hudakz | 0:84353c479782 | 83 | else |
hudakz | 0:84353c479782 | 84 | setLed(Blue); |
hudakz | 0:84353c479782 | 85 | }; |
hudakz | 0:84353c479782 | 86 | return 0; |
hudakz | 0:84353c479782 | 87 | }; |
hudakz | 0:84353c479782 | 88 | /**@}*/ |
hudakz | 0:84353c479782 | 89 | |
hudakz | 0:84353c479782 | 90 | /** @name PS4Parser implementation */ |
hudakz | 0:84353c479782 | 91 | virtual void sendOutputReport(PS4Output *output) { // Source: https://github.com/chrippa/ds4drv |
hudakz | 0:84353c479782 | 92 | uint8_t buf[32]; |
hudakz | 0:84353c479782 | 93 | memset(buf, 0, sizeof(buf)); |
hudakz | 0:84353c479782 | 94 | |
hudakz | 0:84353c479782 | 95 | buf[0] = 0x05; // Report ID |
hudakz | 0:84353c479782 | 96 | buf[1]= 0xFF; |
hudakz | 0:84353c479782 | 97 | |
hudakz | 0:84353c479782 | 98 | buf[4] = output->smallRumble; // Small Rumble |
hudakz | 0:84353c479782 | 99 | buf[5] = output->bigRumble; // Big rumble |
hudakz | 0:84353c479782 | 100 | |
hudakz | 0:84353c479782 | 101 | buf[6] = output->r; // Red |
hudakz | 0:84353c479782 | 102 | buf[7] = output->g; // Green |
hudakz | 0:84353c479782 | 103 | buf[8] = output->b; // Blue |
hudakz | 0:84353c479782 | 104 | |
hudakz | 0:84353c479782 | 105 | buf[9] = output->flashOn; // Time to flash bright (255 = 2.5 seconds) |
hudakz | 0:84353c479782 | 106 | buf[10] = output->flashOff; // Time to flash dark (255 = 2.5 seconds) |
hudakz | 0:84353c479782 | 107 | |
hudakz | 0:84353c479782 | 108 | output->reportChanged = false; |
hudakz | 0:84353c479782 | 109 | |
hudakz | 0:84353c479782 | 110 | // The PS4 console actually set the four last bytes to a CRC32 checksum, but it seems like it is actually not needed |
hudakz | 0:84353c479782 | 111 | |
hudakz | 0:84353c479782 | 112 | pUsb->outTransfer(bAddress, epInfo[epInterruptOutIndex].epAddr, sizeof(buf), buf); |
hudakz | 0:84353c479782 | 113 | }; |
hudakz | 0:84353c479782 | 114 | /**@}*/ |
hudakz | 0:84353c479782 | 115 | |
hudakz | 0:84353c479782 | 116 | /** @name USBDeviceConfig implementation */ |
hudakz | 0:84353c479782 | 117 | /** |
hudakz | 0:84353c479782 | 118 | * Used by the USB core to check what this driver support. |
hudakz | 0:84353c479782 | 119 | * @param vid The device's VID. |
hudakz | 0:84353c479782 | 120 | * @param pid The device's PID. |
hudakz | 0:84353c479782 | 121 | * @return Returns true if the device's VID and PID matches this driver. |
hudakz | 0:84353c479782 | 122 | */ |
hudakz | 0:84353c479782 | 123 | virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) { |
hudakz | 0:84353c479782 | 124 | return (vid == PS4_VID && (pid == PS4_PID || HIDUniversal::PID == PS4_PID_SLIM)); |
hudakz | 0:84353c479782 | 125 | }; |
hudakz | 0:84353c479782 | 126 | /**@}*/ |
hudakz | 0:84353c479782 | 127 | |
hudakz | 0:84353c479782 | 128 | private: |
hudakz | 0:84353c479782 | 129 | void (*pFuncOnInit)(void); // Pointer to function called in onInit() |
hudakz | 0:84353c479782 | 130 | }; |
hudakz | 0:84353c479782 | 131 | #endif |