This commit is contained in:
parent
5953a5d6e6
commit
6c00f2c959
|
@ -74,4 +74,7 @@
|
|||
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
|
||||
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
|
||||
|
||||
// 是否启用存储功能
|
||||
#define STORAGE_ENABLE 1 // 设置为 0 可以禁用存储功能
|
||||
|
||||
#endif // __CONSTS_H__
|
||||
|
|
|
@ -2,14 +2,37 @@
|
|||
#define STORAGE_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <SPIFFS.h>
|
||||
#include "consts.h"
|
||||
|
||||
#if STORAGE_ENABLE
|
||||
#include <SPIFFS.h>
|
||||
#endif
|
||||
|
||||
class Storage
|
||||
{
|
||||
public:
|
||||
static bool isMounted;
|
||||
static void init();
|
||||
|
||||
// 默认参数定义
|
||||
static constexpr uint8_t DEFAULT_BASE_SPEED = 50;
|
||||
static constexpr uint8_t DEFAULT_TURN_SPEED = 50;
|
||||
static constexpr uint8_t DEFAULT_ROTATE_SENSITIVE = 3;
|
||||
static constexpr unsigned int DEFAULT_SENSITIVITY = 0xFF;
|
||||
static constexpr float DEFAULT_KP = 1.0f;
|
||||
static constexpr float DEFAULT_KI = 0.0f;
|
||||
static constexpr float DEFAULT_KD = 0.0f;
|
||||
|
||||
// 当前参数(用于非存储模式)
|
||||
static uint8_t currentBaseSpeed;
|
||||
static uint8_t currentTurnSpeed;
|
||||
static uint8_t currentRotateSensitive;
|
||||
static unsigned int currentSensitivity;
|
||||
static float currentKp;
|
||||
static float currentKi;
|
||||
static float currentKd;
|
||||
static String currentName;
|
||||
|
||||
static void setPID(float kp, float ki, float kd);
|
||||
static void getPID(float &kp, float &ki, float &kd);
|
||||
static void setSensitivity(unsigned int sensitivity);
|
||||
|
|
161
src/storage.cpp
161
src/storage.cpp
|
@ -1,9 +1,18 @@
|
|||
#include "storage.h"
|
||||
|
||||
bool Storage::isMounted;
|
||||
bool Storage::isMounted = false;
|
||||
uint8_t Storage::currentBaseSpeed = DEFAULT_BASE_SPEED;
|
||||
uint8_t Storage::currentTurnSpeed = DEFAULT_TURN_SPEED;
|
||||
uint8_t Storage::currentRotateSensitive = DEFAULT_ROTATE_SENSITIVE;
|
||||
unsigned int Storage::currentSensitivity = DEFAULT_SENSITIVITY;
|
||||
float Storage::currentKp = DEFAULT_KP;
|
||||
float Storage::currentKi = DEFAULT_KI;
|
||||
float Storage::currentKd = DEFAULT_KD;
|
||||
String Storage::currentName = DEVICE_NAME;
|
||||
|
||||
void Storage::init()
|
||||
{
|
||||
#if STORAGE_ENABLE
|
||||
if (!SPIFFS.begin(true))
|
||||
{
|
||||
Serial.println("SPIFFS Mount Failed");
|
||||
|
@ -14,6 +23,97 @@ void Storage::init()
|
|||
Serial.println("SPIFFS Mount Success");
|
||||
isMounted = true;
|
||||
}
|
||||
#else
|
||||
Serial.println("Storage disabled");
|
||||
isMounted = false;
|
||||
#endif
|
||||
}
|
||||
|
||||
void Storage::setTrackingParams(uint8_t baseSpeed, uint8_t turnSpeed, uint8_t rotateSensitive)
|
||||
{
|
||||
currentBaseSpeed = baseSpeed;
|
||||
currentTurnSpeed = turnSpeed;
|
||||
currentRotateSensitive = rotateSensitive;
|
||||
|
||||
#if STORAGE_ENABLE
|
||||
if (!isMounted) return;
|
||||
|
||||
File file = SPIFFS.open("/tracking.txt", "w");
|
||||
file.println(baseSpeed);
|
||||
file.println(turnSpeed);
|
||||
file.println(rotateSensitive);
|
||||
file.close();
|
||||
#endif
|
||||
}
|
||||
|
||||
void Storage::getTrackingParams(uint8_t &baseSpeed, uint8_t &turnSpeed, uint8_t &rotateSensitive)
|
||||
{
|
||||
#if STORAGE_ENABLE
|
||||
if (!isMounted || !SPIFFS.exists("/tracking.txt")) {
|
||||
baseSpeed = currentBaseSpeed;
|
||||
turnSpeed = currentTurnSpeed;
|
||||
rotateSensitive = currentRotateSensitive;
|
||||
return;
|
||||
}
|
||||
|
||||
File file = SPIFFS.open("/tracking.txt", "r");
|
||||
baseSpeed = file.readStringUntil('\n').toInt();
|
||||
turnSpeed = file.readStringUntil('\n').toInt();
|
||||
rotateSensitive = file.readStringUntil('\n').toInt();
|
||||
file.close();
|
||||
|
||||
// 更新当前值
|
||||
currentBaseSpeed = baseSpeed;
|
||||
currentTurnSpeed = turnSpeed;
|
||||
currentRotateSensitive = rotateSensitive;
|
||||
#else
|
||||
baseSpeed = currentBaseSpeed;
|
||||
turnSpeed = currentTurnSpeed;
|
||||
rotateSensitive = currentRotateSensitive;
|
||||
#endif
|
||||
}
|
||||
|
||||
void Storage::setPID(float kp, float ki, float kd)
|
||||
{
|
||||
currentKp = kp;
|
||||
currentKi = ki;
|
||||
currentKd = kd;
|
||||
|
||||
#if STORAGE_ENABLE
|
||||
if (!isMounted) return;
|
||||
|
||||
File file = SPIFFS.open("/pid.txt", "w");
|
||||
file.println(kp);
|
||||
file.println(ki);
|
||||
file.println(kd);
|
||||
file.close();
|
||||
#endif
|
||||
}
|
||||
|
||||
void Storage::getPID(float &kp, float &ki, float &kd)
|
||||
{
|
||||
#if STORAGE_ENABLE
|
||||
if (!isMounted || !SPIFFS.exists("/pid.txt")) {
|
||||
kp = currentKp;
|
||||
ki = currentKi;
|
||||
kd = currentKd;
|
||||
return;
|
||||
}
|
||||
|
||||
File file = SPIFFS.open("/pid.txt", "r");
|
||||
kp = file.readStringUntil('\n').toFloat();
|
||||
ki = file.readStringUntil('\n').toFloat();
|
||||
kd = file.readStringUntil('\n').toFloat();
|
||||
file.close();
|
||||
|
||||
currentKp = kp;
|
||||
currentKi = ki;
|
||||
currentKd = kd;
|
||||
#else
|
||||
kp = currentKp;
|
||||
ki = currentKi;
|
||||
kd = currentKd;
|
||||
#endif
|
||||
}
|
||||
|
||||
unsigned int Storage::getSensitivity()
|
||||
|
@ -41,30 +141,6 @@ void Storage::setSensitivity(unsigned int sensitivity)
|
|||
file.close();
|
||||
}
|
||||
|
||||
void Storage::setPID(float kp, float ki, float kd)
|
||||
{
|
||||
if (!isMounted)
|
||||
return;
|
||||
|
||||
File file = SPIFFS.open("/pid.txt", "w");
|
||||
file.println(kp);
|
||||
file.println(ki);
|
||||
file.println(kd);
|
||||
file.close();
|
||||
}
|
||||
|
||||
void Storage::getPID(float &kp, float &ki, float &kd)
|
||||
{
|
||||
if (!isMounted)
|
||||
return;
|
||||
|
||||
File file = SPIFFS.open("/pid.txt", "r");
|
||||
kp = file.readStringUntil('\n').toFloat();
|
||||
ki = file.readStringUntil('\n').toFloat();
|
||||
kd = file.readStringUntil('\n').toFloat();
|
||||
file.close();
|
||||
}
|
||||
|
||||
void Storage::setName(String name)
|
||||
{
|
||||
if (!isMounted)
|
||||
|
@ -86,38 +162,3 @@ String Storage::getName()
|
|||
|
||||
return name;
|
||||
}
|
||||
|
||||
void Storage::setTrackingParams(uint8_t baseSpeed, uint8_t turnSpeed, uint8_t rotateSensitive)
|
||||
{
|
||||
if (!isMounted)
|
||||
return;
|
||||
|
||||
File file = SPIFFS.open("/tracking.txt", "w");
|
||||
file.println(baseSpeed);
|
||||
file.println(turnSpeed);
|
||||
file.println(rotateSensitive);
|
||||
file.close();
|
||||
}
|
||||
|
||||
void Storage::getTrackingParams(uint8_t &baseSpeed, uint8_t &turnSpeed, uint8_t &rotateSensitive)
|
||||
{
|
||||
if (!isMounted) {
|
||||
baseSpeed = 50;
|
||||
turnSpeed = 50;
|
||||
rotateSensitive = 3;
|
||||
return;
|
||||
}
|
||||
|
||||
File file = SPIFFS.open("/tracking.txt", "r");
|
||||
if (!file) {
|
||||
baseSpeed = 50;
|
||||
turnSpeed = 50;
|
||||
rotateSensitive = 3;
|
||||
return;
|
||||
}
|
||||
|
||||
baseSpeed = file.readStringUntil('\n').toInt();
|
||||
turnSpeed = file.readStringUntil('\n').toInt();
|
||||
rotateSensitive = file.readStringUntil('\n').toInt();
|
||||
file.close();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue