Skip to content

Commit

Permalink
Added ability to send data to controller
Browse files Browse the repository at this point in the history
  • Loading branch information
aed3 committed Dec 30, 2019
1 parent d157d00 commit 81df9c6
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <PS4Controller.h>

void setUp()
void setup()
{
Serial.begin(9600);
PS4.begin("03:03:03:03:03:03");
Expand All @@ -12,22 +12,22 @@ void loop()
// Below has all accessible outputs from the controller
if(PS4.isConnected()) {

if ( PS4.data.button.Up )
if ( PS4.data.button.up )
Serial.println("Up Button");
if ( PS4.data.button.Down )
if ( PS4.data.button.down )
Serial.println("Down Button");
if ( PS4.data.button.Left )
if ( PS4.data.button.left )
Serial.println("Left Button");
if ( PS4.data.button.Right )
if ( PS4.data.button.right )
Serial.println("Right Button");

if ( PS4.data.button.UpRight )
if ( PS4.data.button.upright )
Serial.println("Up Right");
if ( PS4.data.button.UpLeft )
if ( PS4.data.button.upleft )
Serial.println("Up Left");
if ( PS4.data.button.DownLeft )
if ( PS4.data.button.downleft )
Serial.println("Down Left");
if ( PS4.data.button.DownRight )
if ( PS4.data.button.downright )
Serial.println("Down Right");

if ( PS4.data.button.triangle )
Expand Down
56 changes: 56 additions & 0 deletions examples/PS4SendData/PS4SendData.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <PS4Controller.h>

int r = 255;
int g = 0;
int b = 0;

// Calculates the next value in a rainbow sequence
void nextRainbowColor() {
if(r > 0 && b == 0){
r--;
g++;
}
if(g > 0 && r == 0){
g--;
b++;
}
if(b > 0 && g == 0){
r++;
b--;
}
}

void setup()
{
Serial.begin(9600);
PS4.begin("03:03:03:03:03:03");
Serial.println("Ready.");
}

void loop()
{
if (PS4.isConnected()){
nextRainbowColor();

// Sets the color of the controller's front light
// Params: Red, Green,and Blue
// See here for details: https://www.w3schools.com/colors/colors_rgb.asp
PS4.setLed(r, g, b);

// Sets how fast the controller's front light flashes
// Params: How long the light is on in ms, how long the light is off in ms
// Range: 0->2550 ms, Set to 0,0 for the light to remain on
PS4.setFlashRate(1000, 1000);

// Sets the rumble of the controllers
// Params: Weak rumble intensity, Strong rumble intensity
// Range: 0->255
PS4.setRumble(100, 0);

// Sends data set in the above three instructions to the controller
PS4.sendToController();

// Don't send data to the controller immediately, will cause buffer overflow
delay(10);
}
}
6 changes: 3 additions & 3 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ begin KEYWORD2
end KEYWORD2
isConnected KEYWORD2
setLed KEYWORD2
setRumble KEYWORD2
setFlashRate KEYWORD2
sendToController KEYWORD2
attach KEYWORD2
attachOnConnect KEYWORD2
attachOnDisconnect KEYWORD2

data KEYWORD3
event KEYWORD3

button KEYWORD4
analog KEYWORD4
35 changes: 29 additions & 6 deletions src/PS4Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include <esp_bt_main.h>
#include <esp_bt_defs.h>

#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define CONSTRAIN_8BIT(a) MIN(MAX(a, 0), 255)

extern "C" {
#include "include/ps4.h"
}
Expand Down Expand Up @@ -76,13 +80,35 @@ bool PS4Controller::isConnected()
}


void PS4Controller::setLed(int led)
void PS4Controller::setLed(int r, int g, int b)
{
output.r = CONSTRAIN_8BIT(r);
output.g = CONSTRAIN_8BIT(g);
output.b = CONSTRAIN_8BIT(b);
}


void PS4Controller::setRumble(int small, int large)
{
ps4SetLed(led);
output.smallRumble = CONSTRAIN_8BIT(small);
output.largeRumble = CONSTRAIN_8BIT(large);
}


void PS4Controller::setFlashRate(int onTime, int offTime)
{
output.flashOn = CONSTRAIN_8BIT(onTime/10);
output.flashOff = CONSTRAIN_8BIT(offTime/10);
}


void PS4Controller::sendToController()
{
ps4SetOutput(output);
}



void PS4Controller::attach(callback_t callback)
{
_callback_event = callback;
Expand Down Expand Up @@ -123,10 +149,7 @@ void PS4Controller::_connection_callback(void *object, uint8_t is_connected)
if (is_connected)
{
delay(250); // ToDo: figure out how to know when the channel is free again so this delay can be removed

// Set LED1 by default
This->setLed(1);


if (This->_callback_connect){
This->_callback_connect();
}
Expand Down
7 changes: 6 additions & 1 deletion src/PS4Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class PS4Controller

ps4_t data;
ps4_event_t event;
ps4_cmd_t output;

PS4Controller();

Expand All @@ -26,7 +27,11 @@ class PS4Controller

bool isConnected();

void setLed(int led);
void setLed(int r, int g, int b);
void setRumble(int small, int large);
void setFlashRate(int onTime, int offTime);

void sendToController();

void attach(callback_t callback);
void attachOnConnect(callback_t callback);
Expand Down
17 changes: 5 additions & 12 deletions src/include/ps4.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,9 @@ typedef struct {
/*******************/

typedef struct {
/* Rumble control */
uint8_t rumble_right_duration;
uint8_t rumble_right_intensity;
uint8_t rumble_left_duration;
uint8_t rumble_left_intensity;

/* LED control */
uint8_t led1 : 1;
uint8_t led2 : 1;
uint8_t led3 : 1;
uint8_t led4 : 1;
uint8_t smallRumble, largeRumble; // Rumble
uint8_t r, g, b; // RGB
uint8_t flashOn, flashOff; // Time to flash bright/dark (255 = 2.5 seconds)
} ps4_cmd_t;

typedef struct {
Expand Down Expand Up @@ -165,7 +157,8 @@ void ps4SetConnectionCallback( ps4_connection_callback_t cb );
void ps4SetConnectionObjectCallback( void *object, ps4_connection_object_callback_t cb );
void ps4SetEventCallback( ps4_event_callback_t cb );
void ps4SetEventObjectCallback( void *object, ps4_event_object_callback_t cb );
void ps4SetLed( uint8_t led );
void ps4SetLed( uint8_t r, uint8_t g, uint8_t b );
void ps4SetOutput( ps4_cmd_t prev_cmd );
void ps4SetBluetoothMacAddress( const uint8_t *mac );


Expand Down
55 changes: 32 additions & 23 deletions src/ps4.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/* C O N S T A N T S */
/********************************************************************************/

static const uint8_t hid_cmd_payload_ps4_enable[] = { 0x42, 0x03, 0x00, 0x00 };
static const uint8_t hid_cmd_payload_ps4_enable[] = { 0x43, 0x02 };
static const uint8_t hid_cmd_payload_led_arguments[] = { 0xff, 0x27, 0x10, 0x00, 0x32 };


Expand Down Expand Up @@ -102,55 +102,64 @@ void ps4Enable()
*******************************************************************************/
void ps4Cmd( ps4_cmd_t cmd )
{
hid_cmd_t hid_cmd = { .data = {0} };
hid_cmd_t hid_cmd = { .data = {0x80, 0x00, 0xFF} };
uint16_t len = sizeof(hid_cmd.data);

hid_cmd.code = hid_cmd_code_set_report | hid_cmd_code_type_output;
hid_cmd.identifier = hid_cmd_identifier_ps4_control;

hid_cmd.data[ps4_control_packet_index_rumble_right_duration] = cmd.rumble_right_duration;
hid_cmd.data[ps4_control_packet_index_rumble_right_intensity] = cmd.rumble_right_intensity;
hid_cmd.data[ps4_control_packet_index_rumble_left_duration] = cmd.rumble_left_duration;
hid_cmd.data[ps4_control_packet_index_rumble_left_intensity] = cmd.rumble_left_intensity;
hid_cmd.data[ps4_control_packet_index_small_rumble] = cmd.smallRumble; // Small Rumble
hid_cmd.data[ps4_control_packet_index_large_rumble] = cmd.largeRumble; // Big rumble

hid_cmd.data[ps4_control_packet_index_leds] = 0;
if (cmd.led1) hid_cmd.data[ps4_control_packet_index_leds] |= ps4_led_mask_led1;
if (cmd.led2) hid_cmd.data[ps4_control_packet_index_leds] |= ps4_led_mask_led2;
if (cmd.led3) hid_cmd.data[ps4_control_packet_index_leds] |= ps4_led_mask_led3;
if (cmd.led4) hid_cmd.data[ps4_control_packet_index_leds] |= ps4_led_mask_led4;
hid_cmd.data[ps4_control_packet_index_red] = cmd.r; // Red
hid_cmd.data[ps4_control_packet_index_green] = cmd.g; // Green
hid_cmd.data[ps4_control_packet_index_blue] = cmd.b; // Blue

if (cmd.led1) memcpy( hid_cmd.data + ps4_control_packet_index_led1_arguments, hid_cmd_payload_led_arguments, sizeof(hid_cmd_payload_led_arguments));
if (cmd.led2) memcpy( hid_cmd.data + ps4_control_packet_index_led2_arguments, hid_cmd_payload_led_arguments, sizeof(hid_cmd_payload_led_arguments));
if (cmd.led3) memcpy( hid_cmd.data + ps4_control_packet_index_led3_arguments, hid_cmd_payload_led_arguments, sizeof(hid_cmd_payload_led_arguments));
if (cmd.led4) memcpy( hid_cmd.data + ps4_control_packet_index_led4_arguments, hid_cmd_payload_led_arguments, sizeof(hid_cmd_payload_led_arguments));
hid_cmd.data[ps4_control_packet_index_flash_on_time] = cmd.flashOn; // Time to flash bright (255 = 2.5 seconds)
hid_cmd.data[ps4_control_packet_index_flash_off_time] = cmd.flashOff; // Time to flash dark (255 = 2.5 seconds)

ps4_gap_send_hid( &hid_cmd, len );
}


/*******************************************************************************
**
** Function ps4SetLed
** Function ps4SetLedOnly
**
** Description Sets one of the LEDs on the PS4 controller.
** Description Sets the LEDs on the PS4 controller.
**
**
** Returns void
**
*******************************************************************************/
void ps4SetLed( uint8_t led )
void ps4SetLed(uint8_t r, uint8_t g, uint8_t b)
{
ps4_cmd_t cmd = {0};
ps4_cmd_t cmd = { 0 };

cmd.led1 = led == 1;
cmd.led2 = led == 2;
cmd.led3 = led == 3;
cmd.led4 = led == 4;
cmd.r = r;
cmd.g = g;
cmd.b = b;

ps4Cmd(cmd);
}


/*******************************************************************************
**
** Function ps4SetOutput
**
** Description Sets feedback on the PS4 controller.
**
**
** Returns void
**
*******************************************************************************/
void ps4SetOutput(ps4_cmd_t prev_cmd)
{
ps4Cmd(prev_cmd);
}


/*******************************************************************************
**
** Function ps4SetConnectionCallback
Expand Down
12 changes: 8 additions & 4 deletions src/ps4_gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,14 @@ void ps4_gap_send_hid( hid_cmd_t *hid_cmd, uint8_t len )

result = GAP_ConnBTWrite(gap_handle_hidc, p_buf);

if (result == BT_PASS)
ESP_LOGI(PS4_TAG, "[%s] sending command: success", __func__);
else
ESP_LOGE(PS4_TAG, "[%s] sending command: failed", __func__);
if (result == BT_PASS) {
ESP_LOGI(PS4_TAG, "[%s] sending command: success\n", __func__);
//printf("[%s] sending command: success", __func__);
}
else {
ESP_LOGE(PS4_TAG, "[%s] sending command: failed\n", __func__);
//printf("[%s] sending command: success", __func__);
}
}


Expand Down
28 changes: 10 additions & 18 deletions src/ps4_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#endif

/** Size of the output report buffer for the Dualshock and Navigation controllers */
#define PS4_REPORT_BUFFER_SIZE 48
#define PS4_REPORT_BUFFER_SIZE 77
#define PS4_HID_BUFFER_SIZE 50

/********************************************************************************/
Expand All @@ -58,7 +58,7 @@ enum hid_cmd_code {

enum hid_cmd_identifier {
hid_cmd_identifier_ps4_enable = 0xf4,
hid_cmd_identifier_ps4_control = 0x01
hid_cmd_identifier_ps4_control = 0x11
};


Expand All @@ -70,23 +70,15 @@ typedef struct {
} hid_cmd_t;

enum ps4_control_packet_index {
ps4_control_packet_index_rumble_right_duration = 1,
ps4_control_packet_index_rumble_right_intensity = 2,
ps4_control_packet_index_rumble_left_duration = 3,
ps4_control_packet_index_rumble_left_intensity = 4,

ps4_control_packet_index_leds = 9,
ps4_control_packet_index_led4_arguments = 10,
ps4_control_packet_index_led3_arguments = 15,
ps4_control_packet_index_led2_arguments = 20,
ps4_control_packet_index_led1_arguments = 25
};
ps4_control_packet_index_small_rumble = 5,
ps4_control_packet_index_large_rumble = 6,

ps4_control_packet_index_red = 7,
ps4_control_packet_index_green = 8,
ps4_control_packet_index_blue = 9,

enum ps4_led_mask {
ps4_led_mask_led1 = 1 << 1,
ps4_led_mask_led2 = 1 << 2,
ps4_led_mask_led3 = 1 << 3,
ps4_led_mask_led4 = 1 << 4,
ps4_control_packet_index_flash_on_time = 10,
ps4_control_packet_index_flash_off_time = 11
};


Expand Down
Loading

0 comments on commit 81df9c6

Please sign in to comment.