rewrite chaingame
This commit is contained in:
@@ -16,55 +16,117 @@ void c_chaingame::nextPattern(void)
|
|||||||
|
|
||||||
void c_chaingame::runGame(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]))
|
if (buttonIsPressed(ledpattern[patternIndex]))
|
||||||
{
|
{
|
||||||
//pattern button pressed, turn on LED, set flag
|
state = cg_play;
|
||||||
turnOnLed(ledpattern[patternIndex]);
|
|
||||||
patternFlag = true;
|
|
||||||
cheatbutton = 0;
|
|
||||||
}
|
}
|
||||||
|
// -> cg_cheat
|
||||||
else if (buttonIsPressed(cheatbutton))
|
else if (buttonIsPressed(cheatbutton))
|
||||||
{
|
{
|
||||||
// cheatbutton pressed, turn on cheat led, set flag
|
state = cg_cheat;
|
||||||
turnOnLed(cheatbutton);
|
|
||||||
cheatButtonFlag = true;
|
|
||||||
}
|
|
||||||
else if (anybutton())
|
|
||||||
{
|
|
||||||
// if any other button is pressed, clear cheat button
|
|
||||||
//cheatbutton = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
if (!buttonIsPressed(ledpattern[patternIndex]))
|
case cg_play:
|
||||||
{
|
{
|
||||||
// pattern switch is open, turn off pattern LED
|
// enable pattern LED and wait for all switches OFF
|
||||||
turnOffLed(ledpattern[patternIndex]);
|
if (buttonIsPressed(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)
|
|
||||||
{
|
{
|
||||||
// pattern LED was triggerd, reset flag, move to next pattern
|
turnOnLed(ledpattern[patternIndex]);
|
||||||
patternFlag = false;
|
}
|
||||||
|
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();
|
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))
|
if (buttonIsPressed(4) && (cheatbutton == 0))
|
||||||
{
|
{
|
||||||
// cheatbutton 4 (momentary 1) was closed, set cheatbutton to 1
|
// cheatbutton 4 (momentary 1) was closed, set cheatbutton to 1
|
||||||
@@ -81,6 +143,7 @@ void c_chaingame::runGame(void)
|
|||||||
cheatbutton = 3;
|
cheatbutton = 3;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool c_chaingame::initGame(void)
|
bool c_chaingame::initGame(void)
|
||||||
{
|
{
|
||||||
patternIndex = 0;
|
patternIndex = 0;
|
||||||
|
|||||||
@@ -5,6 +5,18 @@
|
|||||||
#include "buttons.h"
|
#include "buttons.h"
|
||||||
#include "game.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
|
class c_chaingame : public c_game
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
@@ -14,11 +26,14 @@ private:
|
|||||||
bool cheatButtonFlag = false;
|
bool cheatButtonFlag = false;
|
||||||
uint16_t ledpattern[4] = {1, 3, 1, 2};
|
uint16_t ledpattern[4] = {1, 3, 1, 2};
|
||||||
int patternlength = sizeof(ledpattern) / sizeof(ledpattern[0]);
|
int patternlength = sizeof(ledpattern) / sizeof(ledpattern[0]);
|
||||||
|
cg_states state = cg_reset;
|
||||||
|
uint32_t playNextTimer=0;
|
||||||
|
|
||||||
void nextPattern(void);
|
void nextPattern(void);
|
||||||
|
void updateCheatButton(void);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
c_chaingame(e_ledcolor gamecolor): c_game{chaingame, gamecolor} {}
|
c_chaingame(e_ledcolor gamecolor) : c_game{chaingame, gamecolor} {}
|
||||||
void runGame(void);
|
void runGame(void);
|
||||||
bool initGame(void);
|
bool initGame(void);
|
||||||
void resetGame(void);
|
void resetGame(void);
|
||||||
@@ -27,5 +42,4 @@ public:
|
|||||||
// void HandleChainGame( bool newstate );
|
// void HandleChainGame( bool newstate );
|
||||||
// void ResetChainGame(void);
|
// void ResetChainGame(void);
|
||||||
|
|
||||||
|
#endif // CHAINGAMEH
|
||||||
#endif //CHAINGAMEH
|
|
||||||
Reference in New Issue
Block a user