From f6132f1c85034080277a5616b29ec2f8365c512d Mon Sep 17 00:00:00 2001 From: Fenoglio Date: Sat, 7 Jul 2018 22:50:05 +0200 Subject: [PATCH] Added new state IDLE --- speedclock.h | 7 +++--- speedclock.ino | 68 +++++++++++++++++++++++++++++--------------------- 2 files changed, 44 insertions(+), 31 deletions(-) diff --git a/speedclock.h b/speedclock.h index e44d8a9..6dc4539 100644 --- a/speedclock.h +++ b/speedclock.h @@ -52,12 +52,13 @@ typedef struct transcv_struct{ #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 const float LEDStates[][3] = { [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_STARTED] = {READY_LED_ON, 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 ---------------------------------------------- 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 startSequence(void); -void update_statemassage(timer_state_e timer_state); +void update_statemessage(timer_state_e timer_state); void failSequence(void); #endif diff --git a/speedclock.ino b/speedclock.ino index 12aa72d..2345aa5 100644 --- a/speedclock.ino +++ b/speedclock.ino @@ -176,8 +176,11 @@ void loop(void) { // set state to 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; // set LEDs @@ -185,15 +188,13 @@ void loop(void) { switch(timer_state){ case TIMER_INIT: - update_screen(timer_state); // check if we are ready ... if(counter_time_offset > REQUIRED_NUMBER_MEANVALS){ // check if offset is OK - if not .. set state back to INIT - timer_new_state = TIMER_READY; + timer_new_state = TIMER_IDLE; } break; - case TIMER_READY: - update_screen(timer_state); + case TIMER_IDLE: warn_during_run = false; if(counter_time_offset < REQUIRED_NUMBER_MEANVALS){ // 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 ... if(digitalRead(FAILSTARTBUTTON_IN) == FAILSTARTBUTTON_PRESSED){ //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 ... 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; - } + timer_new_state = TIMER_READY; } } } 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: - update_screen(timer_state); //initialize the start countdown timer_new_state = TIMER_RUNNING; startSequence(); break; case TIMER_RUNNING: noTone(PIEZO_PIN); - update_screen(timer_state); if(counter_time_offset < REQUIRED_NUMBER_MEANVALS){ // check if offset is still OK - if not .. set state to TIMER_RUNNING warn_during_run = true; @@ -243,7 +250,6 @@ void loop(void) { //calculate the run_time and switch to WAIT run_time = (radio_data.topbuttonpressedtime - running_time_offset) - start_time; runner_run_time = runner_start_time - run_time; - update_screen(timer_state); timer_new_state = TIMER_WAIT; break; case TIMER_FAIL: @@ -251,21 +257,18 @@ void loop(void) { failSequence(); run_time = 99999; runner_run_time = runner_start_time - start_time; - update_screen(timer_state); timer_new_state = TIMER_WAIT; break; case TIMER_CANCELLED: // what to do in chancel mode ? run_time = 99999; runner_run_time = runner_start_time - start_time; - update_screen(timer_state); timer_new_state = TIMER_WAIT; break; case TIMER_TIMEDOUT: // time out run_time = millis() - start_time; runner_run_time = runner_start_time - start_time; - update_screen(timer_state); timer_new_state = TIMER_WAIT; break; case TIMER_WAIT: @@ -273,7 +276,7 @@ void loop(void) { detachInterrupt(digitalPinToInterrupt(FAILSTARTBUTTON_IN)); // wait until the chancel button was pressed to go ahead if(digitalRead(CANCELBUTTON_IN) == CANCELBUTTON_PRESSED){ - timer_new_state = TIMER_READY; + timer_new_state = TIMER_IDLE; } break; } @@ -283,11 +286,14 @@ void loop(void) { //####################### HELPER FUNCTIONS ########################### -void update_statemassage(timer_state_e timer_state){ +void update_statemessage(timer_state_e timer_state){ switch(timer_state){ case TIMER_INIT: Serial.println("*** TIMER_INIT ***"); break; + case TIMER_IDLE: + Serial.println("*** TIMER_IDLE ***"); + break; case TIMER_READY: Serial.println("*** TIMER_READY ***"); 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; int ypos = 64-42/2; String header = "no state"; @@ -330,16 +336,21 @@ void update_screen(timer_state_e timer_state){ float curr_time_test = 0.0; - switch(timer_state){ + switch(state){ case TIMER_INIT: header = "Init"; content = "..."; footer = "please wait"; break; + case TIMER_IDLE: + header = "Idle!"; + content = "00:00"; + footer = "Waiting for climber"; + break; case TIMER_READY: header = "Ready!"; content = "00:00"; - footer = "Waiting for climber"; + footer = "Press Startbuton to run ..."; break; case TIMER_STARTED: header = "Starting ..."; @@ -367,10 +378,11 @@ void update_screen(timer_state_e timer_state){ scr_update = false; break; } - if(timer_new_state != timer_state){ - display.clear(); - } + if(scr_update == true){ + if(timer_new_state != timer_state){ + display.clear(); + } //snprintf( string_to_char, sizeof(string_to_char),"%s", header); header.toCharArray(header_to_char, sizeof(header_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 Serial.println("** Interrupt service routine started: false_start_ISR **"); 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; detachInterrupt(digitalPinToInterrupt(FAILSTARTBUTTON_IN)); noTone(PIEZO_PIN); } else { - if(timer_state == TIMER_RUNNING | timer_new_state == TIMER_RUNNING ){ + if((timer_state == TIMER_RUNNING) | (timer_new_state == TIMER_RUNNING) ){ // disable this interrupt; detachInterrupt(digitalPinToInterrupt(FAILSTARTBUTTON_IN)); }