diff --git a/main.cpp b/main.cpp index 4f1f318..fda6587 100644 --- a/main.cpp +++ b/main.cpp @@ -52,12 +52,17 @@ void LED_control(bool d1, bool d2, bool d3); // globalni promenna pro stav tlacitka SW1 bool SW1_pressed; +/* we'll do blinking, but not by default, values are -1/1 */ +int blinkingpls = -1; // Promenna state je nove lokalni uvnitr tasku (funkce) pro blikani // Prototypy funkci void TaskSwitches(void); void TaskEffect(void); void TaskGreenLed(void); +void TaskL1(void); +void TaskL2(void); +void TaskL3(void); int main(void) { // inicializace ovladace pinu a delay @@ -68,6 +73,9 @@ int main(void) { TaskSwitches(); TaskEffect(); TaskGreenLed(); + TaskL1(); + TaskL2(); + TaskL3(); } // while @@ -78,10 +86,17 @@ int main(void) { // Uloha, ktera se stara o obsluhu tlacitek void TaskSwitches(void) { - if (switch1_read() == SWITCH_PRESSED) + if (switch1_read() == SWITCH_PRESSED) { SW1_pressed = true; - else + /* ignoring the legacy option above, when pressing the button the value of either + * 1 or -1 is multiplied by -1 resulting in the value assigned back to the variable + * being - again - either 1 or -1 (meaning the opposite of the one before) creating + * an easily switchable thingy + */ + blinkingpls *= -1; + } else { SW1_pressed = false; + } } // Uloha, ktera se stara o blikani LED @@ -152,6 +167,143 @@ void TaskEffect(void) { // Doba svitu/zhasnuti zelene LED #define GREEN_ON_DELAY 200 #define GREEN_OFF_DELAY 700 +/* as per the requirements */ +#define L1_ON_DELAY 500 +#define L1_OFF_DELAY 500 +#define L2_ON_DELAY 200 +#define L2_OFF_DELAY 650 +#define L3_ON_DELAY 800 +#define L3_OFF_DELAY 300 + +void TaskL1() { + static enum { + ST_LED_ON, + ST_ON_WAIT, + ST_LED_OFF, + ST_OFF_WAIT + + } state = ST_LED_ON; + + static uint32_t start_time; + if (blinkingpls > 0) { + + switch (state) { + + case ST_LED_ON: + pinWrite(LED1, LOW); + start_time = SYSTICK_millis(); + state = ST_ON_WAIT; + break; + + case ST_ON_WAIT: + if (SYSTICK_millis() - start_time >= L1_ON_DELAY) + state = ST_LED_OFF; + break; + + case ST_LED_OFF: + pinWrite(LED1, HIGH); + start_time = SYSTICK_millis(); + state = ST_OFF_WAIT; + break; + + case ST_OFF_WAIT: + if (SYSTICK_millis() - start_time >= L1_OFF_DELAY) + state = ST_LED_ON; + break; + } // switch + } else { + /* turn the LED off and reset its state */ + pinWrite(LED1, HIGH); + state = ST_LED_ON; + } +} + +void TaskL2() { + static enum { + ST_LED_ON, + ST_ON_WAIT, + ST_LED_OFF, + ST_OFF_WAIT + + } state = ST_LED_ON; + + static uint32_t start_time; + if (blinkingpls > 0) { + + switch (state) { + + case ST_LED_ON: + pinWrite(LED2, LOW); + start_time = SYSTICK_millis(); + state = ST_ON_WAIT; + break; + + case ST_ON_WAIT: + if (SYSTICK_millis() - start_time >= L2_ON_DELAY) + state = ST_LED_OFF; + break; + + case ST_LED_OFF: + pinWrite(LED1, HIGH); + start_time = SYSTICK_millis(); + state = ST_OFF_WAIT; + break; + + case ST_OFF_WAIT: + if (SYSTICK_millis() - start_time >= L2_OFF_DELAY) + state = ST_LED_ON; + break; + } // switch + } else { + /* turn the LED off and reset its state */ + pinWrite(LED2, HIGH); + state = ST_LED_ON; + } +} + +void TaskL3() { + static enum { + ST_LED_ON, + ST_ON_WAIT, + ST_LED_OFF, + ST_OFF_WAIT + + } state = ST_LED_ON; + + static uint32_t start_time; + if (blinkingpls > 0) { + + switch (state) { + + case ST_LED_ON: + pinWrite(LED3, LOW); + start_time = SYSTICK_millis(); + state = ST_ON_WAIT; + break; + + case ST_ON_WAIT: + if (SYSTICK_millis() - start_time >= L3_ON_DELAY) + state = ST_LED_OFF; + break; + + case ST_LED_OFF: + pinWrite(LED3, HIGH); + start_time = SYSTICK_millis(); + state = ST_OFF_WAIT; + break; + + case ST_OFF_WAIT: + if (SYSTICK_millis() - start_time >= L3_OFF_DELAY) + state = ST_LED_ON; + break; + } // switch + } else { + /* turn the LED off and reset its state */ + pinWrite(LED3, HIGH); + state = ST_LED_ON; + } +} + // Ukazka dalsiho tasku - blika pri stisknutem tlacitku RGB LED void TaskGreenLed() {