2014-01-18 22:36:01 +01:00
|
|
|
/* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _ps4usb_h_
|
|
|
|
#define _ps4usb_h_
|
|
|
|
|
|
|
|
#include "hiduniversal.h"
|
|
|
|
#include "PS4Parser.h"
|
|
|
|
|
2014-02-03 23:06:27 +01:00
|
|
|
#define PS4_VID 0x054C // Sony Corporation
|
|
|
|
#define PS4_PID 0x05C4 // PS4 Controller
|
2014-01-18 22:36:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class implements support for the PS4 controller via USB.
|
|
|
|
* It uses the HIDUniversal class for all the USB communication.
|
|
|
|
*/
|
|
|
|
class PS4USB : public HIDUniversal, public PS4Parser {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Constructor for the PS4USB class.
|
2016-01-19 16:34:45 +01:00
|
|
|
* @param p Pointer to the USBHost class instance.
|
2014-01-18 22:36:01 +01:00
|
|
|
*/
|
2016-01-19 16:34:45 +01:00
|
|
|
PS4USB(USBHost *p) :
|
2014-01-18 22:36:01 +01:00
|
|
|
HIDUniversal(p) {
|
|
|
|
PS4Parser::Reset();
|
|
|
|
};
|
|
|
|
|
2014-01-19 16:58:29 +01:00
|
|
|
/**
|
|
|
|
* Used to check if a PS4 controller is connected.
|
|
|
|
* @return Returns true if it is connected.
|
|
|
|
*/
|
|
|
|
bool connected() {
|
|
|
|
return HIDUniversal::isReady() && HIDUniversal::VID == PS4_VID && HIDUniversal::PID == PS4_PID;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to call your own function when the device is successfully initialized.
|
|
|
|
* @param funcOnInit Function to call.
|
|
|
|
*/
|
|
|
|
void attachOnInit(void (*funcOnInit)(void)) {
|
|
|
|
pFuncOnInit = funcOnInit;
|
|
|
|
};
|
|
|
|
|
|
|
|
protected:
|
2014-01-18 22:36:01 +01:00
|
|
|
/** @name HIDUniversal implementation */
|
|
|
|
/**
|
|
|
|
* Used to parse USB HID data.
|
|
|
|
* @param hid Pointer to the HID class.
|
|
|
|
* @param is_rpt_id Only used for Hubs.
|
|
|
|
* @param len The length of the incoming data.
|
|
|
|
* @param buf Pointer to the data buffer.
|
|
|
|
*/
|
2016-01-16 20:01:11 +01:00
|
|
|
virtual void ParseHIDData(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {
|
2014-01-18 22:36:01 +01:00
|
|
|
if (HIDUniversal::VID == PS4_VID && HIDUniversal::PID == PS4_PID)
|
|
|
|
PS4Parser::Parse(len, buf);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a device is successfully initialized.
|
|
|
|
* Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
|
|
|
|
* This is useful for instance if you want to set the LEDs in a specific way.
|
|
|
|
*/
|
|
|
|
virtual uint8_t OnInitSuccessful() {
|
2014-02-17 00:21:21 +01:00
|
|
|
if (HIDUniversal::VID == PS4_VID && HIDUniversal::PID == PS4_PID) {
|
|
|
|
PS4Parser::Reset();
|
|
|
|
if (pFuncOnInit)
|
|
|
|
pFuncOnInit(); // Call the user function
|
|
|
|
else
|
|
|
|
setLed(Blue);
|
|
|
|
};
|
2014-01-18 22:36:01 +01:00
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
/**@}*/
|
|
|
|
|
2014-02-17 00:21:21 +01:00
|
|
|
/** @name PS4Parser implementation */
|
|
|
|
virtual void sendOutputReport(PS4Output *output) { // Source: https://github.com/chrippa/ds4drv
|
|
|
|
uint8_t buf[32];
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
|
|
|
|
buf[0] = 0x05; // Report ID
|
|
|
|
buf[1]= 0xFF;
|
|
|
|
|
2014-02-17 17:39:15 +01:00
|
|
|
buf[4] = output->smallRumble; // Small Rumble
|
|
|
|
buf[5] = output->bigRumble; // Big rumble
|
2014-02-17 00:21:21 +01:00
|
|
|
|
|
|
|
buf[6] = output->r; // Red
|
|
|
|
buf[7] = output->g; // Green
|
|
|
|
buf[8] = output->b; // Blue
|
|
|
|
|
|
|
|
buf[9] = output->flashOn; // Time to flash bright (255 = 2.5 seconds)
|
|
|
|
buf[10] = output->flashOff; // Time to flash dark (255 = 2.5 seconds)
|
|
|
|
|
|
|
|
output->reportChanged = false;
|
|
|
|
|
|
|
|
// The PS4 console actually set the four last bytes to a CRC32 checksum, but it seems like it is actually not needed
|
|
|
|
|
|
|
|
pUsb->outTransfer(bAddress, epInfo[ hidInterfaces[0].epIndex[epInterruptOutIndex] ].epAddr, sizeof(buf), buf);
|
|
|
|
};
|
|
|
|
/**@}*/
|
|
|
|
|
2014-07-01 13:17:56 +02:00
|
|
|
/** @name USBDeviceConfig implementation */
|
|
|
|
/**
|
|
|
|
* Used by the USB core to check what this driver support.
|
|
|
|
* @param vid The device's VID.
|
|
|
|
* @param pid The device's PID.
|
|
|
|
* @return Returns true if the device's VID and PID matches this driver.
|
|
|
|
*/
|
2015-02-19 09:14:39 +01:00
|
|
|
virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
|
2014-07-01 13:17:56 +02:00
|
|
|
return (vid == PS4_VID && pid == PS4_PID);
|
|
|
|
};
|
|
|
|
/**@}*/
|
|
|
|
|
2014-02-17 00:50:02 +01:00
|
|
|
private:
|
2014-01-18 22:36:01 +01:00
|
|
|
void (*pFuncOnInit)(void); // Pointer to function called in onInit()
|
|
|
|
};
|
2015-03-12 22:27:21 +01:00
|
|
|
#endif
|