From 48a1126a1cc2afb0297980669c4c8a7a64b10865 Mon Sep 17 00:00:00 2001 From: willem Date: Fri, 22 Oct 2021 16:31:26 +0200 Subject: [PATCH] rewrite chaingame --- src/chainGame.cpp | 133 ++++++++++++++++++++++++++++++++++------------ src/chainGame.h | 20 +++++-- 2 files changed, 115 insertions(+), 38 deletions(-) diff --git a/src/chainGame.cpp b/src/chainGame.cpp index 844aa74..298bdfb 100644 --- a/src/chainGame.cpp +++ b/src/chainGame.cpp @@ -16,55 +16,117 @@ void c_chaingame::nextPattern(void) void c_chaingame::runGame(void) { - if (!patternFlag && !cheatButtonFlag) + switch (state) { + case cg_reset: + { + // clear all vars + turnOffAllLed(); + state = cg_idle; + } + break; + case cg_idle: + { + // wait for button + updateCheatButton(); + turnOffAllLed(); + // -> cg_play + if (buttonIsPressed(ledpattern[patternIndex])) { - //pattern button pressed, turn on LED, set flag - turnOnLed(ledpattern[patternIndex]); - patternFlag = true; - cheatbutton = 0; + state = cg_play; } + // -> cg_cheat else if (buttonIsPressed(cheatbutton)) { - // cheatbutton pressed, turn on cheat led, set flag - turnOnLed(cheatbutton); - cheatButtonFlag = true; - } - else if (anybutton()) - { - // if any other button is pressed, clear cheat button - //cheatbutton = 0; + state = cg_cheat; } } - - if (!buttonIsPressed(ledpattern[patternIndex])) + break; + case cg_play: { - // pattern switch is open, turn off pattern LED - turnOffLed(ledpattern[patternIndex]); - } - - if (!buttonIsPressed(cheatbutton) && cheatButtonFlag) - { - // cheat switch is open, turn of cheat LED - turnOffLed(cheatbutton); - cheatButtonFlag = false; - cheatbutton = 0; - } - - if (!anybutton()) - { - //all switches are open, turn off all LEDs - turnOffAllLed(); - if (patternFlag) + // enable pattern LED and wait for all switches OFF + if (buttonIsPressed(ledpattern[patternIndex])) { - // pattern LED was triggerd, reset flag, move to next pattern - patternFlag = false; + turnOnLed(ledpattern[patternIndex]); + } + else + { + turnOffLed(ledpattern[patternIndex]); + } + if (!anybutton()) + { + // -> cg_play_next + turnOffAllLed(); + playNextTimer = millis(); + state = cg_play_next; + } + } + break; + case cg_play_next: + { + uint32_t timeNow = millis(); + if (anybutton()) + { + // switched on again, return to cg_play state + state = cg_play; + } + if (timeNow - playNextTimer > PLAYNEXTTIMEOUT) + { + // increase patterm counter after 3sec timeout -> cg_idle nextPattern(); + state = cg_idle; } } + break; + case cg_cheat: + { + // enable CHEAT LED on + if (buttonIsPressed(cheatbutton)) + { + turnOnLed(cheatbutton); + } + else + { + turnOffLed(cheatbutton); + } + if (!anybutton()) + { + turnOffAllLed(); + playNextTimer = millis(); + state = cg_cheat_next; + } + // wait for all switches off + // -> cg_idle + } + break; + case cg_cheat_next: + { + uint32_t timeNow = millis(); + if (anybutton()) + { + // switched on again, return to cg_play state + state = cg_cheat; + } + if (timeNow - playNextTimer > PLAYNEXTTIMEOUT) + { + // increase patterm counter after 3sec timeout -> cg_idle + cheatbutton = 0; + state = cg_idle; + } + } + break; + default: + { + state = cg_reset; + } + } - //check cheatbuttons +} + +void c_chaingame::updateCheatButton(void) +{ + // check cheatbuttons if (buttonIsPressed(4) && (cheatbutton == 0)) { // cheatbutton 4 (momentary 1) was closed, set cheatbutton to 1 @@ -81,6 +143,7 @@ void c_chaingame::runGame(void) cheatbutton = 3; } } + bool c_chaingame::initGame(void) { patternIndex = 0; diff --git a/src/chainGame.h b/src/chainGame.h index dd91eab..444957a 100644 --- a/src/chainGame.h +++ b/src/chainGame.h @@ -5,6 +5,18 @@ #include "buttons.h" #include "game.h" +#define PLAYNEXTTIMEOUT 3000 // 3sec * 1000ms + +typedef enum +{ + cg_idle, + cg_play, + cg_play_next, + cg_cheat, + cg_cheat_next, + cg_reset +} cg_states; + class c_chaingame : public c_game { private: @@ -14,11 +26,14 @@ private: bool cheatButtonFlag = false; uint16_t ledpattern[4] = {1, 3, 1, 2}; int patternlength = sizeof(ledpattern) / sizeof(ledpattern[0]); + cg_states state = cg_reset; + uint32_t playNextTimer=0; void nextPattern(void); + void updateCheatButton(void); public: - c_chaingame(e_ledcolor gamecolor): c_game{chaingame, gamecolor} {} + c_chaingame(e_ledcolor gamecolor) : c_game{chaingame, gamecolor} {} void runGame(void); bool initGame(void); void resetGame(void); @@ -27,5 +42,4 @@ public: // void HandleChainGame( bool newstate ); // void ResetChainGame(void); - -#endif //CHAINGAMEH \ No newline at end of file +#endif // CHAINGAMEH \ No newline at end of file