This commit is contained in:
parent
9773660492
commit
1fb8e6f30b
15
packet.md
15
packet.md
|
@ -28,13 +28,24 @@
|
|||
|
||||
返回示例 `01 05 10 01 FE`
|
||||
|
||||
### 查询超声波距离 `0x11`
|
||||
### 查询 SPI Flash 挂载状态 `0x11`
|
||||
|
||||
查询示例 `00 04 11 FF`
|
||||
|
||||
| 返回数据 | 含义 |
|
||||
| -------- | ---- |
|
||||
| 01 | 已挂载 |
|
||||
| 00 | 未挂载 |
|
||||
|
||||
返回示例 `01 05 11 01 FE`
|
||||
|
||||
### 查询超声波距离 `0x12`
|
||||
|
||||
查询示例 `00 04 12 FF`
|
||||
|
||||
返回一个 4 字节浮点数,表示距离,单位 m
|
||||
|
||||
返回示例 `01 08 11 距离3 距离2 距离1 距离0 FE`
|
||||
返回示例 `01 08 12 距离3 距离2 距离1 距离0 FE`
|
||||
|
||||
## 控制
|
||||
|
||||
|
|
61
src/main.cpp
61
src/main.cpp
|
@ -3,6 +3,7 @@
|
|||
#include <BLEServer.h>
|
||||
#include <BLEUtils.h>
|
||||
#include <BLE2902.h>
|
||||
#include <SPIFFS.h>
|
||||
#include "consts.h"
|
||||
|
||||
// 使用 2 片 TB6612 控制 4 个电机
|
||||
|
@ -16,7 +17,8 @@
|
|||
|
||||
// 指令定义
|
||||
#define CMD_GET_BT_STATUS 0x10
|
||||
#define CMD_GET_DISTANCE 0x11
|
||||
#define CMD_GET_SPIFFS_STATUS 0x11
|
||||
#define CMD_GET_DISTANCE 0x12
|
||||
#define CMD_MOTOR_MOVE_CONTROL 0x20
|
||||
#define CMD_MOTOR_ROTATE_CONTROL 0x21
|
||||
|
||||
|
@ -121,6 +123,48 @@ float getDistance()
|
|||
return distance;
|
||||
}
|
||||
|
||||
class CarStorage
|
||||
{
|
||||
public:
|
||||
static bool isMounted;
|
||||
|
||||
static void init()
|
||||
{
|
||||
if (!SPIFFS.begin(true))
|
||||
{
|
||||
Serial.println("SPIFFS Mount Failed");
|
||||
isMounted = false;
|
||||
}
|
||||
else
|
||||
isMounted = true;
|
||||
}
|
||||
|
||||
static unsigned int getSensitivity()
|
||||
{
|
||||
if (!isMounted)
|
||||
return 0xFF;
|
||||
|
||||
File file = SPIFFS.open("/sensitivity.txt", "r");
|
||||
if (!file)
|
||||
return 0xFF;
|
||||
|
||||
String sensitivity = file.readStringUntil('\n');
|
||||
file.close();
|
||||
|
||||
return sensitivity.toInt();
|
||||
}
|
||||
|
||||
static void setSensitivity(unsigned int sensitivity)
|
||||
{
|
||||
if (!isMounted)
|
||||
return;
|
||||
|
||||
File file = SPIFFS.open("/sensitivity.txt", "w");
|
||||
file.println(sensitivity);
|
||||
file.close();
|
||||
}
|
||||
};
|
||||
|
||||
void setup()
|
||||
{
|
||||
// 初始化串口
|
||||
|
@ -148,6 +192,9 @@ void setup()
|
|||
pService->start();
|
||||
pServer->getAdvertising()->start();
|
||||
|
||||
// 初始化 EEPROM
|
||||
CarStorage::init();
|
||||
|
||||
// 设置引脚模式
|
||||
pinMode(STATUS_LED, OUTPUT);
|
||||
digitalWrite(STATUS_LED, HIGH);
|
||||
|
@ -198,6 +245,18 @@ void handleSerialPacket(uint8_t *packet, int length, BLECharacteristic &characte
|
|||
characteristic.notify();
|
||||
break;
|
||||
|
||||
case CMD_GET_SPIFFS_STATUS:
|
||||
Serial.println("CMD_GET_SPIFFS_STATUS");
|
||||
// 构建响应数据
|
||||
buffer[0] = PACKET_T_HEAD;
|
||||
buffer[1] = 0x05;
|
||||
buffer[2] = CMD_GET_SPIFFS_STATUS;
|
||||
buffer[3] = (uint8_t)(CarStorage::isMounted ? 0x01 : 0x00);
|
||||
buffer[4] = PACKET_T_TAIL;
|
||||
characteristic.setValue(buffer, 5);
|
||||
characteristic.notify();
|
||||
break;
|
||||
|
||||
case CMD_GET_DISTANCE:
|
||||
distance = getDistance();
|
||||
Serial.println("CMD_GET_DISTANCE, distance: " + String(distance));
|
||||
|
|
Loading…
Reference in New Issue