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 Class Reference
This class implements support for the PS4 controller via USB. More...
#include <PS4USB.h>
Inherits HIDUniversal, and PS4Parser.
Public Member Functions | |
PS4USB (Usb *p) | |
Constructor for the PS4USB class. | |
bool | connected () |
Used to check if a PS4 controller is connected. | |
void | attachOnInit (void(*funcOnInit)(void)) |
Used to call your own function when the device is successfully initialized. | |
PS4 Controller functions | |
bool | getButtonPress (ButtonEnum b) |
getButtonPress(ButtonEnum b) will return true as long as the button is held down. | |
bool | getButtonClick (ButtonEnum b) |
uint8_t | getAnalogButton (ButtonEnum b) |
Used to get the analog value from button presses. | |
uint8_t | getAnalogHat (AnalogHatEnum a) |
Used to read the analog joystick. | |
uint16_t | getX (uint8_t finger=0, uint8_t xyId=0) |
Get the x-coordinate of the touchpad. | |
uint16_t | getY (uint8_t finger=0, uint8_t xyId=0) |
Get the y-coordinate of the touchpad. | |
bool | isTouching (uint8_t finger=0, uint8_t xyId=0) |
Returns whenever the user is toucing the touchpad. | |
uint8_t | getTouchCounter (uint8_t finger=0, uint8_t xyId=0) |
This counter increments every time a finger touches the touchpad. | |
float | getAngle (AngleEnum a) |
Get the angle of the controller calculated using the accelerometer. | |
int16_t | getSensor (SensorEnum s) |
Used to get the raw values from the 3-axis gyroscope and 3-axis accelerometer inside the PS4 controller. | |
uint8_t | getBatteryLevel () |
Return the battery level of the PS4 controller. | |
bool | getUsbStatus () |
Use this to check if an USB cable is connected to the PS4 controller. | |
bool | getAudioStatus () |
Use this to check if an audio jack cable is connected to the PS4 controller. | |
bool | getMicStatus () |
Use this to check if a microphone is connected to the PS4 controller. | |
void | setAllOff () |
Turn both rumble and the LEDs off. | |
void | setRumbleOff () |
Set rumble off. | |
void | setRumbleOn (RumbleEnum mode) |
Turn on rumble. | |
void | setRumbleOn (uint8_t bigRumble, uint8_t smallRumble) |
Turn on rumble. | |
void | setLedOff () |
Turn all LEDs off. | |
void | setLed (uint8_t r, uint8_t g, uint8_t b) |
Use this to set the color using RGB values. | |
void | setLed (ColorsEnum color) |
Use this to set the color using the predefined colors in ColorsEnum. | |
void | setLedFlash (uint8_t flashOn, uint8_t flashOff) |
Set the LEDs flash time. | |
Protected Member Functions | |
void | Parse (uint8_t len, uint8_t *buf) |
Used to parse data sent from the PS4 controller. | |
void | Reset () |
Used to reset the different buffers to their default values. | |
HIDUniversal implementation | |
virtual void | ParseHIDData (USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) |
Used to parse USB HID data. | |
virtual uint8_t | OnInitSuccessful () |
Called when a device is successfully initialized. | |
PS4Parser implementation | |
virtual void | sendOutputReport (PS4Output *output) |
Send the output to the PS4 controller. | |
USBDeviceConfig implementation | |
virtual bool | VIDPIDOK (uint16_t vid, uint16_t pid) |
Used by the USB core to check what this driver support. |
Detailed Description
This class implements support for the PS4 controller via USB.
It uses the HIDUniversal class for all the USB communication.
Definition at line 32 of file PS4USB.h.
Constructor & Destructor Documentation
PS4USB | ( | Usb * | p ) |
Member Function Documentation
void attachOnInit | ( | void(*)(void) | funcOnInit ) |
bool connected | ( | ) |
uint8_t getAnalogButton | ( | ButtonEnum | b ) | [inherited] |
Used to get the analog value from button presses.
- Parameters:
-
b The ButtonEnum to read. The supported buttons are: L2 and R2.
- Returns:
- Analog value in the range of 0-255.
Definition at line 64 of file PS4Parser.cpp.
uint8_t getAnalogHat | ( | AnalogHatEnum | a ) | [inherited] |
Used to read the analog joystick.
- Parameters:
-
a LeftHatX, LeftHatY, RightHatX, and RightHatY.
- Returns:
- Return the analog value in the range of 0-255.
Definition at line 72 of file PS4Parser.cpp.
float getAngle | ( | AngleEnum | a ) | [inherited] |
Get the angle of the controller calculated using the accelerometer.
- Parameters:
-
a Either Pitch or Roll.
- Returns:
- Return the angle in the range of 0-360.
Definition at line 215 of file PS4Parser.h.
bool getAudioStatus | ( | ) | [inherited] |
Use this to check if an audio jack cable is connected to the PS4 controller.
- Returns:
- Returns true if an audio jack cable is connected.
Definition at line 266 of file PS4Parser.h.
uint8_t getBatteryLevel | ( | ) | [inherited] |
Return the battery level of the PS4 controller.
- Returns:
- The battery level in the range 0-15.
Definition at line 250 of file PS4Parser.h.
bool getButtonPress | ( | ButtonEnum | b ) | [inherited] |
getButtonPress(ButtonEnum b) will return true as long as the button is held down.
While getButtonClick(ButtonEnum b) will only return it once.
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).
- Parameters:
-
b ButtonEnum to read.
- Returns:
- 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.
Definition at line 50 of file PS4Parser.cpp.
bool getMicStatus | ( | ) | [inherited] |
Use this to check if a microphone is connected to the PS4 controller.
- Returns:
- Returns true if a microphone is connected.
Definition at line 274 of file PS4Parser.h.
int16_t getSensor | ( | SensorEnum | s ) | [inherited] |
Used to get the raw values from the 3-axis gyroscope and 3-axis accelerometer inside the PS4 controller.
- Parameters:
-
s The sensor to read.
- Returns:
- Returns the raw sensor reading.
Definition at line 227 of file PS4Parser.h.
uint8_t getTouchCounter | ( | uint8_t | finger = 0 , |
uint8_t | xyId = 0 |
||
) | [inherited] |
This counter increments every time a finger touches the touchpad.
- Parameters:
-
finger 0 = first finger, 1 = second finger. If omitted, then 0 will be used. xyId The controller sends out three packets with the same structure. The third one will contain the last measure, but if you read from the controller then there is only be data in the first one. For that reason it will be set to 0 if the argument is omitted.
- Returns:
- Return the value of the counter, note that it is only a 7-bit value.
Definition at line 206 of file PS4Parser.h.
bool getUsbStatus | ( | ) | [inherited] |
Use this to check if an USB cable is connected to the PS4 controller.
- Returns:
- Returns true if an USB cable is connected.
Definition at line 258 of file PS4Parser.h.
uint16_t getX | ( | uint8_t | finger = 0 , |
uint8_t | xyId = 0 |
||
) | [inherited] |
Get the x-coordinate of the touchpad.
Position 0 is in the top left.
- Parameters:
-
finger 0 = first finger, 1 = second finger. If omitted, then 0 will be used. xyId The controller sends out three packets with the same structure. The third one will contain the last measure, but if you read from the controller then there is only be data in the first one. For that reason it will be set to 0 if the argument is omitted.
- Returns:
- Returns the x-coordinate of the finger.
Definition at line 170 of file PS4Parser.h.
uint16_t getY | ( | uint8_t | finger = 0 , |
uint8_t | xyId = 0 |
||
) | [inherited] |
Get the y-coordinate of the touchpad.
Position 0 is in the top left.
- Parameters:
-
finger 0 = first finger, 1 = second finger. If omitted, then 0 will be used. xyId The controller sends out three packets with the same structure. The third one will contain the last measure, but if you read from the controller then there is only be data in the first one. For that reason it will be set to 0 if the argument is omitted.
- Returns:
- Returns the y-coordinate of the finger.
Definition at line 182 of file PS4Parser.h.
bool isTouching | ( | uint8_t | finger = 0 , |
uint8_t | xyId = 0 |
||
) | [inherited] |
Returns whenever the user is toucing the touchpad.
- Parameters:
-
finger 0 = first finger, 1 = second finger. If omitted, then 0 will be used. xyId The controller sends out three packets with the same structure. The third one will contain the last measure, but if you read from the controller then there is only be data in the first one. For that reason it will be set to 0 if the argument is omitted.
- Returns:
- Returns true if the specific finger is touching the touchpad.
Definition at line 194 of file PS4Parser.h.
virtual uint8_t OnInitSuccessful | ( | ) | [protected, virtual] |
void Parse | ( | uint8_t | len, |
uint8_t * | buf | ||
) | [protected, inherited] |
Used to parse data sent from the PS4 controller.
- Parameters:
-
len Length of the data. buf Pointer to the data buffer.
Definition at line 76 of file PS4Parser.cpp.
virtual void ParseHIDData | ( | USBHID * | hid, |
bool | is_rpt_id, | ||
uint8_t | len, | ||
uint8_t * | buf | ||
) | [protected, virtual] |
void Reset | ( | ) | [protected, inherited] |
Used to reset the different buffers to their default values.
Definition at line 130 of file PS4Parser.cpp.
virtual void sendOutputReport | ( | PS4Output * | output ) | [protected, virtual] |
void setAllOff | ( | ) | [inherited] |
Turn both rumble and the LEDs off.
Definition at line 279 of file PS4Parser.h.
void setLed | ( | uint8_t | r, |
uint8_t | g, | ||
uint8_t | b | ||
) | [inherited] |
Use this to set the color using RGB values.
- Parameters:
-
r,g,b RGB value.
Definition at line 320 of file PS4Parser.h.
void setLed | ( | ColorsEnum | color ) | [inherited] |
Use this to set the color using the predefined colors in ColorsEnum.
- Parameters:
-
color The desired color.
Definition at line 331 of file PS4Parser.h.
void setLedFlash | ( | uint8_t | flashOn, |
uint8_t | flashOff | ||
) | [inherited] |
Set the LEDs flash time.
- Parameters:
-
flashOn Time to flash bright (255 = 2.5 seconds). flashOff Time to flash dark (255 = 2.5 seconds).
Definition at line 340 of file PS4Parser.h.
void setLedOff | ( | ) | [inherited] |
Turn all LEDs off.
Definition at line 312 of file PS4Parser.h.
void setRumbleOff | ( | ) | [inherited] |
Set rumble off.
Definition at line 285 of file PS4Parser.h.
void setRumbleOn | ( | uint8_t | bigRumble, |
uint8_t | smallRumble | ||
) | [inherited] |
Turn on rumble.
- Parameters:
-
bigRumble Value for big motor. smallRumble Value for small motor.
Definition at line 305 of file PS4Parser.h.
void setRumbleOn | ( | RumbleEnum | mode ) | [inherited] |
Turn on rumble.
- Parameters:
-
mode Either RumbleHigh or RumbleLow.
Definition at line 293 of file PS4Parser.h.
Generated on Tue Jul 12 2022 18:12:05 by
