disable 12v after 20s of inactivity

This commit is contained in:
2019-07-15 21:12:17 +03:00
parent 133a4abc5e
commit d2b824d016
4 changed files with 42 additions and 7 deletions

View File

@@ -0,0 +1,9 @@
#ifndef GLOBALS_H
#define GLOBALS_H
#include <Arduino.h>
#include "Power.h"
Power power;
#endif // GLOBALS_H

View File

@@ -6,10 +6,14 @@
#include "Config.h"
class Power {
private:
bool enabled12v = false;
public:
Power();
void disable12v();
void enable12v();
bool isEnabled12v();
};
#endif // POWER_H

View File

@@ -2,9 +2,19 @@
Power::Power() {
pinMode(pin12v, OUTPUT);
enable12v();
disable12v();
}
void Power::disable12v() { digitalWrite(pin12v, false); }
void Power::disable12v() {
digitalWrite(pin12v, false);
enabled12v = false;
}
void Power::enable12v() { digitalWrite(pin12v, true); }
void Power::enable12v() {
digitalWrite(pin12v, true);
enabled12v = true;
}
bool Power::isEnabled12v() {
return enabled12v;
}

View File

@@ -2,6 +2,8 @@
#include <ESP8266WiFi.h>
#include <Ticker.h>
#include <Wire.h>
#include "Globals.h"
#include "Power.h"
#include "GCodeParser.h"
#include "common/Commands.h"
@@ -71,18 +73,28 @@ void execCommand(Command command) {
void setup() {
Serial.begin(115200);
Wire.begin(12, 13);
pinMode(5, OUTPUT);
digitalWrite(5, true);
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;
if (inChar == '\n') {
inString.trim();
if(!power.isEnabled12v()){
power.enable12v();
delay(100);
}
execCommand(parseGCode(inString));
commandTime = millis();
unsigned long reqTime = millis();
while (waitingForNext) {
while (!Wire.available()) {