#include "codex_pad.h"
using namespace gamepad::input;
namespace {
const std::string kBluetoothDeviceAddress = "E4:66:E5:A2:17:06";
std::string ButtonToString(Button button) {
switch (button) {
case Button::kUp: {
return "Up";
}
case Button::kDown: {
return "Down";
}
case Button::kLeft: {
return "Left";
}
case Button::kRight: {
return "Right";
}
case Button::kSquareX: {
return "Square(X)";
}
case Button::kTriangleY: {
return "Triangle(Y)";
}
case Button::kCrossA: {
return "Cross(A)";
}
case Button::kCircleB: {
return "Circle(B)";
}
case Button::kL1: {
return "L1";
}
case Button::kL2: {
return "L2";
}
case Button::kL3: {
return "L3";
}
case Button::kR1: {
return "R1";
}
case Button::kR2: {
return "R2";
}
case Button::kR3: {
return "R3";
}
case Button::kSelect: {
return "Select";
}
case Button::kStart: {
return "Start";
}
case Button::kHome: {
return "Home";
}
default: {
return {};
}
}
}
void Connect() {
printf("Start to connect %s\n", kBluetoothDeviceAddress.c_str());
while (!g_codex_pad.
Connect(kBluetoothDeviceAddress, 5000)) {
printf("Retry to connect %s\n", kBluetoothDeviceAddress.c_str());
}
if (
const auto ble_client = g_codex_pad.
ble_client(); ble_client !=
nullptr) {
printf("Remote Bluetooth Device Address: %s\n", ble_client->getPeerAddress().toString().c_str());
} else {
printf("Remote Bluetooth Device Address: unknown\n");
}
printf("Set remote tx power to 0dBm successfully\n");
}
printf("Connected\n");
}
}
void setup() {
Serial.begin(115200);
printf("Init\n");
g_codex_pad.Init();
Connect();
}
void loop() {
const gamepad::input::Tracker& it = g_codex_pad.Update();
if (!g_codex_pad.is_connected()) {
printf("Disconnected, start to reconnect\n");
Connect();
return;
}
for (auto button : {Button::kUp, Button::kDown, Button::kLeft, Button::kRight, Button::kSquareX, Button::kTriangleY,
Button::kCrossA, Button::kCircleB, Button::kL1, Button::kL2, Button::kL3, Button::kR1, Button::kR2,
Button::kR3, Button::kSelect, Button::kStart, Button::kHome}) {
if (it.pressed(button)) {
printf("Button %s: pressed\n", ButtonToString(button).c_str());
} else if (it.released(button)) {
printf("Button %s: released\n", ButtonToString(button).c_str());
} else if (it.holding(button)) {
printf("Button %s: holding\n", ButtonToString(button).c_str());
}
}
constexpr uint8_t kAxisValueChangeThreshold = 2;
if (it.AxisChanged(Axis::kLeftStickX, kAxisValueChangeThreshold) ||
it.AxisChanged(Axis::kLeftStickY, kAxisValueChangeThreshold) ||
it.AxisChanged(Axis::kRightStickX, kAxisValueChangeThreshold) ||
it.AxisChanged(Axis::kRightStickY, kAxisValueChangeThreshold)) {
printf("L(X: %3" PRIu8 ", Y:%3" PRIu8 "), R(X: %3" PRIu8 ", Y: %3" PRIu8 ")\n", it[Axis::kLeftStickX],
it[Axis::kLeftStickY], it[Axis::kRightStickX], it[Axis::kRightStickY]);
}
}
bool Connect(const std::string &bluetooth_device_address, uint32_t timeout_ms=5000) noexcept
连接
const std::array< uint8_t, 3 > & remote_firmware_version() const noexcept
获取CodexPad的固件版本
const std::string & remote_model_number() const noexcept
获取CodexPad的型号
NimBLEClient * ble_client() const noexcept
获取 BLE 客户端对象
const std::string & remote_device_name() const noexcept
获取CodexPad的型号
bool set_remote_tx_power(TxPower power) noexcept
设置发射功率,连接状态下调用,立即生效于当前连接,下次连接生效