10#if defined(__cplusplus) && __cplusplus >= 201703L && defined(__has_include)
13#if !__has_include("NimBLEDevice.h")
14#error "NimBLEDevice.h not found. CodexPad requires the NimBLE-Arduino library."
15#error "Please install the latest version of NimBLE-Arduino via Arduino Library Manager or PlatformIO's lib_deps."
17#error "未找到 NimBLEDevice.h 文件。CodexPad 依赖于 NimBLE-Arduino 库。"
18#error "请通过 Arduino 库管理器或 PlatformIO 的 lib_deps 安装最新版本的 NimBLE-Arduino 库。"
27#warning "CodexPad depends on NimBLE-Arduino. Ensure 'NimBLEDevice.h' is in your include path."
28#warning "If you encounter errors about missing 'NimBLEDevice.h', please install the latest NimBLE-Arduino library."
30#warning "CodexPad 依赖于 NimBLE-Arduino 库。请确保 'NimBLEDevice.h' 在您的头文件包含路径中。"
31#warning "如果遇到找不到 'NimBLEDevice.h' 的错误,请安装最新版本的 NimBLE-Arduino 库。"
34#include "NimBLEDevice.h"
444#if __cplusplus < 201402L
445 static constexpr uint32_t ButtonMask() {
return 0; }
492 template <
typename FirstButton,
typename... RestButtons>
493 static constexpr uint32_t
ButtonMask(FirstButton first, RestButtons... rest) {
494 return static_cast<uint32_t
>(first) | ButtonMask(rest...);
543 template <
typename...
Button>
544 static constexpr uint32_t ButtonMask(
Button... button) {
545 return (
static_cast<uint32_t
>(button) | ... | uint32_t{0});
596 bool Connect(
const std::string& bluetooth_device_address,
const uint32_t timeout_ms = 5000);
871 std::array<uint8_t, kAxisValueNum>
axis_values()
const;
906 static constexpr size_t kButtonNum = 32;
907 static constexpr size_t kInputsQueueMax = 10;
908 static constexpr size_t kInputsBytes = 8;
910 uint32_t button_states = 0;
914 static_assert(
sizeof(Inputs) == kInputsBytes);
916 bool Connect(
const NimBLEAddress& address,
bool async_connect,
const uint32_t timeout_ms);
917 void OnNotify(
const NimBLERemoteCharacteristic* remote_characteristic,
const uint8_t* data,
const size_t length,
const bool is_notify);
920 mutable std::mutex mutex_;
921 NimBLEClient* ble_client_ =
nullptr;
922 std::string remote_device_name_;
923 std::string remote_model_number_;
924 std::array<uint8_t, 3> remote_firmware_version_ = {0, 0, 0};
926 Inputs current_inputs_;
927 std::queue<Inputs> inputs_queue_;
const std::array< uint8_t, 3 > remote_firmware_version() const
Get firmware version of the CodexPad.
uint32_t button_states() const
Get all button states, return a 32-bit unsigned integer where each bit represents the state of a spec...
const std::string & remote_model_number() const
Get model number of the CodexPad.
bool ScanAndConnect(const uint32_t button_mask)
Scans for nearby CodexPad devices and automatically connects to a device whose button state matches t...
@ kLeftStickX
Left stick X axis.
@ kRightStickY
Right stick Y axis.
@ kLeftStickY
Left stick Y axis.
@ kRightStickX
Right stick X axis.
bool pressed(const Button button) const
check if a button is pressed
void Disconnect()
Disconnect.
static constexpr size_t kAxisValueNum
Number of axis values.
bool Connect(const std::string &bluetooth_device_address, const uint32_t timeout_ms=5000)
Connect.
@ kTriangleY
Triangle or Y.
const std::string & remote_device_name() const
Get model number of the CodexPad.
NimBLEClient * ble_client() const
Get the BLE client object.
static constexpr uint32_t ButtonMask(FirstButton first, RestButtons... rest)
Combines multiple button identifiers into a single 32-bit button mask.
uint8_t axis_value(const Axis axis) const
Get axis value.
bool HasAxisValueChanged(const Axis axis, const uint8_t threshold) const
check if an axis value has changed
static constexpr uint8_t kAxisCenter
Axis center 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.
bool released(const Button button) const
check if a button is released
std::array< uint8_t, kAxisValueNum > axis_values() const
Get current values of all analog axes.
void Update()
Update, need to be called in Loop.
bool button_state(const Button button) const
check if a button is pressed or held
bool holding(const Button button) const
check if a button is held