config manager thingy, local commands

use std::string as much as possible
This commit is contained in:
2019-07-25 17:52:04 +03:00
parent eca88cdafb
commit 76a4bc6b76
9 changed files with 195 additions and 9 deletions

View File

@@ -1,6 +1,20 @@
#ifndef CONFIG_MANAGER_H
#define CONFIG_MANAGER_H
#include <Arduino.h>
#include <string>
#include <unordered_map>
class ConfigManager {
private:
public:
std::unordered_map<std::string, std::string> map;
ConfigManager();
std::string get(std::string &prop);
void update(std::string &prop, std::string &val);
void load();
};
extern ConfigManager configManager;
#endif // CONFIG_MANAGER_H

View File

@@ -4,6 +4,6 @@
#include <Arduino.h>
#include "common/Commands.h"
Command parseGCode(String gcode);
Command parseGCode(std::string gcode);
#endif // PARSER_H

View File

@@ -0,0 +1,63 @@
#ifndef LOCAL_COMMAND_H
#define LOCAL_COMMAND_H
#include <Arduino.h>
enum class LCommandType {
unk = 0,
ConfGet, // LCG
ConfList, // LCL
ConfSet, // LCS
};
struct LCommand {
LCommandType type = LCommandType::unk;
char arg1[25];
char arg2[25];
char arg3[25];
LCommand(LCommandType type) : type(type){};
LCommand(char *cmd) { fromChars(cmd); }
LCommand(std::string cmd) {
char buf[50];
strncpy(buf, cmd.c_str(), 50);
fromChars(buf);
}
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

View File

@@ -0,0 +1,18 @@
#ifndef LOCAL_EXECUTOR_H
#define LOCAL_EXECUTOR_H
#include <Arduino.h>
#include "LocalCommand.h"
class LocalExecutor
{
private:
public:
LocalExecutor();
void execCommand(LCommand cmd);
};
extern LocalExecutor localExecutor;
#endif // LOCAL_EXECUTOR_H

View File

@@ -0,0 +1,26 @@
#include "EEPROM.h"
#include "ConfigManager.h"
ConfigManager::ConfigManager() { map.reserve(20); }
void ConfigManager::update(std::string &prop, std::string &val) {
auto valp = map.find(prop);
if (valp != map.end()) {
map.erase(valp);
}
map.emplace(prop, val);
}
std::string ConfigManager::get(std::string &prop) {
auto valp = map.find(prop);
if (valp != map.end()) {
return valp->second;
} else {
return "";
}
}
void ConfigManager::load() {}
ConfigManager configManager;

View File

@@ -5,11 +5,11 @@
Command bufcmd;
Command parseGCode(String gcode) {
Command parseGCode(std::string gcode) {
char commandString[50];
std::unordered_map<char, float> argsMap;
gcode.toCharArray(commandString, 50);
strncpy(commandString, gcode.c_str(), 50);
// Convert command to uppercase
for (int i = 0; commandString[i] != '\0'; i++) {

View File

@@ -0,0 +1,2 @@
#include "LocalCommand.h"

View File

@@ -0,0 +1,39 @@
#include "LocalExecutor.h"
#include <string>
#include "ConfigManager.h"
LocalExecutor::LocalExecutor() {}
void LocalExecutor::execCommand(LCommand cmd) {
if (cmd.type == LCommandType::ConfGet) {
Serial.print(cmd.arg1);
Serial.print(": ");
std::string arg1 = std::string(cmd.arg1);
Serial.println(configManager.get(arg1).c_str());
return;
}
if (cmd.type == LCommandType::ConfSet) {
Serial.print(cmd.arg1);
Serial.print(": ");
std::string arg1 = std::string(cmd.arg1);
std::string arg2 = std::string(cmd.arg2);
configManager.update(arg1, arg2);
Serial.println(configManager.get(arg1).c_str());
return;
}
if(cmd.type == LCommandType::ConfList) {
for(auto &val : configManager.map) {
Serial.print(val.first.c_str());
Serial.print(": ");
Serial.println(val.second.c_str());
}
}
}
LocalExecutor localExecutor;

View File

@@ -2,29 +2,39 @@
#include <ESP8266WiFi.h>
#include <Ticker.h>
#include <Wire.h>
#include <string>
#include "Executor.h"
#include "GCodeParser.h"
#include "Globals.h"
#include "LocalExecutor.h"
#include "Power.h"
#include "common/Commands.h"
String inString;
String commandBuf;
std::string inString;
std::string commandBuf;
bool newCommand;
bool localCmd;
bool shouldPrintSts;
void setup() {
Serial.begin(115200);
Wire.begin(12, 13);
power.enable12v();
inString.reserve(50);
commandBuf.reserve(50);
}
void sendCommand(String command) {
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;
@@ -38,13 +48,22 @@ void printSts(I2CStatusMsg status) {
void loop() {
while (Serial.available() > 0) {
char inChar = Serial.read();
inString += inChar;
// CR breaks some things, so we completely ignore it
if (inChar == '\r') {
break;
}
if (inString.length() == 0 && toupper(inChar) == 'L') {
localCmd = true;
}
if (inChar == '\n') {
inString.trim();
commandBuf = inString;
inString = "";
newCommand = true;
} else {
inString += inChar;
}
}
@@ -53,8 +72,13 @@ void loop() {
shouldPrintSts = false;
printSts(status);
}
if (newCommand && status == I2CStatusMsg::NEXT) {
if (!localCmd && newCommand && status == I2CStatusMsg::NEXT) {
newCommand = false;
sendCommand(commandBuf);
}
if (localCmd && newCommand) {
localCmd = false;
newCommand = false;
execLocalCommand(commandBuf);
}
}