50using namespace gamepad::input;
69constexpr auto kExpectedButtonMask = Button::kStart | Button::kCrossA;
75const std::map<Button, std::string> kButtonNames{
77 {Button::kDown,
"Down"},
78 {Button::kLeft,
"Left"},
79 {Button::kRight,
"Right"},
80 {Button::kSquareX,
"Square(X)"},
81 {Button::kTriangleY,
"Triangle(Y)"},
82 {Button::kCrossA,
"Cross(A)"},
83 {Button::kCircleB,
"Circle(B)"},
90 {Button::kSelect,
"Select"},
91 {Button::kStart,
"Start"},
92 {Button::kHome,
"Home"},
98 CLOGI(
"Start to scan and connect, button mask: 0x%08" PRIX32,
static_cast<uint32_t
>(kExpectedButtonMask));
100 while (!g_codex_pad_client.
Connect(kExpectedButtonMask)) {
101 CLOGI(
"Retry to scan and connect, button mask: 0x%08" PRIX32,
static_cast<uint32_t
>(kExpectedButtonMask));
109 if (
const auto ble_client = g_codex_pad_client.
ble_client(); ble_client !=
nullptr) {
110 CLOGI(
"Remote Bluetooth Device Address: %s", ble_client->getPeerAddress().toString().c_str());
112 CLOGI(
"Remote Bluetooth Device Address: unknown");
119 static uint64_t s_last_time = 0;
121 if (s_last_time == 0 || millis() - s_last_time > 1000) {
122 const int32_t rssi = g_codex_pad_client.
rssi();
125 CLOGI(
"rssi: %" PRId32
" dBm, | [#][#][#][#] | strong", rssi);
126 }
else if (rssi >= -80) {
127 CLOGI(
"rssi: %" PRId32
" dBm, | [#][#][#][ ] | decent", rssi);
128 }
else if (rssi >= -95) {
129 CLOGW(
"rssi: %" PRId32
" dBm, | [#][#][ ][ ] | weak", rssi);
131 CLOGW(
"rssi: %" PRId32
" dBm, | [#][ ][ ][ ] | critical", rssi);
134 CLOGI(
"rssi: unknown\n");
137 s_last_time = millis();
143 Serial.begin(115200);
150 const int8_t tx_power = 9;
151 g_codex_pad_client.
Init(tx_power);
178 const gamepad::input::Tracker& it = g_codex_pad_client.
Update();
194 CLOGW(
"Disconnected, start to reconnect");
218 static std::map<Button, uint64_t> s_button_last_time;
220 for (
const auto& [button, name] : kButtonNames) {
221 if (it.pressed(button)) {
222 CLOGI(
"Button mask: 0x%08" PRIX32
", button %s: pressed", it.button_mask(), name.c_str());
223 s_button_last_time[button] = millis();
224 }
else if (it.released(button)) {
225 CLOGI(
"Button mask: 0x%08" PRIX32
", button %s: released", it.button_mask(), name.c_str());
226 s_button_last_time[button] = millis();
227 }
else if (it.holding(button)) {
228 if (s_button_last_time.find(button) == s_button_last_time.end() || s_button_last_time[button] == 0 ||
229 millis() - s_button_last_time[button] > 100) {
230 CLOGI(
"Button mask: 0x%08" PRIX32
", button %s: holding", it.button_mask(), name.c_str());
231 s_button_last_time[button] = millis();
247 constexpr uint8_t kAxisValueChangeThreshold = 5;
249 if (it.AxisChanged(Axis::kLeftStickX, kAxisValueChangeThreshold) ||
250 it.AxisChanged(Axis::kLeftStickY, kAxisValueChangeThreshold) ||
251 it.AxisChanged(Axis::kRightStickX, kAxisValueChangeThreshold) ||
252 it.AxisChanged(Axis::kRightStickY, kAxisValueChangeThreshold)) {
253 CLOGI(
"L(X: %3" PRIu8
", Y:%3" PRIu8
"), R(X: %3" PRIu8
", Y: %3" PRIu8
")", it[Axis::kLeftStickX], it[Axis::kLeftStickY],
254 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.
void Init(int8_t local_tx_power=3) noexcept
Initialize.
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.
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.