diff --git a/platformio.ini b/platformio.ini index e59c71b..b327a44 100644 --- a/platformio.ini +++ b/platformio.ini @@ -8,23 +8,19 @@ ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html -[env:LedBoardV10] +[env] platform = ststm32 -board = STM32L031K6 framework = arduino upload_port = stlink debug_tool = stlink lib_ldf_mode = deep+ + +[env:LedBoardV10] +board = STM32L031K6 build_flags = -DHARDWAREVERSION=10 [env:ledboard_PROTO] -platform = ststm32 board = nucleo_l031K6 -framework = arduino -upload_port = stlink -debug_tool = stlink -boards_dir = boards -lib_ldf_mode = deep+ build_flags = -DHARDWAREVERSION=09 \ No newline at end of file diff --git a/src/board.h b/src/board.h index ae447e9..fe4f35e 100644 --- a/src/board.h +++ b/src/board.h @@ -1,7 +1,10 @@ #ifndef BOARDH #define BOARDH - +#define IDLESHUTDOWN 900000 // 15min* 60 sec * 1000ms +#define VBATTMIN 3200 +#define VBATTMAX 4180 +#define VBATTREF 3300 #ifndef UNIT_TEST diff --git a/src/buttons.cpp b/src/buttons.cpp index 62c28d7..4cd60f9 100644 --- a/src/buttons.cpp +++ b/src/buttons.cpp @@ -48,6 +48,20 @@ bool anybutton(void) return false; } +bool anyButtonChanged(void) +{ + handleButtons(); + for (auto &&i : buttonlist) + { + if (i->isChanged()) + { + return true; + } + } + return false; +} + + bool buttonIsPressed(e_ledcolor index) { c_button *thisbutton = getButton(index); diff --git a/src/buttons.h b/src/buttons.h index 50f90d5..cc67060 100644 --- a/src/buttons.h +++ b/src/buttons.h @@ -18,10 +18,12 @@ public: e_ledcolor getColor( void ){return _color;} uint8_t getIndex( void ) {return _index;} + bool isChanged( void ) {return changed();} }; bool anybutton(void); +bool anyButtonChanged(void); void initButtons(void); void handleButtons(void); c_button *getButton(unsigned int index); @@ -34,4 +36,5 @@ bool buttonIsPressed(uint16_t index); bool buttonWasPressed(e_ledcolor index); + #endif //BUTTONSH \ No newline at end of file diff --git a/src/chainGame.cpp b/src/chainGame.cpp index f25d090..5b295f1 100644 --- a/src/chainGame.cpp +++ b/src/chainGame.cpp @@ -40,7 +40,7 @@ void ResetChainGame(void) cheatButtonFlag = false; } -void HandleChainGame(void) +void HandleChainGame(bool newstate) { if (!patternFlag && !cheatButtonFlag) { diff --git a/src/chainGame.h b/src/chainGame.h index 8efecfe..5c29883 100644 --- a/src/chainGame.h +++ b/src/chainGame.h @@ -4,7 +4,7 @@ #include "Arduino.h" #include "buttons.h" -void HandleChainGame( void ); +void HandleChainGame( bool newstate ); void ResetChainGame(void); diff --git a/src/detectled.cpp b/src/detectled.cpp index a84e795..2a7622e 100644 --- a/src/detectled.cpp +++ b/src/detectled.cpp @@ -11,7 +11,7 @@ extern std::vector buttonlist; c_leds *ledlist_ptr; -void handleDetectLed(void) +void handleDetectLed(bool newstate) { for(auto &&button : buttonlist) diff --git a/src/detectled.h b/src/detectled.h index 2a817ee..aa91057 100644 --- a/src/detectled.h +++ b/src/detectled.h @@ -6,8 +6,7 @@ #include "led.h" #include "vector" -void handleDetectLed( void ); -void initDetectLed( void ); - +void handleDetectLed(bool newstate); +void initDetectLed(void); #endif //DETECTLED \ No newline at end of file diff --git a/src/game.cpp b/src/game.cpp new file mode 100644 index 0000000..ef85547 --- /dev/null +++ b/src/game.cpp @@ -0,0 +1,54 @@ +#include "game.h" +#include + +#include "simpleled.h" + + +std::vector gameslist; + +void initGames(void) +{ + gameslist.clear(); + gameslist.push_back(new c_simpleLed(simpleled)); +} + +void runGame(void) +{ + for(auto &&game: gameslist) + { + if(game->getStatus() == active) + { + game->runGame(); + return; + } + if(game->getStatus() == init) + { + game->initGame(); + return; + } + } +} + +void activateGame(e_game nextgame) +{ + for(auto &&game: gameslist) + { + if(game->getIndex() == nextgame) + { + if(game->getStatus() != active) + { + game->setStatus(init); + } + } + else + { + game->setStatus(disabled); + } + } +} + + + + + + diff --git a/src/game.h b/src/game.h index 9de6af9..14fd06a 100644 --- a/src/game.h +++ b/src/game.h @@ -1,18 +1,35 @@ #ifndef GAMEH #define GAMEH +typedef enum +{ + simpleled, + chaingame, + magicswitchboard, + detectled +} e_game; + +typedef enum +{ + disabled, + init, + active +} e_state; + class c_game { - -protected: - bool _status; +private: + const e_game _gameindex; + e_state _status; public: - c_game(void): _status(false) {}; + c_game(e_game index) : _gameindex(index) {_status = disabled;} - void runGame(void); - void initGame(void); - bool getStatus(void) { return _status;} + virtual void runGame(void); + virtual void initGame(void); + e_state getStatus(void) { return _status; } + e_game getIndex(void) { return _gameindex; } + void setStatus(e_state newstate) { _status = newstate;} }; #endif //GAMEH \ No newline at end of file diff --git a/src/magicSwitchBoard.cpp b/src/magicSwitchBoard.cpp index 5825ec9..8f1704d 100644 --- a/src/magicSwitchBoard.cpp +++ b/src/magicSwitchBoard.cpp @@ -16,7 +16,7 @@ typedef enum states state = last; uint8_t sequence[CHANNELS] = {0xFF, 0xFF, 0xFF}; const uint8_t buttonIndex[CHANNELS] = {1, 2, 3}; -const uint32_t leds[CHANNELS] = {LED1, LED2, LED3}; +const uint16_t leds[CHANNELS] = {1, 2, 3}; uint64_t lastTime = 0; @@ -41,12 +41,14 @@ void showLeds(void) { //check if the position is already programmed //write sequence led on - digitalWrite(leds[i], 1); + turnOnLed(leds[i]); + //digitalWrite(leds[i], 1); } else { //write sequence led off - digitalWrite(leds[i], 0); + turnOffLed(leds[i]); + //digitalWrite(leds[i], 0); } } } @@ -59,8 +61,8 @@ void resetMagicSwitchBoard(void) for (int i = 0; i < CHANNELS; i++) { sequence[i] = 0xff; - digitalWrite(leds[i], 0); } + turnOffAllLed(); } bool CheckTimeOut(void) @@ -131,14 +133,11 @@ void handleIdle(void) } else { - for (auto &&i : leds) - { - digitalWrite(i, 0); - } + turnOffAllLed(); } } -void handleMagicSwitchBoard(void) +void handleMagicSwitchBoard(bool newstate) { switch (state) { @@ -171,4 +170,7 @@ void handleMagicSwitchBoard(void) } - +void initMagicSwitchBoard(void) +{ + resetMagicSwitchBoard(); +} diff --git a/src/magicSwitchBoard.h b/src/magicSwitchBoard.h index bd4c1d5..a251e24 100644 --- a/src/magicSwitchBoard.h +++ b/src/magicSwitchBoard.h @@ -3,6 +3,7 @@ #include "buttons.h" -void handleMagicSwitchBoard( void ); +void handleMagicSwitchBoard( bool newstate ); +void initMagicSwitchBoard(void); #endif //MAGICSWITCHBOARDH \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index c4f5d46..5712602 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,9 +10,7 @@ #include "led.h" #include "power.h" - -#define TIMEOUT 900000 // 15min* 60 sec * 1000ms -#define GAMESELECTTIMEOUT 10000 // 7sec * 1000ms +#define GAMESELECTTIMEOUT 10000 // 10sec * 1000ms typedef enum { @@ -28,12 +26,20 @@ typedef enum game currentGame = none; game nextGame = none; +bool newstate = false; uint8_t gameState = 0; -uint64_t lasttimeOut = 0; uint64_t GameSelectTimer = 0; -bool buttonChanged = false; -void HandleIdle(void) +void setNewState(game nextstate) +{ + if (nextstate != currentGame) + { + currentGame = nextstate; + newstate = true; + } +} + +void HandleIdle(bool newstate) { //green button first released if (!buttonIsPressed(YELLOW) && buttonIsPressed(RED) && buttonIsPressed(GREEN) && (nextGame == none)) @@ -63,7 +69,7 @@ void HandleIdle(void) //wait for all buttons to be switched off if (!anybutton()) { - currentGame = nextGame; + setNewState(nextGame); nextGame = none; } } @@ -84,7 +90,7 @@ void HandleGameSelectTimeout(void) //check timeout if (currentmillis - GameSelectTimer > GAMESELECTTIMEOUT) { - currentGame = idle; + setNewState(idle); GameSelectTimer = 0; } } @@ -96,109 +102,94 @@ void HandleGameSelectTimeout(void) } } -void HandleTimeOut(void) -{ - uint64_t currentmillis = millis(); - if (!lasttimeOut) - { - lasttimeOut = currentmillis; - buttonChanged = anybutton(); - } - - //check if lastTime is initialized or timeout expired - if ((currentmillis - lasttimeOut > TIMEOUT)) - { - currentGame = sleep; - turnOffAllLed(); - shutdown(); - } - else - { - if (buttonChanged != anybutton()) - { - buttonChanged = anybutton(); - //game in progress, update timer - lasttimeOut = currentmillis; - } - } -} - - void setup() { initLeds(); initButtons(); - initDetectLed(); - initSimpleLed(); initLowPower(); initBattery(); + + initDetectLed(); + initMagicSwitchBoard(); + initSimpleLed(); } void loop() { handleButtons(); - HandleTimeOut(); + HandlePower(); HandleGameSelectTimeout(); switch (currentGame) { case idle: { - HandleIdle(); + HandleIdle(newstate); + newstate = false; } break; case SimpleLed: { - handleSimpleLed(); + handleSimpleLed(newstate); + newstate = false; } break; case magicSwitchBoard: { - handleMagicSwitchBoard(); + handleMagicSwitchBoard(newstate); + newstate = false; } break; case detectLED: { - handleDetectLed(); + handleDetectLed(newstate); + newstate = false; } break; case ChainGame: default: { - HandleChainGame(); + HandleChainGame(newstate); + newstate = false; } break; case none: { - currentGame = SimpleLed; batteryCheck(); batterydisplay(); delay(1000); - if (buttonIsPressed(GREEN)) + turnOffAllLed(); + if (buttonIsPressed(GREEN) && currentGame == none) { - currentGame = ChainGame; + setNewState(ChainGame); turnOnLed(GREEN); } - if (buttonIsPressed(RED)) + if (buttonIsPressed(RED) && currentGame == none) { - currentGame = magicSwitchBoard; + setNewState(magicSwitchBoard); turnOnLed(RED); } - if (buttonIsPressed(YELLOW)) + if (buttonIsPressed(YELLOW) && currentGame == none) { - currentGame = SimpleLed; + setNewState(SimpleLed); turnOnLed(YELLOW); } + if (currentGame == none) + { + setNewState(SimpleLed); + } + //wait for all buttons idle while (anybutton()) - {} + { + } turnOffAllLed(); } diff --git a/src/power.cpp b/src/power.cpp index e0e7ded..2de243c 100644 --- a/src/power.cpp +++ b/src/power.cpp @@ -3,35 +3,25 @@ #include "rtc.h" #include "low_Power.h" #include "led.h" +#include "buttons.h" #ifdef VBATTPIN #include "Battery.h" - Battery battery(2500, 4160, VBATTPIN); + Battery battery(VBATTMIN, VBATTMAX, VBATTPIN); #endif void initBattery(void) { #ifdef VBATTPIN - battery.begin(3300, (R12+R13)/R13); //R1 = 220K, R2 = 100K, factor = (R1+R2)/R2 + battery.begin(VBATTREF, (R12+R13)/R13); //R1 = 220K, R2 = 100K, factor = (R1+R2)/R2 #endif } -uint16_t batteryGetVoltage( void ) -{ -#ifdef VBATTPIN - return battery.voltage(); -#endif -return 4200; -} - void batterydisplay(void) { #ifdef VBATTPIN uint16_t currentlevel = battery.level(); - uint16_t currentvoltage = batteryGetVoltage(); - - if(currentvoltage) - { + if (currentlevel > 90) { turnOnLed(3); @@ -44,14 +34,14 @@ void batterydisplay(void) { turnOnLed(1); } - } + #endif } void batteryCheck(void) { #ifdef VBATTPIN - if (battery.level() < 10) + if (battery.voltage() < VBATTMIN) { for( int i = 0; i < 10;i++) { @@ -75,4 +65,40 @@ void initLowPower(void) void shutdown(void) { LowPower_shutdown(); +} + +void HandlePower(void) +{ + HandleTimeOut(); + batteryCheck(); +} + + +void HandleTimeOut(void) +{ + uint64_t currentmillis = millis(); + static uint64_t lasttimeOut = 0; + static bool buttonChanged = false; + + if (!lasttimeOut) + { + lasttimeOut = currentmillis; + buttonChanged = anybutton(); + } + + //check if lastTime is initialized or timeout expired + if ((currentmillis - lasttimeOut > IDLESHUTDOWN)) + { + turnOffAllLed(); + shutdown(); + } + else + { + if (buttonChanged != anybutton()) + { + buttonChanged = anybutton(); + //game in progress, update timer + lasttimeOut = currentmillis; + } + } } \ No newline at end of file diff --git a/src/power.h b/src/power.h index 6ae4326..3af41ef 100644 --- a/src/power.h +++ b/src/power.h @@ -1,10 +1,12 @@ #pragma once //battery -void initBattery( void ); -void batterydisplay( void ); +void initBattery(void); +void batterydisplay(void); void batteryCheck(void); //low power -void initLowPower( void ); -void shutdown( void ); +void initLowPower(void); +void shutdown(void); +void HandleTimeOut(void); +void HandlePower(void); diff --git a/src/simpleled.cpp b/src/simpleled.cpp index cf32ae0..8140836 100644 --- a/src/simpleled.cpp +++ b/src/simpleled.cpp @@ -1,15 +1,16 @@ #include "simpleled.h" + void c_simpleLed::runGame(void) { - handleSimpleLed(); + handleSimpleLed(false); } void c_simpleLed::initGame(void) { initSimpleLed(); - _status = true; + setStatus(active); } @@ -22,7 +23,7 @@ void initSimpleLed(void) status = true; } -void handleSimpleLed(void) +void handleSimpleLed(bool newstate) { for (auto &&button : buttonlist) { diff --git a/src/simpleled.h b/src/simpleled.h index c8a6fa6..6a2effc 100644 --- a/src/simpleled.h +++ b/src/simpleled.h @@ -8,14 +8,14 @@ class c_simpleLed : public c_game { +public: + c_simpleLed(e_game game): c_game{game} {} void runGame(void); void initGame(void); }; - -void initSimpleLed( void ); -void handleSimpleLed( void ); -bool getStatusSimpleLed( void ); - +void initSimpleLed(void); +void handleSimpleLed(bool newstate); +bool getStatusSimpleLed(void); #endif //SIMPLELEDH \ No newline at end of file