aed3 / PS4-esp32

Use a ps4 controller with an esp32
328 stars 103 forks source link

Events not working as expected #35

Open av4625 opened 2 years ago

av4625 commented 2 years ago

Issue

I was trying to use the event callback method that this library provides. I have a callback that gets called when the controller is connected and this works fine.

I then have a callback for controller events void event().

event seems to get called over and over as fast as it can even when I am not touching the controller. This doesn't seem correct?

The button_down and button_up events don't behave properly either. When I press the circle button with the code below. Nothing happens. When I let go of the button Down gets printed. Then after a ~second multiple Up's will get printed. I would expect one Down to get printed and one Up. Also multiple ups get printed just after the controller connects before touching any buttons.

Code Change in Library

I had to change a define or the library wouldn't build.

In ps4_int.h I changed: #define CONFIG_IDF_COMPATIBILITY IDF_COMPATIBILITY_MASTER_21165ED to: #define CONFIG_IDF_COMPATIBILITY IDF_COMPATIBILITY_MASTER_21AF1D7

Code

#include <PS4Controller.h>

void setup()
{
    Serial.begin(115200);
    PS4.attach(event);
    PS4.begin("01:01:01:01:01:01");
    PS4.attachOnConnect(connected);
    Serial.println("Ready.");
}

void event()
{
    if(PS4.event.button_down.circle)
    {
        Serial.println("Down");
    }
    if(PS4.event.button_up.circle)
    {
        Serial.println("Up");
    }
}

void connected()
{
    if (PS4.Charging())
    {
        Serial.println("The controller is charging");
    }

    Serial.printf("Battery Level : %d\n", PS4.Battery());
    Serial.println();
}

void loop()
{
    vTaskDelete(NULL);
}

Output

I connected the controller. Then pressed and released circle 5 times.

Ready.
Battery Level : 0

Up
Up
Up
Up
Up
Up
Up
Down
Down
Down
Down
Down
// 1 to 2 second gap of not touching controller
Up
Up
Up
Up
Up
Up
Up