init issue fixed

This commit is contained in:
2021-10-23 12:14:26 +02:00
parent 2e5caea417
commit e7f436118f
4 changed files with 67 additions and 43 deletions

View File

@@ -4,26 +4,37 @@
#include "simpleled.h" #include "simpleled.h"
#include "magicSwitchBoard.h" #include "magicSwitchBoard.h"
#include "chainGame.h" #include "chainGame.h"
#include "detectled.h" //#include "detectled.h"
std::vector<c_game *> gameslist; std::vector<c_game *> gameslist;
uint64_t GameSelectTimer = 0; uint64_t GameSelectTimer = 0;
e_state currentState = state_init; e_state currentState = state_init;
bool gamesConstructed = false;
c_game *game_simpleled = new c_simpleLed(YELLOW); c_game *game_simpleled = new c_simpleLed(YELLOW);
c_game *game_magicswitchboard = new c_magicSwitchBoard(RED); c_game *game_magicswitchboard = new c_magicSwitchBoard(RED);
c_game *game_chaingame = new c_chaingame(GREEN); c_game *game_chaingame = new c_chaingame(GREEN);
c_game *game_detectled = new c_detectled(NONE); // c_game *game_detectled = new c_detectled(NONE);
void setGameState(e_state newstate);
void constructGames(void);
void initGames(void) void initGames(void)
{
constructGames();
setGameState(state_init);
}
void constructGames(void)
{
if (!gamesConstructed)
{ {
gameslist.clear(); gameslist.clear();
gameslist.push_back(game_simpleled); gameslist.push_back(game_simpleled);
gameslist.push_back(game_magicswitchboard); gameslist.push_back(game_magicswitchboard);
gameslist.push_back(game_chaingame); gameslist.push_back(game_chaingame);
gameslist.push_back(game_detectled); gamesConstructed = true;
}
activateGame(simpleled);
} }
c_game *getGame(e_game game) c_game *getGame(e_game game)
@@ -160,6 +171,10 @@ void HandleGameSelectTimeout(void)
void HandleGameIdle(void) void HandleGameIdle(void)
{ {
e_game nextGame = none; e_game nextGame = none;
if(!gamesConstructed)
{
initGames();
}
for (auto &&thisgame : gameslist) for (auto &&thisgame : gameslist)
{ {
@@ -167,22 +182,25 @@ void HandleGameIdle(void)
{ {
// prepare for next game // prepare for next game
nextGame = thisgame->getIndex(); nextGame = thisgame->getIndex();
turnOffLed(thisgame->getGameColor()); turnOnLed(thisgame->getGameColor());
break; break;
} }
} }
// wait for all buttons to be switched off // wait for all buttons to be switched off
if (!anybutton()) if (!anybutton())
{
if (nextGame != none)
{ {
activateGame(nextGame); activateGame(nextGame);
currentState = state_play; setGameState(state_play);
}
} }
} }
void setGameState(e_state newstate) void setGameState(e_state newstate)
{ {
currentState = state_play; currentState = newstate;
} }
e_state getCurrentState(void) e_state getCurrentState(void)
@@ -196,6 +214,24 @@ void handleGames(void)
switch (getCurrentState()) switch (getCurrentState())
{ {
case state_init:
{
disableAllGames();
setGameState(state_init);
GameSelectTimer = 0;
if (anybutton())
{
setGameState(state_idle);
}
else
{
activateGame(simpleled);
setGameState(state_play);
}
}
break;
case state_idle: case state_idle:
{ {
HandleGameIdle(); HandleGameIdle();
@@ -207,32 +243,9 @@ void handleGames(void)
runGames(); runGames();
} }
break; break;
default:
case state_init:
{ {
//delay(1000); currentState = state_init;
turnOffAllLed();
activateGame(simpleled);
for (auto &&thisgame : gameslist)
{
if (onlyButton(thisgame->getGameColor()))
{
//prepare for next game
activateGame(thisgame->getIndex());
turnOnLed(thisgame->getGameColor());
break;
} }
} }
//wait for all buttons idle
while (anybutton())
{
}
turnOffAllLed();
setGameState(state_play);
}
break;
}
} }

View File

@@ -48,12 +48,15 @@ public:
e_ledcolor getGameColor(void) { return _gamecolor; } e_ledcolor getGameColor(void) { return _gamecolor; }
}; };
e_game getActiveGame(void);
c_game *getGame(e_game game, e_ledcolor gamecolor); c_game *getGame(e_game game, e_ledcolor gamecolor);
void runGame(e_game game); void runGame(e_game game);
void runGames(void); void runGames(void);
void activateGame(e_game nextgame); void activateGame(e_game nextgame);
void disableAllGames(void);
void initGames(void); void initGames(void);
void handleGames(); void handleGames();
#endif //GAMEH #endif //GAMEH

View File

@@ -150,7 +150,7 @@ void handlePowerState(void)
{ {
if (!buttonread) if (!buttonread)
{ {
powerstate = on; powerstate = powerinit;
powerOn(); powerOn();
turnOffAllLed(); turnOffAllLed();
delay(200); delay(200);
@@ -164,6 +164,12 @@ void handlePowerState(void)
} }
} }
break; break;
case powerinit:
{
initGames();
powerstate = on;
}
break;
case on: case on:
{ {
if (buttonPower.pressedFor(100)) if (buttonPower.pressedFor(100))

View File

@@ -3,6 +3,7 @@
#include "led.h" #include "led.h"
#include "buttons.h" #include "buttons.h"
#include "board.h" #include "board.h"
#include "game.h"
#ifdef BTN_PWR #ifdef BTN_PWR
#include "JC_Button.h" #include "JC_Button.h"
@@ -21,6 +22,7 @@ typedef enum
off, off,
poweringOn, poweringOn,
poweringOn2, poweringOn2,
powerinit,
on, on,
poweringOff, poweringOff,
poweringOff2, poweringOff2,