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

View File

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

View File

@@ -2,6 +2,14 @@
#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
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) {
map = defaults;
EEPROM.end();
configManager.write();
return;
}
@@ -81,20 +91,20 @@ void ConfigManager::write() {
int curPair = 0;
char buffer[2048];
memset(buffer, 0, 2047);
memset(buffer, 0, 2048);
strncpy(buffer, "good", 24);
strncpy(buffer, "good", 25);
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);
strncpy(&buffer[curPair * 50], val.first.c_str(), 25);
strncpy(&buffer[(curPair * 50) + 25], val.second.c_str(), 25);
curPair++;
}
strncpy(&buffer[curPair * 50], "end", 24);
strncpy(&buffer[curPair * 50], "end", 25);
curPair++;
for (int i = 0; i < curPair * 50; i++) {
@@ -104,6 +114,20 @@ void ConfigManager::write() {
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; }
ConfigManager configManager;

View File

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

View File

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

View File

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