2012-05-26 01:48:12 +02:00
|
|
|
/* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
|
2013-03-28 09:46:43 +01:00
|
|
|
|
2012-05-26 01:48:12 +02:00
|
|
|
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").
|
2013-03-28 09:46:43 +01:00
|
|
|
|
2012-05-26 01:48:12 +02:00
|
|
|
Contact information
|
|
|
|
-------------------
|
2013-03-28 09:46:43 +01:00
|
|
|
|
2012-05-26 01:48:12 +02:00
|
|
|
Kristian Lauszus, TKJ Electronics
|
|
|
|
Web : http://www.tkjelectronics.com
|
|
|
|
e-mail : kristianl@tkjelectronics.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _ps3usb_h_
|
|
|
|
#define _ps3usb_h_
|
|
|
|
|
|
|
|
#include "Usb.h"
|
2012-09-09 22:13:52 +02:00
|
|
|
#include "PS3Enums.h"
|
2012-05-26 01:48:12 +02:00
|
|
|
|
|
|
|
/* PS3 data taken from descriptors */
|
2013-11-12 19:44:12 +01:00
|
|
|
#define EP_MAXPKTSIZE 64 // max size for data via USB
|
2012-05-26 01:48:12 +02:00
|
|
|
|
|
|
|
/* Names we give to the 3 ps3 pipes - this is only used for setting the bluetooth address into the ps3 controllers */
|
2013-11-12 19:44:12 +01:00
|
|
|
#define PS3_CONTROL_PIPE 0
|
|
|
|
#define PS3_OUTPUT_PIPE 1
|
|
|
|
#define PS3_INPUT_PIPE 2
|
2012-05-26 01:48:12 +02:00
|
|
|
|
|
|
|
//PID and VID of the different devices
|
2013-11-12 19:44:12 +01:00
|
|
|
#define PS3_VID 0x054C // Sony Corporation
|
|
|
|
#define PS3_PID 0x0268 // PS3 Controller DualShock 3
|
|
|
|
#define PS3NAVIGATION_PID 0x042F // Navigation controller
|
|
|
|
#define PS3MOVE_PID 0x03D5 // Motion controller
|
2012-05-26 01:48:12 +02:00
|
|
|
|
2013-11-12 19:44:12 +01:00
|
|
|
// Used in control endpoint header for HID Commands
|
|
|
|
#define bmREQ_HID_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
|
|
|
|
#define bmREQ_HID_IN USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
|
2012-05-26 01:48:12 +02:00
|
|
|
|
2013-11-12 19:44:12 +01:00
|
|
|
#define HID_REQUEST_GET_REPORT 0x01
|
|
|
|
#define HID_REQUEST_SET_REPORT 0x09
|
|
|
|
|
|
|
|
#define PS3_MAX_ENDPOINTS 3
|
2012-05-26 01:48:12 +02:00
|
|
|
|
2013-02-05 19:51:45 +01:00
|
|
|
/**
|
|
|
|
* This class implements support for all the official PS3 Controllers:
|
|
|
|
* Dualshock 3, Navigation or a Motion controller via USB.
|
2013-03-28 09:46:43 +01:00
|
|
|
*
|
2013-02-17 01:25:49 +01:00
|
|
|
* One can only set the color of the bulb, set the rumble, set and get the bluetooth address and calibrate the magnetometer via USB on the Move controller.
|
2013-03-28 09:46:43 +01:00
|
|
|
*
|
2013-02-05 19:51:45 +01:00
|
|
|
* Information about the protocol can be found at the wiki: https://github.com/felis/USB_Host_Shield_2.0/wiki/PS3-Information.
|
|
|
|
*/
|
2012-08-04 19:08:43 +02:00
|
|
|
class PS3USB : public USBDeviceConfig {
|
2012-05-26 01:48:12 +02:00
|
|
|
public:
|
2013-03-30 09:15:25 +01:00
|
|
|
/**
|
|
|
|
* Constructor for the PS3USB class.
|
|
|
|
* @param pUsb Pointer to USB class instance.
|
|
|
|
* @param btadr5,btadr4,btadr3,btadr2,btadr1,btadr0
|
|
|
|
* Pass your dongles Bluetooth address into the constructor,
|
|
|
|
* so you are able to pair the controller with a Bluetooth dongle.
|
|
|
|
*/
|
|
|
|
PS3USB(USB *pUsb, uint8_t btadr5 = 0, uint8_t btadr4 = 0, uint8_t btadr3 = 0, uint8_t btadr2 = 0, uint8_t btadr1 = 0, uint8_t btadr0 = 0);
|
2013-03-30 05:54:13 +01:00
|
|
|
|
2013-03-30 09:15:25 +01:00
|
|
|
/** @name USBDeviceConfig implementation */
|
|
|
|
/**
|
|
|
|
* Initialize the PS3 Controller.
|
|
|
|
* @param parent Hub number.
|
|
|
|
* @param port Port number on the hub.
|
|
|
|
* @param lowspeed Speed of the device.
|
|
|
|
* @return 0 on success.
|
|
|
|
*/
|
|
|
|
virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
|
|
|
|
/**
|
|
|
|
* Release the USB device.
|
|
|
|
* @return 0 on success.
|
|
|
|
*/
|
|
|
|
virtual uint8_t Release();
|
|
|
|
/**
|
|
|
|
* Poll the USB Input endpoins and run the state machines.
|
|
|
|
* @return 0 on success.
|
|
|
|
*/
|
|
|
|
virtual uint8_t Poll();
|
2013-03-30 05:54:13 +01:00
|
|
|
|
2013-03-30 09:15:25 +01:00
|
|
|
/**
|
|
|
|
* Get the device address.
|
|
|
|
* @return The device address.
|
|
|
|
*/
|
|
|
|
virtual uint8_t GetAddress() {
|
|
|
|
return bAddress;
|
|
|
|
};
|
2013-03-30 05:54:13 +01:00
|
|
|
|
2013-03-30 09:15:25 +01:00
|
|
|
/**
|
|
|
|
* Used to check if the controller has been initialized.
|
|
|
|
* @return True if it's ready.
|
|
|
|
*/
|
|
|
|
virtual bool isReady() {
|
|
|
|
return bPollEnable;
|
|
|
|
};
|
2013-07-15 18:20:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
virtual boolean VIDPIDOK(uint16_t vid, uint16_t pid) {
|
|
|
|
return (vid == PS3_VID && (pid == PS3_PID || pid == PS3NAVIGATION_PID || pid == PS3MOVE_PID));
|
|
|
|
};
|
2013-03-30 09:15:25 +01:00
|
|
|
/**@}*/
|
2013-03-30 05:54:13 +01:00
|
|
|
|
2013-03-30 09:15:25 +01:00
|
|
|
/**
|
|
|
|
* Used to set the Bluetooth address inside the Dualshock 3 and Navigation controller.
|
2013-11-12 19:44:12 +01:00
|
|
|
* Set using LSB first.
|
|
|
|
* @param bdaddr Your dongles Bluetooth address.
|
|
|
|
*/
|
|
|
|
void setBdaddr(uint8_t *bdaddr);
|
|
|
|
/**
|
|
|
|
* Used to get the Bluetooth address inside the Dualshock 3 and Navigation controller.
|
|
|
|
* Will return LSB first.
|
|
|
|
* @param bdaddr Your dongles Bluetooth address.
|
2013-03-30 09:15:25 +01:00
|
|
|
*/
|
2013-11-12 19:44:12 +01:00
|
|
|
void getBdaddr(uint8_t *bdaddr);
|
|
|
|
|
2013-03-30 09:15:25 +01:00
|
|
|
/**
|
|
|
|
* Used to set the Bluetooth address inside the Move controller.
|
2013-11-12 19:44:12 +01:00
|
|
|
* Set using LSB first.
|
|
|
|
* @param bdaddr Your dongles Bluetooth address.
|
|
|
|
*/
|
|
|
|
void setMoveBdaddr(uint8_t *bdaddr);
|
|
|
|
/**
|
|
|
|
* Used to get the Bluetooth address inside the Move controller.
|
|
|
|
* Will return LSB first.
|
|
|
|
* @param bdaddr Your dongles Bluetooth address.
|
|
|
|
*/
|
|
|
|
void getMoveBdaddr(uint8_t *bdaddr);
|
|
|
|
/**
|
|
|
|
* Used to get the calibration data inside the Move controller.
|
2013-11-25 00:43:47 +01:00
|
|
|
* @param data Buffer to store data in. Must be at least 147 bytes
|
2013-03-30 09:15:25 +01:00
|
|
|
*/
|
2013-11-12 19:44:12 +01:00
|
|
|
void getMoveCalibration(uint8_t *data);
|
2013-03-30 05:54:13 +01:00
|
|
|
|
2013-03-30 09:15:25 +01:00
|
|
|
/** @name PS3 Controller functions */
|
|
|
|
/**
|
2014-01-04 13:43:49 +01:00
|
|
|
* getButtonPress(ButtonEnum b) will return true as long as the button is held down.
|
2013-03-30 09:15:25 +01:00
|
|
|
*
|
2014-01-04 13:43:49 +01:00
|
|
|
* While getButtonClick(ButtonEnum b) will only return it once.
|
2013-03-30 09:15:25 +01:00
|
|
|
*
|
2014-01-04 13:43:49 +01:00
|
|
|
* So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b),
|
|
|
|
* but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b).
|
|
|
|
* @param b ::ButtonEnum to read.
|
|
|
|
* @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.
|
2013-03-30 09:15:25 +01:00
|
|
|
*/
|
2014-01-04 13:43:49 +01:00
|
|
|
bool getButtonPress(ButtonEnum b);
|
|
|
|
bool getButtonClick(ButtonEnum b);
|
2013-03-30 09:15:25 +01:00
|
|
|
/**@}*/
|
|
|
|
/** @name PS3 Controller functions */
|
|
|
|
/**
|
|
|
|
* Used to get the analog value from button presses.
|
2014-01-04 13:43:49 +01:00
|
|
|
* @param a The ::ButtonEnum to read.
|
2013-03-30 09:15:25 +01:00
|
|
|
* The supported buttons are:
|
|
|
|
* ::UP, ::RIGHT, ::DOWN, ::LEFT, ::L1, ::L2, ::R1, ::R2,
|
|
|
|
* ::TRIANGLE, ::CIRCLE, ::CROSS, ::SQUARE, and ::T.
|
|
|
|
* @return Analog value in the range of 0-255.
|
|
|
|
*/
|
2014-01-04 13:43:49 +01:00
|
|
|
uint8_t getAnalogButton(ButtonEnum a);
|
2013-03-30 09:15:25 +01:00
|
|
|
/**
|
|
|
|
* Used to read the analog joystick.
|
|
|
|
* @param a ::LeftHatX, ::LeftHatY, ::RightHatX, and ::RightHatY.
|
|
|
|
* @return Return the analog value in the range of 0-255.
|
|
|
|
*/
|
2014-01-04 13:43:49 +01:00
|
|
|
uint8_t getAnalogHat(AnalogHatEnum a);
|
2013-03-30 09:15:25 +01:00
|
|
|
/**
|
|
|
|
* Used to read the sensors inside the Dualshock 3 controller.
|
|
|
|
* @param a
|
|
|
|
* The Dualshock 3 has a 3-axis accelerometer and a 1-axis gyro inside.
|
|
|
|
* @return Return the raw sensor value.
|
|
|
|
*/
|
2014-01-04 13:43:49 +01:00
|
|
|
uint16_t getSensor(SensorEnum a);
|
2013-03-30 09:15:25 +01:00
|
|
|
/**
|
|
|
|
* Use this to get ::Pitch and ::Roll calculated using the accelerometer.
|
|
|
|
* @param a Either ::Pitch or ::Roll.
|
|
|
|
* @return Return the angle in the range of 0-360.
|
|
|
|
*/
|
2014-01-04 13:43:49 +01:00
|
|
|
double getAngle(AngleEnum a);
|
2013-03-30 09:15:25 +01:00
|
|
|
/**
|
2014-01-04 13:43:49 +01:00
|
|
|
* Get the ::StatusEnum from the controller.
|
|
|
|
* @param c The ::StatusEnum you want to read.
|
2013-03-30 09:15:25 +01:00
|
|
|
* @return True if correct and false if not.
|
|
|
|
*/
|
2014-01-04 13:43:49 +01:00
|
|
|
bool getStatus(StatusEnum c);
|
2014-01-04 14:14:22 +01:00
|
|
|
/** Read all the available statuses from the controller and prints it as a nice formated string. */
|
|
|
|
void printStatusString();
|
2013-03-30 05:54:13 +01:00
|
|
|
|
2014-01-04 13:43:49 +01:00
|
|
|
/** Used to set all LEDs and rumble off. */
|
2013-03-30 09:15:25 +01:00
|
|
|
void setAllOff();
|
2014-01-04 13:43:49 +01:00
|
|
|
/** Turn off rumble. */
|
2013-03-30 09:15:25 +01:00
|
|
|
void setRumbleOff();
|
|
|
|
/**
|
2014-01-04 13:43:49 +01:00
|
|
|
* Turn on rumble.
|
2013-03-30 09:15:25 +01:00
|
|
|
* @param mode Either ::RumbleHigh or ::RumbleLow.
|
|
|
|
*/
|
2014-01-04 13:43:49 +01:00
|
|
|
void setRumbleOn(RumbleEnum mode);
|
2013-03-30 09:15:25 +01:00
|
|
|
/**
|
2014-01-04 13:43:49 +01:00
|
|
|
* Turn on rumble using custom duration and power.
|
2013-03-30 09:15:25 +01:00
|
|
|
* @param rightDuration The duration of the right/low rumble effect.
|
|
|
|
* @param rightPower The intensity of the right/low rumble effect.
|
|
|
|
* @param leftDuration The duration of the left/high rumble effect.
|
|
|
|
* @param leftPower The intensity of the left/high rumble effect.
|
|
|
|
*/
|
|
|
|
void setRumbleOn(uint8_t rightDuration, uint8_t rightPower, uint8_t leftDuration, uint8_t leftPower);
|
2013-04-01 15:38:15 +02:00
|
|
|
|
|
|
|
/**
|
2014-01-04 13:43:49 +01:00
|
|
|
* Set LED value without using the ::LEDEnum.
|
|
|
|
* @param value See: ::LEDEnum.
|
2013-04-01 15:38:15 +02:00
|
|
|
*/
|
|
|
|
void setLedRaw(uint8_t value);
|
2013-12-25 11:09:57 +01:00
|
|
|
|
2013-11-15 23:19:10 +01:00
|
|
|
/** Turn all LEDs off. */
|
|
|
|
void setLedOff() {
|
|
|
|
setLedRaw(0);
|
|
|
|
}
|
2013-03-30 09:15:25 +01:00
|
|
|
/**
|
2014-01-04 13:43:49 +01:00
|
|
|
* Turn the specific ::LEDEnum off.
|
|
|
|
* @param a The ::LEDEnum to turn off.
|
2013-03-30 09:15:25 +01:00
|
|
|
*/
|
2014-01-04 13:43:49 +01:00
|
|
|
void setLedOff(LEDEnum a);
|
2013-03-30 09:15:25 +01:00
|
|
|
/**
|
2014-01-04 13:43:49 +01:00
|
|
|
* Turn the specific ::LEDEnum on.
|
|
|
|
* @param a The ::LEDEnum to turn on.
|
2013-03-30 09:15:25 +01:00
|
|
|
*/
|
2014-01-04 13:43:49 +01:00
|
|
|
void setLedOn(LEDEnum a);
|
2013-03-30 09:15:25 +01:00
|
|
|
/**
|
2014-01-04 13:43:49 +01:00
|
|
|
* Toggle the specific ::LEDEnum.
|
|
|
|
* @param a The ::LEDEnum to toggle.
|
2013-03-30 09:15:25 +01:00
|
|
|
*/
|
2014-01-04 13:43:49 +01:00
|
|
|
void setLedToggle(LEDEnum a);
|
2013-03-30 09:15:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Use this to set the Color using RGB values.
|
|
|
|
* @param r,g,b RGB value.
|
|
|
|
*/
|
|
|
|
void moveSetBulb(uint8_t r, uint8_t g, uint8_t b);
|
|
|
|
/**
|
2014-01-04 13:43:49 +01:00
|
|
|
* Use this to set the color using the predefined colors in ::ColorsEnum.
|
2013-03-30 09:15:25 +01:00
|
|
|
* @param color The desired color.
|
|
|
|
*/
|
2014-01-04 13:43:49 +01:00
|
|
|
void moveSetBulb(ColorsEnum color);
|
2013-03-30 09:15:25 +01:00
|
|
|
/**
|
|
|
|
* Set the rumble value inside the Move controller.
|
|
|
|
* @param rumble The desired value in the range from 64-255.
|
|
|
|
*/
|
|
|
|
void moveSetRumble(uint8_t rumble);
|
2013-07-14 00:43:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to call your own function when the controller is successfully initialized.
|
|
|
|
* @param funcOnInit Function to call.
|
|
|
|
*/
|
|
|
|
void attachOnInit(void (*funcOnInit)(void)) {
|
|
|
|
pFuncOnInit = funcOnInit;
|
|
|
|
};
|
2013-03-30 09:15:25 +01:00
|
|
|
/**@}*/
|
|
|
|
|
|
|
|
/** Variable used to indicate if the normal playstation controller is successfully connected. */
|
|
|
|
bool PS3Connected;
|
|
|
|
/** Variable used to indicate if the move controller is successfully connected. */
|
|
|
|
bool PS3MoveConnected;
|
|
|
|
/** Variable used to indicate if the navigation controller is successfully connected. */
|
|
|
|
bool PS3NavigationConnected;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/** Pointer to USB class instance. */
|
|
|
|
USB *pUsb;
|
|
|
|
/** Device address. */
|
|
|
|
uint8_t bAddress;
|
|
|
|
/** Endpoint info structure. */
|
|
|
|
EpInfo epInfo[PS3_MAX_ENDPOINTS];
|
|
|
|
|
|
|
|
private:
|
2013-07-14 00:43:35 +02:00
|
|
|
/**
|
|
|
|
* Called when the controller 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.
|
|
|
|
*/
|
|
|
|
void onInit();
|
|
|
|
void (*pFuncOnInit)(void); // Pointer to function called in onInit()
|
2013-09-24 00:40:10 +02:00
|
|
|
|
2013-03-30 09:15:25 +01:00
|
|
|
bool bPollEnable;
|
|
|
|
|
|
|
|
uint32_t timer; // used to continuously set PS3 Move controller Bulb and rumble values
|
|
|
|
|
|
|
|
uint32_t ButtonState;
|
|
|
|
uint32_t OldButtonState;
|
|
|
|
uint32_t ButtonClickState;
|
|
|
|
|
|
|
|
uint8_t my_bdaddr[6]; // Change to your dongles Bluetooth address in the constructor
|
|
|
|
uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
|
|
|
|
uint8_t writeBuf[EP_MAXPKTSIZE]; // General purpose buffer for output data
|
|
|
|
|
|
|
|
void readReport(); // read incoming data
|
|
|
|
void printReport(); // print incoming date - Uncomment for debugging
|
|
|
|
|
|
|
|
/* Private commands */
|
2013-11-12 19:44:12 +01:00
|
|
|
void PS3_Command(uint8_t *data, uint16_t nbytes);
|
2013-03-30 09:15:25 +01:00
|
|
|
void enable_sixaxis(); // Command used to enable the Dualshock 3 and Navigation controller to send data via USB
|
2013-11-12 19:44:12 +01:00
|
|
|
void Move_Command(uint8_t *data, uint16_t nbytes);
|
2012-05-26 01:48:12 +02:00
|
|
|
};
|
|
|
|
#endif
|