-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
254 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
#include "FaultHandling.h" | ||
#include "Arduino.h" | ||
|
||
Fault::Handler* Fault::Handler::inst = 0; | ||
|
||
Fault::Handler* Fault::Handler::instance() | ||
{ | ||
if (inst == 0) { | ||
inst = new Handler(); | ||
} | ||
return inst; | ||
} | ||
|
||
Fault::Handler::Handler() | ||
{ | ||
for (int i = 0; i < ALL_OK; i++) { | ||
faults[i] = false; | ||
} | ||
} | ||
|
||
void Fault::Handler::setFaultCode(Type fault) | ||
{ | ||
if (!this->faults[fault]) { | ||
this->faults[fault] = true; | ||
this->minorFaultState=0; | ||
this->lastTs = millis(); | ||
} | ||
} | ||
|
||
void Fault::Handler::unlatchFaultCode(Type fault) | ||
{ | ||
this->faults[fault] = false; | ||
} | ||
|
||
bool Fault::Handler::hasFault(Type fault) | ||
{ | ||
return this->faults[fault]; | ||
|
||
} | ||
|
||
bool Fault::Handler::hasFaultOfType(Type start, Type end) | ||
{ | ||
for (int i = start; i < end; i++) { | ||
if (hasFault((Type) i)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
int Fault::Handler::numFaults() | ||
{ | ||
int count = 0; | ||
for (int i = 0; i < ALL_OK; i++) { | ||
if (faults[i] == true) { | ||
count++; | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
int Fault::Handler::nextFault(Type start) | ||
{ | ||
for (int i = start; i < ALL_OK; i++) { | ||
if (faults[i] == true) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
|
||
void Fault::Handler::printFaultReport() | ||
{ | ||
Serial.println("FAULT REPORT:"); | ||
for (int i = ZERO+1; i < FATAL_END_SENTINEL; i++) { | ||
Serial.print("Fatal Fault #"); | ||
Serial.print(i); | ||
Serial.print(": "); | ||
Serial.println(faults[i]); | ||
} | ||
for (int i = FATAL_END_SENTINEL+1; i < RETRYABLE_END_SENTINEL; i++) { | ||
Serial.print("Recoverable Fault #"); | ||
Serial.print(i); | ||
Serial.print(": "); | ||
Serial.println(faults[i]); | ||
} | ||
} | ||
|
||
void Fault::Handler::faultFlasherPeriodic(const int LEDPIN) | ||
{ | ||
if (!hasFault()) { | ||
digitalWrite(LEDPIN, HIGH); | ||
return; | ||
} | ||
|
||
if (hasMajorFault()) { | ||
int fault = nextFault(ZERO); | ||
while (true) { | ||
for (int i = 0; i < 5; i++) { | ||
digitalWrite(LEDPIN, LOW); | ||
delay(100); | ||
digitalWrite(LEDPIN, HIGH); | ||
delay(200); | ||
digitalWrite(LEDPIN, LOW); | ||
delay(100); | ||
} | ||
for (int i = 0; i < fault; i++) { | ||
digitalWrite(LEDPIN, LOW); | ||
delay(100); | ||
digitalWrite(LEDPIN, HIGH); | ||
delay(1000); | ||
digitalWrite(LEDPIN, LOW); | ||
delay(100); | ||
} | ||
printFaultReport(); | ||
} | ||
} | ||
|
||
if (hasMinorFault()) { | ||
int fault = nextFault(FATAL_END_SENTINEL); | ||
unsigned long millisDiff = millis() - lastTs; | ||
if (millisDiff > 100 || millisDiff < 0) { | ||
minorFaultState++; | ||
lastTs = millis(); | ||
} | ||
int fs = minorFaultState % (fault*6+4); | ||
if (fs < 4) { | ||
digitalWrite(LEDPIN, fs%2 == 0); | ||
} | ||
|
||
if (fs >= 4) { | ||
digitalWrite(LEDPIN, fs%6 <= 4); | ||
} | ||
|
||
if (fs == (fault*6+3)) { | ||
printFaultReport(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#ifndef POOL_FAULT_HANDLING_H | ||
#define POOL_FAULT_HANDLING_H | ||
|
||
namespace Fault { | ||
enum Type { | ||
ZERO, | ||
ACCEL_INIT, | ||
FRAM_INIT, | ||
FATAL_END_SENTINEL, | ||
ACCEL_NOT_READY, | ||
ACCEL_IMPLAUSIBLE_READING, | ||
ACCEL_PARITY_FAILURE, | ||
RETRYABLE_END_SENTINEL, | ||
ALL_OK | ||
}; | ||
|
||
class Handler { | ||
public: | ||
static Handler* instance(); | ||
void setFaultCode(Type fault); | ||
bool hasMinorFault() { return hasFaultOfType(FATAL_END_SENTINEL, ALL_OK); }; | ||
bool hasMajorFault() { return hasFaultOfType(ZERO, FATAL_END_SENTINEL); }; | ||
bool hasFault() { return hasFaultOfType(ZERO, ALL_OK); }; | ||
void faultFlasherPeriodic(const int LEDPIN); | ||
void unlatchFaultCode(Type fault); | ||
private: | ||
bool faults[ALL_OK]; | ||
static Handler* inst; | ||
int minorFaultState = 0; | ||
unsigned long lastTs; | ||
|
||
bool hasFault(Type fault); | ||
bool hasFaultOfType(Type start, Type end); | ||
int numFaults(); | ||
int nextFault(Type start); | ||
void printFaultReport(); | ||
Handler(); | ||
}; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters