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
PS4BT Class Reference
This class implements support for the PS4 controller via Bluetooth. More...
#include <PS4BT.h>
Inherits BTHID, and PS4Parser.
Public Member Functions | |
PS4BT (BTD *p, bool pair=false, const char *pin="0000") | |
Constructor for the PS4BT class. | |
bool | connected () |
Used to check if a PS4 controller is connected. | |
HIDReportParser * | GetReportParser (uint8_t id) |
Get HIDReportParser. | |
bool | SetReportParser (uint8_t id, HIDReportParser *prs) |
Set HIDReportParser to be used. | |
void | setProtocolMode (uint8_t mode) |
Set HID protocol mode. | |
void | pair (void) |
Call this to start the pairing sequence with a device. | |
void | attachOnInit (void(*funcOnInit)(void)) |
Used to call your own function when the device is successfully initialized. | |
void | setLeds (struct KBDLEDS data) |
Used to set the leds on a keyboard. | |
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. | |
Data Fields | |
bool | connected |
True if a device is connected. | |
Protected Member Functions | |
bool | checkHciHandle (uint8_t *buf, uint16_t handle) |
Used to check if the incoming L2CAP data matches the HCI Handle. | |
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. | |
BTHID implementation | |
virtual void | ParseBTHIDData (uint8_t len, uint8_t *buf) |
Used to parse Bluetooth HID data. | |
virtual void | OnInitBTHID () |
Called when a device is successfully initialized. | |
virtual void | ResetBTHID () |
Used to reset the different buffers to there default values. | |
PS4Parser implementation | |
virtual void | sendOutputReport (PS4Output *output) |
Send the output to the PS4 controller. | |
Overridable functions | |
virtual void | ParseBTHIDData (uint8_t len __attribute__((unused)), uint8_t *buf __attribute__((unused))) |
Used to parse Bluetooth HID data to any class that inherits this class. | |
Protected Attributes | |
uint8_t | control_scid [2] |
L2CAP source CID for HID_Control. | |
uint8_t | interrupt_scid [2] |
L2CAP source CID for HID_Interrupt. | |
void(* | pFuncOnInit )(void) |
Pointer to function called in onInit(). | |
BTD * | pBtd |
Pointer to BTD instance. | |
uint16_t | hci_handle |
The HCI Handle for the connection. | |
uint32_t | l2cap_event_flag |
L2CAP flags of received Bluetooth events. | |
uint8_t | identifier |
Identifier for L2CAP commands. | |
BluetoothService implementation | |
void | disconnect () |
Used this to disconnect the devices. | |
void | ACLData (uint8_t *ACLData) |
Used to pass acldata to the services. | |
void | Run () |
Used to run part of the state machine. | |
void | Reset () |
Use this to reset the service. | |
void | onInit () |
Called when a device is successfully initialized. |
Detailed Description
This class implements support for the PS4 controller via Bluetooth.
It uses the BTHID class for all the Bluetooth communication.
Definition at line 28 of file PS4BT.h.
Constructor & Destructor Documentation
Constructor for the PS4BT class.
- Parameters:
-
p Pointer to the BTD class instance. pair Set this to true in order to pair with the device. If the argument is omitted then it will not pair with it. One can use PAIR to set it to true. pin Write the pin to BTD::btdPin. If argument is omitted, then "0000" will be used.
Member Function Documentation
void ACLData | ( | uint8_t * | ACLData ) | [protected, virtual, inherited] |
Used to pass acldata to the services.
- Parameters:
-
ACLData Incoming acldata.
Implements BluetoothService.
void attachOnInit | ( | void(*)(void) | funcOnInit ) | [inherited] |
bool checkHciHandle | ( | uint8_t * | buf, |
uint16_t | handle | ||
) | [protected, inherited] |
bool connected | ( | ) |
void disconnect | ( | ) | [virtual, inherited] |
Used this to disconnect the devices.
Implements BluetoothService.
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.
HIDReportParser* GetReportParser | ( | uint8_t | id ) | [inherited] |
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.
void onInit | ( | ) | [protected, virtual, inherited] |
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.
Implements BluetoothService.
virtual void OnInitBTHID | ( | ) | [protected, virtual] |
void pair | ( | void | ) | [inherited] |
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 ParseBTHIDData | ( | uint8_t len | __attribute__(unused), |
uint8_t *buf | __attribute__(unused) | ||
) | [protected, virtual, inherited] |
virtual void ParseBTHIDData | ( | 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.
void Reset | ( | ) | [protected, virtual, inherited] |
Use this to reset the service.
Implements BluetoothService.
virtual void ResetBTHID | ( | ) | [protected, virtual] |
void Run | ( | ) | [protected, virtual, inherited] |
Used to run part of the state machine.
Implements BluetoothService.
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 | ( | 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 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 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 setLeds | ( | struct KBDLEDS | data ) | [inherited] |
void setProtocolMode | ( | uint8_t | mode ) | [inherited] |
bool SetReportParser | ( | uint8_t | id, |
HIDReportParser * | prs | ||
) | [inherited] |
void setRumbleOff | ( | ) | [inherited] |
Set rumble off.
Definition at line 285 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.
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.
Field Documentation
uint8_t control_scid[2] [protected, inherited] |
uint16_t hci_handle [protected, inherited] |
uint8_t identifier [protected, inherited] |
uint8_t interrupt_scid[2] [protected, inherited] |
uint32_t l2cap_event_flag [protected, inherited] |
void(* pFuncOnInit)(void) [protected, inherited] |
Generated on Tue Jul 12 2022 18:12:05 by
