make a QueueManager

This commit is contained in:
2019-08-01 21:29:14 +03:00
parent 62e86aa4c2
commit 25ad9e46cb
5 changed files with 85 additions and 52 deletions

View File

@@ -7,7 +7,4 @@
#include "LocalCommand.h"
#include "common/Commands.h"
extern std::queue<Command> commandQueue;
extern std::queue<LCommand> lCommandQueue;
#endif // GLOBALS_H

View File

@@ -0,0 +1,35 @@
#ifndef QUEUE_MANAGER_H
#define QUEUE_MANAGER_h
#include <Arduino.h>
#include <string>
#include <queue>
#include "common/Commands.h"
#include "LocalCommand.h"
#include "Executor.h"
#include "LocalExecutor.h"
#include "common/Status.h"
#include "Power.h"
#include "GCodeParser.h"
class QueueManager {
private:
std::queue<Command> commandQueue;
std::queue<LCommand> lCommandQueue;
bool shouldPrintSts;
void printSts(Status status);
public:
QueueManager();
void init();
void loopRoutine();
uint8_t execQueueNum();
void putCommand(std::string cmd);
void putCommand(char *cmd);
};
extern QueueManager queueManager;
#endif

View File

@@ -1,4 +1 @@
#include "Globals.h"
std::queue<Command> commandQueue;
std::queue<LCommand> lCommandQueue;

View File

@@ -0,0 +1,47 @@
#include "QueueManager.h"
QueueManager::QueueManager() {}
void QueueManager::init() {}
void QueueManager::printSts(Status status) {
if (status.type == StatusType::WAIT) {
shouldPrintSts = true;
} else if (status.type == StatusType::NEXT) {
Serial.println("OK");
} else if (status.type == StatusType::TIMEOUT) {
Serial.println("Timeout");
} else {
Serial.print("Error: ");
Serial.println(static_cast<int>(status.type));
}
}
void QueueManager::loopRoutine() {
Status status = executor.status();
if (shouldPrintSts) {
shouldPrintSts = false;
printSts(status);
}
if (status.type == StatusType::NEXT && !commandQueue.empty()) {
power.commandHook();
executor.execCommand(commandQueue.front());
commandQueue.pop();
shouldPrintSts = true;
}
if (!lCommandQueue.empty()) {
localExecutor.execCommand(lCommandQueue.front());
lCommandQueue.pop();
shouldPrintSts = true;
}
}
void QueueManager::putCommand(std::string cmd) {
if (toupper(cmd[0]) == 'L') {
lCommandQueue.emplace(cmd);
} else {
commandQueue.push(parseGCode(cmd));
}
}
QueueManager queueManager;

View File

@@ -15,10 +15,9 @@
#include "Power.h"
#include "WiFiManager.h"
#include "WebAPI.h"
#include "QueueManager.h"
#include "common/Commands.h"
bool shouldPrintSts;
void setup() {
Serial.begin(115200);
Wire.begin(12, 13);
@@ -30,22 +29,8 @@ void setup() {
webApi.init();
}
void printSts(Status status) {
if (status.type == StatusType::WAIT) {
shouldPrintSts = true;
} else if (status.type == StatusType::NEXT) {
Serial.println("OK");
} else if (status.type == StatusType::TIMEOUT) {
Serial.println("Timeout");
} else {
Serial.print("Error: ");
Serial.println(static_cast<int>(status.type));
}
}
void serialLoop() {
static std::string inString;
static bool localCmd = false;
while (Serial.available() > 0) {
char inChar = Serial.read();
@@ -54,17 +39,8 @@ void serialLoop() {
break;
}
if (inString.length() == 0 && toupper(inChar) == 'L') {
localCmd = true;
}
if (inChar == '\n') {
if (localCmd) {
lCommandQueue.emplace(inString);
localCmd = false;
} else {
commandQueue.push(parseGCode(inString));
}
queueManager.putCommand(inString);
inString = "";
} else {
inString += inChar;
@@ -72,28 +48,9 @@ void serialLoop() {
}
}
void commandsLoop() {
Status status = executor.status();
if (shouldPrintSts) {
shouldPrintSts = false;
printSts(status);
}
if (status.type == StatusType::NEXT && !commandQueue.empty()) {
power.commandHook();
executor.execCommand(commandQueue.front());
commandQueue.pop();
shouldPrintSts = true;
}
if (!lCommandQueue.empty()) {
localExecutor.execCommand(lCommandQueue.front());
lCommandQueue.pop();
shouldPrintSts = true;
}
}
void loop() {
serialLoop();
commandsLoop();
queueManager.loopRoutine();
MDNS.update();
webApi.loopRoutine();