CodexPad Arduino Lib 2.1.3
Loading...
Searching...
No Matches
basic_polling.ino
Go to the documentation of this file.
1/**
2 * @~English
3 * @file basic_polling.ino
4 * @example basic_polling.ino
5 * @brief Demonstrates the basic polling method to periodically query and print all CodexPad button states and joystick values.
6 * @details This example establishes a connection to a specific CodexPad device (by Bluetooth Device Address) and implements a simple polling loop.
7 * Every 30 milliseconds, it queries and prints the current state (pressed/released) of all buttons and the raw analog values (0-255) of both
8 * joysticks. It showcases the fundamental usage of `button_state()` for discrete button queries and `axis_value()` for continuous joystick readings.
9 * @note This example uses a simple timing mechanism (`millis()`) to print at a fixed interval, which is suitable for monitoring or logging.
10 * For real-time control, ensure `Update()` is called as frequently as possible without blocking delays.
11 * @see CodexPad::button_state
12 * @see CodexPad::axis_value
13 * @see CodexPad::Update
14 */
15/**
16 * @~Chinese
17 * @file basic_polling.ino
18 * @example basic_polling.ino
19 * @brief 演示通过基本轮询方式定期查询并打印 CodexPad 所有按钮状态与摇杆值。
20 * @details 本示例通过Bluetooth Device Address连接到指定的 CodexPad 设备,并实现了一个简单的轮询循环。
21 * 每隔 30 毫秒,它会查询并打印所有按钮的当前状态(按下/弹起)以及两个摇杆的原始模拟值(0-255)。
22 * 它展示了 `button_state()` 用于离散按钮查询和 `axis_value()` 用于连续摇杆读取的基本用法。
23 * @note 本示例使用简单的定时机制(`millis()`)以固定间隔打印,适用于状态监控或日志记录。
24 * 对于实时控制应用,请确保尽可能频繁地调用 `Update()` 且无阻塞延时。
25 * @see CodexPad::button_state
26 * @see CodexPad::axis_value
27 * @see CodexPad::Update
28 */
29
30#include "codex_pad.h"
31
32namespace {
33// Replace with your CodexPad device's Bluetooth device address
34// 替换为你的 CodexPad 的 Bluetooth device address
35const std::string kBluetoothDeviceAddress = "E4:66:E5:A2:24:5D";
36
37CodexPad g_codex_pad;
38
39void Connect() {
40 printf("Start to connect %s\n", kBluetoothDeviceAddress.c_str());
41 // Connect to the CodexPad with specified Bluetooth device address
42 // 连接到指定蓝牙设备地址的手柄
43 while (!g_codex_pad.Connect(kBluetoothDeviceAddress, 5000)) {
44 printf("Retry to connect %s\n", kBluetoothDeviceAddress.c_str());
45 }
46
47 printf("Remote device name: %s\n", g_codex_pad.remote_device_name().c_str());
48 printf("Remote model number: %s\n", g_codex_pad.remote_model_number().c_str());
49 printf("Remote firmware revision: %u.%u.%u\n",
50 g_codex_pad.remote_firmware_version()[0],
51 g_codex_pad.remote_firmware_version()[1],
52 g_codex_pad.remote_firmware_version()[2]);
53
54 if (const auto ble_client = g_codex_pad.ble_client(); ble_client != nullptr) {
55 printf("Remote Bluetooth Device Address: %s\n", ble_client->getPeerAddress().toString().c_str());
56 } else {
57 printf("Remote Bluetooth Device Address: unknown\n");
58 }
59
60 // Set transmission power to 0dBm
61 // Transmission power affects communication range and power consumption:
62 // Higher power provides longer range but consumes more battery
63 // Choose appropriate power level based on your application to balance range and battery life
64 // 设置发射功率为0dBm
65 // 发射功率影响通信距离和功耗:功率越高,通信距离越远,但功耗也越大
66 // 建议根据实际应用场景选择合适的功率等级以平衡距离和电池寿命
68 printf("Set remote tx power to 0dBm successfully\n");
69 }
70
71 printf("Connected\n");
72}
73} // namespace
74
75void setup() {
76 Serial.begin(115200);
77
78 printf("Init\n");
79 g_codex_pad.Init();
80
81 Connect();
82}
83
84void loop() {
85 // Important: Update() method must be called as frequently as possible in the loop, no delays should be added
86 // This method processes all received Bluetooth packets, delays will cause data loss and response lag
87 // For real-time control applications, high-frequency calls are essential to ensure prompt response to gamepad input
88 // 重要:Update()方法必须在循环中尽可能频繁地调用,不能添加延时
89 // 该方法负责处理所有接收到的蓝牙数据包,延时会导致数据丢失和响应延迟
90 // 对于实时控制应用,必须保持高频率调用以确保及时响应手柄输入
91 g_codex_pad.Update();
92
93 if (!g_codex_pad.is_connected()) {
94 printf("Disconnected, start to reconnect\n");
95 Connect();
96 return;
97 }
98
99 static uint32_t s_print_time = 0;
100 if (s_print_time == 0 || s_print_time + 30 < millis()) {
101 s_print_time = millis();
102
103 printf(
104 "Up:%u, Down:%u, Left:%u, Right:%u, Square(X):%u, Triangle(Y):%u, Cross(A):%u, Circle(B):%u, L1:%u, L2:%u, L3:%u, R1:%u, R2:%u, "
105 "R3:%u, Select:%u, "
106 "Start:%u, Home:%u, L(X:%3u, Y:%3u), R(X:%3u, Y:%3u)\n",
107
108 // Get button states, button_state() returns bool type, true means pressed, false means released
109 // 获取各个按钮的状态,button_state()返回bool类型,true表示按下,false表示弹起
127
128 // Get joystick axis data, axis_value() returns value from 0 to 255
129 // Center position is around 128, values represent stick deflection
130 // 获取摇杆轴数据,axis_value()返回0~255的数值
131 // 中间位置约为128,数值范围表示摇杆的偏移程度
136 }
137}
CodexPad main class.
Definition codex_pad.h:46
const std::array< uint8_t, 3 > remote_firmware_version() const
Get firmware version of the CodexPad.
Definition codex_pad.h:734
const std::string & remote_model_number() const
Get model number of the CodexPad.
Definition codex_pad.h:722
@ kLeftStickX
Left stick X axis.
Definition codex_pad.h:411
@ kRightStickY
Right stick Y axis.
Definition codex_pad.h:441
@ kLeftStickY
Left stick Y axis.
Definition codex_pad.h:421
@ kRightStickX
Right stick X axis.
Definition codex_pad.h:431
bool Connect(const std::string &bluetooth_device_address, const uint32_t timeout_ms=5000)
Connect.
Definition codex_pad.cpp:38
@ kSelect
Select.
Definition codex_pad.h:369
@ kCrossA
Cross or A.
Definition codex_pad.h:289
@ kSquareX
Square or X.
Definition codex_pad.h:269
@ kCircleB
Circle or B.
Definition codex_pad.h:299
@ kTriangleY
Triangle or Y.
Definition codex_pad.h:279
const std::string & remote_device_name() const
Get model number of the CodexPad.
Definition codex_pad.h:710
NimBLEClient * ble_client() const
Get the BLE client object.
Definition codex_pad.h:903
uint8_t axis_value(const Axis axis) const
Get axis value.
bool set_remote_tx_power(const TxPower power)
Set transmission power, only effective when connected, immediately effective for current connection,...
bool is_connected() const
Is connected.
void Init()
Initialize.
Definition codex_pad.cpp:32
void Update()
Update, need to be called in Loop.
bool button_state(const Button button) const
check if a button is pressed or held