50using namespace gamepad::input;
55const std::string kBluetoothDeviceAddress =
"E4:66:E5:A2:17:06";
57const std::map<Button, std::string> kButtonNames{
59 {Button::kDown,
"Down"},
60 {Button::kLeft,
"Left"},
61 {Button::kRight,
"Right"},
62 {Button::kSquareX,
"Square(X)"},
63 {Button::kTriangleY,
"Triangle(Y)"},
64 {Button::kCrossA,
"Cross(A)"},
65 {Button::kCircleB,
"Circle(B)"},
72 {Button::kSelect,
"Select"},
73 {Button::kStart,
"Start"},
74 {Button::kHome,
"Home"},
80 CLOGI(
"Start to connect %s", kBluetoothDeviceAddress.c_str());
83 while (!g_codex_pad_client.
Connect(kBluetoothDeviceAddress, 5000)) {
84 CLOGI(
"Retry to connect %s", kBluetoothDeviceAddress.c_str());
92 if (
const auto ble_client = g_codex_pad_client.
ble_client(); ble_client !=
nullptr) {
93 CLOGI(
"Remote Bluetooth Device Address: %s", ble_client->getPeerAddress().toString().c_str());
95 CLOGI(
"Remote Bluetooth Device Address: unknown");
102 static uint64_t s_last_time = 0;
104 if (s_last_time == 0 || millis() - s_last_time > 1000) {
105 const int32_t rssi = g_codex_pad_client.
rssi();
108 CLOGI(
"rssi: %" PRId32
" dBm, | [#][#][#][#] | strong", rssi);
109 }
else if (rssi >= -80) {
110 CLOGI(
"rssi: %" PRId32
" dBm, | [#][#][#][ ] | decent", rssi);
111 }
else if (rssi >= -95) {
112 CLOGW(
"rssi: %" PRId32
" dBm, | [#][#][ ][ ] | weak", rssi);
114 CLOGW(
"rssi: %" PRId32
" dBm, | [#][ ][ ][ ] | critical", rssi);
117 CLOGI(
"rssi: unknown\n");
120 s_last_time = millis();
126 Serial.begin(115200);
129 g_codex_pad_client.
Init();
156 const gamepad::input::Tracker& it = g_codex_pad_client.
Update();
172 CLOGW(
"Disconnected, start to reconnect");
197 static std::map<Button, uint64_t> s_button_last_time;
199 for (
const auto& [button, name] : kButtonNames) {
200 if (it.pressed(button)) {
201 CLOGI(
"Button mask: 0x%08" PRIX32
", button %s: pressed", it.raw().buttons, name.c_str());
202 s_button_last_time[button] = millis();
203 }
else if (it.released(button)) {
204 CLOGI(
"Button mask: 0x%08" PRIX32
", button %s: released", it.raw().buttons, name.c_str());
205 s_button_last_time[button] = millis();
206 }
else if (it.holding(button)) {
207 if (s_button_last_time.find(button) == s_button_last_time.end() || s_button_last_time[button] == 0 ||
208 millis() - s_button_last_time[button] > 100) {
209 CLOGI(
"Button mask: 0x%08" PRIX32
", button %s: holding", it.raw().buttons, name.c_str());
210 s_button_last_time[button] = millis();
226 constexpr uint8_t kAxisValueChangeThreshold = 5;
228 if (it.AxisChanged(Axis::kLeftStickX, kAxisValueChangeThreshold) ||
229 it.AxisChanged(Axis::kLeftStickY, kAxisValueChangeThreshold) ||
230 it.AxisChanged(Axis::kRightStickX, kAxisValueChangeThreshold) ||
231 it.AxisChanged(Axis::kRightStickY, kAxisValueChangeThreshold)) {
232 CLOGI(
"L(X: %3" PRIu8
", Y:%3" PRIu8
"), R(X: %3" PRIu8
", Y: %3" PRIu8
")", it[Axis::kLeftStickX], it[Axis::kLeftStickY],
233 it[Axis::kRightStickX], it[Axis::kRightStickY]);
const gamepad::input::Tracker & Update() noexcept
Update, need to be called in Loop.
const std::string & remote_device_name() const noexcept
Get model number of the CodexPad.
const std::array< uint8_t, 3 > & remote_firmware_version() const noexcept
Get firmware version of the CodexPad.
bool is_connected() const noexcept
Is connected.
NimBLEClient * ble_client() const noexcept
Get the BLE client object.
void Init() noexcept
Initialize.
bool Connect(const std::string &bluetooth_device_address, uint32_t timeout_ms=5000) noexcept
Connect.
const std::string & remote_model_number() const noexcept
Get model number of the CodexPad.
int32_t rssi() const noexcept
Get the RSSI of the CodexPad.