feat: add inital boot

This commit is contained in:
eneller
2024-08-31 22:01:04 +02:00
parent 09efbb6536
commit 5ba2bd19df

View File

@@ -1,10 +1,13 @@
const int HEARTBEAT_MAX_DELAY_MILLIS = 20000; //this is coupled to the HEARTBEAT_INTERVAL in src/main.rs const int HEARTBEAT_REBOOT_DELAY_MILLIS = 20000; //this is coupled to the HEARTBEAT_INTERVAL in src/main.rs
const int PIN_SWITCH = 2; const int HEARTBEAT_BOOT_DELAY_MILLIS = 60000;
const int PIN_REBOOT = 2;
const int PIN_BOOT = 3;
unsigned long startTimeMillis; unsigned long startTimeMillis;
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
pinMode(PIN_SWITCH, OUTPUT); pinMode(PIN_REBOOT, OUTPUT);
pinMode(PIN_BOOT, OUTPUT);
waitForSerial(); waitForSerial();
//NOTE might want to replace this initialization with a check in the first loop, so we dont instantly reboot //NOTE might want to replace this initialization with a check in the first loop, so we dont instantly reboot
startTimeMillis = millis(); startTimeMillis = millis();
@@ -12,9 +15,7 @@ void setup() {
} }
void loop(){ void loop(){
unsigned long currentTimeMillis = millis(); if (millis() - startTimeMillis > HEARTBEAT_REBOOT_DELAY_MILLIS){pin_switch(PIN_REBOOT);}
unsigned long tickMillis = currentTimeMillis - startTimeMillis;
if (tickMillis > HEARTBEAT_MAX_DELAY_MILLIS){rst();}
if (Serial.available() > 0) { // if something has been written to the serial port if (Serial.available() > 0) { // if something has been written to the serial port
startTimeMillis = millis(); startTimeMillis = millis();
clearSerial(); clearSerial();
@@ -22,12 +23,12 @@ void loop(){
} }
void rst(){ void pin_switch(int pin){
//TODO connect the RST pins on the motherboard //TODO connect the RST pins on the motherboard
Serial.println("Error!"); Serial.println("Error!");
digitalWrite(PIN_SWITCH, LOW); digitalWrite(pin, LOW);
delay(500); delay(500);
digitalWrite(PIN_SWITCH, HIGH); digitalWrite(pin, HIGH);
waitForSerial(); waitForSerial();
} }
@@ -35,7 +36,12 @@ void rst(){
busy wait for first serial input busy wait for first serial input
*/ */
void waitForSerial(){ void waitForSerial(){
while(Serial.available() == 0){} while(Serial.available() == 0){
//check if we may need to initiate a normal boot instead of a reboot, e.g. after a power outage
if (millis() - startTimeMillis > HEARTBEAT_BOOT_DELAY_MILLIS){
pin_switch(PIN_BOOT);
}
}
} }
void clearSerial(){ void clearSerial(){