Подключаем геймпад PS3 к ESP32
Контроллеры некоторых игровых приставок, например, PlayStation, имеют беспроводную связь, благодаря чему ими можно воспользоваться в радиолюбительских проектах, подключив к ESP32.

В данном материале будет объяснено, как подключить контроллер PS3 к ESP32, используя ядро Arduino и специальную библиотеку (https://github.com/jvpernis/esp32-ps3). Соединение между устройствами будет установлено с использованием протокола Bluetooth.
Сначала подключите контроллер PS3 к ПК, запишите MAC-адрес для контроллера PS3, используя SixaxisPairTool.


Можно установить любое число, например, 11:22:33:44:55:66
Клонируйте компонент ps3 в esp-idf.

Возьмите пример проекта. Настройте проект, можно использовать другую версию esp-idf с jvpernis.



Скомпилируйте и прошейте код в ESP32.

Соединитесь с ps3 и проверьте выход последовательного порта ESP32.

Код ESP32 следующий:
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "ps3.h"
void controller_event_cb( ps3_t ps3, ps3_event_t event )
{
// Event handling here...
if ( event.button_down.cross )
printf("The user started pressing the X button\r\n");
if ( event.button_up.cross )
printf("The user released the X button\r\n");
if ( event.button_down.square )
printf("The user started pressing the square button\r\n");
if ( event.button_up.square )
printf("The user released the square button\r\n");
if ( event.button_down.triangle )
printf("The user started pressing the triangle button\r\n");
if ( event.button_up.triangle )
printf("The user released the triangle button\r\n");
if ( event.button_down.circle )
printf("The user started pressing the circle button\r\n");
if ( event.button_up.circle )
printf("The user released the circle button\r\n");
if ( event.button_down.up )
printf("The user started pressing the up button\r\n");
if ( event.button_up.up )
printf("The user released the up button\r\n");
if ( event.button_down.down )
printf("The user started pressing the down button\r\n");
if ( event.button_up.down )
printf("The user released the down button\r\n");
if ( event.button_down.left )
printf("The user started pressing the left button\r\n");
if ( event.button_up.left )
printf("The user released the left button\r\n");
if ( event.button_down.right )
printf("The user started pressing the right button\r\n");
if ( event.button_up.right )
printf("The user released the right button\r\n");
if ( event.button_down.l1 )
printf("The user started pressing the l1 button\r\n");
if ( event.button_up.l1 )
printf("The user released the l1 button\r\n");
if ( event.button_down.l2 )
printf("The user started pressing the l2 button\r\n");
if ( event.button_up.l2 )
printf("The user released the l2 button\r\n");
if ( event.button_down.r1 )
printf("The user started pressing the r1 button\r\n");
if ( event.button_up.r1 )
printf("The user released the r1 button\r\n");
if ( event.button_down.r2 )
printf("The user started pressing the r2 button\r\n");
if ( event.button_up.r2 )
printf("The user released the r2 button\r\n");
}
void app_main()
{
printf("Hello world!\n");
ps3SetEventCallback(controller_event_cb);
uint8_t new_mac[8] = {0x11,0x22,0x33,0x44,0x55,0x66};
ps3SetBluetoothMacAddress(new_mac);
ps3Init();
while (!ps3IsConnected()){
// Prevent the Task Watchdog from triggering
vTaskDelay(10 / portTICK_PERIOD_MS);
}
printf ("connected\r\n");
while(1){
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
esp_restart();
}
То же самое можно проделать в среде Arduino. Добавьте пакет esp32 json в менеджере плат.

Установите плату ESP32 в Arduino IDE.

Загрузите библиотеку ps3 arduino (https://codeload.github.com/jvpernis/esp32-ps3/zip/develop) и добавьте эту zip-библиотеку. Запустите пример кода.
#include <Ps3Controller.h>
void setup()
{
Serial.begin(115200);
Ps3.begin("11:22:33:44:55:66");
Serial.println("Ready.");
}
void loop()
{
if (Ps3.isConnected()){
if( Ps3.data.button.cross ){
Serial.println("Pressing the cross button");
}
if( Ps3.data.button.square ){
Serial.println("Pressing the square button");
}
if( Ps3.data.button.triangle ){
Serial.println("Pressing the triangle button");
}
if( Ps3.data.button.circle ){
Serial.println("Pressing the circle button");
}
}
}
Откройте последовательный порт и убедитесь, что все работает.

© digitrode.ru