mirror of
https://github.com/usatiuk/EggbotWireless.git
synced 2025-10-26 16:57:48 +01:00
clean up loop()
This commit is contained in:
@@ -10,7 +10,9 @@
|
|||||||
class Executor
|
class Executor
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
/* data */
|
unsigned long lastStsTime;
|
||||||
|
I2CStatusMsg lastSts;
|
||||||
|
static constexpr int lastStsTTL = 1;
|
||||||
public:
|
public:
|
||||||
Executor(/* args */);
|
Executor(/* args */);
|
||||||
void execCommand(Command command);
|
void execCommand(Command command);
|
||||||
|
|||||||
@@ -64,15 +64,27 @@ void Executor::execCommand(Command command) {
|
|||||||
|
|
||||||
I2CStatusMsg Executor::status() {
|
I2CStatusMsg Executor::status() {
|
||||||
unsigned long reqTime = millis();
|
unsigned long reqTime = millis();
|
||||||
Wire.requestFrom(8, 1);
|
int tries = 0;
|
||||||
|
|
||||||
|
if (reqTime - lastStsTime < lastStsTTL) {
|
||||||
|
return lastSts;
|
||||||
|
}
|
||||||
|
|
||||||
|
Wire.requestFrom(8, 1);
|
||||||
while (!Wire.available()) {
|
while (!Wire.available()) {
|
||||||
if (millis() - reqTime > 100) {
|
if (millis() - reqTime > 10 && tries < 10) {
|
||||||
Wire.requestFrom(8, 1);
|
Wire.requestFrom(8, 1);
|
||||||
|
tries++;
|
||||||
reqTime = millis();
|
reqTime = millis();
|
||||||
|
} else {
|
||||||
|
return I2CStatusMsg::TIMEOUT;
|
||||||
}
|
}
|
||||||
|
delay(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int resp = Wire.read();
|
int resp = Wire.read();
|
||||||
return static_cast<I2CStatusMsg>(resp);
|
|
||||||
|
lastStsTime = millis();
|
||||||
|
lastSts = static_cast<I2CStatusMsg>(resp);
|
||||||
|
|
||||||
|
return lastSts;
|
||||||
}
|
}
|
||||||
@@ -27,10 +27,12 @@ void Power::enable12v() {
|
|||||||
enabled12v = true;
|
enabled12v = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Power::isEnabled12v() {
|
bool Power::isEnabled12v() { return enabled12v; }
|
||||||
return enabled12v;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Power::commandHook() {
|
void Power::commandHook() {
|
||||||
lastCmdTime = millis();
|
lastCmdTime = millis();
|
||||||
|
if (!isEnabled12v()) {
|
||||||
|
enable12v();
|
||||||
|
delay(100);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -2,13 +2,16 @@
|
|||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <Ticker.h>
|
#include <Ticker.h>
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
|
#include "Executor.h"
|
||||||
#include "GCodeParser.h"
|
#include "GCodeParser.h"
|
||||||
#include "Globals.h"
|
#include "Globals.h"
|
||||||
#include "Executor.h"
|
|
||||||
#include "Power.h"
|
#include "Power.h"
|
||||||
#include "common/Commands.h"
|
#include "common/Commands.h"
|
||||||
|
|
||||||
String inString;
|
String inString;
|
||||||
|
String commandBuf;
|
||||||
|
bool newCommand;
|
||||||
|
bool shouldPrintSts;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
@@ -16,6 +19,21 @@ void setup() {
|
|||||||
power.enable12v();
|
power.enable12v();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void sendCommand(String command) {
|
||||||
|
power.commandHook();
|
||||||
|
executor.execCommand(parseGCode(command));
|
||||||
|
shouldPrintSts = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void printSts(I2CStatusMsg status) {
|
||||||
|
if (status == I2CStatusMsg::WAIT) {
|
||||||
|
shouldPrintSts = true;
|
||||||
|
} else if (status == I2CStatusMsg::NEXT) {
|
||||||
|
Serial.println("OK");
|
||||||
|
} else {
|
||||||
|
Serial.println("Error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
while (Serial.available() > 0) {
|
while (Serial.available() > 0) {
|
||||||
@@ -24,24 +42,19 @@ void loop() {
|
|||||||
|
|
||||||
if (inChar == '\n') {
|
if (inChar == '\n') {
|
||||||
inString.trim();
|
inString.trim();
|
||||||
if (!power.isEnabled12v()) {
|
commandBuf = inString;
|
||||||
power.enable12v();
|
|
||||||
delay(100);
|
|
||||||
}
|
|
||||||
power.commandHook();
|
|
||||||
executor.execCommand(parseGCode(inString));
|
|
||||||
I2CStatusMsg response;
|
|
||||||
do {
|
|
||||||
response = executor.status();
|
|
||||||
if (response == I2CStatusMsg::WAIT) {
|
|
||||||
delay(1);
|
|
||||||
} else if (response == I2CStatusMsg::NEXT) {
|
|
||||||
Serial.println("OK");
|
|
||||||
} else {
|
|
||||||
Serial.println("Error");
|
|
||||||
}
|
|
||||||
} while (response != I2CStatusMsg::NEXT);
|
|
||||||
inString = "";
|
inString = "";
|
||||||
|
newCommand = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
I2CStatusMsg status = executor.status();
|
||||||
|
if (shouldPrintSts) {
|
||||||
|
shouldPrintSts = false;
|
||||||
|
printSts(status);
|
||||||
|
}
|
||||||
|
if (newCommand && status == I2CStatusMsg::NEXT) {
|
||||||
|
newCommand = false;
|
||||||
|
sendCommand(commandBuf);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ enum bcAxis {
|
|||||||
enum class I2CStatusMsg {
|
enum class I2CStatusMsg {
|
||||||
WAIT = 0,
|
WAIT = 0,
|
||||||
NEXT,
|
NEXT,
|
||||||
|
TIMEOUT,
|
||||||
|
ERR,
|
||||||
};
|
};
|
||||||
|
|
||||||
enum PosMsg {
|
enum PosMsg {
|
||||||
|
|||||||
Reference in New Issue
Block a user