From e345a8a68715261996b5c4a8ccf8092b9324f43f Mon Sep 17 00:00:00 2001 From: Willem Oldemans Date: Wed, 31 Mar 2021 20:20:18 +0200 Subject: [PATCH] moved all code to gameclass --- platformio.ini | 8 +-- src/buttons.cpp | 38 +++++++++++-- src/buttons.h | 14 ++++- src/chainGame.cpp | 49 ++++++++--------- src/chainGame.h | 24 +++++++- src/game.cpp | 73 +++++++++++++++++++------ src/game.h | 24 +++++--- src/led.cpp | 29 ++++++++-- src/led.h | 4 ++ src/magicSwitchBoard.cpp | 57 +++++++++++-------- src/magicSwitchBoard.h | 11 ++++ src/main.cpp | 115 ++++++++++++--------------------------- src/power.cpp | 4 +- src/simpleled.cpp | 33 +++-------- src/simpleled.h | 7 +-- 15 files changed, 290 insertions(+), 200 deletions(-) diff --git a/platformio.ini b/platformio.ini index b327a44..5501b91 100644 --- a/platformio.ini +++ b/platformio.ini @@ -20,7 +20,7 @@ board = STM32L031K6 build_flags = -DHARDWAREVERSION=10 -[env:ledboard_PROTO] -board = nucleo_l031K6 -build_flags = - -DHARDWAREVERSION=09 \ No newline at end of file +; [env:ledboard_PROTO] +; board = nucleo_l031K6 +; build_flags = +; -DHARDWAREVERSION=09 \ No newline at end of file diff --git a/src/buttons.cpp b/src/buttons.cpp index 4cd60f9..ed03dd0 100644 --- a/src/buttons.cpp +++ b/src/buttons.cpp @@ -4,12 +4,12 @@ std::vector buttonlist; -c_button button1(SWITCH1, YELLOW, 1); -c_button button2(SWITCH2, RED, 2); -c_button button3(SWITCH3, GREEN, 3); -c_button button4(SWITCH12, YELLOW2, 4); -c_button button5(SWITCH22, RED2, 5); -c_button button6(SWITCH32, GREEN2, 6); +c_button button1(SWITCH1, YELLOW, 1, type_switch); +c_button button2(SWITCH2, RED, 2, type_switch); +c_button button3(SWITCH3, GREEN, 3, type_switch); +c_button button4(SWITCH12, YELLOW2, 4, type_momentary); +c_button button5(SWITCH22, RED2, 5, type_momentary); +c_button button6(SWITCH32, GREEN2, 6, type_momentary); void buttonbegin(c_button *thisbutton) { @@ -61,6 +61,32 @@ bool anyButtonChanged(void) 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) { diff --git a/src/buttons.h b/src/buttons.h index cc67060..c6dbc23 100644 --- a/src/buttons.h +++ b/src/buttons.h @@ -7,16 +7,24 @@ #include "board.h" #include "led.h" +typedef enum +{ + type_switch, + type_momentary +}e_switchtype; + class c_button : public ToggleButton { const e_ledcolor _color; const uint8_t _index; + const e_switchtype _switchtype; public: - c_button(uint8_t pin, e_ledcolor color, uint8_t index) - : ToggleButton(pin), _color(color), _index(index) {} + c_button(uint8_t pin, e_ledcolor color, uint8_t index, e_switchtype switchtype) + : ToggleButton(pin), _color(color), _index(index), _switchtype(switchtype) {} e_ledcolor getColor( void ){return _color;} + e_switchtype getType(void) { return _switchtype;} uint8_t getIndex( void ) {return _index;} bool isChanged( void ) {return changed();} @@ -26,6 +34,8 @@ bool anybutton(void); bool anyButtonChanged(void); void initButtons(void); void handleButtons(void); +bool allButtons(void); +bool onlyButton(e_ledcolor color); c_button *getButton(unsigned int index); c_button *getButton(e_ledcolor index); diff --git a/src/chainGame.cpp b/src/chainGame.cpp index 5b295f1..33d8385 100644 --- a/src/chainGame.cpp +++ b/src/chainGame.cpp @@ -1,25 +1,23 @@ #ifndef UNIT_TEST -#ifdef ARDUINO #include "chainGame.h" -uint8_t patternIndex = 0; -bool patternFlag = false; +//uint8_t patternIndex = 0; +//bool patternFlag = false; //bool firstpattern = false; -uint16_t cheatbutton = 0; -bool cheatButtonFlag = false; +// uint16_t cheatbutton = 0; +//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]); - -void nextPattern(void) +void c_chaingame::nextPattern(void) { if (patternIndex < patternlength - 1) { @@ -31,16 +29,7 @@ void nextPattern(void) } } -void ResetChainGame(void) -{ - patternIndex = 0; - patternFlag = false; - //firstpattern = false; - cheatbutton = 0; - cheatButtonFlag = false; -} - -void HandleChainGame(bool newstate) +void c_chaingame::runGame(void) { if (!patternFlag && !cheatButtonFlag) { @@ -107,6 +96,16 @@ void HandleChainGame(bool newstate) cheatbutton = 3; } } +void c_chaingame::initGame(void) +{ + patternIndex = 0; + patternFlag = false; + cheatbutton = 0; + cheatButtonFlag = false; +} +void c_chaingame::resetGame(void) +{ + initGame(); +} #endif -#endif diff --git a/src/chainGame.h b/src/chainGame.h index 5c29883..f634c1a 100644 --- a/src/chainGame.h +++ b/src/chainGame.h @@ -3,9 +3,29 @@ #include "Arduino.h" #include "buttons.h" +#include "game.h" -void HandleChainGame( bool newstate ); -void ResetChainGame(void); +class c_chaingame : public c_game +{ +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 \ No newline at end of file diff --git a/src/game.cpp b/src/game.cpp index ef85547..61547ca 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -2,53 +2,94 @@ #include #include "simpleled.h" - +#include "magicSwitchBoard.h" +#include "chainGame.h" std::vector gameslist; void initGames(void) { 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(); return; } - if(game->getStatus() == init) + if (game->getStatus() == init) { game->initGame(); + game->setStatus(active); 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) { - 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 { - game->setStatus(disabled); + thisgame->setStatus(disabled); } } } - - - - - +void disableAllGames(void) +{ + for (auto &&thisgame : gameslist) + { + thisgame->setStatus(disabled); + } +} diff --git a/src/game.h b/src/game.h index 14fd06a..e5fba8a 100644 --- a/src/game.h +++ b/src/game.h @@ -3,10 +3,10 @@ typedef enum { + none, simpleled, chaingame, - magicswitchboard, - detectled + magicswitchboard } e_game; typedef enum @@ -14,22 +14,30 @@ typedef enum disabled, init, active -} e_state; +} e_gamestate; class c_game { -private: +protected: const e_game _gameindex; - e_state _status; + e_gamestate _status; 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 initGame(void); - e_state getStatus(void) { return _status; } + virtual void resetGame(void); + e_gamestate getStatus(void) { return _status; } 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 \ No newline at end of file diff --git a/src/led.cpp b/src/led.cpp index 59b43d6..b7b75d0 100644 --- a/src/led.cpp +++ b/src/led.cpp @@ -17,8 +17,8 @@ void initLeds(void) { ledlist.init(); ledlist.AddLed(LED1, DETECT1, 1, 844, YELLOW, true); - ledlist.AddLed(LED2, DETECT2, 2, 512, RED, true); - ledlist.AddLed(LED3, DETECT3, 3, 92, GREEN, true); + ledlist.AddLed(LED2, DETECT2, 2, 512, RED, true); + ledlist.AddLed(LED3, DETECT3, 3, 92, GREEN, true); ledlist.begin(); } @@ -48,6 +48,11 @@ void turnOffAllLed() ledlist.turnAllOff(); } +void turnOnAllLed() +{ + ledlist.turnAllOn(); +} + //############################################# //# leds functions # //############################################# @@ -108,10 +113,27 @@ void c_leds::turnOffLed(uint16_t index) } 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) { - port.turnOff(); + if (state) + { + port.turnOn(); + } + else + { + port.turnOff(); + } } } @@ -238,4 +260,3 @@ bool c_led::checkIndex(uint16_t index) } return false; } - diff --git a/src/led.h b/src/led.h index 93599a6..6a7f0cf 100644 --- a/src/led.h +++ b/src/led.h @@ -84,6 +84,8 @@ public: void turnAllOff(void); + void turnAllOn(void); + void setAllLeds(bool state); }; c_leds *getledlist(void); @@ -94,6 +96,8 @@ void turnOffLed(e_ledcolor color); void turnOffLed(uint16_t index); void turnOnLed(uint16_t index); void turnOffAllLed(); +void turnOnAllLed(); + #endif //LEDH diff --git a/src/magicSwitchBoard.cpp b/src/magicSwitchBoard.cpp index 8f1704d..04c0832 100644 --- a/src/magicSwitchBoard.cpp +++ b/src/magicSwitchBoard.cpp @@ -7,13 +7,12 @@ typedef enum { - idle, + wait, learn, - active, - last + play } states; -states state = last; +states state = wait; uint8_t sequence[CHANNELS] = {0xFF, 0xFF, 0xFF}; const uint8_t buttonIndex[CHANNELS] = {1, 2, 3}; const uint16_t leds[CHANNELS] = {1, 2, 3}; @@ -22,6 +21,9 @@ uint64_t lastTime = 0; uint8_t learnIndex = 0; + + + void showLeds(void) { //loop through the button list @@ -55,14 +57,14 @@ void showLeds(void) void resetMagicSwitchBoard(void) { - state = idle; - lastTime = 0; - learnIndex = 0; - for (int i = 0; i < CHANNELS; i++) - { - sequence[i] = 0xff; - } - turnOffAllLed(); + // state = idle; + // lastTime = 0; + // learnIndex = 0; + // for (int i = 0; i < CHANNELS; i++) + // { + // sequence[i] = 0xff; + // } + // turnOffAllLed(); } bool CheckTimeOut(void) @@ -121,7 +123,7 @@ void handleLearn(void) } 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(); } @@ -153,7 +156,7 @@ void handleMagicSwitchBoard(bool newstate) } break; - case active: + case play: { CheckTimeOut(); } @@ -161,16 +164,26 @@ void handleMagicSwitchBoard(bool newstate) default: { - state = idle; + state = wait; } break; } 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(); } diff --git a/src/magicSwitchBoard.h b/src/magicSwitchBoard.h index a251e24..cb47bd2 100644 --- a/src/magicSwitchBoard.h +++ b/src/magicSwitchBoard.h @@ -2,6 +2,17 @@ #define MAGICSWITCHBOARDH #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 initMagicSwitchBoard(void); diff --git a/src/main.cpp b/src/main.cpp index 5712602..6d136d6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,68 +9,53 @@ #include "buttons.h" #include "led.h" #include "power.h" +#include "game.h" #define GAMESELECTTIMEOUT 10000 // 10sec * 1000ms typedef enum { - none, - sleep, - idle, - SimpleLed, - ChainGame, - magicSwitchBoard, - detectLED, - last -} game; + state_init, + state_idle, + state_play +}e_state; -game currentGame = none; -game nextGame = none; -bool newstate = false; -uint8_t gameState = 0; +e_state currentState = state_init; 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 - if (!buttonIsPressed(YELLOW) && buttonIsPressed(RED) && buttonIsPressed(GREEN) && (nextGame == none)) + e_game nextGame = none; + //yellow button first released + if (onlyButton(YELLOW) && (nextGame == none)) { //prepare for next game - nextGame = ChainGame; - ResetChainGame(); + nextGame = chaingame; turnOffLed(YELLOW); } //red button first released - if (buttonIsPressed(YELLOW) && !buttonIsPressed(RED) && buttonIsPressed(GREEN) & (nextGame == none)) + if (onlyButton(RED) && (nextGame == none)) { //prepare for next game - nextGame = magicSwitchBoard; + nextGame = magicswitchboard; turnOffLed(RED); } //green button first released - if (buttonIsPressed(YELLOW) && buttonIsPressed(RED) && !buttonIsPressed(GREEN) & (nextGame == none)) + if (onlyButton(GREEN) && (nextGame == none)) { //prepare for next game - nextGame = SimpleLed; + nextGame = simpleled; turnOffLed(GREEN); } //wait for all buttons to be switched off if (!anybutton()) { - setNewState(nextGame); - nextGame = none; + activateGame(nextGame); + currentState = state_play; } } @@ -78,7 +63,7 @@ void HandleGameSelectTimeout(void) { uint64_t currentmillis = millis(); // yellow && red && green all on - if (buttonIsPressed(YELLOW) && buttonIsPressed(RED) && buttonIsPressed(GREEN)) + if (allButtons()) { //all buttons pressed, wait for next game if (!GameSelectTimer) @@ -90,7 +75,7 @@ void HandleGameSelectTimeout(void) //check timeout if (currentmillis - GameSelectTimer > GAMESELECTTIMEOUT) { - setNewState(idle); + currentState = state_idle; GameSelectTimer = 0; } } @@ -108,10 +93,7 @@ void setup() initButtons(); initLowPower(); initBattery(); - - initDetectLed(); - initMagicSwitchBoard(); - initSimpleLed(); + initGames(); } void loop() @@ -120,78 +102,51 @@ void loop() HandlePower(); HandleGameSelectTimeout(); - switch (currentGame) + switch (currentState) { - case idle: + case state_idle: { - HandleIdle(newstate); - newstate = false; + HandleIdle(); } break; - case SimpleLed: + case state_play: { - handleSimpleLed(newstate); - newstate = false; + runGames(); } break; - case magicSwitchBoard: - { - handleMagicSwitchBoard(newstate); - newstate = false; - } - break; - - case detectLED: - { - handleDetectLed(newstate); - newstate = false; - } - break; - - case ChainGame: - default: - { - HandleChainGame(newstate); - newstate = false; - } - break; - - case none: + case state_init: { batteryCheck(); batterydisplay(); delay(1000); turnOffAllLed(); - if (buttonIsPressed(GREEN) && currentGame == none) + activateGame(simpleled); + + if (buttonIsPressed(GREEN)) { - setNewState(ChainGame); + activateGame(chaingame); turnOnLed(GREEN); } - if (buttonIsPressed(RED) && currentGame == none) + if (buttonIsPressed(RED)) { - setNewState(magicSwitchBoard); + activateGame(magicswitchboard); turnOnLed(RED); } - - if (buttonIsPressed(YELLOW) && currentGame == none) + if (buttonIsPressed(YELLOW)) { - setNewState(SimpleLed); + activateGame(simpleled); turnOnLed(YELLOW); } - if (currentGame == none) - { - setNewState(SimpleLed); - } - //wait for all buttons idle while (anybutton()) { } turnOffAllLed(); + currentState = state_play; } break; } diff --git a/src/power.cpp b/src/power.cpp index 2de243c..1850782 100644 --- a/src/power.cpp +++ b/src/power.cpp @@ -13,7 +13,7 @@ void initBattery(void) { #ifdef VBATTPIN - battery.begin(VBATTREF, (R12+R13)/R13); //R1 = 220K, R2 = 100K, factor = (R1+R2)/R2 + battery.begin(VBATTREF, (R12+R13)/R13); #endif } @@ -22,7 +22,7 @@ void batterydisplay(void) #ifdef VBATTPIN uint16_t currentlevel = battery.level(); - if (currentlevel > 90) + if (currentlevel > 80) { turnOnLed(3); } diff --git a/src/simpleled.cpp b/src/simpleled.cpp index 8140836..6acdcaf 100644 --- a/src/simpleled.cpp +++ b/src/simpleled.cpp @@ -1,30 +1,10 @@ #include "simpleled.h" - - -void c_simpleLed::runGame(void) -{ - handleSimpleLed(false); -} - -void c_simpleLed::initGame(void) -{ - initSimpleLed(); - setStatus(active); -} - - extern std::vector buttonlist; -bool status = false; - -void initSimpleLed(void) -{ - status = true; -} - -void handleSimpleLed(bool newstate) +void c_simpleLed::runGame(void) { + for (auto &&button : buttonlist) { 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 } \ No newline at end of file diff --git a/src/simpleled.h b/src/simpleled.h index 6a2effc..dc9db85 100644 --- a/src/simpleled.h +++ b/src/simpleled.h @@ -9,13 +9,10 @@ class c_simpleLed : public c_game { public: - c_simpleLed(e_game game): c_game{game} {} + c_simpleLed(): c_game{simpleled} {} void runGame(void); void initGame(void); + void resetGame(void); }; -void initSimpleLed(void); -void handleSimpleLed(bool newstate); -bool getStatusSimpleLed(void); - #endif //SIMPLELEDH \ No newline at end of file