Skip to content

Commit 03ba149

Browse files
committed
Add isWaitingForFirstNotification
1 parent b311a0c commit 03ba149

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

examples/connect/connect.ino

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,26 @@ void setup() {
1616
void loop() {
1717
xboxController.onLoop();
1818
if (xboxController.isConnected()) {
19-
Serial.println("Address: " + xboxController.buildDeviceAddressStr());
20-
Serial.print(xboxController.xboxNotif.toString());
21-
unsigned long receivedAt = xboxController.getReceiveNotificationAt();
22-
uint16_t joystickMax = XboxControllerNotificationParser::maxJoy;
23-
Serial.print("joyLHori rate: ");
24-
Serial.println((float)xboxController.xboxNotif.joyLHori / joystickMax);
25-
Serial.print("joyLVert rate: ");
26-
Serial.println((float)xboxController.xboxNotif.joyLVert / joystickMax);
27-
Serial.println("battery " + String(xboxController.battery) + "%");
28-
Serial.println("received at " + String(receivedAt));
19+
if (xboxController.isWaitingForFirstNotification()) {
20+
Serial.println("waiting for first notification");
21+
} else {
22+
Serial.println("Address: " + xboxController.buildDeviceAddressStr());
23+
Serial.print(xboxController.xboxNotif.toString());
24+
unsigned long receivedAt = xboxController.getReceiveNotificationAt();
25+
uint16_t joystickMax = XboxControllerNotificationParser::maxJoy;
26+
Serial.print("joyLHori rate: ");
27+
Serial.println((float)xboxController.xboxNotif.joyLHori / joystickMax);
28+
Serial.print("joyLVert rate: ");
29+
Serial.println((float)xboxController.xboxNotif.joyLVert / joystickMax);
30+
Serial.println("battery " + String(xboxController.battery) + "%");
31+
Serial.println("received at " + String(receivedAt));
32+
}
2933
} else {
30-
Serial.print("not connected");
34+
Serial.println("not connected");
3135
if (xboxController.getCountFailedConnection() > 2) {
3236
ESP.restart();
3337
}
3438
}
35-
Serial.println(" at " + String(millis()));
39+
Serial.println("at " + String(millis()));
3640
delay(500);
3741
}

src/XboxSeriesXControllerESP32_asukiaaa.hpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ String controllerManufacturerDataSearching = "0600030080";
2727

2828
enum class ConnectionState : uint8_t {
2929
Connected = 0,
30-
WaitingFirstNotification = 1,
30+
WaitingForFirstNotification = 1,
3131
Found = 2,
3232
Scanning = 3,
3333
};
@@ -43,7 +43,7 @@ class ClientCallbacks : public NimBLEClientCallbacks {
4343
#ifdef XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL
4444
XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL.println("Connected");
4545
#endif
46-
*pConnectionState = ConnectionState::WaitingFirstNotification;
46+
*pConnectionState = ConnectionState::WaitingForFirstNotification;
4747
// pClient->updateConnParams(120,120,0,60);
4848
};
4949

@@ -209,8 +209,11 @@ class Core {
209209
const size_t notifByteLen = XboxControllerNotificationParser::expectedDataLen;
210210
uint8_t notifByteArr[XboxControllerNotificationParser::expectedDataLen];
211211

212+
bool isWaitingForFirstNotification() {
213+
return connectionState == ConnectionState::WaitingForFirstNotification;
214+
}
212215
bool isConnected() {
213-
return connectionState == ConnectionState::WaitingFirstNotification ||
216+
return connectionState == ConnectionState::WaitingForFirstNotification ||
214217
connectionState == ConnectionState::Connected;
215218
}
216219
unsigned long getReceiveNotificationAt() { return receivedNotificationAt; }
@@ -376,23 +379,34 @@ class Core {
376379
charaPrintId(pChara);
377380
XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL.println(" canNotify ");
378381
#endif
379-
// Serial.println("can notify");
380382
if (pChara->subscribe(
381383
true,
382384
std::bind(&Core::notifyCB, this, std::placeholders::_1,
383385
std::placeholders::_2, std::placeholders::_3,
384386
std::placeholders::_4),
385387
true)) {
386-
// Serial.println("set notifyCb");
388+
#ifdef XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL
389+
XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL.println(
390+
"succeeded in subscribing");
391+
#endif
387392
} else {
388-
// Serial.println("failed to subscribe");
393+
#ifdef XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL
394+
XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL.println("failed subscribing");
395+
#endif
389396
}
390397
}
391398
}
392399

393400
void notifyCB(NimBLERemoteCharacteristic* pRemoteCharacteristic,
394401
uint8_t* pData, size_t length, bool isNotify) {
395402
auto sUuid = pRemoteCharacteristic->getRemoteService()->getUUID();
403+
if (connectionState != ConnectionState::Connected) {
404+
#ifdef XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL
405+
XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL.println(
406+
"Received first notification");
407+
#endif
408+
connectionState = ConnectionState::Connected;
409+
}
396410
if (sUuid.equals(uuidServiceHid)) {
397411
#ifdef XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL
398412
static bool isPrinting = false;
@@ -417,7 +431,6 @@ class Core {
417431
}
418432
XBOX_SERIES_X_CONTROLLER_DEBUG_SERIAL.println("");
419433
#endif
420-
connectionState = ConnectionState::Connected;
421434
xboxNotif.update(pData, length);
422435
memcpy(notifByteArr, pData,
423436
length < notifByteLen ? length : notifByteLen);

0 commit comments

Comments
 (0)