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(); I2CStatusMsg status();
}; };
extern Executor executor;
#endif #endif

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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