moved all code to gameclass

This commit is contained in:
2021-03-31 20:20:18 +02:00
parent 5acc1b8c3c
commit e345a8a687
15 changed files with 290 additions and 200 deletions

View File

@@ -20,7 +20,7 @@ board = STM32L031K6
build_flags = build_flags =
-DHARDWAREVERSION=10 -DHARDWAREVERSION=10
[env:ledboard_PROTO] ; [env:ledboard_PROTO]
board = nucleo_l031K6 ; board = nucleo_l031K6
build_flags = ; build_flags =
-DHARDWAREVERSION=09 ; -DHARDWAREVERSION=09

View File

@@ -4,12 +4,12 @@
std::vector<c_button *> buttonlist; std::vector<c_button *> buttonlist;
c_button button1(SWITCH1, YELLOW, 1); c_button button1(SWITCH1, YELLOW, 1, type_switch);
c_button button2(SWITCH2, RED, 2); c_button button2(SWITCH2, RED, 2, type_switch);
c_button button3(SWITCH3, GREEN, 3); c_button button3(SWITCH3, GREEN, 3, type_switch);
c_button button4(SWITCH12, YELLOW2, 4); c_button button4(SWITCH12, YELLOW2, 4, type_momentary);
c_button button5(SWITCH22, RED2, 5); c_button button5(SWITCH22, RED2, 5, type_momentary);
c_button button6(SWITCH32, GREEN2, 6); c_button button6(SWITCH32, GREEN2, 6, type_momentary);
void buttonbegin(c_button *thisbutton) void buttonbegin(c_button *thisbutton)
{ {
@@ -61,6 +61,32 @@ bool anyButtonChanged(void)
return false; return false;
} }
bool allButtons(void)
{
for( auto&& thisbutton : buttonlist)
{
if(thisbutton->getType() == type_switch)
{
if(!thisbutton->isPressed())
{
return false;
}
}
}
return true;
}
bool onlyButton(e_ledcolor color)
{
for (auto &&thisbutton : buttonlist)
{
if (thisbutton->isPressed() && thisbutton->getColor() != color)
{
return false;
}
}
return getButton(color)->isPressed();
}
bool buttonIsPressed(e_ledcolor index) bool buttonIsPressed(e_ledcolor index)
{ {

View File

@@ -7,16 +7,24 @@
#include "board.h" #include "board.h"
#include "led.h" #include "led.h"
typedef enum
{
type_switch,
type_momentary
}e_switchtype;
class c_button : public ToggleButton class c_button : public ToggleButton
{ {
const e_ledcolor _color; const e_ledcolor _color;
const uint8_t _index; const uint8_t _index;
const e_switchtype _switchtype;
public: public:
c_button(uint8_t pin, e_ledcolor color, uint8_t index) c_button(uint8_t pin, e_ledcolor color, uint8_t index, e_switchtype switchtype)
: ToggleButton(pin), _color(color), _index(index) {} : ToggleButton(pin), _color(color), _index(index), _switchtype(switchtype) {}
e_ledcolor getColor( void ){return _color;} e_ledcolor getColor( void ){return _color;}
e_switchtype getType(void) { return _switchtype;}
uint8_t getIndex( void ) {return _index;} uint8_t getIndex( void ) {return _index;}
bool isChanged( void ) {return changed();} bool isChanged( void ) {return changed();}
@@ -26,6 +34,8 @@ bool anybutton(void);
bool anyButtonChanged(void); bool anyButtonChanged(void);
void initButtons(void); void initButtons(void);
void handleButtons(void); void handleButtons(void);
bool allButtons(void);
bool onlyButton(e_ledcolor color);
c_button *getButton(unsigned int index); c_button *getButton(unsigned int index);
c_button *getButton(e_ledcolor index); c_button *getButton(e_ledcolor index);

View File

@@ -1,25 +1,23 @@
#ifndef UNIT_TEST #ifndef UNIT_TEST
#ifdef ARDUINO
#include "chainGame.h" #include "chainGame.h"
uint8_t patternIndex = 0; //uint8_t patternIndex = 0;
bool patternFlag = false; //bool patternFlag = false;
//bool firstpattern = false; //bool firstpattern = false;
uint16_t cheatbutton = 0; // uint16_t cheatbutton = 0;
bool cheatButtonFlag = false; //bool cheatButtonFlag = false;
// uint16_t ledpattern[4] =
// {
// 1,
// 3,
// 1,
// 2};
uint16_t ledpattern[4] = // int patternlength = sizeof(ledpattern) / sizeof(ledpattern[0]);
{
1,
3,
1,
2};
int patternlength = sizeof(ledpattern) / sizeof(ledpattern[0]); void c_chaingame::nextPattern(void)
void nextPattern(void)
{ {
if (patternIndex < patternlength - 1) if (patternIndex < patternlength - 1)
{ {
@@ -31,16 +29,7 @@ void nextPattern(void)
} }
} }
void ResetChainGame(void) void c_chaingame::runGame(void)
{
patternIndex = 0;
patternFlag = false;
//firstpattern = false;
cheatbutton = 0;
cheatButtonFlag = false;
}
void HandleChainGame(bool newstate)
{ {
if (!patternFlag && !cheatButtonFlag) if (!patternFlag && !cheatButtonFlag)
{ {
@@ -107,6 +96,16 @@ void HandleChainGame(bool newstate)
cheatbutton = 3; cheatbutton = 3;
} }
} }
void c_chaingame::initGame(void)
{
patternIndex = 0;
patternFlag = false;
cheatbutton = 0;
cheatButtonFlag = false;
}
void c_chaingame::resetGame(void)
{
initGame();
}
#endif #endif
#endif

View File

@@ -3,9 +3,29 @@
#include "Arduino.h" #include "Arduino.h"
#include "buttons.h" #include "buttons.h"
#include "game.h"
void HandleChainGame( bool newstate ); class c_chaingame : public c_game
void ResetChainGame(void); {
private:
uint8_t patternIndex;
bool patternFlag = false;
uint16_t cheatbutton = 0;
bool cheatButtonFlag = false;
uint16_t ledpattern[4] = {1, 3, 1, 2};
int patternlength = sizeof(ledpattern) / sizeof(ledpattern[0]);
void nextPattern(void);
public:
c_chaingame(): c_game{chaingame} {}
void runGame(void);
void initGame(void);
void resetGame(void);
};
// void HandleChainGame( bool newstate );
// void ResetChainGame(void);
#endif //CHAINGAMEH #endif //CHAINGAMEH

View File

@@ -2,53 +2,94 @@
#include <vector> #include <vector>
#include "simpleled.h" #include "simpleled.h"
#include "magicSwitchBoard.h"
#include "chainGame.h"
std::vector<c_game *> gameslist; std::vector<c_game *> gameslist;
void initGames(void) void initGames(void)
{ {
gameslist.clear(); gameslist.clear();
gameslist.push_back(new c_simpleLed(simpleled)); gameslist.push_back(new c_simpleLed());
gameslist.push_back(new c_magicSwitchBoard());
gameslist.push_back(new c_chaingame());
activateGame(simpleled);
} }
void runGame(void) c_game *getGame(e_game game)
{ {
for(auto &&game: gameslist) for (auto &&thisgame : gameslist)
{ {
if(game->getStatus() == active) if (thisgame->getIndex() == game)
{
return thisgame;
}
}
return NULL;
}
void runGames(void)
{
for (auto &&game : gameslist)
{
if (game->getStatus() == active)
{ {
game->runGame(); game->runGame();
return; return;
} }
if(game->getStatus() == init) if (game->getStatus() == init)
{ {
game->initGame(); game->initGame();
game->setStatus(active);
return; return;
} }
} }
} }
void runGame(e_game game)
{
for (auto &&thisgame : gameslist)
{
if (thisgame->getIndex() == game)
{
if (thisgame->getStatus() == active)
{
thisgame->runGame();
return;
}
if (thisgame->getStatus() == init)
{
thisgame->initGame();
thisgame->setStatus(active);
return;
}
}
}
}
void activateGame(e_game nextgame) void activateGame(e_game nextgame)
{ {
for(auto &&game: gameslist) for (auto &&thisgame : gameslist)
{ {
if(game->getIndex() == nextgame) if (thisgame->getIndex() == nextgame)
{ {
if(game->getStatus() != active) if (thisgame->getStatus() != active)
{ {
game->setStatus(init); thisgame->setStatus(init);
} }
} }
else else
{ {
game->setStatus(disabled); thisgame->setStatus(disabled);
} }
} }
} }
void disableAllGames(void)
{
for (auto &&thisgame : gameslist)
{
thisgame->setStatus(disabled);
}
}

View File

@@ -3,10 +3,10 @@
typedef enum typedef enum
{ {
none,
simpleled, simpleled,
chaingame, chaingame,
magicswitchboard, magicswitchboard
detectled
} e_game; } e_game;
typedef enum typedef enum
@@ -14,22 +14,30 @@ typedef enum
disabled, disabled,
init, init,
active active
} e_state; } e_gamestate;
class c_game class c_game
{ {
private: protected:
const e_game _gameindex; const e_game _gameindex;
e_state _status; e_gamestate _status;
public: public:
c_game(e_game index) : _gameindex(index) {_status = disabled;} c_game(e_game index) : _gameindex(index) { _status = disabled; }
virtual void runGame(void); virtual void runGame(void);
virtual void initGame(void); virtual void initGame(void);
e_state getStatus(void) { return _status; } virtual void resetGame(void);
e_gamestate getStatus(void) { return _status; }
e_game getIndex(void) { return _gameindex; } e_game getIndex(void) { return _gameindex; }
void setStatus(e_state newstate) { _status = newstate;} void setStatus(e_gamestate newstate) { _status = newstate; }
}; };
c_game *getGame(e_game game);
void runGame(e_game game);
void runGames(void);
void activateGame(e_game nextgame);
void initGames(void);
#endif //GAMEH #endif //GAMEH

View File

@@ -17,8 +17,8 @@ void initLeds(void)
{ {
ledlist.init(); ledlist.init();
ledlist.AddLed(LED1, DETECT1, 1, 844, YELLOW, true); ledlist.AddLed(LED1, DETECT1, 1, 844, YELLOW, true);
ledlist.AddLed(LED2, DETECT2, 2, 512, RED, true); ledlist.AddLed(LED2, DETECT2, 2, 512, RED, true);
ledlist.AddLed(LED3, DETECT3, 3, 92, GREEN, true); ledlist.AddLed(LED3, DETECT3, 3, 92, GREEN, true);
ledlist.begin(); ledlist.begin();
} }
@@ -48,6 +48,11 @@ void turnOffAllLed()
ledlist.turnAllOff(); ledlist.turnAllOff();
} }
void turnOnAllLed()
{
ledlist.turnAllOn();
}
//############################################# //#############################################
//# leds functions # //# leds functions #
//############################################# //#############################################
@@ -108,10 +113,27 @@ void c_leds::turnOffLed(uint16_t index)
} }
void c_leds::turnAllOff(void) void c_leds::turnAllOff(void)
{
setAllLeds(false);
}
void c_leds::turnAllOn(void)
{
setAllLeds(true);
}
void c_leds::setAllLeds(bool state)
{ {
for (auto &&port : v_ledports) for (auto &&port : v_ledports)
{ {
port.turnOff(); if (state)
{
port.turnOn();
}
else
{
port.turnOff();
}
} }
} }
@@ -238,4 +260,3 @@ bool c_led::checkIndex(uint16_t index)
} }
return false; return false;
} }

View File

@@ -84,6 +84,8 @@ public:
void turnAllOff(void); void turnAllOff(void);
void turnAllOn(void);
void setAllLeds(bool state);
}; };
c_leds *getledlist(void); c_leds *getledlist(void);
@@ -94,6 +96,8 @@ void turnOffLed(e_ledcolor color);
void turnOffLed(uint16_t index); void turnOffLed(uint16_t index);
void turnOnLed(uint16_t index); void turnOnLed(uint16_t index);
void turnOffAllLed(); void turnOffAllLed();
void turnOnAllLed();
#endif //LEDH #endif //LEDH

View File

@@ -7,13 +7,12 @@
typedef enum typedef enum
{ {
idle, wait,
learn, learn,
active, play
last
} states; } states;
states state = last; states state = wait;
uint8_t sequence[CHANNELS] = {0xFF, 0xFF, 0xFF}; uint8_t sequence[CHANNELS] = {0xFF, 0xFF, 0xFF};
const uint8_t buttonIndex[CHANNELS] = {1, 2, 3}; const uint8_t buttonIndex[CHANNELS] = {1, 2, 3};
const uint16_t leds[CHANNELS] = {1, 2, 3}; const uint16_t leds[CHANNELS] = {1, 2, 3};
@@ -22,6 +21,9 @@ uint64_t lastTime = 0;
uint8_t learnIndex = 0; uint8_t learnIndex = 0;
void showLeds(void) void showLeds(void)
{ {
//loop through the button list //loop through the button list
@@ -55,14 +57,14 @@ void showLeds(void)
void resetMagicSwitchBoard(void) void resetMagicSwitchBoard(void)
{ {
state = idle; // state = idle;
lastTime = 0; // lastTime = 0;
learnIndex = 0; // learnIndex = 0;
for (int i = 0; i < CHANNELS; i++) // for (int i = 0; i < CHANNELS; i++)
{ // {
sequence[i] = 0xff; // sequence[i] = 0xff;
} // }
turnOffAllLed(); // turnOffAllLed();
} }
bool CheckTimeOut(void) bool CheckTimeOut(void)
@@ -121,7 +123,7 @@ void handleLearn(void)
} }
if (learnIndex == CHANNELS) if (learnIndex == CHANNELS)
{ {
state = active; state = play;
} }
} }
@@ -137,11 +139,12 @@ void handleIdle(void)
} }
} }
void handleMagicSwitchBoard(bool newstate)
void c_magicSwitchBoard::runGame(void)
{ {
switch (state) switch (state)
{ {
case idle: case wait:
{ {
handleIdle(); handleIdle();
} }
@@ -153,7 +156,7 @@ void handleMagicSwitchBoard(bool newstate)
} }
break; break;
case active: case play:
{ {
CheckTimeOut(); CheckTimeOut();
} }
@@ -161,16 +164,26 @@ void handleMagicSwitchBoard(bool newstate)
default: default:
{ {
state = idle; state = wait;
} }
break; break;
} }
showLeds(); showLeds();
} }
void initMagicSwitchBoard(void) void c_magicSwitchBoard::initGame(void)
{ {
resetMagicSwitchBoard(); state = wait;
lastTime = 0;
learnIndex = 0;
for (int i = 0; i < CHANNELS; i++)
{
sequence[i] = 0xff;
}
turnOffAllLed();
}
void c_magicSwitchBoard::resetGame(void)
{
initGame();
} }

View File

@@ -2,6 +2,17 @@
#define MAGICSWITCHBOARDH #define MAGICSWITCHBOARDH
#include "buttons.h" #include "buttons.h"
#include "game.h"
class c_magicSwitchBoard : public c_game
{
public:
c_magicSwitchBoard(): c_game{magicswitchboard} {}
void runGame(void);
void initGame(void);
void resetGame(void);
};
void handleMagicSwitchBoard( bool newstate ); void handleMagicSwitchBoard( bool newstate );
void initMagicSwitchBoard(void); void initMagicSwitchBoard(void);

View File

@@ -9,68 +9,53 @@
#include "buttons.h" #include "buttons.h"
#include "led.h" #include "led.h"
#include "power.h" #include "power.h"
#include "game.h"
#define GAMESELECTTIMEOUT 10000 // 10sec * 1000ms #define GAMESELECTTIMEOUT 10000 // 10sec * 1000ms
typedef enum typedef enum
{ {
none, state_init,
sleep, state_idle,
idle, state_play
SimpleLed, }e_state;
ChainGame,
magicSwitchBoard,
detectLED,
last
} game;
game currentGame = none; e_state currentState = state_init;
game nextGame = none;
bool newstate = false;
uint8_t gameState = 0;
uint64_t GameSelectTimer = 0; uint64_t GameSelectTimer = 0;
void setNewState(game nextstate)
{
if (nextstate != currentGame)
{
currentGame = nextstate;
newstate = true;
}
}
void HandleIdle(bool newstate) void HandleIdle()
{ {
//green button first released e_game nextGame = none;
if (!buttonIsPressed(YELLOW) && buttonIsPressed(RED) && buttonIsPressed(GREEN) && (nextGame == none)) //yellow button first released
if (onlyButton(YELLOW) && (nextGame == none))
{ {
//prepare for next game //prepare for next game
nextGame = ChainGame; nextGame = chaingame;
ResetChainGame();
turnOffLed(YELLOW); turnOffLed(YELLOW);
} }
//red button first released //red button first released
if (buttonIsPressed(YELLOW) && !buttonIsPressed(RED) && buttonIsPressed(GREEN) & (nextGame == none)) if (onlyButton(RED) && (nextGame == none))
{ {
//prepare for next game //prepare for next game
nextGame = magicSwitchBoard; nextGame = magicswitchboard;
turnOffLed(RED); turnOffLed(RED);
} }
//green button first released //green button first released
if (buttonIsPressed(YELLOW) && buttonIsPressed(RED) && !buttonIsPressed(GREEN) & (nextGame == none)) if (onlyButton(GREEN) && (nextGame == none))
{ {
//prepare for next game //prepare for next game
nextGame = SimpleLed; nextGame = simpleled;
turnOffLed(GREEN); turnOffLed(GREEN);
} }
//wait for all buttons to be switched off //wait for all buttons to be switched off
if (!anybutton()) if (!anybutton())
{ {
setNewState(nextGame); activateGame(nextGame);
nextGame = none; currentState = state_play;
} }
} }
@@ -78,7 +63,7 @@ void HandleGameSelectTimeout(void)
{ {
uint64_t currentmillis = millis(); uint64_t currentmillis = millis();
// yellow && red && green all on // yellow && red && green all on
if (buttonIsPressed(YELLOW) && buttonIsPressed(RED) && buttonIsPressed(GREEN)) if (allButtons())
{ {
//all buttons pressed, wait for next game //all buttons pressed, wait for next game
if (!GameSelectTimer) if (!GameSelectTimer)
@@ -90,7 +75,7 @@ void HandleGameSelectTimeout(void)
//check timeout //check timeout
if (currentmillis - GameSelectTimer > GAMESELECTTIMEOUT) if (currentmillis - GameSelectTimer > GAMESELECTTIMEOUT)
{ {
setNewState(idle); currentState = state_idle;
GameSelectTimer = 0; GameSelectTimer = 0;
} }
} }
@@ -108,10 +93,7 @@ void setup()
initButtons(); initButtons();
initLowPower(); initLowPower();
initBattery(); initBattery();
initGames();
initDetectLed();
initMagicSwitchBoard();
initSimpleLed();
} }
void loop() void loop()
@@ -120,78 +102,51 @@ void loop()
HandlePower(); HandlePower();
HandleGameSelectTimeout(); HandleGameSelectTimeout();
switch (currentGame) switch (currentState)
{ {
case idle: case state_idle:
{ {
HandleIdle(newstate); HandleIdle();
newstate = false;
} }
break; break;
case SimpleLed: case state_play:
{ {
handleSimpleLed(newstate); runGames();
newstate = false;
} }
break; break;
case magicSwitchBoard: case state_init:
{
handleMagicSwitchBoard(newstate);
newstate = false;
}
break;
case detectLED:
{
handleDetectLed(newstate);
newstate = false;
}
break;
case ChainGame:
default:
{
HandleChainGame(newstate);
newstate = false;
}
break;
case none:
{ {
batteryCheck(); batteryCheck();
batterydisplay(); batterydisplay();
delay(1000); delay(1000);
turnOffAllLed(); turnOffAllLed();
if (buttonIsPressed(GREEN) && currentGame == none) activateGame(simpleled);
if (buttonIsPressed(GREEN))
{ {
setNewState(ChainGame); activateGame(chaingame);
turnOnLed(GREEN); turnOnLed(GREEN);
} }
if (buttonIsPressed(RED) && currentGame == none) if (buttonIsPressed(RED))
{ {
setNewState(magicSwitchBoard); activateGame(magicswitchboard);
turnOnLed(RED); turnOnLed(RED);
} }
if (buttonIsPressed(YELLOW))
if (buttonIsPressed(YELLOW) && currentGame == none)
{ {
setNewState(SimpleLed); activateGame(simpleled);
turnOnLed(YELLOW); turnOnLed(YELLOW);
} }
if (currentGame == none)
{
setNewState(SimpleLed);
}
//wait for all buttons idle //wait for all buttons idle
while (anybutton()) while (anybutton())
{ {
} }
turnOffAllLed(); turnOffAllLed();
currentState = state_play;
} }
break; break;
} }

View File

@@ -13,7 +13,7 @@
void initBattery(void) void initBattery(void)
{ {
#ifdef VBATTPIN #ifdef VBATTPIN
battery.begin(VBATTREF, (R12+R13)/R13); //R1 = 220K, R2 = 100K, factor = (R1+R2)/R2 battery.begin(VBATTREF, (R12+R13)/R13);
#endif #endif
} }
@@ -22,7 +22,7 @@ void batterydisplay(void)
#ifdef VBATTPIN #ifdef VBATTPIN
uint16_t currentlevel = battery.level(); uint16_t currentlevel = battery.level();
if (currentlevel > 90) if (currentlevel > 80)
{ {
turnOnLed(3); turnOnLed(3);
} }

View File

@@ -1,30 +1,10 @@
#include "simpleled.h" #include "simpleled.h"
void c_simpleLed::runGame(void)
{
handleSimpleLed(false);
}
void c_simpleLed::initGame(void)
{
initSimpleLed();
setStatus(active);
}
extern std::vector<c_button *> buttonlist; extern std::vector<c_button *> buttonlist;
bool status = false; void c_simpleLed::runGame(void)
void initSimpleLed(void)
{
status = true;
}
void handleSimpleLed(bool newstate)
{ {
for (auto &&button : buttonlist) for (auto &&button : buttonlist)
{ {
if (button->isPressed()) if (button->isPressed())
@@ -38,7 +18,12 @@ void handleSimpleLed(bool newstate)
} }
} }
bool getStatusSimpleLed( void ) void c_simpleLed::initGame(void)
{ {
return status; //no init required
}
void c_simpleLed::resetGame(void)
{
//no reset required
} }

View File

@@ -9,13 +9,10 @@
class c_simpleLed : public c_game class c_simpleLed : public c_game
{ {
public: public:
c_simpleLed(e_game game): c_game{game} {} c_simpleLed(): c_game{simpleled} {}
void runGame(void); void runGame(void);
void initGame(void); void initGame(void);
void resetGame(void);
}; };
void initSimpleLed(void);
void handleSimpleLed(bool newstate);
bool getStatusSimpleLed(void);
#endif //SIMPLELEDH #endif //SIMPLELEDH