#include <List.hpp>
#define NB_VALVES 8
#define DEBUG 1
int size = 0;
int nowTimer;
int nowDisplay;
int startTimer;
int startDisplay;
char programIndex = 1;
bool menuDisplayed = false;
// Struct definition
struct kStep {
char name[32]; // name of action
char type; // type of action 0 -> ACTION, 1 -> RUN, 2 -> TIMER
bool isComplete;
int params[NB_VALVES];
};
// Define the program here
struct kStep causticProgram[] = {
{"Charger la soude", 0, false, {0, 1, 2}},
{"Lancer moteur", 1, false, {0,2,3}},
{"Cycle 20'soude", 2, false, {5000, 0}}
};
struct kStep kegProgram[] = {
{"Purger fut", 0, false, {0, 1, 2}},
{"Lancer moteur", 1, false, {0,2,3}},
{"Cycle 5'soude", 2, false, {0,2,3,5}}
};
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(13, OUTPUT);
};
bool displayTime() {
nowDisplay = millis();
if (nowDisplay - startDisplay >= 1000) {
startDisplay = nowDisplay;
if (DEBUG)
Serial.print(nowDisplay/1000);
return true;
}
}
bool isFinished(int interval) {
nowTimer = millis();
if (nowTimer - startTimer >= interval) // save the last time you blinked the LED
return true;
return false;
}
bool doAction(int params[]){
if (DEBUG)
Serial.print("Lets do : \n");
for (int a = 0; a < NB_VALVES; a++) {
if (DEBUG) {
Serial.print("\tvalve");
Serial.print(a);
Serial.print(": ");
Serial.print(params[a] ? "open\n" : "close\n");
}
}
if (DEBUG)
Serial.print("\n");
return true;
}
bool doRun(int params[]){
return true;
}
bool finishIt(){
programIndex = 0; // + should re init all flags
if (DEBUG)
Serial.print("Program finished !\n\n");
}
void printDebug(int a, char *step, struct kStep *program, bool shouldDisplay){
Serial.print("Step: ");
Serial.print(a);
Serial.print(step);
Serial.print(program[a].name);
Serial.print("\n");
if (shouldDisplay) {
program[a].params[1] = 1;
}
}
bool doProgram(struct kStep *program, int size) {
for (int a = 0; a < size; a++) {
if (program[a].isComplete == false) {
// Do action :
switch (program[a].type) {
case 0: // Action
if (DEBUG)
printDebug(a, " => Action: ", program, false);
if (!doAction(program[a].params))
return false; // error during action
program[a].isComplete = true;
if (a == size - 1) // programm fini !
finishIt();
return true;// we go out the entire function in order to let the loop in place
case 1: // Run
if (DEBUG)
printDebug(a, " => Run: ", program, false);
if (!doRun(program[a].params))
return false; // error during doRun
program[a].isComplete = true;
if (a == size - 1) // programm fini !
finishIt();
return true;
case 2: // Timer
if (DEBUG)
if (!program[a].params[1])
printDebug(a, " => Timer: ", program, true);
if (!isFinished(program[a].params[0])) {
displayTime();
return true;
}
else {
program[a].isComplete = true;
if (a == size - 1) // programm fini !
finishIt();
return true;
}
default:
break;
}
}
}
}
void loop() {
// put your main code here, to run repeatedly:
switch (programIndex) {
case 0:
//Serial.print("Menu");
break;
case 1:
//Serial.print("Caustic Washing");
size = sizeof(causticProgram) / sizeof(causticProgram[0]);
doProgram(causticProgram, size);
break;
case 2:
//Serial.print("Keg Washing");
size = sizeof(kegProgram) / sizeof(kegProgram[0]);
doProgram(kegProgram, size);
break;
default:
break;
}
}