Demonstrates how to scan for and connect to a CodexPad device by matching specific button presses.
Demonstrates how to scan for and connect to a CodexPad device by matching specific button presses. This example shows the usage of the Connect() function. The device will scan for nearby CodexPad devices and automatically connect to one where the operator is holding down the predefined combination of buttons (the button mask). The button mask is defined by the kExpectedButtonMask constant. You must physically press and hold the corresponding button(s) on the target CodexPad for the connection to succeed.
#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));
while (!g_codex_pad_client.
Connect(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.button_mask(), name.c_str());
s_button_last_time[button] = millis();
} else if (it.released(button)) {
CLOGI("Button mask: 0x%08" PRIX32 ", button %s: released", it.button_mask(), 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.button_mask(), 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
Get model number of the CodexPad.
const std::array< uint8_t, 3 > & remote_firmware_version() const noexcept
Get firmware version of the CodexPad.
NimBLEClient * ble_client() const noexcept
Get the BLE client object.
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.