mirror of
https://github.com/usatiuk/EggbotWireless.git
synced 2025-10-26 16:57:48 +01:00
store config in "eeprom"
This commit is contained in:
@@ -7,12 +7,16 @@
|
|||||||
|
|
||||||
class ConfigManager {
|
class ConfigManager {
|
||||||
private:
|
private:
|
||||||
|
bool saved = false;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
std::unordered_map<std::string, std::string> map;
|
std::unordered_map<std::string, std::string> map;
|
||||||
ConfigManager();
|
ConfigManager();
|
||||||
std::string get(std::string &prop);
|
std::string get(std::string &prop);
|
||||||
void update(std::string &prop, std::string &val);
|
void update(std::string &prop, std::string &val);
|
||||||
void load();
|
void load();
|
||||||
|
void write();
|
||||||
|
bool isSaved();
|
||||||
};
|
};
|
||||||
|
|
||||||
extern ConfigManager configManager;
|
extern ConfigManager configManager;
|
||||||
|
|||||||
@@ -6,8 +6,10 @@
|
|||||||
enum class LCommandType {
|
enum class LCommandType {
|
||||||
unk = 0,
|
unk = 0,
|
||||||
ConfGet, // LCG
|
ConfGet, // LCG
|
||||||
ConfList, // LCL
|
ConfList, // LCS
|
||||||
ConfSet, // LCS
|
ConfPut, // LCP
|
||||||
|
ConfLoad, // LCL
|
||||||
|
ConfWrite, // LCW
|
||||||
};
|
};
|
||||||
|
|
||||||
struct LCommand {
|
struct LCommand {
|
||||||
@@ -26,38 +28,7 @@ struct LCommand {
|
|||||||
fromChars(buf);
|
fromChars(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
void fromChars(char *cmd) {
|
void fromChars(char *cmd);
|
||||||
char *token = strtok(cmd, " ");
|
|
||||||
|
|
||||||
memset(arg1, 0, 25);
|
|
||||||
memset(arg2, 0, 25);
|
|
||||||
memset(arg3, 0, 25);
|
|
||||||
|
|
||||||
for (int i = 0; token != NULL && i < 4; i++) {
|
|
||||||
if (i == 0) {
|
|
||||||
char cmd[4];
|
|
||||||
strncpy(cmd, token, 4);
|
|
||||||
for (int i = 0; cmd[i] != '\0'; i++) {
|
|
||||||
cmd[i] = toupper(cmd[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strcmp("LCG", cmd) == 0) {
|
|
||||||
type = LCommandType::ConfGet;
|
|
||||||
} else if (strcmp("LCS", cmd) == 0) {
|
|
||||||
type = LCommandType::ConfSet;
|
|
||||||
} else if (strcmp("LCL", cmd) == 0) {
|
|
||||||
type = LCommandType::ConfList;
|
|
||||||
}
|
|
||||||
} else if (i == 1) {
|
|
||||||
strncpy(arg1, token, 25);
|
|
||||||
} else if (i == 2) {
|
|
||||||
strncpy(arg2, token, 25);
|
|
||||||
} else if (i == 3) {
|
|
||||||
strncpy(arg3, token, 25);
|
|
||||||
}
|
|
||||||
token = strtok(NULL, " ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // LOCAL_COMMAND_H
|
#endif // LOCAL_COMMAND_H
|
||||||
@@ -2,14 +2,27 @@
|
|||||||
|
|
||||||
#include "ConfigManager.h"
|
#include "ConfigManager.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
Max string length is 25
|
||||||
|
Pairs are stored in EEPROM as 50 chars (25 chars for one string)
|
||||||
|
First EEPROM pair is current status: "good", "reset"
|
||||||
|
Last pair is "end"
|
||||||
|
*/
|
||||||
|
|
||||||
ConfigManager::ConfigManager() { map.reserve(20); }
|
ConfigManager::ConfigManager() { map.reserve(20); }
|
||||||
|
|
||||||
void ConfigManager::update(std::string &prop, std::string &val) {
|
void ConfigManager::update(std::string &prop, std::string &val) {
|
||||||
|
if (prop == "good" || prop == "reset" || prop == "end") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
auto valp = map.find(prop);
|
auto valp = map.find(prop);
|
||||||
if (valp != map.end()) {
|
if (valp != map.end()) {
|
||||||
map.erase(valp);
|
map.erase(valp);
|
||||||
}
|
}
|
||||||
map.emplace(prop, val);
|
map.emplace(prop, val);
|
||||||
|
|
||||||
|
saved = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ConfigManager::get(std::string &prop) {
|
std::string ConfigManager::get(std::string &prop) {
|
||||||
@@ -21,6 +34,76 @@ std::string ConfigManager::get(std::string &prop) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConfigManager::load() {}
|
void ConfigManager::load() {
|
||||||
|
saved = true;
|
||||||
|
unsigned int cur = 0;
|
||||||
|
char curStr[25];
|
||||||
|
bool val = false;
|
||||||
|
memset(curStr, 0, 25);
|
||||||
|
|
||||||
|
EEPROM.begin(2048);
|
||||||
|
|
||||||
|
for (; cur < 25; cur++) {
|
||||||
|
curStr[cur] = EEPROM.read(cur);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strcmp(curStr, "good") != 0) {
|
||||||
|
EEPROM.end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cur = 50;
|
||||||
|
|
||||||
|
char prop[25];
|
||||||
|
do {
|
||||||
|
for (int i = 0; i < 25; cur++ && i++) {
|
||||||
|
curStr[i] = EEPROM.read(cur);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strcmp(curStr, "end") == 0 && !val) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!val) {
|
||||||
|
strncpy(prop, curStr, 25);
|
||||||
|
val = true;
|
||||||
|
} else {
|
||||||
|
map.emplace(prop, curStr);
|
||||||
|
val = false;
|
||||||
|
}
|
||||||
|
} while (strcmp(curStr, "end") != 0 && cur < EEPROM.length());
|
||||||
|
|
||||||
|
EEPROM.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConfigManager::write() {
|
||||||
|
saved = true;
|
||||||
|
|
||||||
|
int curPair = 0;
|
||||||
|
char buffer[2048];
|
||||||
|
memset(buffer, 0, 2047);
|
||||||
|
|
||||||
|
strncpy(buffer, "good", 24);
|
||||||
|
curPair++;
|
||||||
|
|
||||||
|
EEPROM.begin(2048);
|
||||||
|
|
||||||
|
for (auto &val : map) {
|
||||||
|
strncpy(&buffer[curPair * 50], val.first.c_str(), 24);
|
||||||
|
strncpy(&buffer[(curPair * 50) + 25], val.second.c_str(), 24);
|
||||||
|
curPair++;
|
||||||
|
}
|
||||||
|
|
||||||
|
strncpy(&buffer[curPair * 50], "end", 24);
|
||||||
|
curPair++;
|
||||||
|
|
||||||
|
for (int i = 0; i < curPair * 50; i++) {
|
||||||
|
EEPROM.write(i, buffer[i]);
|
||||||
|
}
|
||||||
|
EEPROM.commit();
|
||||||
|
EEPROM.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ConfigManager::isSaved() { return saved; }
|
||||||
|
|
||||||
ConfigManager configManager;
|
ConfigManager configManager;
|
||||||
@@ -1,2 +1,38 @@
|
|||||||
#include "LocalCommand.h"
|
#include "LocalCommand.h"
|
||||||
|
|
||||||
|
void LCommand::fromChars(char *cmd) {
|
||||||
|
char *token = strtok(cmd, " ");
|
||||||
|
|
||||||
|
memset(arg1, 0, 25);
|
||||||
|
memset(arg2, 0, 25);
|
||||||
|
memset(arg3, 0, 25);
|
||||||
|
|
||||||
|
for (int i = 0; token != NULL && i < 4; i++) {
|
||||||
|
if (i == 0) {
|
||||||
|
char cmd[4];
|
||||||
|
strncpy(cmd, token, 4);
|
||||||
|
for (int i = 0; cmd[i] != '\0'; i++) {
|
||||||
|
cmd[i] = toupper(cmd[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strcmp("LCG", cmd) == 0) {
|
||||||
|
type = LCommandType::ConfGet;
|
||||||
|
} else if (strcmp("LCP", cmd) == 0) {
|
||||||
|
type = LCommandType::ConfPut;
|
||||||
|
} else if (strcmp("LCS", cmd) == 0) {
|
||||||
|
type = LCommandType::ConfList;
|
||||||
|
} else if (strcmp("LCL", cmd) == 0) {
|
||||||
|
type = LCommandType::ConfLoad;
|
||||||
|
} else if (strcmp("LCW", cmd) == 0) {
|
||||||
|
type = LCommandType::ConfWrite;
|
||||||
|
}
|
||||||
|
} else if (i == 1) {
|
||||||
|
strncpy(arg1, token, 25);
|
||||||
|
} else if (i == 2) {
|
||||||
|
strncpy(arg2, token, 25);
|
||||||
|
} else if (i == 3) {
|
||||||
|
strncpy(arg3, token, 25);
|
||||||
|
}
|
||||||
|
token = strtok(NULL, " ");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,7 +14,7 @@ void LocalExecutor::execCommand(LCommand cmd) {
|
|||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (cmd.type == LCommandType::ConfSet) {
|
if (cmd.type == LCommandType::ConfPut) {
|
||||||
Serial.print(cmd.arg1);
|
Serial.print(cmd.arg1);
|
||||||
Serial.print(": ");
|
Serial.print(": ");
|
||||||
std::string arg1 = std::string(cmd.arg1);
|
std::string arg1 = std::string(cmd.arg1);
|
||||||
@@ -34,6 +34,15 @@ void LocalExecutor::execCommand(LCommand cmd) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(cmd.type == LCommandType::ConfLoad) {
|
||||||
|
configManager.load();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(cmd.type == LCommandType::ConfWrite) {
|
||||||
|
configManager.write();
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LocalExecutor localExecutor;
|
LocalExecutor localExecutor;
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
#include <Ticker.h>
|
#include <Ticker.h>
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include "ConfigManager.h"
|
||||||
#include "Executor.h"
|
#include "Executor.h"
|
||||||
#include "GCodeParser.h"
|
#include "GCodeParser.h"
|
||||||
#include "Globals.h"
|
#include "Globals.h"
|
||||||
@@ -22,6 +23,7 @@ void setup() {
|
|||||||
power.enable12v();
|
power.enable12v();
|
||||||
inString.reserve(50);
|
inString.reserve(50);
|
||||||
commandBuf.reserve(50);
|
commandBuf.reserve(50);
|
||||||
|
configManager.load();
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendCommand(std::string command) {
|
void sendCommand(std::string command) {
|
||||||
|
|||||||
Reference in New Issue
Block a user