cleanup power handling code

This commit is contained in:
2019-07-22 15:21:56 +03:00
parent e641afeac8
commit c50c3354fb
7 changed files with 30 additions and 12 deletions

View File

@@ -17,6 +17,6 @@ public:
I2CStatusMsg status();
};
extern Executor executor;
#endif

View File

@@ -2,10 +2,5 @@
#define GLOBALS_H
#include <Arduino.h>
#include "Power.h"
#include "Executor.h"
Power power;
Executor executor;
#endif // GLOBALS_H

View File

@@ -2,18 +2,26 @@
#define POWER_H
#include <Arduino.h>
#include <functional>
#include <Ticker.h>
#include "Config.h"
class Power {
private:
bool enabled12v = false;
unsigned long lastCmdTime = 0;
void tickerRoutine();
Ticker ticker;
public:
Power();
void disable12v();
void enable12v();
bool isEnabled12v();
void commandHook();
};
extern Power power;
#endif // POWER_H

View File

@@ -1,5 +1,7 @@
#include "Executor.h"
Executor executor;
Executor::Executor() {}
void Executor::execCommand(Command command) {

View File

@@ -0,0 +1 @@
#include "Globals.h"

View File

@@ -1,8 +1,20 @@
#include "Power.h"
#include "Globals.h"
Power power;
constexpr unsigned long commandTimeout = 20000;
Power::Power() {
pinMode(pin12v, OUTPUT);
disable12v();
ticker.attach(0.1, std::bind(&Power::tickerRoutine, this));
}
void Power::tickerRoutine() {
if (millis() - lastCmdTime > commandTimeout) {
disable12v();
}
}
void Power::disable12v() {
@@ -18,3 +30,7 @@ void Power::enable12v() {
bool Power::isEnabled12v() {
return enabled12v;
}
void Power::commandHook() {
lastCmdTime = millis();
}

View File

@@ -4,6 +4,7 @@
#include <Wire.h>
#include "GCodeParser.h"
#include "Globals.h"
#include "Executor.h"
#include "Power.h"
#include "common/Commands.h"
@@ -15,13 +16,8 @@ void setup() {
power.enable12v();
}
unsigned long commandTime = 0;
constexpr unsigned long commandTimeout = 20000;
void loop() {
if (millis() - commandTime > commandTimeout) {
power.disable12v();
}
while (Serial.available() > 0) {
char inChar = Serial.read();
inString += inChar;
@@ -32,8 +28,8 @@ void loop() {
power.enable12v();
delay(100);
}
power.commandHook();
executor.execCommand(parseGCode(inString));
commandTime = millis();
I2CStatusMsg response;
do {
response = executor.status();