Signed-off-by: myperworld <myperworld@126.com>
This commit is contained in:
1 parent
0986832fd1
commit
aacd052c06
3403 files changed
+597376
-12
No files matched your search
@@ -0,0 +1,132 @@
|
||||
#include <Arduino.h>
|
||||
#include <XboxSeriesXControllerESP32_asukiaaa.hpp>
|
||||
#include <Preferences.h>
|
||||
|
||||
// 全局变量
|
||||
int led1 = 2;
|
||||
Preferences preferences;
|
||||
XboxSeriesXControllerESP32_asukiaaa::Core* xboxController = nullptr;
|
||||
int connnect_flag = 0;
|
||||
|
||||
// 蓝牙地址管理函数
|
||||
void saveBluetoothAddress(const String& addr) {
|
||||
preferences.begin("xbox", false);
|
||||
preferences.putString("bt_addr", addr);
|
||||
preferences.end();
|
||||
}
|
||||
|
||||
String loadBluetoothAddress() {
|
||||
preferences.begin("xbox", true);
|
||||
String addr = preferences.getString("bt_addr", "");
|
||||
preferences.end();
|
||||
return addr;
|
||||
}
|
||||
|
||||
void resetBluetoothAddressAndRestart() {
|
||||
saveBluetoothAddress(""); // 保存空地址
|
||||
Serial.println("Bluetooth address cleared, restarting...");
|
||||
delay(500);
|
||||
ESP.restart(); // 重启后会用空地址,自动进入全扫描模式
|
||||
}
|
||||
|
||||
// 串口命令处理函数
|
||||
void handleSerialCommands() {
|
||||
if (Serial.available()) {
|
||||
String addr = Serial.readStringUntil('\n');
|
||||
addr.trim();
|
||||
if (addr.equalsIgnoreCase("set")) { // 输入set 查找手柄蓝牙MAC地址
|
||||
resetBluetoothAddressAndRestart();
|
||||
} else if (addr.equalsIgnoreCase("get")) { // 获取目前保存的手柄蓝牙MAC地址
|
||||
String curAddr = loadBluetoothAddress();
|
||||
if (curAddr.length() == 17) {
|
||||
Serial.println("Current Bluetooth address: " + curAddr);
|
||||
} else {
|
||||
Serial.println("No address saved (scan all devices).");
|
||||
}
|
||||
} else if (addr.length() == 17) { // 直接输入17位蓝牙MAC地址
|
||||
saveBluetoothAddress(addr);
|
||||
Serial.println("Address saved: " + addr);
|
||||
delay(500);
|
||||
ESP.restart();
|
||||
} else {
|
||||
Serial.println("Invalid address format!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Xbox控制器数据处理函数
|
||||
String xbox_string() {
|
||||
String str = String(xboxController->xboxNotif.btnY) + "," + // 所有按钮,除了4个摇杆都是默认0,按下1
|
||||
String(xboxController->xboxNotif.btnX) + "," +
|
||||
String(xboxController->xboxNotif.btnB) + "," +
|
||||
String(xboxController->xboxNotif.btnA) + "," +
|
||||
String(xboxController->xboxNotif.btnLB) + "," + // 肩部左按钮
|
||||
String(xboxController->xboxNotif.btnRB) + "," + // 肩部右按钮
|
||||
String(xboxController->xboxNotif.btnSelect) + "," + // 上方左按钮
|
||||
String(xboxController->xboxNotif.btnStart) + "," + // 上方右按钮
|
||||
String(xboxController->xboxNotif.btnXbox) + "," + // 上方上按钮 开机按钮
|
||||
String(xboxController->xboxNotif.btnShare) + "," + // 上方下按钮
|
||||
String(xboxController->xboxNotif.btnLS) + "," + // 左摇杆中键
|
||||
String(xboxController->xboxNotif.btnRS) + "," + // 右摇杆中键
|
||||
String(xboxController->xboxNotif.btnDirUp) + "," + // 上方向键
|
||||
String(xboxController->xboxNotif.btnDirRight) + "," + // 右方向键
|
||||
String(xboxController->xboxNotif.btnDirDown) + "," + // 下方向键
|
||||
String(xboxController->xboxNotif.btnDirLeft) + "," + // 左方向键
|
||||
String(xboxController->xboxNotif.joyLHori) + "," + // 左摇杆水平轴 中间值=32768 范围[左,右],[0, 65536]
|
||||
String(xboxController->xboxNotif.joyLVert) + "," + // 左摇杆垂直轴 中间值=32768 范围[上,下],[0, 65536]
|
||||
String(xboxController->xboxNotif.joyRHori) + "," + // 右摇杆水平轴 中间值=32768 范围[左,右],[0, 65536]
|
||||
String(xboxController->xboxNotif.joyRVert) + "," + // 右摇杆垂直轴 中间值=32768 范围[上,下],[0, 65536]
|
||||
String(xboxController->xboxNotif.trigLT) + "," + // 左扳机轴 中间值=0 范围[未压,压满],[0, 100]
|
||||
String(xboxController->xboxNotif.trigRT) + "\n"; // 右扳机轴 中间值=0 范围[未压,压满],[0, 100]
|
||||
return str;
|
||||
}
|
||||
|
||||
// Xbox控制器连接管理函数
|
||||
void handleXboxConnection() {
|
||||
if (xboxController) xboxController->onLoop();
|
||||
|
||||
if (xboxController && xboxController->isConnected()) {
|
||||
if (xboxController->isWaitingForFirstNotification()) {
|
||||
Serial.println("waiting for first notification");
|
||||
} else {
|
||||
if(connnect_flag == 0) {
|
||||
connnect_flag = 1;
|
||||
// 可以在这里添加连接成功后的初始化代码
|
||||
Serial.println("Xbox controller connected!");
|
||||
}
|
||||
Serial.print(xbox_string());
|
||||
// 连接状态下LED常亮
|
||||
digitalWrite(led1, HIGH);
|
||||
delay(10);
|
||||
}
|
||||
} else {
|
||||
connnect_flag = 0;
|
||||
memset(xboxController->xboxNotif.rx_buf, 0, sizeof(xboxController->xboxNotif.rx_buf));
|
||||
// 未连接状态下LED熄灭
|
||||
digitalWrite(led1, LOW);
|
||||
delay(10);
|
||||
|
||||
if (xboxController && xboxController->getCountFailedConnection() > 2) {
|
||||
ESP.restart();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 简化的setup函数
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Serial.println("Starting NimBLE Client");
|
||||
pinMode(led1, OUTPUT);
|
||||
String btAddr = loadBluetoothAddress();
|
||||
xboxController = new XboxSeriesXControllerESP32_asukiaaa::Core(btAddr);
|
||||
xboxController->begin();
|
||||
digitalWrite(led1, LOW);
|
||||
}
|
||||
|
||||
// 简化的loop函数
|
||||
void loop() {
|
||||
handleSerialCommands(); // 处理串口命令
|
||||
handleXboxConnection(); // 处理Xbox控制器连接
|
||||
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user