187 lines
6.1 KiB
C++
187 lines
6.1 KiB
C++
#include "btstack_port_esp32.h"
|
|
#include "btstack_run_loop.h"
|
|
#include "btstack_stdio_esp32.h"
|
|
#include "uni.h"
|
|
|
|
#include "Board/ogxm_log.h"
|
|
#include "BTManager/BTManager.h"
|
|
|
|
void BTManager::init(int argc, const char** arg_V)
|
|
{
|
|
OGXM_LOG("BP32: Initializing Bluepad32\n");
|
|
}
|
|
|
|
void BTManager::init_complete_cb(void)
|
|
{
|
|
uni_bt_enable_new_connections_unsafe(true);
|
|
// uni_bt_del_keys_unsafe();
|
|
uni_property_dump_all();
|
|
}
|
|
|
|
uni_error_t BTManager::device_discovered_cb(bd_addr_t addr, const char* name, uint16_t cod, uint8_t rssi)
|
|
{
|
|
if (!((cod & UNI_BT_COD_MINOR_MASK) & UNI_BT_COD_MINOR_GAMEPAD))
|
|
{
|
|
return UNI_ERROR_IGNORE_DEVICE;
|
|
}
|
|
return UNI_ERROR_SUCCESS;
|
|
}
|
|
|
|
void BTManager::device_connected_cb(uni_hid_device_t* device)
|
|
{
|
|
OGXM_LOG("BP32: Device connected, addr: %p, index: %i\n", device, uni_hid_device_get_idx_for_instance(device));
|
|
}
|
|
|
|
void BTManager::device_disconnected_cb(uni_hid_device_t* device)
|
|
{
|
|
int idx = uni_hid_device_get_idx_for_instance(device);
|
|
if (idx >= MAX_GAMEPADS || idx < 0)
|
|
{
|
|
return;
|
|
}
|
|
manage_connection(static_cast<uint8_t>(idx), false);
|
|
}
|
|
|
|
uni_error_t BTManager::device_ready_cb(uni_hid_device_t* device)
|
|
{
|
|
int idx = uni_hid_device_get_idx_for_instance(device);
|
|
if (idx >= MAX_GAMEPADS || idx < 0)
|
|
{
|
|
return UNI_ERROR_IGNORE_DEVICE;
|
|
}
|
|
manage_connection(static_cast<uint8_t>(idx), true);
|
|
return UNI_ERROR_SUCCESS;
|
|
}
|
|
|
|
void BTManager::controller_data_cb(uni_hid_device_t* bp_device, uni_controller_t* controller)
|
|
{
|
|
static uni_gamepad_t prev_uni_gps[MAX_GAMEPADS] = {};
|
|
|
|
if (controller->klass != UNI_CONTROLLER_CLASS_GAMEPAD)
|
|
{
|
|
return;
|
|
}
|
|
|
|
int idx = uni_hid_device_get_idx_for_instance(bp_device);
|
|
uni_gamepad_t *uni_gp = &controller->gamepad;
|
|
|
|
if (idx < 0 || idx >= MAX_GAMEPADS || std::memcmp(uni_gp, &prev_uni_gps[idx], sizeof(uni_gamepad_t)) == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
I2CDriver::PacketIn& packet_in = devices_[idx].packet_in;
|
|
GamepadMapper& mapper = devices_[idx].mapper;
|
|
|
|
packet_in = I2CDriver::PacketIn();
|
|
packet_in.packet_id = I2CDriver::PacketID::SET_PAD;
|
|
packet_in.index = static_cast<uint8_t>(idx);
|
|
|
|
switch (uni_gp->dpad)
|
|
{
|
|
case DPAD_UP:
|
|
packet_in.dpad = mapper.DPAD_UP;
|
|
break;
|
|
case DPAD_DOWN:
|
|
packet_in.dpad = mapper.DPAD_DOWN;
|
|
break;
|
|
case DPAD_LEFT:
|
|
packet_in.dpad = mapper.DPAD_LEFT;
|
|
break;
|
|
case DPAD_RIGHT:
|
|
packet_in.dpad = mapper.DPAD_RIGHT;
|
|
break;
|
|
case (DPAD_UP | DPAD_RIGHT):
|
|
packet_in.dpad = mapper.DPAD_UP_RIGHT;
|
|
break;
|
|
case (DPAD_DOWN | DPAD_RIGHT):
|
|
packet_in.dpad = mapper.DPAD_DOWN_RIGHT;
|
|
break;
|
|
case (DPAD_DOWN | DPAD_LEFT):
|
|
packet_in.dpad = mapper.DPAD_DOWN_LEFT;
|
|
break;
|
|
case (DPAD_UP | DPAD_LEFT):
|
|
packet_in.dpad = mapper.DPAD_UP_LEFT;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
if (uni_gp->buttons & BUTTON_A) packet_in.buttons |= mapper.BUTTON_A;
|
|
if (uni_gp->buttons & BUTTON_B) packet_in.buttons |= mapper.BUTTON_B;
|
|
if (uni_gp->buttons & BUTTON_X) packet_in.buttons |= mapper.BUTTON_X;
|
|
if (uni_gp->buttons & BUTTON_Y) packet_in.buttons |= mapper.BUTTON_Y;
|
|
if (uni_gp->buttons & BUTTON_SHOULDER_L) packet_in.buttons |= mapper.BUTTON_LB;
|
|
if (uni_gp->buttons & BUTTON_SHOULDER_R) packet_in.buttons |= mapper.BUTTON_RB;
|
|
if (uni_gp->buttons & BUTTON_THUMB_L) packet_in.buttons |= mapper.BUTTON_L3;
|
|
if (uni_gp->buttons & BUTTON_THUMB_R) packet_in.buttons |= mapper.BUTTON_R3;
|
|
if (uni_gp->misc_buttons & MISC_BUTTON_BACK) packet_in.buttons |= mapper.BUTTON_BACK;
|
|
if (uni_gp->misc_buttons & MISC_BUTTON_START) packet_in.buttons |= mapper.BUTTON_START;
|
|
if (uni_gp->misc_buttons & MISC_BUTTON_SYSTEM) packet_in.buttons |= mapper.BUTTON_SYS;
|
|
if (uni_gp->misc_buttons & MISC_BUTTON_CAPTURE) packet_in.buttons |= mapper.BUTTON_MISC;
|
|
|
|
packet_in.trigger_l = mapper.trigger_l(uni_gp->brake);
|
|
packet_in.trigger_r = mapper.trigger_r(uni_gp->throttle);
|
|
|
|
packet_in.joystick_lx = mapper.joystick_lx(uni_gp->axis_x);
|
|
packet_in.joystick_ly = mapper.joystick_ly(uni_gp->axis_y);
|
|
packet_in.joystick_rx = mapper.joystick_rx(uni_gp->axis_rx);
|
|
packet_in.joystick_ry = mapper.joystick_ry(uni_gp->axis_ry);
|
|
|
|
i2c_driver_.write_packet(I2CDriver::MULTI_SLAVE ? packet_in.index + 1 : 0x01, packet_in);
|
|
|
|
std::memcpy(&prev_uni_gps[idx], uni_gp, sizeof(uni_gamepad_t));
|
|
}
|
|
|
|
const uni_property_t* BTManager::get_property_cb(uni_property_idx_t idx)
|
|
{
|
|
return nullptr;
|
|
}
|
|
|
|
void BTManager::oob_event_cb(uni_platform_oob_event_t event, void* data)
|
|
{
|
|
return;
|
|
}
|
|
|
|
uni_platform* BTManager::get_bp32_driver()
|
|
{
|
|
static uni_platform driver = {
|
|
.name = "OGXMiniW",
|
|
.init =
|
|
[](int argc, const char** argv)
|
|
{ get_instance().init(argc, argv); },
|
|
|
|
.on_init_complete =
|
|
[](void)
|
|
{ get_instance().init_complete_cb(); },
|
|
|
|
.on_device_discovered =
|
|
[](bd_addr_t addr, const char* name, uint16_t cod, uint8_t rssi)
|
|
{ return get_instance().device_discovered_cb(addr, name, cod, rssi); },
|
|
|
|
.on_device_connected =
|
|
[](uni_hid_device_t* device)
|
|
{ get_instance().device_connected_cb(device); },
|
|
|
|
.on_device_disconnected =
|
|
[](uni_hid_device_t* device)
|
|
{ get_instance().device_disconnected_cb(device); },
|
|
|
|
.on_device_ready =
|
|
[](uni_hid_device_t* device)
|
|
{ return get_instance().device_ready_cb(device); },
|
|
|
|
.on_controller_data =
|
|
[](uni_hid_device_t* device, uni_controller_t* controller)
|
|
{ get_instance().controller_data_cb(device, controller); },
|
|
|
|
.get_property =
|
|
[](uni_property_idx_t idx)
|
|
{ return get_instance().get_property_cb(idx); },
|
|
|
|
.on_oob_event =
|
|
[](uni_platform_oob_event_t event, void* data)
|
|
{ get_instance().oob_event_cb(event, data); },
|
|
};
|
|
return &driver;
|
|
} |