Zoltan Hudak / UsbHostMAX3421E

Dependents:   UsbHostMAX3421E_Hello

Embed: (wiki syntax)

« Back to documentation index

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().
BTDpBtd
 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

PS4BT ( BTD p,
bool  pair = false,
const char *  pin = "0000" 
)

Constructor for the PS4BT class.

Parameters:
pPointer to the BTD class instance.
pairSet 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.
pinWrite the pin to BTD::btdPin. If argument is omitted, then "0000" will be used.

Definition at line 36 of file PS4BT.h.


Member Function Documentation

void ACLData ( uint8_t *  ACLData ) [protected, virtual, inherited]

Used to pass acldata to the services.

Parameters:
ACLDataIncoming acldata.

Implements BluetoothService.

Definition at line 56 of file BTHID.cpp.

void attachOnInit ( void(*)(void)  funcOnInit ) [inherited]

Used to call your own function when the device is successfully initialized.

Parameters:
funcOnInitFunction to call.

Definition at line 592 of file BTD.h.

bool checkHciHandle ( uint8_t *  buf,
uint16_t  handle 
) [protected, inherited]

Used to check if the incoming L2CAP data matches the HCI Handle.

Definition at line 605 of file BTD.h.

bool connected (  )

Used to check if a PS4 controller is connected.

Returns:
Returns true if it is connected.

Definition at line 45 of file PS4BT.h.

void disconnect (  ) [virtual, inherited]

Used this to disconnect the devices.

Implements BluetoothService.

Definition at line 49 of file BTHID.cpp.

uint8_t getAnalogButton ( ButtonEnum  b ) [inherited]

Used to get the analog value from button presses.

Parameters:
bThe 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:
aLeftHatX, 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:
aEither 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:
bButtonEnum 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]

Get HIDReportParser.

Parameters:
idID of parser.
Returns:
Returns the corresponding HIDReportParser. Returns NULL if id is not valid.

Definition at line 49 of file BTHID.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:
sThe 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:
finger0 = first finger, 1 = second finger. If omitted, then 0 will be used.
xyIdThe 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:
finger0 = first finger, 1 = second finger. If omitted, then 0 will be used.
xyIdThe 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:
finger0 = first finger, 1 = second finger. If omitted, then 0 will be used.
xyIdThe 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:
finger0 = first finger, 1 = second finger. If omitted, then 0 will be used.
xyIdThe 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.

Definition at line 112 of file BTHID.h.

virtual void OnInitBTHID (  ) [protected, virtual]

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.

Reimplemented from BTHID.

Definition at line 65 of file PS4BT.h.

void pair ( void   ) [inherited]

Call this to start the pairing sequence with a device.

Definition at line 91 of file BTHID.h.

void Parse ( uint8_t  len,
uint8_t *  buf 
) [protected, inherited]

Used to parse data sent from the PS4 controller.

Parameters:
lenLength of the data.
bufPointer 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]

Used to parse Bluetooth HID data to any class that inherits this class.

Parameters:
lenThe length of the incoming data.
bufPointer to the data buffer.

Definition at line 125 of file BTHID.h.

virtual void ParseBTHIDData ( uint8_t  len,
uint8_t *  buf 
) [protected, virtual]

Used to parse Bluetooth HID data.

Parameters:
lenThe length of the incoming data.
bufPointer to the data buffer.

Definition at line 56 of file PS4BT.h.

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.

Definition at line 41 of file BTHID.cpp.

virtual void ResetBTHID (  ) [protected, virtual]

Used to reset the different buffers to there default values.

Reimplemented from BTHID.

Definition at line 75 of file PS4BT.h.

void Run (  ) [protected, virtual, inherited]

Used to run part of the state machine.

Implements BluetoothService.

Definition at line 344 of file BTHID.cpp.

virtual void sendOutputReport ( PS4Output *  output ) [protected, virtual]

Send the output to the PS4 controller.

This is implemented in PS4BT.h and PS4USB.h.

Parameters:
outputPointer to PS4Output buffer;

Implements PS4Parser.

Definition at line 81 of file PS4BT.h.

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:
colorThe 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,bRGB value.

Definition at line 320 of file PS4Parser.h.

void setLedFlash ( uint8_t  flashOn,
uint8_t  flashOff 
) [inherited]

Set the LEDs flash time.

Parameters:
flashOnTime to flash bright (255 = 2.5 seconds).
flashOffTime 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]

Used to set the leds on a keyboard.

Parameters:
dataSee KBDLEDS in hidboot.h

Definition at line 81 of file BTHID.h.

void setProtocolMode ( uint8_t  mode ) [inherited]

Set HID protocol mode.

Parameters:
modeHID protocol to use. Either USB_HID_BOOT_PROTOCOL or HID_RPT_PROTOCOL.

Definition at line 72 of file BTHID.h.

bool SetReportParser ( uint8_t  id,
HIDReportParser *  prs 
) [inherited]

Set HIDReportParser to be used.

Parameters:
idId of parser.
prsPointer to HIDReportParser.
Returns:
Returns true if the HIDReportParser is set. False otherwise.

Definition at line 61 of file BTHID.h.

void setRumbleOff (  ) [inherited]

Set rumble off.

Definition at line 285 of file PS4Parser.h.

void setRumbleOn ( RumbleEnum  mode ) [inherited]

Turn on rumble.

Parameters:
modeEither RumbleHigh or RumbleLow.

Definition at line 293 of file PS4Parser.h.

void setRumbleOn ( uint8_t  bigRumble,
uint8_t  smallRumble 
) [inherited]

Turn on rumble.

Parameters:
bigRumbleValue for big motor.
smallRumbleValue for small motor.

Definition at line 305 of file PS4Parser.h.


Field Documentation

bool connected [inherited]

True if a device is connected.

Definition at line 88 of file BTHID.h.

uint8_t control_scid[2] [protected, inherited]

L2CAP source CID for HID_Control.

Definition at line 139 of file BTHID.h.

uint16_t hci_handle [protected, inherited]

The HCI Handle for the connection.

Definition at line 616 of file BTD.h.

uint8_t identifier [protected, inherited]

Identifier for L2CAP commands.

Definition at line 622 of file BTD.h.

uint8_t interrupt_scid[2] [protected, inherited]

L2CAP source CID for HID_Interrupt.

Definition at line 142 of file BTHID.h.

uint32_t l2cap_event_flag [protected, inherited]

L2CAP flags of received Bluetooth events.

Definition at line 619 of file BTD.h.

BTD* pBtd [protected, inherited]

Pointer to BTD instance.

Definition at line 613 of file BTD.h.

void(* pFuncOnInit)(void) [protected, inherited]

Pointer to function called in onInit().

Definition at line 610 of file BTD.h.