10constexpr uint16_t kGapServiceUuid{0x1800};
11constexpr uint16_t kGapDeviceNameUuid{0x2A00};
13constexpr uint16_t kInputsServiceUuid{0xFFA0};
14constexpr uint16_t kInputsCharacteristicUuid{0xFFA1};
16constexpr uint16_t kBatteryServiceUuid{0x180F};
17constexpr uint16_t kBatteryLevelCharacteristicUuid{0x2A19};
19constexpr uint16_t kDeviceInfoServiceUuid{0x180A};
20constexpr uint16_t kModelNumberCharacteristicUuid{0x2A24};
21constexpr uint16_t kSerialNumberCharacteristicUuid{0x2A25};
22constexpr uint16_t kFirmwareRevisionCharacteristicUuid{0x2A26};
23constexpr uint16_t kManufacturerNameCharacteristicUuid{0x2A29};
25bool HasAxisValueChangedSignificantly(
const int16_t prev_value,
const int16_t current_value,
const uint8_t threshold) {
26 return prev_value != current_value && (current_value == 0 || current_value == 255 || std::abs(current_value - prev_value) >= threshold);
35 if (!NimBLEDevice::isInitialized()) {
36 NimBLEDevice::init(
"CodexPadClient");
40bool CodexPad::Connect(
const std::string& bluetooth_device_address,
const uint32_t timeout_ms) {
42 if (bluetooth_device_address.length() != 17 || bluetooth_device_address[2] !=
':' || bluetooth_device_address[5] !=
':' ||
43 bluetooth_device_address[8] !=
':' || bluetooth_device_address[11] !=
':' || bluetooth_device_address[14] !=
':') {
48 return Connect(NimBLEAddress(bluetooth_device_address, 0),
false, timeout_ms);
52 auto scanner = NimBLEDevice::getScan();
53 scanner->setActiveScan(
true);
54 scanner->setInterval(1000);
55 scanner->setWindow(999);
57 if (!scanner->start(1000)) {
59 scanner->clearResults();
64 while (scanner->isScanning()) {
70 int8_t rssi = INT8_MIN;
71 NimBLEAddress address;
74 struct ManufacturerData {
75 uint16_t company_id = 0xFFFF;
76 uint8_t header[8] = {
'C',
'o',
'd',
'e',
'x',
'P',
'a',
'd'};
77 uint8_t version_major = 0;
78 uint8_t version_minor = 0;
79 uint8_t version_patch = 0;
84 for (
const auto device : scanner->getResults()) {
85 if (device->haveName() && String(device->getName().c_str()).startsWith(
"CodexPad-") && device->haveManufacturerData()) {
87 const auto manufacturer_data = device->getManufacturerData();
88 if (manufacturer_data.length() >=
sizeof(ManufacturerData)) {
89 const auto data =
reinterpret_cast<const ManufacturerData*
>(manufacturer_data.c_str());
90 if (data->company_id == 0xFFFF && memcmp(data->header,
"CodexPad", 8) == 0 && data->button_state == button_mask && device->getRSSI() > rssi) {
91 rssi = device->getRSSI();
92 address = device->getAddress();
99 scanner->clearResults();
101 return address.isNull() ? false :
Connect(address, 2000);
105 if (ble_client_ ==
nullptr) {
109 if (!ble_client_->isConnected()) {
114 prev_inputs_ = current_inputs_;
116 std::lock_guard<std::mutex> l(mutex_);
117 if (inputs_queue_.empty()) {
120 current_inputs_ = std::move(inputs_queue_.front());
128 if (ble_client_ ==
nullptr) {
132 if (!ble_client_->isConnected()) {
136 auto remote_service = ble_client_->getService(uint16_t{0x1804});
137 if (remote_service ==
nullptr) {
141 auto remote_characteristic = remote_service->getCharacteristic(uint16_t{0x2A07});
142 if (remote_characteristic ==
nullptr) {
146 return remote_characteristic->writeValue(
static_cast<uint8_t
>(tx_power));
150 return (prev_inputs_.button_states &
static_cast<uint32_t
>(button)) == 0 && (current_inputs_.button_states &
static_cast<uint32_t
>(button)) != 0;
154 return (prev_inputs_.button_states &
static_cast<uint32_t
>(button)) != 0 && (current_inputs_.button_states &
static_cast<uint32_t
>(button)) == 0;
158 return (prev_inputs_.button_states &
static_cast<uint32_t
>(button)) != 0 && (current_inputs_.button_states &
static_cast<uint32_t
>(button)) != 0;
176 return HasAxisValueChangedSignificantly(
177 current_inputs_.axis_values[
static_cast<size_t>(axis)], prev_inputs_.axis_values[
static_cast<size_t>(axis)], threshold);
180bool CodexPad::Connect(
const NimBLEAddress& address,
bool async_connect,
const uint32_t timeout_ms) {
182 assert(ble_client_ ==
nullptr);
183 ble_client_ = NimBLEDevice::createClient(address);
184 ble_client_->setConnectTimeout(timeout_ms);
185 auto ret = ble_client_->connect(
true, async_connect,
true);
187 if (!ret || !ble_client_->isConnected()) {
191 remote_device_name_ = ble_client_->getValue(kGapServiceUuid, kGapDeviceNameUuid);
192 remote_model_number_ = ble_client_->getValue(kDeviceInfoServiceUuid, kModelNumberCharacteristicUuid);
194 auto firmware_revision = ble_client_->getValue(kDeviceInfoServiceUuid, kFirmwareRevisionCharacteristicUuid);
195 if (firmware_revision.length() ==
sizeof(remote_firmware_version_)) {
196 memcpy(remote_firmware_version_.data(), firmware_revision.data(), firmware_revision.length());
201 auto remote_service = ble_client_->getService(kInputsServiceUuid);
202 if (remote_service ==
nullptr) {
206 auto remote_characteristic = remote_service->getCharacteristic(kInputsCharacteristicUuid);
207 if (remote_characteristic ==
nullptr) {
211 if (!remote_characteristic->canNotify()) {
215 if (!remote_characteristic->subscribe(
216 true, std::bind(&CodexPad::OnNotify,
this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4))) {
228void CodexPad::OnNotify(
const NimBLERemoteCharacteristic* remote_characteristic,
const uint8_t* data,
const size_t length,
const bool is_notify) {
229 if (remote_characteristic !=
nullptr && remote_characteristic->getUUID().equals(kInputsCharacteristicUuid)) {
230 if (length !=
sizeof(Inputs)) {
231 printf(
"WARNING: length != sizeof(Inputs)\n");
235 std::lock_guard<std::mutex> l(mutex_);
236 if (inputs_queue_.size() > kInputsQueueMax) {
240 memcpy(&inputs, data,
sizeof(inputs));
241 inputs_queue_.emplace(std::move(inputs));
245void CodexPad::Reset() {
246 if (ble_client_ !=
nullptr) {
247 ble_client_->cancelConnect();
248 ble_client_->disconnect();
249 NimBLEDevice::deleteClient(ble_client_);
250 ble_client_ =
nullptr;
253 remote_device_name_.clear();
254 remote_model_number_.clear();
255 remote_firmware_version_.fill(0);
257 current_inputs_ = {};
258 std::lock_guard<std::mutex> l(mutex_);
uint32_t button_states() const
以位掩码形式获取所有按键的当前状态
bool ScanAndConnect(const uint32_t button_mask)
扫描附近的 CodexPad 设备,并自动连接到一个按键状态与指定掩码匹配的设备。
bool pressed(const Button button) const
查询按键是否被按下
static constexpr size_t kAxisValueNum
轴值数量
bool Connect(const std::string &bluetooth_device_address, const uint32_t timeout_ms=5000)
连接
uint8_t axis_value(const Axis axis) const
获取轴值
bool HasAxisValueChanged(const Axis axis, const uint8_t threshold) const
查询轴值是否发生变化
static constexpr uint8_t kAxisCenter
轴中心值
bool set_remote_tx_power(const TxPower power)
设置发射功率,连接状态下调用,立即生效于当前连接,下次连接生效
bool is_connected() const
是否连接
bool released(const Button button) const
查询按键是否被释放
std::array< uint8_t, kAxisValueNum > axis_values() const
获取所有模拟轴的当前值
void Update()
更新,需要在Loop中不断调用
bool button_state(const Button button) const
查询按键是否被按下或持续按下
bool holding(const Button button) const
查询按键是否被持续按下