mirror of
https://github.com/usatiuk/EggbotWireless.git
synced 2025-10-26 16:57:48 +01:00
cleanup power handling code
This commit is contained in:
@@ -17,6 +17,6 @@ public:
|
||||
I2CStatusMsg status();
|
||||
};
|
||||
|
||||
|
||||
extern Executor executor;
|
||||
|
||||
#endif
|
||||
@@ -2,10 +2,5 @@
|
||||
#define GLOBALS_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "Power.h"
|
||||
#include "Executor.h"
|
||||
|
||||
Power power;
|
||||
Executor executor;
|
||||
|
||||
#endif // GLOBALS_H
|
||||
@@ -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
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "Executor.h"
|
||||
|
||||
Executor executor;
|
||||
|
||||
Executor::Executor() {}
|
||||
|
||||
void Executor::execCommand(Command command) {
|
||||
|
||||
1
Firmware/EggbotWireless/src/Globals.cpp
Normal file
1
Firmware/EggbotWireless/src/Globals.cpp
Normal file
@@ -0,0 +1 @@
|
||||
#include "Globals.h"
|
||||
@@ -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() {
|
||||
@@ -17,4 +29,8 @@ void Power::enable12v() {
|
||||
|
||||
bool Power::isEnabled12v() {
|
||||
return enabled12v;
|
||||
}
|
||||
|
||||
void Power::commandHook() {
|
||||
lastCmdTime = millis();
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user