command queue, default settings

This commit is contained in:
2019-07-26 19:56:10 +03:00
parent dacbdb33c9
commit efd7f59a31
6 changed files with 68 additions and 32 deletions

View File

@@ -5,6 +5,8 @@
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
extern const std::unordered_map<std::string, std::string> defaults;
class ConfigManager { class ConfigManager {
private: private:
bool saved = false; bool saved = false;
@@ -16,6 +18,7 @@ class ConfigManager {
void update(std::string &prop, std::string &val); void update(std::string &prop, std::string &val);
void load(); void load();
void write(); void write();
void reset();
bool isSaved(); bool isSaved();
}; };

View File

@@ -10,6 +10,7 @@ enum class LCommandType {
ConfPut, // LCP ConfPut, // LCP
ConfLoad, // LCL ConfLoad, // LCL
ConfWrite, // LCW ConfWrite, // LCW
ConfReset, // LCR
}; };
struct LCommand { struct LCommand {

View File

@@ -2,6 +2,14 @@
#include "ConfigManager.h" #include "ConfigManager.h"
const std::unordered_map<std::string, std::string> defaults{
{{"wifiClient", "off"},
{"wifiClientSSID", ""},
{"wifiClientPass", ""},
{"wifiAP", "on"},
{"wifiAPSSID", "eggbot"},
{"wifiAPPASS", "eggbot"}}};
/* /*
Max string length is 25 Max string length is 25
Pairs are stored in EEPROM as 50 chars (25 chars for one string) Pairs are stored in EEPROM as 50 chars (25 chars for one string)
@@ -48,7 +56,9 @@ void ConfigManager::load() {
} }
if (strcmp(curStr, "good") != 0) { if (strcmp(curStr, "good") != 0) {
map = defaults;
EEPROM.end(); EEPROM.end();
configManager.write();
return; return;
} }
@@ -81,20 +91,20 @@ void ConfigManager::write() {
int curPair = 0; int curPair = 0;
char buffer[2048]; char buffer[2048];
memset(buffer, 0, 2047); memset(buffer, 0, 2048);
strncpy(buffer, "good", 24); strncpy(buffer, "good", 25);
curPair++; curPair++;
EEPROM.begin(2048); EEPROM.begin(2048);
for (auto &val : map) { for (auto &val : map) {
strncpy(&buffer[curPair * 50], val.first.c_str(), 24); strncpy(&buffer[curPair * 50], val.first.c_str(), 25);
strncpy(&buffer[(curPair * 50) + 25], val.second.c_str(), 24); strncpy(&buffer[(curPair * 50) + 25], val.second.c_str(), 25);
curPair++; curPair++;
} }
strncpy(&buffer[curPair * 50], "end", 24); strncpy(&buffer[curPair * 50], "end", 25);
curPair++; curPair++;
for (int i = 0; i < curPair * 50; i++) { for (int i = 0; i < curPair * 50; i++) {
@@ -104,6 +114,20 @@ void ConfigManager::write() {
EEPROM.end(); EEPROM.end();
} }
void ConfigManager::reset() {
char buffer[50];
memset(buffer, 0, 50);
strncpy(buffer, "reset", 50);
EEPROM.begin(50);
for (unsigned int i = 0; i < sizeof(buffer); i++) {
EEPROM.write(i, buffer[i]);
}
EEPROM.commit();
EEPROM.end();
}
bool ConfigManager::isSaved() { return saved; } bool ConfigManager::isSaved() { return saved; }
ConfigManager configManager; ConfigManager configManager;

View File

@@ -25,6 +25,8 @@ void LCommand::fromChars(char *cmd) {
type = LCommandType::ConfLoad; type = LCommandType::ConfLoad;
} else if (strcmp("LCW", cmd) == 0) { } else if (strcmp("LCW", cmd) == 0) {
type = LCommandType::ConfWrite; type = LCommandType::ConfWrite;
} else if (strcmp("LCR", cmd) == 0) {
type = LCommandType::ConfReset;
} }
} else if (i == 1) { } else if (i == 1) {
strncpy(arg1, token, 25); strncpy(arg1, token, 25);

View File

@@ -43,6 +43,11 @@ void LocalExecutor::execCommand(LCommand cmd) {
configManager.write(); configManager.write();
return; return;
} }
if (cmd.type == LCommandType::ConfReset) {
configManager.reset();
return;
}
} }
LocalExecutor localExecutor; LocalExecutor localExecutor;

View File

@@ -2,6 +2,7 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <Ticker.h> #include <Ticker.h>
#include <Wire.h> #include <Wire.h>
#include <queue>
#include <string> #include <string>
#include "ConfigManager.h" #include "ConfigManager.h"
#include "Executor.h" #include "Executor.h"
@@ -11,32 +12,17 @@
#include "Power.h" #include "Power.h"
#include "common/Commands.h" #include "common/Commands.h"
std::string inString; std::queue<Command> commandQueue;
std::string commandBuf; std::queue<LCommand> lCommandQueue;
bool newCommand;
bool localCmd;
bool shouldPrintSts; bool shouldPrintSts;
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
Wire.begin(12, 13); Wire.begin(12, 13);
power.enable12v(); power.enable12v();
inString.reserve(50);
commandBuf.reserve(50);
configManager.load(); configManager.load();
} }
void sendCommand(std::string command) {
power.commandHook();
executor.execCommand(parseGCode(command));
shouldPrintSts = true;
}
void execLocalCommand(std::string command) {
localExecutor.execCommand(LCommand(command));
shouldPrintSts = true;
}
void printSts(I2CStatusMsg status) { void printSts(I2CStatusMsg status) {
if (status == I2CStatusMsg::WAIT) { if (status == I2CStatusMsg::WAIT) {
shouldPrintSts = true; shouldPrintSts = true;
@@ -47,7 +33,9 @@ void printSts(I2CStatusMsg status) {
} }
} }
void loop() { void serialLoop() {
static std::string inString;
static bool localCmd = false;
while (Serial.available() > 0) { while (Serial.available() > 0) {
char inChar = Serial.read(); char inChar = Serial.read();
@@ -61,26 +49,39 @@ void loop() {
} }
if (inChar == '\n') { if (inChar == '\n') {
commandBuf = inString; if (localCmd) {
lCommandQueue.emplace(inString);
localCmd = false;
} else {
commandQueue.push(parseGCode(inString));
}
inString = ""; inString = "";
newCommand = true;
} else { } else {
inString += inChar; inString += inChar;
} }
} }
}
void commandsLoop() {
I2CStatusMsg status = executor.status(); I2CStatusMsg status = executor.status();
if (shouldPrintSts) { if (shouldPrintSts) {
shouldPrintSts = false; shouldPrintSts = false;
printSts(status); printSts(status);
} }
if (!localCmd && newCommand && status == I2CStatusMsg::NEXT) { if (status == I2CStatusMsg::NEXT && !commandQueue.empty()) {
newCommand = false; power.commandHook();
sendCommand(commandBuf); executor.execCommand(commandQueue.front());
commandQueue.pop();
shouldPrintSts = true;
} }
if (localCmd && newCommand) { if (!lCommandQueue.empty()) {
localCmd = false; localExecutor.execCommand(lCommandQueue.front());
newCommand = false; lCommandQueue.pop();
execLocalCommand(commandBuf); shouldPrintSts = true;
} }
} }
void loop() {
serialLoop();
commandsLoop();
}