esp32-car/src/storage.cpp

124 lines
2.4 KiB
C++
Raw Normal View History

2024-12-27 09:47:56 +08:00
#include "storage.h"
bool Storage::isMounted;
void Storage::init()
{
if (!SPIFFS.begin(true))
{
Serial.println("SPIFFS Mount Failed");
isMounted = false;
}
else
2024-12-27 15:56:14 +08:00
{
Serial.println("SPIFFS Mount Success");
2024-12-27 09:47:56 +08:00
isMounted = true;
2024-12-27 15:56:14 +08:00
}
2024-12-27 09:47:56 +08:00
}
unsigned int Storage::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();
}
void Storage::setSensitivity(unsigned int sensitivity)
{
if (!isMounted)
return;
File file = SPIFFS.open("/sensitivity.txt", "w");
file.println(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)
return;
File file = SPIFFS.open("/name.txt", "w");
file.println(name);
file.close();
}
String Storage::getName()
{
if (!isMounted)
2024-12-27 10:16:08 +08:00
return DEVICE_NAME;
2024-12-27 09:47:56 +08:00
File file = SPIFFS.open("/name.txt", "r");
String name = file.readStringUntil('\n');
file.close();
return name;
}
2025-01-01 17:53:59 +08:00
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();
}