feat: update device name
This commit is contained in:
parent
eda05007cc
commit
821c264400
|
@ -17,6 +17,8 @@ public:
|
||||||
static BLECharacteristic *pTxCharacteristic;
|
static BLECharacteristic *pTxCharacteristic;
|
||||||
|
|
||||||
static void init(String deviceName);
|
static void init(String deviceName);
|
||||||
|
static void updateDeviceName(const String &newName);
|
||||||
|
static void restart();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -12,6 +12,9 @@
|
||||||
#define CMD_GET_BT_STATUS 0x10
|
#define CMD_GET_BT_STATUS 0x10
|
||||||
#define CMD_GET_SPIFFS_STATUS 0x11
|
#define CMD_GET_SPIFFS_STATUS 0x11
|
||||||
#define CMD_GET_DISTANCE 0x12
|
#define CMD_GET_DISTANCE 0x12
|
||||||
|
|
||||||
|
#define CMD_SET_NAME 0xA1
|
||||||
|
|
||||||
#define CMD_MOTOR_MOVE_CONTROL 0x20
|
#define CMD_MOTOR_MOVE_CONTROL 0x20
|
||||||
#define CMD_MOTOR_STEER_CONTROL 0x21
|
#define CMD_MOTOR_STEER_CONTROL 0x21
|
||||||
#define CMD_MOTOR_SINGLE_CONTROL 0x22
|
#define CMD_MOTOR_SINGLE_CONTROL 0x22
|
||||||
|
@ -28,6 +31,7 @@ public:
|
||||||
static void getBTStatus(BLECharacteristic &characteristic);
|
static void getBTStatus(BLECharacteristic &characteristic);
|
||||||
static void getSPIFFSStatus(BLECharacteristic &characteristic);
|
static void getSPIFFSStatus(BLECharacteristic &characteristic);
|
||||||
static void getDistance(BLECharacteristic &characteristic);
|
static void getDistance(BLECharacteristic &characteristic);
|
||||||
|
static void setName(BLECharacteristic &characteristic, uint8_t *packet);
|
||||||
static void motorMoveControl(BLECharacteristic &characteristic, uint8_t *packet);
|
static void motorMoveControl(BLECharacteristic &characteristic, uint8_t *packet);
|
||||||
static void motorSteerControl(BLECharacteristic &characteristic, uint8_t *packet);
|
static void motorSteerControl(BLECharacteristic &characteristic, uint8_t *packet);
|
||||||
static void motorRotateControl(BLECharacteristic &characteristic, uint8_t *packet);
|
static void motorRotateControl(BLECharacteristic &characteristic, uint8_t *packet);
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <SPIFFS.h>
|
#include <SPIFFS.h>
|
||||||
|
#include "consts.h"
|
||||||
|
|
||||||
class Storage
|
class Storage
|
||||||
{
|
{
|
||||||
|
|
12
packet.md
12
packet.md
|
@ -110,6 +110,18 @@
|
||||||
|
|
||||||
控制示例 `00 07 24 01 01 01 FF`
|
控制示例 `00 07 24 01 01 01 FF`
|
||||||
|
|
||||||
|
## 设置
|
||||||
|
|
||||||
|
### 设置设备名称 `0xA1`
|
||||||
|
|
||||||
|
> ⚠️ 注意: 设备名称长度不能超过 16 个字符,此时最大包体长度 20,仅支持 ASCII 码
|
||||||
|
|
||||||
|
包体 `00 包体长度 A1 设备名称 FF`
|
||||||
|
|
||||||
|
> 示例:WhiteTiger
|
||||||
|
|
||||||
|
设置示例 `00 0E A1 57 68 69 74 65 54 69 67 65 72 FF`
|
||||||
|
|
||||||
## 状态回报
|
## 状态回报
|
||||||
|
|
||||||
### 电机状态汇报 `0xE0`
|
### 电机状态汇报 `0xE0`
|
||||||
|
|
21
src/ble.cpp
21
src/ble.cpp
|
@ -57,3 +57,24 @@ void BLEManager::init(String deviceName)
|
||||||
pService->start();
|
pService->start();
|
||||||
pServer->getAdvertising()->start();
|
pServer->getAdvertising()->start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BLEManager::updateDeviceName(const String &newName) {
|
||||||
|
// 保存新的设备名称
|
||||||
|
Storage::setName(newName);
|
||||||
|
|
||||||
|
// 停止广播
|
||||||
|
if (pServer) {
|
||||||
|
pServer->getAdvertising()->stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新设备名称
|
||||||
|
BLEDevice::deinit(true);
|
||||||
|
BLEDevice::init(newName.c_str());
|
||||||
|
|
||||||
|
// 重新初始化 BLE 服务
|
||||||
|
init(newName);
|
||||||
|
}
|
||||||
|
|
||||||
|
void BLEManager::restart() {
|
||||||
|
ESP.restart(); // 重启 ESP32
|
||||||
|
}
|
||||||
|
|
|
@ -42,6 +42,25 @@ void Handlers::getDistance(BLECharacteristic &characteristic)
|
||||||
characteristic.notify();
|
characteristic.notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Handlers::setName(BLECharacteristic &characteristic, uint8_t *packet)
|
||||||
|
{
|
||||||
|
unsigned char packetLength = packet[1];
|
||||||
|
unsigned char nameLength = packetLength - 4;
|
||||||
|
char name[17];
|
||||||
|
for (int i = 0; i <= nameLength; i++)
|
||||||
|
{
|
||||||
|
if (i == nameLength)
|
||||||
|
{
|
||||||
|
name[i] = '\0';
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
name[i] = packet[i + 3];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
BLEManager::updateDeviceName(name);
|
||||||
|
}
|
||||||
|
|
||||||
void Handlers::motorMoveControl(BLECharacteristic &characteristic, uint8_t *packet)
|
void Handlers::motorMoveControl(BLECharacteristic &characteristic, uint8_t *packet)
|
||||||
{
|
{
|
||||||
uint8_t direction = packet[3];
|
uint8_t direction = packet[3];
|
||||||
|
|
|
@ -15,7 +15,7 @@ void setup()
|
||||||
Storage::init();
|
Storage::init();
|
||||||
|
|
||||||
// 初始化 BLE
|
// 初始化 BLE
|
||||||
BLEManager::init(DEVICE_NAME);
|
BLEManager::init(Storage::getName());
|
||||||
|
|
||||||
// 初始化电机
|
// 初始化电机
|
||||||
MotorController::init(
|
MotorController::init(
|
||||||
|
|
|
@ -75,7 +75,7 @@ void Storage::setName(String name)
|
||||||
String Storage::getName()
|
String Storage::getName()
|
||||||
{
|
{
|
||||||
if (!isMounted)
|
if (!isMounted)
|
||||||
return "";
|
return DEVICE_NAME;
|
||||||
|
|
||||||
File file = SPIFFS.open("/name.txt", "r");
|
File file = SPIFFS.open("/name.txt", "r");
|
||||||
String name = file.readStringUntil('\n');
|
String name = file.readStringUntil('\n');
|
||||||
|
|
Loading…
Reference in New Issue