From 4c9ee885081bd0148ee7a2848afc1ae698b89839 Mon Sep 17 00:00:00 2001 From: Willem Oldemans Date: Wed, 16 Dec 2020 19:38:11 +0100 Subject: [PATCH] fixes to chaingame --- src/chainGame.cpp | 102 +++++++++++++++------------------------------- src/chainGame.h | 1 + src/detectled.cpp | 8 ---- src/main.cpp | 10 +++-- 4 files changed, 41 insertions(+), 80 deletions(-) diff --git a/src/chainGame.cpp b/src/chainGame.cpp index 6daafe3..50e4739 100644 --- a/src/chainGame.cpp +++ b/src/chainGame.cpp @@ -7,7 +7,7 @@ // extern c_button button3; uint8_t patternIndex = 0; -uint8_t nextPatternIndex = 1; +//uint8_t nextPatternIndex = 1; bool patternFlag = false; bool firstpattern = false; e_ledcolor cheatbutton = NONE; @@ -41,20 +41,41 @@ void nextPattern(void) } } +void ResetChainGame(void) +{ + patternIndex = 0; + patternFlag = false; + firstpattern = false; + cheatbutton = NONE; + cheatButtonFlag = false; +} + void HandleChainGame(void) { - if (buttonIsPressed(ledpattern[patternIndex])) + if (buttonIsPressed(ledpattern[patternIndex]) && !cheatButtonFlag) { turnOnLed(ledpattern[patternIndex]); patternFlag = true; - cheatbutton = NONE; } - else if (buttonIsPressed(cheatbutton)) + else if (buttonIsPressed(cheatbutton) && !patternFlag && !cheatButtonFlag) { turnOnLed(cheatbutton); cheatButtonFlag = true; } - else if (!anybutton()) + + if (!buttonIsPressed(ledpattern[patternIndex])) + { + turnOffLed(ledpattern[patternIndex]); + } + + if (!buttonIsPressed(cheatbutton) && cheatButtonFlag) + { + turnOffLed(cheatbutton); + cheatButtonFlag = false; + cheatbutton = NONE; + } + + if (!anybutton()) { turnOffAllLed(); if (patternFlag) @@ -62,76 +83,19 @@ void HandleChainGame(void) patternFlag = false; nextPattern(); } - if(cheatButtonFlag) - { - cheatButtonFlag = false; - cheatbutton = NONE; - } } - - if(buttonIsPressed(YELLOW2)) + + //check cheatbuttons + if (buttonIsPressed(YELLOW2) && (cheatbutton == NONE)) { cheatbutton = YELLOW; - } - else if(buttonIsPressed(RED2)) + } + else if (buttonIsPressed(RED2) && (cheatbutton == NONE)) { cheatbutton = RED; - } - else if(buttonIsPressed(GREEN2)) + } + else if (buttonIsPressed(GREEN2) && (cheatbutton == NONE)) { cheatbutton = GREEN; } - - // if (buttonIsPressed(YELLOW) | buttonIsPressed(RED) | buttonIsPressed(GREEN)) - // { - // if (!patternFlag) - // { - // //button detected, increase pattern - // if (!firstpattern) - // { - // firstpattern = true; - // } - // else - // { - // nextPattern(); - // } - - // patternFlag = true; - // } - // } - - // if (buttonIsPressed(RED)) - // { - // if (!patternFlag) - // { - // if (!firstpattern) - // { - // firstpattern = true; - // nextPattern(); - // } - // else - // { - // //second input, skip a pattern - // nextPattern(); - // nextPattern(); - // } - // patternFlag = true; - // } - // } - - // if (anybutton() && patternFlag) - // { - // //write pattern to the LEDs - // digitalWrite(LED1, ledpattern[patternIndex][0]); - // digitalWrite(LED2, ledpattern[patternIndex][1]); - // digitalWrite(LED3, ledpattern[patternIndex][2]); - // } - // else - // { - // //leds off - // digitalWrite(LED1, 0); - // digitalWrite(LED2, 0); - // digitalWrite(LED3, 0); - // patternFlag = false; - // } } \ No newline at end of file diff --git a/src/chainGame.h b/src/chainGame.h index 988abb8..c2c1ed8 100644 --- a/src/chainGame.h +++ b/src/chainGame.h @@ -3,6 +3,7 @@ void HandleChainGame( void ); +void ResetChainGame(void); #endif //CHAINGAMEH \ No newline at end of file diff --git a/src/detectled.cpp b/src/detectled.cpp index 5d51436..b5110c5 100644 --- a/src/detectled.cpp +++ b/src/detectled.cpp @@ -8,17 +8,9 @@ #define CHANNELS 3 #define SAMPLES 20 -extern ToggleButton button1; -extern ToggleButton button2; -extern ToggleButton button3; extern std::vector buttonlist; -unsigned int detectled[CHANNELS] = {0, 0, 0}; -uint32_t detectledRaw[CHANNELS][SAMPLES]; -const uint32_t inputs[CHANNELS] = {DETECT1, DETECT2, DETECT3}; -const uint32_t leds[CHANNELS] = {LED1, LED2, LED3}; -uint32_t sampleIndex = 0; c_leds *ledlist_ptr; diff --git a/src/main.cpp b/src/main.cpp index 8fb060c..3df5bf9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,8 +7,8 @@ #include "buttons.h" #include "led.h" -#define TIMEOUT 15000 // 15sec * 1000ms -#define GAMESELECTTIMEOUT 7000 // 7sec * 1000ms +#define TIMEOUT 15000 // 15sec * 1000ms +#define GAMESELECTTIMEOUT 7000 // 7sec * 1000ms typedef enum { @@ -35,6 +35,8 @@ void HandleIdle(void) { //prepare for next game nextGame = ChainGame; + ResetChainGame(); + turnOffLed(YELLOW); } //red button first released @@ -42,6 +44,7 @@ void HandleIdle(void) { //prepare for next game nextGame = magicSwitchBoard; + turnOffLed(RED); } //green button first released @@ -49,6 +52,7 @@ void HandleIdle(void) { //prepare for next game nextGame = detectLED; + turnOffLed(GREEN); } //wait for all buttons to be switched off @@ -62,7 +66,7 @@ void HandleIdle(void) void HandleGameSelectTimeout(void) { uint64_t currentmillis = millis(); - if(buttonIsPressed(YELLOW) && buttonIsPressed(RED) && buttonIsPressed(GREEN)) + if (buttonIsPressed(YELLOW) && buttonIsPressed(RED) && buttonIsPressed(GREEN)) //if (yellow && red && green) { //all buttons pressed, wait for next game