Added new state IDLE
This commit is contained in:
parent
bf2176ed83
commit
f6132f1c85
2 changed files with 44 additions and 31 deletions
|
@ -52,12 +52,13 @@ typedef struct transcv_struct{
|
||||||
|
|
||||||
#define DISPLAY_I2C_ADDRESS 0x3C //Adress of the Display
|
#define DISPLAY_I2C_ADDRESS 0x3C //Adress of the Display
|
||||||
|
|
||||||
typedef enum {TIMER_INIT = 0, TIMER_READY, TIMER_STARTED, TIMER_RUNNING , TIMER_CANCELLED, TIMER_STOPPED, TIMER_TIMEDOUT, TIMER_FAIL, TIMER_WAIT} timer_state_e;
|
typedef enum {TIMER_INIT = 0, TIMER_IDLE, TIMER_READY, TIMER_STARTED, TIMER_RUNNING , TIMER_CANCELLED, TIMER_STOPPED, TIMER_TIMEDOUT, TIMER_FAIL, TIMER_WAIT} timer_state_e;
|
||||||
|
|
||||||
// READY_LED, WARN_LED, RUN_LED, FAIL_LED
|
// READY_LED, WARN_LED, RUN_LED, FAIL_LED
|
||||||
const float LEDStates[][3] =
|
const float LEDStates[][3] =
|
||||||
{
|
{
|
||||||
[TIMER_INIT] = {READY_LED_OFF, RUN_LED_OFF, FAIL_LED_OFF},
|
[TIMER_INIT] = {READY_LED_OFF, RUN_LED_OFF, FAIL_LED_OFF},
|
||||||
|
[TIMER_IDLE] = {READY_LED_ON, RUN_LED_OFF, FAIL_LED_OFF},
|
||||||
[TIMER_READY] = {READY_LED_ON, RUN_LED_OFF, FAIL_LED_OFF},
|
[TIMER_READY] = {READY_LED_ON, RUN_LED_OFF, FAIL_LED_OFF},
|
||||||
[TIMER_STARTED] = {READY_LED_ON, RUN_LED_ON, FAIL_LED_OFF},
|
[TIMER_STARTED] = {READY_LED_ON, RUN_LED_ON, FAIL_LED_OFF},
|
||||||
[TIMER_RUNNING] = {READY_LED_OFF, RUN_LED_ON, FAIL_LED_OFF},
|
[TIMER_RUNNING] = {READY_LED_OFF, RUN_LED_ON, FAIL_LED_OFF},
|
||||||
|
@ -87,10 +88,10 @@ const float LEDStates[][3] =
|
||||||
|
|
||||||
//--------------------------------------- function declarations ----------------------------------------------
|
//--------------------------------------- function declarations ----------------------------------------------
|
||||||
void false_start_isr(void);
|
void false_start_isr(void);
|
||||||
void update_screen(timer_state_e timer_state);
|
void update_screen(timer_state_e state);
|
||||||
void set_state_LEDs(timer_state_e state, boolean warn);
|
void set_state_LEDs(timer_state_e state, boolean warn);
|
||||||
void startSequence(void);
|
void startSequence(void);
|
||||||
void update_statemassage(timer_state_e timer_state);
|
void update_statemessage(timer_state_e timer_state);
|
||||||
void failSequence(void);
|
void failSequence(void);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -176,8 +176,11 @@ void loop(void) {
|
||||||
|
|
||||||
// set state to new_state
|
// set state to new_state
|
||||||
if(timer_state != timer_new_state){
|
if(timer_state != timer_new_state){
|
||||||
update_statemassage(timer_new_state);
|
update_statemessage(timer_new_state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
update_screen(timer_new_state);
|
||||||
|
|
||||||
timer_state = timer_new_state;
|
timer_state = timer_new_state;
|
||||||
|
|
||||||
// set LEDs
|
// set LEDs
|
||||||
|
@ -185,15 +188,13 @@ void loop(void) {
|
||||||
|
|
||||||
switch(timer_state){
|
switch(timer_state){
|
||||||
case TIMER_INIT:
|
case TIMER_INIT:
|
||||||
update_screen(timer_state);
|
|
||||||
// check if we are ready ...
|
// check if we are ready ...
|
||||||
if(counter_time_offset > REQUIRED_NUMBER_MEANVALS){
|
if(counter_time_offset > REQUIRED_NUMBER_MEANVALS){
|
||||||
// check if offset is OK - if not .. set state back to INIT
|
// check if offset is OK - if not .. set state back to INIT
|
||||||
timer_new_state = TIMER_READY;
|
timer_new_state = TIMER_IDLE;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case TIMER_READY:
|
case TIMER_IDLE:
|
||||||
update_screen(timer_state);
|
|
||||||
warn_during_run = false;
|
warn_during_run = false;
|
||||||
if(counter_time_offset < REQUIRED_NUMBER_MEANVALS){
|
if(counter_time_offset < REQUIRED_NUMBER_MEANVALS){
|
||||||
// check if offset is OK - if not .. set state back to INIT
|
// check if offset is OK - if not .. set state back to INIT
|
||||||
|
@ -203,28 +204,34 @@ void loop(void) {
|
||||||
// check if the FALSESTATE button is pressed - somebody is ready to run ...
|
// check if the FALSESTATE button is pressed - somebody is ready to run ...
|
||||||
if(digitalRead(FAILSTARTBUTTON_IN) == FAILSTARTBUTTON_PRESSED){
|
if(digitalRead(FAILSTARTBUTTON_IN) == FAILSTARTBUTTON_PRESSED){
|
||||||
//wait a few milliseconds to prevent keybouncing - this is a very simplistic method here
|
//wait a few milliseconds to prevent keybouncing - this is a very simplistic method here
|
||||||
delay(300);
|
delay(10);
|
||||||
//read again and check if still active ...
|
//read again and check if still active ...
|
||||||
if(digitalRead(FAILSTARTBUTTON_IN) == FAILSTARTBUTTON_PRESSED){
|
if(digitalRead(FAILSTARTBUTTON_IN) == FAILSTARTBUTTON_PRESSED){
|
||||||
// check if the start button was pressed ... there is at least still someone waiting for the run .
|
timer_new_state = TIMER_READY;
|
||||||
if(digitalRead(STARTBUTTON_IN) == STARTBUTTON_PRESSED){
|
|
||||||
// now enable the interrupt for the FALSESTART button
|
|
||||||
attachInterrupt(digitalPinToInterrupt(FAILSTARTBUTTON_IN), false_start_isr, CHANGE);
|
|
||||||
timer_new_state = TIMER_STARTED;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case TIMER_READY:
|
||||||
|
if(digitalRead(FAILSTARTBUTTON_IN) == FAILSTARTBUTTON_PRESSED){
|
||||||
|
// check if the start button was pressed ... there is at least still someone waiting for the run .
|
||||||
|
if(digitalRead(STARTBUTTON_IN) == STARTBUTTON_PRESSED){
|
||||||
|
// now enable the interrupt for the FALSESTART button
|
||||||
|
attachInterrupt(digitalPinToInterrupt(FAILSTARTBUTTON_IN), false_start_isr, CHANGE);
|
||||||
|
timer_new_state = TIMER_STARTED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
timer_new_state = TIMER_IDLE;
|
||||||
|
}
|
||||||
|
break;
|
||||||
case TIMER_STARTED:
|
case TIMER_STARTED:
|
||||||
update_screen(timer_state);
|
|
||||||
//initialize the start countdown
|
//initialize the start countdown
|
||||||
timer_new_state = TIMER_RUNNING;
|
timer_new_state = TIMER_RUNNING;
|
||||||
startSequence();
|
startSequence();
|
||||||
break;
|
break;
|
||||||
case TIMER_RUNNING:
|
case TIMER_RUNNING:
|
||||||
noTone(PIEZO_PIN);
|
noTone(PIEZO_PIN);
|
||||||
update_screen(timer_state);
|
|
||||||
if(counter_time_offset < REQUIRED_NUMBER_MEANVALS){
|
if(counter_time_offset < REQUIRED_NUMBER_MEANVALS){
|
||||||
// check if offset is still OK - if not .. set state to TIMER_RUNNING
|
// check if offset is still OK - if not .. set state to TIMER_RUNNING
|
||||||
warn_during_run = true;
|
warn_during_run = true;
|
||||||
|
@ -243,7 +250,6 @@ void loop(void) {
|
||||||
//calculate the run_time and switch to WAIT
|
//calculate the run_time and switch to WAIT
|
||||||
run_time = (radio_data.topbuttonpressedtime - running_time_offset) - start_time;
|
run_time = (radio_data.topbuttonpressedtime - running_time_offset) - start_time;
|
||||||
runner_run_time = runner_start_time - run_time;
|
runner_run_time = runner_start_time - run_time;
|
||||||
update_screen(timer_state);
|
|
||||||
timer_new_state = TIMER_WAIT;
|
timer_new_state = TIMER_WAIT;
|
||||||
break;
|
break;
|
||||||
case TIMER_FAIL:
|
case TIMER_FAIL:
|
||||||
|
@ -251,21 +257,18 @@ void loop(void) {
|
||||||
failSequence();
|
failSequence();
|
||||||
run_time = 99999;
|
run_time = 99999;
|
||||||
runner_run_time = runner_start_time - start_time;
|
runner_run_time = runner_start_time - start_time;
|
||||||
update_screen(timer_state);
|
|
||||||
timer_new_state = TIMER_WAIT;
|
timer_new_state = TIMER_WAIT;
|
||||||
break;
|
break;
|
||||||
case TIMER_CANCELLED:
|
case TIMER_CANCELLED:
|
||||||
// what to do in chancel mode ?
|
// what to do in chancel mode ?
|
||||||
run_time = 99999;
|
run_time = 99999;
|
||||||
runner_run_time = runner_start_time - start_time;
|
runner_run_time = runner_start_time - start_time;
|
||||||
update_screen(timer_state);
|
|
||||||
timer_new_state = TIMER_WAIT;
|
timer_new_state = TIMER_WAIT;
|
||||||
break;
|
break;
|
||||||
case TIMER_TIMEDOUT:
|
case TIMER_TIMEDOUT:
|
||||||
// time out
|
// time out
|
||||||
run_time = millis() - start_time;
|
run_time = millis() - start_time;
|
||||||
runner_run_time = runner_start_time - start_time;
|
runner_run_time = runner_start_time - start_time;
|
||||||
update_screen(timer_state);
|
|
||||||
timer_new_state = TIMER_WAIT;
|
timer_new_state = TIMER_WAIT;
|
||||||
break;
|
break;
|
||||||
case TIMER_WAIT:
|
case TIMER_WAIT:
|
||||||
|
@ -273,7 +276,7 @@ void loop(void) {
|
||||||
detachInterrupt(digitalPinToInterrupt(FAILSTARTBUTTON_IN));
|
detachInterrupt(digitalPinToInterrupt(FAILSTARTBUTTON_IN));
|
||||||
// wait until the chancel button was pressed to go ahead
|
// wait until the chancel button was pressed to go ahead
|
||||||
if(digitalRead(CANCELBUTTON_IN) == CANCELBUTTON_PRESSED){
|
if(digitalRead(CANCELBUTTON_IN) == CANCELBUTTON_PRESSED){
|
||||||
timer_new_state = TIMER_READY;
|
timer_new_state = TIMER_IDLE;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -283,11 +286,14 @@ void loop(void) {
|
||||||
|
|
||||||
//####################### HELPER FUNCTIONS ###########################
|
//####################### HELPER FUNCTIONS ###########################
|
||||||
|
|
||||||
void update_statemassage(timer_state_e timer_state){
|
void update_statemessage(timer_state_e timer_state){
|
||||||
switch(timer_state){
|
switch(timer_state){
|
||||||
case TIMER_INIT:
|
case TIMER_INIT:
|
||||||
Serial.println("*** TIMER_INIT ***");
|
Serial.println("*** TIMER_INIT ***");
|
||||||
break;
|
break;
|
||||||
|
case TIMER_IDLE:
|
||||||
|
Serial.println("*** TIMER_IDLE ***");
|
||||||
|
break;
|
||||||
case TIMER_READY:
|
case TIMER_READY:
|
||||||
Serial.println("*** TIMER_READY ***");
|
Serial.println("*** TIMER_READY ***");
|
||||||
break;
|
break;
|
||||||
|
@ -317,7 +323,7 @@ void update_statemassage(timer_state_e timer_state){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void update_screen(timer_state_e timer_state){
|
void update_screen(timer_state_e state){
|
||||||
bool scr_update = true;
|
bool scr_update = true;
|
||||||
int ypos = 64-42/2;
|
int ypos = 64-42/2;
|
||||||
String header = "no state";
|
String header = "no state";
|
||||||
|
@ -330,16 +336,21 @@ void update_screen(timer_state_e timer_state){
|
||||||
|
|
||||||
float curr_time_test = 0.0;
|
float curr_time_test = 0.0;
|
||||||
|
|
||||||
switch(timer_state){
|
switch(state){
|
||||||
case TIMER_INIT:
|
case TIMER_INIT:
|
||||||
header = "Init";
|
header = "Init";
|
||||||
content = "...";
|
content = "...";
|
||||||
footer = "please wait";
|
footer = "please wait";
|
||||||
break;
|
break;
|
||||||
|
case TIMER_IDLE:
|
||||||
|
header = "Idle!";
|
||||||
|
content = "00:00";
|
||||||
|
footer = "Waiting for climber";
|
||||||
|
break;
|
||||||
case TIMER_READY:
|
case TIMER_READY:
|
||||||
header = "Ready!";
|
header = "Ready!";
|
||||||
content = "00:00";
|
content = "00:00";
|
||||||
footer = "Waiting for climber";
|
footer = "Press Startbuton to run ...";
|
||||||
break;
|
break;
|
||||||
case TIMER_STARTED:
|
case TIMER_STARTED:
|
||||||
header = "Starting ...";
|
header = "Starting ...";
|
||||||
|
@ -367,10 +378,11 @@ void update_screen(timer_state_e timer_state){
|
||||||
scr_update = false;
|
scr_update = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if(timer_new_state != timer_state){
|
|
||||||
display.clear();
|
|
||||||
}
|
|
||||||
if(scr_update == true){
|
if(scr_update == true){
|
||||||
|
if(timer_new_state != timer_state){
|
||||||
|
display.clear();
|
||||||
|
}
|
||||||
//snprintf( string_to_char, sizeof(string_to_char),"%s", header);
|
//snprintf( string_to_char, sizeof(string_to_char),"%s", header);
|
||||||
header.toCharArray(header_to_char, sizeof(header_to_char));
|
header.toCharArray(header_to_char, sizeof(header_to_char));
|
||||||
content.toCharArray(content_to_char, sizeof(content_to_char));
|
content.toCharArray(content_to_char, sizeof(content_to_char));
|
||||||
|
@ -464,12 +476,12 @@ void false_start_isr(void)
|
||||||
// this will save the time when the runner is really started
|
// this will save the time when the runner is really started
|
||||||
Serial.println("** Interrupt service routine started: false_start_ISR **");
|
Serial.println("** Interrupt service routine started: false_start_ISR **");
|
||||||
runner_start_time = millis();
|
runner_start_time = millis();
|
||||||
if(timer_state == TIMER_STARTED & timer_new_state == TIMER_STARTED){
|
if((timer_state == TIMER_STARTED) & (timer_new_state == TIMER_STARTED)){
|
||||||
timer_new_state = TIMER_FAIL;
|
timer_new_state = TIMER_FAIL;
|
||||||
detachInterrupt(digitalPinToInterrupt(FAILSTARTBUTTON_IN));
|
detachInterrupt(digitalPinToInterrupt(FAILSTARTBUTTON_IN));
|
||||||
noTone(PIEZO_PIN);
|
noTone(PIEZO_PIN);
|
||||||
} else {
|
} else {
|
||||||
if(timer_state == TIMER_RUNNING | timer_new_state == TIMER_RUNNING ){
|
if((timer_state == TIMER_RUNNING) | (timer_new_state == TIMER_RUNNING) ){
|
||||||
// disable this interrupt;
|
// disable this interrupt;
|
||||||
detachInterrupt(digitalPinToInterrupt(FAILSTARTBUTTON_IN));
|
detachInterrupt(digitalPinToInterrupt(FAILSTARTBUTTON_IN));
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue