45using namespace gamepad::input;
50const std::string kBluetoothDeviceAddress =
"E4:66:E5:A2:17:06";
58std::string ButtonToString(Button button) {
69 case Button::kRight: {
72 case Button::kSquareX: {
75 case Button::kTriangleY: {
78 case Button::kCrossA: {
81 case Button::kCircleB: {
102 case Button::kSelect: {
105 case Button::kStart: {
108 case Button::kHome: {
118 printf(
"Start to connect %s\n", kBluetoothDeviceAddress.c_str());
121 while (!g_codex_pad.
Connect(kBluetoothDeviceAddress, 5000)) {
122 printf(
"Retry to connect %s\n", kBluetoothDeviceAddress.c_str());
130 if (
const auto ble_client = g_codex_pad.
ble_client(); ble_client !=
nullptr) {
131 printf(
"Remote Bluetooth Device Address: %s\n", ble_client->getPeerAddress().toString().c_str());
133 printf(
"Remote Bluetooth Device Address: unknown\n");
144 printf(
"Set remote tx power to 0dBm successfully\n");
147 printf(
"Connected\n");
152 Serial.begin(115200);
182 const gamepad::input::Tracker& it = g_codex_pad.
Update();
198 printf(
"Disconnected, start to reconnect\n");
220 for (
auto button : {Button::kUp, Button::kDown, Button::kLeft, Button::kRight, Button::kSquareX, Button::kTriangleY,
221 Button::kCrossA, Button::kCircleB, Button::kL1, Button::kL2, Button::kL3, Button::kR1, Button::kR2,
222 Button::kR3, Button::kSelect, Button::kStart, Button::kHome}) {
223 if (it.pressed(button)) {
224 printf(
"Button %s: pressed\n", ButtonToString(button).c_str());
225 }
else if (it.released(button)) {
226 printf(
"Button %s: released\n", ButtonToString(button).c_str());
227 }
else if (it.holding(button)) {
228 printf(
"Button %s: holding\n", ButtonToString(button).c_str());
243 constexpr uint8_t kAxisValueChangeThreshold = 2;
245 if (it.AxisChanged(Axis::kLeftStickX, kAxisValueChangeThreshold) ||
246 it.AxisChanged(Axis::kLeftStickY, kAxisValueChangeThreshold) ||
247 it.AxisChanged(Axis::kRightStickX, kAxisValueChangeThreshold) ||
248 it.AxisChanged(Axis::kRightStickY, kAxisValueChangeThreshold)) {
249 printf(
"L(X: %3" PRIu8
", Y:%3" PRIu8
"), R(X: %3" PRIu8
", Y: %3" PRIu8
")\n", it[Axis::kLeftStickX],
250 it[Axis::kLeftStickY], it[Axis::kRightStickX], it[Axis::kRightStickY]);
bool Connect(const std::string &bluetooth_device_address, uint32_t timeout_ms=5000) noexcept
连接
bool is_connected() const noexcept
是否连接
const std::array< uint8_t, 3 > & remote_firmware_version() const noexcept
获取CodexPad的固件版本
const gamepad::input::Tracker & Update() noexcept
更新,需要在Loop中不断调用
const std::string & remote_model_number() const noexcept
获取CodexPad的型号
NimBLEClient * ble_client() const noexcept
获取 BLE 客户端对象
const std::string & remote_device_name() const noexcept
获取CodexPad的型号
bool set_remote_tx_power(TxPower power) noexcept
设置发射功率,连接状态下调用,立即生效于当前连接,下次连接生效