Demonstrate how to detect real-time button status and joystick movement of connected CodexPad devices.
Demonstrate how to detect real-time button status and joystick movement of connected CodexPad devices. This example first sends AT commands to establish a BLE connection to a CodexPad device (by Bluetooth Device Address), then continuously monitors all user inputs. It showcases the detection of three distinct button states: pressed (momentary press), released (momentary release), and holding (sustained press). It also monitors the analog joystick axes and prints their values when a significant change beyond a set threshold is detected, filtering out minor jitter.
#include <SoftwareSerial.h>
namespace {
constexpr uint8_t kDebugSerialRxPin = 6;
constexpr uint8_t kDebugSerialTxPin = 5;
constexpr uint8_t kAxisThreshold = 2;
const String kBluetoothDeviceAddress = "0C:3D:5E:9D:80:99";
constexpr uint32_t kBluetoothModuleSerialBaudRate = 115200;
SoftwareSerial g_debug_serial(kDebugSerialRxPin, kDebugSerialTxPin);
switch (button) {
return "Up";
}
return "Down";
}
return "Left";
}
return "Right";
}
return "Square(X)";
}
return "Triangle(Y)";
}
return "Cross(A)";
}
return "Circle(B)";
}
return "L1";
}
return "L2";
}
return "L3";
}
return "R1";
}
return "R2";
}
return "R3";
}
return "Select";
}
return "Start";
}
return "Home";
}
default: {
return {};
}
}
}
void Connect(Stream &bluetooth_stream, const String &bluetooth_device_address) {
if (bluetooth_device_address.length() != 17 || bluetooth_device_address[2] != ':' || bluetooth_device_address[5] != ':' ||
bluetooth_device_address[8] != ':' || bluetooth_device_address[11] != ':' || bluetooth_device_address[14] != ':') {
g_debug_serial.println("Error: Invalid MAC address format. Expected: XX:XX:XX:XX:XX:XX");
while (true);
}
g_debug_serial.print("Start to connect ");
g_debug_serial.println(kBluetoothDeviceAddress);
bluetooth_stream.println("AT+DISCON");
delay(100);
bluetooth_stream.println("AT+RESET");
delay(100);
bluetooth_stream.println("AT+ECHO=0");
delay(100);
bluetooth_stream.println("AT+ROLE=0");
delay(100);
bluetooth_stream.println("AT+AUTOCON=0");
delay(100);
bluetooth_stream.print("AT+CON=");
bluetooth_stream.println(bluetooth_device_address);
delay(100);
g_debug_serial.println("Connected");
}
}
void setup() {
g_debug_serial.begin(115200);
Serial.begin(kBluetoothModuleSerialBaudRate);
Connect(Serial, kBluetoothDeviceAddress);
}
void loop() {
g_codex_pad_frame_decoder.Update();
for (auto button : kAllButtons) {
if (g_codex_pad_frame_decoder.pressed(button)) {
g_debug_serial.print("Button ");
g_debug_serial.print(ButtonToString(button));
g_debug_serial.println(": pressed");
}
else if (g_codex_pad_frame_decoder.released(button)) {
g_debug_serial.print("Button ");
g_debug_serial.print(ButtonToString(button));
g_debug_serial.println(": released");
}
else if (g_codex_pad_frame_decoder.holding(button)) {
g_debug_serial.print("Button ");
g_debug_serial.print(ButtonToString(button));
g_debug_serial.println(": holding");
}
}
g_debug_serial.print("L(X:");
g_debug_serial.print(
g_debug_serial.print(", Y:");
g_debug_serial.print(
g_debug_serial.print("), R(X:");
g_debug_serial.print(
g_debug_serial.print(", Y:");
g_debug_serial.print(
g_debug_serial.println(")");
}
}
CodexPad controller data frame decoding main class.
Button
Gamepad button mask enumeration.
@ kTriangleY
Triangle or Y.
@ kLeftStickX
Left stick X axis.
@ kRightStickY
Right stick Y axis.
@ kLeftStickY
Left stick Y axis.
@ kRightStickX
Right stick X axis.