#include <map>
#include "codex_pad.h"
#include "cyf.h"
#include "cyf/log.h"
using namespace gamepad::input;
namespace {
constexpr auto kExpectedButtonMask = Button::kStart | Button::kCrossA;
const std::map<Button, std::string> kButtonNames{
{Button::kUp, "Up"},
{Button::kDown, "Down"},
{Button::kLeft, "Left"},
{Button::kRight, "Right"},
{Button::kSquareX, "Square(X)"},
{Button::kTriangleY, "Triangle(Y)"},
{Button::kCrossA, "Cross(A)"},
{Button::kCircleB, "Circle(B)"},
{Button::kL1, "L1"},
{Button::kR1, "R1"},
{Button::kL2, "L2"},
{Button::kR2, "R2"},
{Button::kL3, "L3"},
{Button::kR3, "R3"},
{Button::kSelect, "Select"},
{Button::kStart, "Start"},
{Button::kHome, "Home"},
};
void Connect() {
CLOGI("Start to scan and connect, button mask: 0x%08" PRIX32, static_cast<uint32_t>(kExpectedButtonMask));
CLOGI("Retry to scan and connect, button mask: 0x%08" PRIX32, static_cast<uint32_t>(kExpectedButtonMask));
}
if (
const auto ble_client = g_codex_pad_client.
ble_client(); ble_client !=
nullptr) {
CLOGI("Remote Bluetooth Device Address: %s", ble_client->getPeerAddress().toString().c_str());
} else {
CLOGI("Remote Bluetooth Device Address: unknown");
}
CLOGI("Connected");
}
void RssiMonitor() {
static uint64_t s_last_time = 0;
if (s_last_time == 0 || millis() - s_last_time > 1000) {
const int32_t rssi = g_codex_pad_client.
rssi();
if (rssi != 0) {
if (rssi >= -65) {
CLOGI("rssi: %" PRId32 " dBm, | [#][#][#][#] | strong", rssi);
} else if (rssi >= -80) {
CLOGI("rssi: %" PRId32 " dBm, | [#][#][#][ ] | decent", rssi);
} else if (rssi >= -95) {
CLOGW("rssi: %" PRId32 " dBm, | [#][#][ ][ ] | weak", rssi);
} else {
CLOGW("rssi: %" PRId32 " dBm, | [#][ ][ ][ ] | critical", rssi);
}
} else {
CLOGI("rssi: unknown\n");
}
s_last_time = millis();
}
}
}
void setup() {
Serial.begin(115200);
CLOGI("Init");
const int8_t tx_power = 9;
g_codex_pad_client.Init(tx_power);
Connect();
}
void loop() {
const gamepad::input::Tracker& it = g_codex_pad_client.Update();
if (!g_codex_pad_client.is_connected()) {
CLOGW("Disconnected, start to reconnect");
Connect();
return;
}
RssiMonitor();
static std::map<Button, uint64_t> s_button_last_time;
for (const auto& [button, name] : kButtonNames) {
if (it.pressed(button)) {
CLOGI("Button mask: 0x%08" PRIX32 ", button %s: pressed", it.raw().buttons, name.c_str());
s_button_last_time[button] = millis();
} else if (it.released(button)) {
CLOGI("Button mask: 0x%08" PRIX32 ", button %s: released", it.raw().buttons, name.c_str());
s_button_last_time[button] = millis();
} else if (it.holding(button)) {
if (s_button_last_time.find(button) == s_button_last_time.end() || s_button_last_time[button] == 0 ||
millis() - s_button_last_time[button] > 100) {
CLOGI("Button mask: 0x%08" PRIX32 ", button %s: holding", it.raw().buttons, name.c_str());
s_button_last_time[button] = millis();
}
}
}
constexpr uint8_t kAxisValueChangeThreshold = 5;
if (it.AxisChanged(Axis::kLeftStickX, kAxisValueChangeThreshold) ||
it.AxisChanged(Axis::kLeftStickY, kAxisValueChangeThreshold) ||
it.AxisChanged(Axis::kRightStickX, kAxisValueChangeThreshold) ||
it.AxisChanged(Axis::kRightStickY, kAxisValueChangeThreshold)) {
CLOGI("L(X: %3" PRIu8 ", Y:%3" PRIu8 "), R(X: %3" PRIu8 ", Y: %3" PRIu8 ")", it[Axis::kLeftStickX], it[Axis::kLeftStickY],
it[Axis::kRightStickX], it[Axis::kRightStickY]);
}
}
const std::string & remote_device_name() const noexcept
获取CodexPad的型号
const std::array< uint8_t, 3 > & remote_firmware_version() const noexcept
获取CodexPad的固件版本
NimBLEClient * ble_client() const noexcept
获取 BLE 客户端对象
bool ScanAndConnect(gamepad::input::Button buttons) noexcept
扫描附近的 CodexPad 设备,并自动连接到一个按键状态与指定掩码匹配的设备。
const std::string & remote_model_number() const noexcept
获取CodexPad的型号
int32_t rssi() const noexcept
获取CodexPad的RSSI