mirror of
https://github.com/okalachev/flix.git
synced 2025-08-17 17:16:10 +00:00
Implement parameters subsystem
* Unified parameters storage. * Store parameters in flash on the hardware. * Store parameters in text file in simulation. * Work with parameters in command line. * Support parameters in MAVLink for working with parameters in QGC.
This commit is contained in:
63
gazebo/Preferences.h
Normal file
63
gazebo/Preferences.h
Normal file
@@ -0,0 +1,63 @@
|
||||
// Copyright (c) 2024 Oleg Kalachev <okalachev@gmail.com>
|
||||
// Repository: https://github.com/okalachev/flix
|
||||
|
||||
// Partial implementation of the ESP32 Preferences library for the simulation
|
||||
|
||||
#include <map>
|
||||
#include <fstream>
|
||||
#include "util.h"
|
||||
|
||||
class Preferences {
|
||||
private:
|
||||
std::map<std::string, float> storage;
|
||||
std::string storagePath;
|
||||
|
||||
void readFromFile() {
|
||||
std::ifstream file(storagePath);
|
||||
std::string key;
|
||||
float value;
|
||||
while (file >> key >> value) {
|
||||
storage[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
void writeToFile() {
|
||||
std::ofstream file(storagePath);
|
||||
for (auto &pair : storage) {
|
||||
file << pair.first << " " << pair.second << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
bool begin(const char *name, bool readOnly = false, const char *partition_label = NULL) {
|
||||
storagePath = getPluginPath().parent_path() / (std::string(name) + ".txt");
|
||||
gzmsg << "Preferences initialized: " << storagePath << std::endl;
|
||||
readFromFile();
|
||||
return true;
|
||||
}
|
||||
|
||||
void end();
|
||||
|
||||
bool isKey(const char *key) {
|
||||
return storage.find(key) != storage.end();
|
||||
}
|
||||
|
||||
size_t putFloat(const char *key, float value) {
|
||||
storage[key] = value;
|
||||
writeToFile();
|
||||
return sizeof(value);
|
||||
}
|
||||
|
||||
float getFloat(const char *key, float defaultValue = NAN) {
|
||||
if (!isKey(key)) {
|
||||
return defaultValue;
|
||||
}
|
||||
return storage[key];
|
||||
}
|
||||
|
||||
bool clear() {
|
||||
storage.clear();
|
||||
writeToFile();
|
||||
return true;
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user