aed3 / PS4-esp32

Use a ps4 controller with an esp32
341 stars 107 forks source link

use-l2cap-insteaad-of-gap-for-esp2.0.0-compatibility #25

Closed Mendrzec closed 3 years ago

Mendrzec commented 3 years ago

Closes https://github.com/aed3/PS4-esp32/issues/21 Closes https://github.com/aed3/PS4-esp32/issues/24 Closes https://github.com/aed3/PS4-esp32/issues/17

shurillu commented 3 years ago

Hello Mendrzec, good work, but it does not handle the pad connection/disconnection. In the previous version, the PS4.isConnected() return true when a pad is connected and false when is disconnected. In your implementation the PS4.isConnected() seems to return always true. I tried to initialize the is_active variable to false (file ps4.c, row 26) and it works only for the first time (programming/resetting after programmed the ESP32): the PS4.isConnected() return false on boot and change to true when a pad is connected. But when I turn off the pad, the PS4.isConnected() is still true and when I reconnect the pad all data is messed up. (this makes me to think that there is no disconnction handling) If I have time, tomorrow I'll try to investigate. Cheers, Stefano

Mendrzec commented 3 years ago

Hi Stefano, Good catch, I missed connection/disconnection handling indeed. Sory for that, if you solve this, it would be really nice because I just based the PR on original PS3 repo and there isConnected is handled in a different way as far as I remember.

Best regards Mendrzec

pon., 27 wrz 2021, 19:26 użytkownik Stefano Ledda @.***> napisał:

Hello Mendrzec, good work, but it does not handle the pad connection/disconnection. In the previous version, the PS4.isConnected() return true when a pad is connected and false when is disconnected. In your implementation the PS4.isConnected() seems to return always true. I tried to initialize the is_active variable to false (file ps4.c, row 26) and it works only for the first time (programming/resetting after programmed the ESP32): the PS4.isConnected() return false on boot and change to true when a pad is connected. But when I turn off the pad, the PS4.isConnected() is still true and when I reconnect the pad all data is messed up. (this makes me to think that there is no disconnction handling) If I have time, tomorrow I'll try to investigate. Cheers, Stefano

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/aed3/PS4-esp32/pull/25#issuecomment-928094082, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEMRQRC5ESHIBYYAYDIDKPDUECSNNANCNFSM5EM3M25Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

shurillu commented 3 years ago

Yesterday I took a look at the PS3 library and in the L2CAP version it does not implement the reconnect option. So we need to implement it. I'm not an expert on Bluetooth, we will see ;-)

AhmadAkbari82 commented 3 years ago

please push your latest change I have some problem. The controller doesn't pair.

shurillu commented 3 years ago

please push your latest change I have some problem. The controller doesn't pair.

Hello AhmadAkbari82, if you want to use the Mendrzec's PR, my advice is: go here and download it (the Mendrzec's PR). Take in mind that sometimes the ESP32/DS4 won't connect at all and you have to fully erase the ESP32 FLASH memory. For example, with esptool, esptool.py --chip esp32 erase_flash Finally, check the matching of BT addresses (DS4 and ESP32)

Hoping this is useful,

Stefano

Mendrzec commented 3 years ago

@shurillu @AhmadAkbari82 I've just added disconnect/reconnect handling. Please check if it works for you and let me know.

JavadRah commented 3 years ago

Hi Thank you @Mendrzec! I checked in macOS and it doesn't work, Neither connected nor its connection handler works.

Mendrzec commented 3 years ago

@JavadRah Hmm that's rather unexpected that nothing works, did the library worked for you before my changes? What version of the esp32 sdk do you use? I used 2.0.0. Could you enable additional logging with modifying following levels to 5 in packages/esp32/hardware/2.0.0/tools/sdk/esp32/include/config/sdkconfig.h

#define CONFIG_ARDUHAL_LOG_DEFAULT_LEVEL 5
#define CONFIG_LOG_DEFAULT_LEVEL 5
#define CONFIG_LOG_MAXIMUM_LEVEL 5

and check serial output when trying to connect the controller?

@shurillu does the latest change (reconnect/disconnect handling) works for you?

saravaezi commented 3 years ago

@Mendrzec I have the same problem like @JavadRah, too.

JavadRah commented 3 years ago

@JavadRah Hmm that's rather unexpected that nothing works, did the library worked for you before my changes? What version of the esp32 sdk do you use? I used 2.0.0. Could you enable additional logging with modifying following levels to 5 in packages/esp32/hardware/2.0.0/tools/sdk/esp32/include/config/sdkconfig.h

#define CONFIG_ARDUHAL_LOG_DEFAULT_LEVEL 5
#define CONFIG_LOG_DEFAULT_LEVEL 5
#define CONFIG_LOG_MAXIMUM_LEVEL 5

and check serial output when trying to connect the controller?

@shurillu does the latest change (reconnect/disconnect handling) works for you?

I tried, here is the Serial output:

E (15) system_api: Base MAC must be a unicast MAC
I (684) PS4_L2CAP: [ps4_l2cap_init_service] Service PS4-HIDC Initialized
I (684) PS4_L2CAP: [ps4_l2cap_init_service] Service PS4-HIDI Initialized
RI (1683) PS4_SPP: ESP_SPP_INIT_EVT
eady.
Battery Level : 0

Battery Level : 0

Battery Level : 0
Mendrzec commented 3 years ago

@JavadRah Thanks for response. Looks like MAC address that you used in PS4.begin() is invalid and program didn't even reached the code responsible for connecting/reconnecting/disconnecting that I've been working on. See how to correctly pair with PS4 at https://github.com/aed3/PS4-esp32#pairing-the-ps4-controller.

JavadRah commented 3 years ago

@Mendrzec thank you for your response!

I have used this library before. I used the original example. I am a professional user, so I know how to use the library.😌😌

shurillu commented 3 years ago

Here I am, sorry but I were not at my home. Finally I checked the last version and man, you got it: it works perfectly! I tried several disconnection and works like a charm! Thanks a lot. Could I suggest a little change?: in the ps4.c file the is_active variable (row 26) should be initialized to false in order to start with no connected controllers. It will change accordingly when a DS4 is connected to the ESP32 Again, thank you for your excellent work.

Here my config:

First the ESP32 print out its own Bluetooth MAC address, then initialize the library with PS4.begin() and finally wait for incoming DS4 pair/connection. I changed the bt address stored in the DS4 accordingly using the SixaxisPairTool At this point every time I turn on the DS4, It will connect to the ESP32; every time I turn off the DS4, the ESP32 will wait for a new incoming connection.

Hoping this can help, cheers

Stefano

Mendrzec commented 3 years ago

@shurillu thanks for testing, glad it works for you. is_active should be false by default, gonna fix that, tanks! @JavadRah could you share the code and project configuration details so I can reprodcue the problem, thanks!

JavadRah commented 3 years ago

@Mendrzec & @shurillu Thank you for your information, I used the PS4connect example with this code:

#include <PS4Controller.h>

void setup() {
  Serial.begin(115200);

  // Replace the "03:03:03:03:03:03" with the MAC address
  // the controller wants to pair to
  PS4.begin("03:03:03:03:03:03");
  Serial.println("Ready.");
}

void loop() {
  if (PS4.isConnected()) {
    Serial.println("Connected!");
  }

  delay(3000);
}
Screen Shot 1400-07-11 at 14 23 36
Mendrzec commented 3 years ago

Hi thanks for sharing your program. You have to replace 03:03:03:03 with the actual MAC of your PS4 controller. In the readme you can find hints how to obtain one. I am going to fix default value for is_active and merge thw changes since your problem is not related to my changes. Cheers!

niedz., 3 paź 2021, 12:55 użytkownik Mohammad Javad Rahmani < @.***> napisał:

@Mendrzec https://github.com/Mendrzec & @shurillu https://github.com/shurillu Thank you for your information, I used the PS4connect example with this code:

include

void setup() { Serial.begin(115200);

// Replace the "03:03:03:03:03:03" with the MAC address // the controller wants to pair to PS4.begin("03:03:03:03:03:03"); Serial.println("Ready."); }

void loop() { if (PS4.isConnected()) { Serial.println("Connected!"); }

delay(3000); }

  • Arduino IDE 1.8.15
  • ESP32 toolchain 2.0.0
  • ESP32 dev module (ESP32Wroom32)
  • macOS bigsur

[image: Screen Shot 1400-07-11 at 14 23 36] https://user-images.githubusercontent.com/9278977/135750499-72d69a0a-3ce5-4124-a00a-442299117cc7.png

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/aed3/PS4-esp32/pull/25#issuecomment-932918253, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEMRQRFZP47KLO3Q7OVFHGLUFAZCLANCNFSM5EM3M25Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

JavadRah commented 3 years ago

@Mendrzec my controller is set to connect to "03:03:03:03:03:03" it works perfectly with esp32 v1.0.4 and @aed3 versions. Anyway, 03:03:03:03:03:03 is a unicast MAC, so what do you think about it?

Mendrzec commented 3 years ago

Alright, sorry, thought you missed the pairing step. I will try to reproduce the issue today and get back to you.

niedz., 3 paź 2021, 13:17 użytkownik Mohammad Javad Rahmani < @.***> napisał:

@Mendrzec https://github.com/Mendrzec my controller is set to connect to "03:03:03:03:03:03" it works perfectly with esp32 v1.0.4 and @aed3 https://github.com/aed3 versions. Anyway, 03:03:03:03:03:03 is a unicast MAC, so what do you think about it?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/aed3/PS4-esp32/pull/25#issuecomment-932929986, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEMRQRAQCDGBUNPT2I7EW43UFA3TXANCNFSM5EM3M25Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

Mendrzec commented 3 years ago

One more thing could you change the adress to something else? Looks like 03:03:03... is actually a multicast see https://en.m.wikipedia.org/wiki/MAC_address. Maybe l2cap doesnt allow multicast a that is the issue

niedz., 3 paź 2021, 13:24 użytkownik Sławomir Lewandowski < @.***> napisał:

Alright, sorry, thought you missed the pairing step. I will try to reproduce the issue today and get back to you.

niedz., 3 paź 2021, 13:17 użytkownik Mohammad Javad Rahmani < @.***> napisał:

@Mendrzec https://github.com/Mendrzec my controller is set to connect to "03:03:03:03:03:03" it works perfectly with esp32 v1.0.4 and @aed3 https://github.com/aed3 versions. Anyway, 03:03:03:03:03:03 is a unicast MAC, so what do you think about it?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/aed3/PS4-esp32/pull/25#issuecomment-932929986, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEMRQRAQCDGBUNPT2I7EW43UFA3TXANCNFSM5EM3M25Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

Mendrzec commented 3 years ago

@JavadRah I had a quick look into espressif esp32 libraries and found the reason of the error you see. Since esp-idf version 4.1 function esp_base_mac_addr_set() requires MAC to be unicast. In ESP32 toolchain 2.0.0 even higher version of esp-idf is used. Any lower version of ESP32 toolchain than 2.0.0 uses esp-idf (like v3.3.5) that doesn't require unicast. See those for reference: https://github.com/espressif/arduino-esp32/releases/tag/2.0.0 https://github.com/espressif/arduino-esp32/releases/tag/1.0.6 https://docs.espressif.com/projects/esp-idf/en/v3.3.5/api-reference/system/system.html#_CPPv421esp_base_mac_addr_setP7uint8_t https://docs.espressif.com/projects/esp-idf/en/v4.1/api-reference/system/system.html#_CPPv421esp_base_mac_addr_setPK7uint8_t

JavadRah commented 3 years ago

@Mendrzec thank you all! It works fine. Please change the example mac to something like :"1a:2b:3c:01:01:01"

JavadRah commented 3 years ago

@Mendrzec today I checked the SendData example and I figure out that the send data function doesn't work. So I put the complete output for you:


19:38:56.609 -> mode:DIO, clock div:1
19:38:56.609 -> load:0x3fff0030,len:1240
19:38:56.609 -> load:0x40078000,len:13012
19:38:56.609 -> load:0x40080400,len:3648
19:38:56.643 -> entry 0x400805f8
19:38:57.646 -> I (726) PS4_L2CAP: [ps4_l2cap_init_service] Service PS4-HIDC Initialized
19:38:57.646 -> I (727) PS4_L2CAP: [ps4_l2cap_init_service] Service PS4-HIDI Initialized
19:38:57.646 -> RI (1769) PS4_SPP: ESP_SPP_INIT_EVT
19:38:57.646 -> eady.
19:39:04.599 -> I (8718) PS4_L2CAP: [ps4_l2cap_connect_ind_cback] bd_addr: ⸮⸮⸮b⸮
19:39:04.599 ->   l2cap_cid: 0x41
19:39:04.599 ->   psm: 17
19:39:04.599 ->   id: 4
19:39:04.599 -> I (8738) PS4_L2CAP: [ps4_l2cap_config_ind_cback] l2cap_cid: 0x41
19:39:04.632 ->   p_cfg->result: 0
19:39:04.632 ->   p_cfg->mtu_present: 1
19:39:04.632 ->   p_cfg->mtu: 672
19:39:04.632 -> I (8740) PS4_L2CAP: [ps4_l2cap_config_cfm_cback] l2cap_cid: 0x41
19:39:04.632 ->   p_cfg->result: 0
19:39:04.632 -> I (8748) PS4_L2CAP: [ps4_l2cap_connect_ind_cback] bd_addr: ⸮⸮⸮b⸮
19:39:04.632 ->   l2cap_cid: 0x40
19:39:04.632 ->   psm: 19
19:39:04.632 ->   id: 6
19:39:04.632 -> I (8764) PS4_L2CAP: [ps4_l2cap_config_ind_cback] l2cap_cid: 0x40
19:39:04.632 ->   p_cfg->result: 0
19:39:04.632 ->   p_cfg->mtu_present: 1
19:39:04.632 ->   p_cfg->mtu: 672
19:39:04.666 -> I (8768) PS4_L2CAP: [ps4_l2cap_config_cfm_cback] l2cap_cid: 0x40
19:39:04.666 ->   p_cfg->result: 0
19:39:04.666 -> I (8775) PS4_L2CAP: [ps4_l2cap_send_hid] sending command: success
19:39:04.800 -> I (8920) PS4_L2CAP: [ps4_l2cap_send_hid] sending command: success
19:39:04.800 -> I (8925) PS4_L2CAP: [ps4_l2cap_send_hid] sending command: success
19:39:04.800 -> I (8935) PS4_L2CAP: [ps4_l2cap_send_hid] sending command: success
19:39:04.836 -> I (8945) PS4_L2CAP: [ps4_l2cap_send_hid] sending command: success
19:39:04.836 -> I (8955) PS4_L2CAP: [ps4_l2cap_send_hid] sending command: success
19:39:04.836 -> I (8965) PS4_L2CAP: [ps4_l2cap_send_hid] sending command: success
19:39:04.836 -> I (8975) PS4_L2CAP: [ps4_l2cap_send_hid] sending command: success
19:39:04.869 -> I (8985) PS4_L2CAP: [ps4_l2cap_send_hid] sending command: success
19:39:04.869 -> I (8995) PS4_L2CAP: [ps4_l2cap_send_hid] sending command: success
19:39:04.869 -> I (9005) PS4_L2CAP: [ps4_l2cap_send_hid] sending command: success
19:39:04.902 -> I (9015) PS4_L2CAP: [ps4_l2cap_send_hid] sending command: success
19:39:04.902 -> I (9025) PS4_L2CAP: [ps4_l2cap_send_hid] sending command: success
19:39:04.902 -> I (9035) PS4_L2CAP: [ps4_l2cap_send_hid] sending command: success
19:39:04.902 -> I (9045) PS4_L2CAP: [ps4_l2cap_send_hid] sending command: success
19:39:04.939 -> I (9055) PS4_L2CAP: [ps4_l2cap_send_hid] sending command: success
19:39:04.939 -> I (9065) PS4_L2CAP: [ps4_l2cap_send_hid] sending command: success
19:39:04.939 -> I (9075) PS4_L2CAP: [ps4_l2cap_send_hid] sending command: success
19:39:04.972 -> I (9085) PS4_L2CAP: [ps4_l2cap_send_hid] sending command: success
19:39:04.972 -> I (9095) PS4_L2CAP: [ps4_l2cap_send_hid] sending command: success
19:39:04.972 -> I (9105) PS4_L2CAP: [ps4_l2cap_send_hid] sending command: success
19:39:04.972 -> I (9115) PS4_L2CAP: [ps4_l2cap_send_hid] sending command: success
19:39:05.006 -> I (9125) PS4_L2CAP: [ps4_l2cap_send_hid] sending command: success
19:39:05.006 -> E (9135) PS4_L2CAP: [ps4_l2cap_send_hid] allocating buffer for sending the command failed
19:39:05.006 -> Guru Meditation Error: Core  1 panic'ed (StoreProhibited). Exception was unhandled.
19:39:05.040 -> 
19:39:05.040 -> Core  1 register dump:
19:39:05.040 -> PC      : 0x400d1fff  PS      : 0x00060730  A0      : 0x800d1bf7  A1      : 0x3ffcdec0  
19:39:05.040 -> A2      : 0x3ffcdefd  A3      : 0x0000004f  A4      : 0x00000000  A5      : 0x00000015  
19:39:05.040 -> A6      : 0x000000ea  A7      : 0x00000000  A8      : 0x800d1ffa  A9      : 0x3ffcde70  
19:39:05.040 -> A10     : 0x3ffcde70  A11     : 0x3f400140  A12     : 0x0000004f  A13     : 0x000023af  
19:39:05.073 -> A14     : 0x3f400140  A15     : 0x3f40046c  SAR     : 0x00000004  EXCCAUSE: 0x0000001d  
19:39:05.073 -> EXCVADDR: 0x00000002  LBEG    : 0x4008fcfd  LEND    : 0x4008fd0d  LCOUNT  : 0xfffffffb  
19:39:05.073 -> 
19:39:05.073 -> 
19:39:05.073 -> Backtrace:0x400d1ffc:0x3ffcdec00x400d1bf4:0x3ffcdee0 0x400d1c89:0x3ffcdf70 0x400d2831:0x3ffcdfa0 0x400d1b56:0x3ffcdfc0 0x400d3974:0x3ffcdfe0 
19:39:05.073 -> 
19:39:05.073 -> 
19:39:05.073 -> 
19:39:05.073 -> 
19:39:05.073 -> ELF file SHA256: 0000000000000000
19:39:05.073 -> 
19:39:05.073 -> Rebooting...
19:39:05.106 -> ets Jun  8 2016 00:22:57
19:39:05.106 -> 
19:39:05.106 -> rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
19:39:05.106 -> configsip: 0, SPIWP:0xee
19:39:05.106 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
19:39:05.106 -> mode:DIO, clock div:1
19:39:05.106 -> load:0x3fff0030,len:1240
19:39:05.106 -> load:0x40078000,len:13012
19:39:05.106 -> load:0x40080400,len:3648
19:39:05.106 -> entry 0x400805f8
19:39:06.109 -> I (710) PS4_L2CAP: [ps4_l2cap_init_service] Service PS4-HIDC Initialized
19:39:06.109 -> I (711) PS4_L2CAP: [ps4_l2cap_init_service] Service PS4-HIDI Initialized
19:39:06.143 -> RI (1737) PS4_SPP: ESP_SPP_INIT_EVT
19:39:06.143 -> eady.

anyway, I was trying to write another program, so I just send setLED command, I receive just: PS4_L2CAP: [ps4_l2cap_send_hid] sending command: success nothing else happened.

JavadRah commented 3 years ago

Update: I wonder that changing the delay at the final line to 100 can solve the problem. (in senddata example) I play with the delay(add some delay before setLED and setRumble, etc...) and I guess I can only send a limited amount of commands in an amount of time.