Demonstrates the basic polling method to periodically query and print all CodexPad button states and joystick values.
Demonstrates the basic polling method to periodically query and print all CodexPad button states and joystick values. This example establishes a connection to a specific CodexPad device (by Bluetooth Device Address) and implements a simple polling loop. Every 50 milliseconds, it queries and prints the current state (pressed/released) of all buttons and the raw analog values (0-255) of both joysticks. It showcases the fundamental usage of gamepad::input::Tracker[Button] for discrete button queries and gamepad::input::Tracker[Axis] for continuous joystick readings.
#include <string>
#include "codex_pad.h"
#include "cyf.h"
#include "cyf/log.h"
using namespace gamepad::input;
namespace {
const std::string kBluetoothDeviceAddress = "E4:66:E5:A2:17:06";
void Connect() {
CLOGI("Start to connect %s", kBluetoothDeviceAddress.c_str());
while (!g_codex_pad_client.
Connect(kBluetoothDeviceAddress, 5000)) {
CLOGI("Retry to connect %s", kBluetoothDeviceAddress.c_str());
}
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 setup() {
Serial.begin(115200);
printf("Init\n");
g_codex_pad_client.Init();
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\n");
Connect();
return;
}
static uint32_t s_print_time = 0;
if (s_print_time != 0 && s_print_time + 50 > millis()) {
return;
}
s_print_time = millis();
std::string log;
char buffer[512] = {0};
size_t length = snprintf(buffer, sizeof(buffer), "Button mask: 0x%08" PRIX32, static_cast<uint32_t>(it.raw().buttons));
log.append(buffer, length);
length = snprintf(buffer, sizeof(buffer), ", L(%3u, %3u), R(%3u, %3u)", it[Axis::kLeftStickX], it[Axis::kLeftStickY],
it[Axis::kRightStickX], it[Axis::kRightStickY]);
log.append(buffer, length);
if (it[Button::kUp]) {
log.append(", Up");
}
if (it[Button::kDown]) {
log.append(", Down");
}
if (it[Button::kLeft]) {
log.append(", Left");
}
if (it[Button::kRight]) {
log.append(", Right");
}
if (it[Button::kSquareX]) {
log.append(", Square(X)");
}
if (it[Button::kTriangleY]) {
log.append(", Triangle(Y)");
}
if (it[Button::kCrossA]) {
log.append(", Cross(A)");
}
if (it[Button::kCircleB]) {
log.append(", Circle(B)");
}
if (it[Button::kL1]) {
log.append(", L1");
}
if (it[Button::kL2]) {
log.append(", L2");
}
if (it[Button::kL3]) {
log.append(", L3");
}
if (it[Button::kR1]) {
log.append(", R1");
}
if (it[Button::kR2]) {
log.append(", R2");
}
if (it[Button::kR3]) {
log.append(", R3");
}
if (it[Button::kSelect]) {
log.append(", Select");
}
if (it[Button::kStart]) {
log.append(", Start");
}
if (it[Button::kHome]) {
log.append(", Home");
}
CLOGI("%s", log.c_str());
}
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.