Skip to content

Commit

Permalink
Can now read battery level and usb, audio and mic status of PS4 contr…
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauszus committed Apr 23, 2014
1 parent d9dfa3c commit 2c5f5e8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
45 changes: 44 additions & 1 deletion PS4Parser.h
Expand Up @@ -81,6 +81,14 @@ struct touchpadXY {
} __attribute__((packed)) finger[2]; // 0 = first finger, 1 = second finger
} __attribute__((packed));

struct PS4Status {
uint8_t battery : 4;
uint8_t usb : 1;
uint8_t audio : 1;
uint8_t mic : 1;
uint8_t unknown : 1; // Extension port?
} __attribute__((packed));

struct PS4Data {
/* Button and joystick values */
uint8_t hatValue[4];
Expand All @@ -92,8 +100,11 @@ struct PS4Data {
int16_t gyroY, gyroZ, gyroX;
int16_t accX, accZ, accY;

uint8_t dummy2[5];
PS4Status status;
uint8_t dummy3[3];

/* The rest is data for the touchpad */
uint8_t dummy2[9]; // Byte 5 looks like some kind of status (maybe battery status), bit 1 of byte 8 is set every time a finger is moving around the touchpad
touchpadXY xy[3]; // It looks like it sends out three coordinates each time, this might be because the microcontroller inside the PS4 controller is much faster than the Bluetooth connection.
// The last data is read from the last position in the array while the oldest measurement is from the first position.
// The first position will also keep it's value after the finger is released, while the other two will set them to zero.
Expand Down Expand Up @@ -245,6 +256,38 @@ class PS4Parser {
}
};

/**
* Return the battery level of the PS4 controller.
* @return The battery level in the range 0-15.
*/
uint8_t getBatteryLevel() {
return ps4Data.status.battery;
};

/**
* Use this to check if an USB cable is connected to the PS4 controller.
* @return Returns true if an USB cable is connected.
*/
bool getUsbStatus() {
return ps4Data.status.usb;
};

/**
* Use this to check if an audio jack cable is connected to the PS4 controller.
* @return Returns true if an audio jack cable is connected.
*/
bool getAudioStatus() {
return ps4Data.status.audio;
};

/**
* Use this to check if a microphone is connected to the PS4 controller.
* @return Returns true if a microphone is connected.
*/
bool getMicStatus() {
return ps4Data.status.mic;
};

/** Turn both rumble and the LEDs off. */
void setAllOff() {
setRumbleOff();
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -122,7 +122,7 @@ It enables me to see the Bluetooth communication between my Mac and any device.
The PS4BT library is split up into the [PS4BT](PS4BT.h) and the [PS4USB](PS4USB.h) library. These allow you to use the Sony PS4 controller via Bluetooth and USB.
The [PS4BT.ino](examples/Bluetooth/PS4BT/PS4BT.ino) and [PS4USB.ino](examples/PS4USB/PS4USB.ino) examples shows how to easily read the buttons, joysticks, touchpad and IMU on the controller via Bluetooth and USB respectively. It is also possible to control the rumble and light on the controller.
The [PS4BT.ino](examples/Bluetooth/PS4BT/PS4BT.ino) and [PS4USB.ino](examples/PS4USB/PS4USB.ino) examples shows how to easily read the buttons, joysticks, touchpad and IMU on the controller via Bluetooth and USB respectively. It is also possible to control the rumble and light on the controller and get the battery level.
Before you can use the PS4 controller via Bluetooth you will need to pair with it.
Expand Down
4 changes: 4 additions & 0 deletions keywords.txt
Expand Up @@ -82,6 +82,10 @@ getX KEYWORD2
getY KEYWORD2
getTouchCounter KEYWORD2

getUsbStatus KEYWORD2
getAudioStatus KEYWORD2
getMicStatus KEYWORD2

####################################################
# Constants and enums (LITERAL1)
####################################################
Expand Down

0 comments on commit 2c5f5e8

Please sign in to comment.