From 2c5f5e8c0964c1639265486b6fac17739a55f1d2 Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Wed, 23 Apr 2014 23:40:47 +0200 Subject: [PATCH] Can now read battery level and usb, audio and mic status of PS4 controller Inspired by: https://github.com/chrippa/ds4drv/blob/master/ds4drv/device.py --- PS4Parser.h | 45 ++++++++++++++++++++++++++++++++++++++++++++- README.md | 2 +- keywords.txt | 4 ++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/PS4Parser.h b/PS4Parser.h index 093701e9..8b328342 100644 --- a/PS4Parser.h +++ b/PS4Parser.h @@ -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]; @@ -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. @@ -245,6 +256,38 @@ public: } }; + /** + * 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(); diff --git a/README.md b/README.md index 43fde867..35a786e5 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/keywords.txt b/keywords.txt index a13ff34d..58496f4b 100644 --- a/keywords.txt +++ b/keywords.txt @@ -82,6 +82,10 @@ getX KEYWORD2 getY KEYWORD2 getTouchCounter KEYWORD2 +getUsbStatus KEYWORD2 +getAudioStatus KEYWORD2 +getMicStatus KEYWORD2 + #################################################### # Constants and enums (LITERAL1) ####################################################