esp32-car/src/storage.cpp

124 lines
2.4 KiB
C++

#include "storage.h"
bool Storage::isMounted;
void Storage::init()
{
if (!SPIFFS.begin(true))
{
Serial.println("SPIFFS Mount Failed");
isMounted = false;
}
else
{
Serial.println("SPIFFS Mount Success");
isMounted = true;
}
}
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)
return DEVICE_NAME;
File file = SPIFFS.open("/name.txt", "r");
String name = file.readStringUntil('\n');
file.close();
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();
}