From f4e23386ef88249e76b24d39dc2d943bddc2ae3e Mon Sep 17 00:00:00 2001 From: Willem Oldemans Date: Mon, 14 Jun 2021 08:19:45 +0200 Subject: [PATCH] firmware recovery 1.3RC --- src/board.h | 2 +- src/buttons.cpp | 22 +++++++-- src/buttons.h | 1 + src/chainGame.cpp | 18 +------ src/chainGame.h | 4 +- src/detectled.cpp | 54 ++++++++++++--------- src/detectled.h | 21 +++++--- src/game.cpp | 100 +++++++++++++++++++++++---------------- src/game.h | 37 +++++++-------- src/magicSwitchBoard.cpp | 51 +++----------------- src/magicSwitchBoard.h | 36 +++++++++----- src/power.cpp | 66 +++++++++++++++++--------- src/power.h | 4 +- src/simpleled.cpp | 12 +++-- src/simpleled.h | 12 ++--- 15 files changed, 237 insertions(+), 203 deletions(-) diff --git a/src/board.h b/src/board.h index a980245..ffd3c32 100644 --- a/src/board.h +++ b/src/board.h @@ -2,7 +2,7 @@ #define BOARDH #define IDLESHUTDOWN 900000 // 15min* 60 sec * 1000ms -#define VBATTMIN 3000 +#define VBATTMIN 3200 #define VBATTMAX 4180 #define VBATTREF 3300 diff --git a/src/buttons.cpp b/src/buttons.cpp index ed03dd0..d9708ae 100644 --- a/src/buttons.cpp +++ b/src/buttons.cpp @@ -63,11 +63,11 @@ bool anyButtonChanged(void) bool allButtons(void) { - for( auto&& thisbutton : buttonlist) + for (auto &&thisbutton : buttonlist) { - if(thisbutton->getType() == type_switch) + if (thisbutton->getType() == type_switch) { - if(!thisbutton->isPressed()) + if (!thisbutton->isPressed()) { return false; } @@ -151,4 +151,20 @@ c_button *getButton(e_ledcolor color) std::vector *getButtonlist(void) { return &buttonlist; +} + +uint8_t buttonPressedCount(void) +{ + uint8_t count = 0; + for (auto &&button : buttonlist) + { + if (!button == NULL) + { + if (button->isPressed()) + { + count++; + } + } + } + return count; } \ No newline at end of file diff --git a/src/buttons.h b/src/buttons.h index c6dbc23..b87c554 100644 --- a/src/buttons.h +++ b/src/buttons.h @@ -44,6 +44,7 @@ std::vector* getButtonlist(void); bool buttonIsPressed(e_ledcolor index); bool buttonIsPressed(uint16_t index); bool buttonWasPressed(e_ledcolor index); +uint8_t buttonPressedCount( void ); diff --git a/src/chainGame.cpp b/src/chainGame.cpp index 33d8385..844aa74 100644 --- a/src/chainGame.cpp +++ b/src/chainGame.cpp @@ -2,21 +2,6 @@ #include "chainGame.h" -//uint8_t patternIndex = 0; -//bool patternFlag = false; -//bool firstpattern = false; -// uint16_t cheatbutton = 0; -//bool cheatButtonFlag = false; - -// uint16_t ledpattern[4] = -// { -// 1, -// 3, -// 1, -// 2}; - -// int patternlength = sizeof(ledpattern) / sizeof(ledpattern[0]); - void c_chaingame::nextPattern(void) { if (patternIndex < patternlength - 1) @@ -96,12 +81,13 @@ void c_chaingame::runGame(void) cheatbutton = 3; } } -void c_chaingame::initGame(void) +bool c_chaingame::initGame(void) { patternIndex = 0; patternFlag = false; cheatbutton = 0; cheatButtonFlag = false; + return true; } void c_chaingame::resetGame(void) { diff --git a/src/chainGame.h b/src/chainGame.h index f634c1a..dd91eab 100644 --- a/src/chainGame.h +++ b/src/chainGame.h @@ -18,9 +18,9 @@ private: void nextPattern(void); public: - c_chaingame(): c_game{chaingame} {} + c_chaingame(e_ledcolor gamecolor): c_game{chaingame, gamecolor} {} void runGame(void); - void initGame(void); + bool initGame(void); void resetGame(void); }; diff --git a/src/detectled.cpp b/src/detectled.cpp index 2a7622e..6ca8890 100644 --- a/src/detectled.cpp +++ b/src/detectled.cpp @@ -1,37 +1,45 @@ #include "detectled.h" - -#define CHANNELS 3 -#define SAMPLES 20 - - -extern std::vector buttonlist; - - -c_leds *ledlist_ptr; - - -void handleDetectLed(bool newstate) +void c_detectled::runGame(void) { - - for(auto &&button : buttonlist) + //check if buttonlist is a valid pointer + if (buttonlist != NULL) { - if(button->isPressed()) + for (auto &&button : *buttonlist) { - ledlist_ptr->turnOnLed(button->getColor()); - } - else - { - ledlist_ptr->turnOffLed(button->getColor()); + if (button->isPressed()) + { + ledlist_ptr->turnOnLed(button->getColor()); + } + else + { + ledlist_ptr->turnOffLed(button->getColor()); + } } } + else + { + setStatus(disabled); + } } -void initDetectLed(void) + +bool c_detectled::initGame(void) { ledlist_ptr = getledlist(); -#ifndef UNIT_TEST + if(ledlist_ptr == NULL) + return false; + + buttonlist = getButtonlist(); + if(buttonlist == NULL) + return false; analogReadResolution(10); -#endif + + return true; } + +void c_detectled::resetGame(void) +{ + initGame(); +} diff --git a/src/detectled.h b/src/detectled.h index aa91057..62d432b 100644 --- a/src/detectled.h +++ b/src/detectled.h @@ -1,12 +1,19 @@ -#ifndef DETECTLEDH -#define DETECTLEDH +#pragma once + +#include "game.h" #include "buttons.h" -#include "board.h" #include "led.h" -#include "vector" +#include -void handleDetectLed(bool newstate); -void initDetectLed(void); +class c_detectled : public c_game +{ + c_leds *ledlist_ptr; + std::vector *buttonlist; -#endif //DETECTLED \ No newline at end of file +public: + c_detectled(e_ledcolor gamecolor): c_game{detectled, gamecolor} {} + void runGame(void); + bool initGame(void); + void resetGame(void); +}; \ No newline at end of file diff --git a/src/game.cpp b/src/game.cpp index e469507..d7e2383 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -4,17 +4,24 @@ #include "simpleled.h" #include "magicSwitchBoard.h" #include "chainGame.h" +#include "detectled.h" std::vector gameslist; uint64_t GameSelectTimer = 0; e_state currentState = state_init; +c_game *game_simpleled = new c_simpleLed(YELLOW); +c_game *game_magicswitchboard = new c_magicSwitchBoard(RED); +c_game *game_chaingame = new c_chaingame(GREEN); +c_game *game_detectled = new c_detectled(NONE); + void initGames(void) { gameslist.clear(); - gameslist.push_back(new c_simpleLed()); - gameslist.push_back(new c_magicSwitchBoard()); - gameslist.push_back(new c_chaingame()); + gameslist.push_back(game_simpleled); + gameslist.push_back(game_magicswitchboard); + gameslist.push_back(game_chaingame); + gameslist.push_back(game_detectled); activateGame(simpleled); } @@ -31,6 +38,18 @@ c_game *getGame(e_game game) return NULL; } +c_game *getGame(e_ledcolor gamecolor) +{ + for (auto &&thisgame : gameslist) + { + if (thisgame->getGameColor() == gamecolor) + { + return thisgame; + } + } + return NULL; +} + void runGames(void) { for (auto &&game : gameslist) @@ -62,8 +81,14 @@ void runGame(e_game game) } if (thisgame->getStatus() == init) { - thisgame->initGame(); - thisgame->setStatus(active); + if (thisgame->initGame()) + { + thisgame->setStatus(active); + } + else + { + thisgame->setStatus(init_error); + } return; } } @@ -88,6 +113,14 @@ void activateGame(e_game nextgame) } } +void c_game::setStatus(e_gamestate newstate) +{ + if (_status != init_error || newstate == init) + { + _status = newstate; + } +} + void disableAllGames(void) { for (auto &&thisgame : gameslist) @@ -127,28 +160,16 @@ void HandleGameSelectTimeout(void) void HandleGameIdle(void) { e_game nextGame = none; - //yellow button first released - if (onlyButton(YELLOW) && (nextGame == none)) - { - //prepare for next game - nextGame = chaingame; - turnOffLed(YELLOW); - } - //red button first released - if (onlyButton(RED) && (nextGame == none)) + for (auto &&thisgame : gameslist) { - //prepare for next game - nextGame = magicswitchboard; - turnOffLed(RED); - } - - //green button first released - if (onlyButton(GREEN) && (nextGame == none)) - { - //prepare for next game - nextGame = simpleled; - turnOffLed(GREEN); + if (onlyButton(thisgame->getGameColor()) && (nextGame == none)) + { + //prepare for next game + nextGame = thisgame->getIndex(); + turnOffLed(thisgame->getGameColor()); + break; + } } //wait for all buttons to be switched off @@ -164,14 +185,14 @@ void setGameState(e_state newstate) currentState = state_play; } -e_state getCurrentState( void ) +e_state getCurrentState(void) { return currentState; } -void handleGames( void ) +void handleGames(void) { -HandleGameSelectTimeout(); + HandleGameSelectTimeout(); switch (getCurrentState()) { @@ -189,24 +210,19 @@ HandleGameSelectTimeout(); case state_init: { - delay(1000); + //delay(1000); turnOffAllLed(); activateGame(simpleled); - if (buttonIsPressed(GREEN)) + for (auto &&thisgame : gameslist) { - activateGame(chaingame); - turnOnLed(GREEN); - } - if (buttonIsPressed(RED)) - { - activateGame(magicswitchboard); - turnOnLed(RED); - } - if (buttonIsPressed(YELLOW)) - { - activateGame(simpleled); - turnOnLed(YELLOW); + if (onlyButton(thisgame->getGameColor())) + { + //prepare for next game + activateGame(thisgame->getIndex()); + turnOnLed(thisgame->getGameColor()); + break; + } } //wait for all buttons idle diff --git a/src/game.h b/src/game.h index 7a239a2..361e55e 100644 --- a/src/game.h +++ b/src/game.h @@ -1,60 +1,59 @@ #ifndef GAMEH #define GAMEH +#include "led.h" + #define GAMESELECTTIMEOUT 10000 // 10sec * 1000ms - -typedef enum +enum e_game { none, simpleled, chaingame, - magicswitchboard -} e_game; + magicswitchboard, + detectled +}; -typedef enum +enum e_gamestate { disabled, init, - active -} e_gamestate; + active, + init_error +}; - -typedef enum +enum e_state { state_init, state_idle, state_play -}e_state; +}; class c_game { protected: const e_game _gameindex; e_gamestate _status; + const e_ledcolor _gamecolor; public: - c_game(e_game index) : _gameindex(index) { _status = disabled; } + c_game(e_game index, e_ledcolor gamecolor) : _gameindex(index), _gamecolor(gamecolor) { _status = disabled; } virtual void runGame(void); - virtual void initGame(void); + virtual bool initGame(void); virtual void resetGame(void); e_gamestate getStatus(void) { return _status; } e_game getIndex(void) { return _gameindex; } - void setStatus(e_gamestate newstate) { _status = newstate; } + void setStatus(e_gamestate newstate); + e_ledcolor getGameColor(void) { return _gamecolor; } }; -c_game *getGame(e_game game); +c_game *getGame(e_game game, e_ledcolor gamecolor); void runGame(e_game game); void runGames(void); void activateGame(e_game nextgame); void initGames(void); -//void HandleGameSelectTimeout(void); -//void HandleGameIdle( void ); -//void setGameState(e_state newstate); -//e_state getCurrentState( void ); void handleGames(); - #endif //GAMEH \ No newline at end of file diff --git a/src/magicSwitchBoard.cpp b/src/magicSwitchBoard.cpp index 04c0832..0864ebc 100644 --- a/src/magicSwitchBoard.cpp +++ b/src/magicSwitchBoard.cpp @@ -1,30 +1,6 @@ - - #include "magicSwitchBoard.h" -#define CHANNELS 3 -#define TIMEOUT 7000 //game timeout - -typedef enum -{ - wait, - learn, - play -} states; - -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}; - -uint64_t lastTime = 0; - -uint8_t learnIndex = 0; - - - - -void showLeds(void) +void c_magicSwitchBoard::showLeds(void) { //loop through the button list for (int i = 0; i < CHANNELS; i++) @@ -44,30 +20,16 @@ void showLeds(void) //check if the position is already programmed //write sequence led on turnOnLed(leds[i]); - //digitalWrite(leds[i], 1); } else { //write sequence led off turnOffLed(leds[i]); - //digitalWrite(leds[i], 0); } } } -void resetMagicSwitchBoard(void) -{ - // state = idle; - // lastTime = 0; - // learnIndex = 0; - // for (int i = 0; i < CHANNELS; i++) - // { - // sequence[i] = 0xff; - // } - // turnOffAllLed(); -} - -bool CheckTimeOut(void) +bool c_magicSwitchBoard::CheckTimeOut(void) { uint64_t currentmillis = millis(); if(!lastTime) @@ -79,7 +41,7 @@ bool CheckTimeOut(void) if ((currentmillis - lastTime > TIMEOUT)) { //handle timeout - resetMagicSwitchBoard(); + initGame(); return true; } else @@ -93,7 +55,7 @@ bool CheckTimeOut(void) return false; } -void handleLearn(void) +void c_magicSwitchBoard::handleLearn(void) { for (int i = 0; i < CHANNELS; i++) { @@ -127,7 +89,7 @@ void handleLearn(void) } } -void handleIdle(void) +void c_magicSwitchBoard::handleIdle(void) { if (anybutton()) { @@ -171,7 +133,7 @@ void c_magicSwitchBoard::runGame(void) showLeds(); } -void c_magicSwitchBoard::initGame(void) +bool c_magicSwitchBoard::initGame(void) { state = wait; lastTime = 0; @@ -181,6 +143,7 @@ void c_magicSwitchBoard::initGame(void) sequence[i] = 0xff; } turnOffAllLed(); + return true; } void c_magicSwitchBoard::resetGame(void) diff --git a/src/magicSwitchBoard.h b/src/magicSwitchBoard.h index cb47bd2..4c02b11 100644 --- a/src/magicSwitchBoard.h +++ b/src/magicSwitchBoard.h @@ -1,20 +1,34 @@ -#ifndef MAGICSWITCHBOARDH -#define MAGICSWITCHBOARDH +#pragma once #include "buttons.h" #include "game.h" +#define CHANNELS 3 +#define TIMEOUT 7000 //game timeout class c_magicSwitchBoard : public c_game { + enum states + { + wait, + learn, + play + }; + + 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}; + uint64_t lastTime = 0; + uint8_t learnIndex = 0; + + void showLeds(void); + bool CheckTimeOut(void); + void handleLearn(void); + void handleIdle(void); + public: - c_magicSwitchBoard(): c_game{magicswitchboard} {} + c_magicSwitchBoard(e_ledcolor gamecolor) : c_game{magicswitchboard, gamecolor} {} void runGame(void); - void initGame(void); + bool initGame(void); void resetGame(void); -}; - - -void handleMagicSwitchBoard( bool newstate ); -void initMagicSwitchBoard(void); - -#endif //MAGICSWITCHBOARDH \ No newline at end of file +}; \ No newline at end of file diff --git a/src/power.cpp b/src/power.cpp index c89f542..ef9d997 100644 --- a/src/power.cpp +++ b/src/power.cpp @@ -9,7 +9,7 @@ Button buttonPower(BTN_PWR, 250UL, 1U, 0); Battery battery(VBATTMIN, VBATTMAX, VBATTPIN); #endif -uint64_t delay_timer = 0; +uint64_t measure_timer = 0; uint64_t powerOnOffDelay = 0; powerStates powerstate = off; @@ -18,8 +18,6 @@ void initBattery(void) #ifdef VBATTPIN #ifdef MEAS_EN battery.onDemand(MEAS_EN, LOW); - //pinMode(MEAS_EN, OUTPUT); - //digitalWrite(MEAS_EN, HIGH); battery.begin(VBATTREF, (R12 + R13) / R13); //R1 = 220K, R2 = 100K, factor = (R1+R2)/R2 #else @@ -59,17 +57,24 @@ void powerOff(void) digitalWrite(PW_HOLD, LOW); } +bool measureBattery(void) +{ + uint16_t vbatt = battery.voltage(); + if (vbatt < VBATTMIN) + { + return true; + } + return false; +} + bool handleBattery(void) { #ifdef VBATTPIN uint64_t currentmillis = millis(); - if (currentmillis - delay_timer > BATTERYMEASUREDELAY) + if (currentmillis - measure_timer > BATTERYMEASUREDELAY) { - uint32_t vbatt = battery.voltage(); - if (vbatt < VBATTMIN) - { - return true; - } + return measureBattery(); + measure_timer = currentmillis; } #endif @@ -126,11 +131,11 @@ void handlePowerState(void) turnOnLed(1); powerstate = poweringOn2; } - else if (buttonPower.pressedFor(800)) + else if (buttonPower.pressedFor(500)) { turnOnLed(2); } - else if (buttonPower.pressedFor(100)) + else if (buttonPower.pressedFor(200)) { turnOnLed(3); } @@ -148,10 +153,14 @@ void handlePowerState(void) powerstate = on; powerOn(); turnOffAllLed(); - delay(500); + delay(200); batterydisplay(); - delay(1000); + delay(500); turnOffAllLed(); + if(measureBattery()) + { + powerstate = lowBatt; + } } } break; @@ -161,14 +170,17 @@ void handlePowerState(void) { powerstate = poweringOff; turnOnAllLed(); + break; } - else if (HandleTimeOut()) + if (HandleTimeOut()) { powerstate = timeOut; + break; } - else if (handleBattery()) + if (handleBattery()) { powerstate = lowBatt; + break; } } break; @@ -179,11 +191,11 @@ void handlePowerState(void) turnOffLed(1); powerstate = poweringOff2; } - else if (buttonPower.pressedFor(950)) + else if (buttonPower.pressedFor(500)) { turnOffLed(2); } - else if (buttonPower.pressedFor(450)) + else if (buttonPower.pressedFor(200)) { turnOffLed(3); } @@ -196,7 +208,7 @@ void handlePowerState(void) break; case poweringOff2: { - if(!buttonread) + if (!buttonread) { powerstate = off; } @@ -224,13 +236,15 @@ void handlePowerState(void) } } +uint64_t lasttimeOut = 0; +bool buttonChanged = false; +uint8_t buttonCount = 0; + //handleTimeout //return true when timed out bool HandleTimeOut(void) { uint64_t currentmillis = millis(); - static uint64_t lasttimeOut = 0; - static bool buttonChanged = false; if (!lasttimeOut) { @@ -245,12 +259,18 @@ bool HandleTimeOut(void) } else { - if (buttonChanged != anybutton()) + if (buttonPressedCount() != buttonCount) { - buttonChanged = anybutton(); - //game in progress, update timer + buttonCount = buttonPressedCount(); lasttimeOut = currentmillis; } + + // if (buttonChanged != anybutton()) + // { + // buttonChanged = anybutton(); + // //game in progress, update timer + // lasttimeOut = currentmillis; + // } } return false; } diff --git a/src/power.h b/src/power.h index 16aaa24..57e47ac 100644 --- a/src/power.h +++ b/src/power.h @@ -13,8 +13,8 @@ #include "Battery.h" #endif -#define POWERBUTTONDELAY 1500 -#define BATTERYMEASUREDELAY 5000 +#define POWERBUTTONDELAY 1000 +#define BATTERYMEASUREDELAY 60000 typedef enum { diff --git a/src/simpleled.cpp b/src/simpleled.cpp index 6acdcaf..37de7a0 100644 --- a/src/simpleled.cpp +++ b/src/simpleled.cpp @@ -1,11 +1,11 @@ #include "simpleled.h" -extern std::vector buttonlist; + void c_simpleLed::runGame(void) { - for (auto &&button : buttonlist) + for (auto &&button : *buttonlist) { if (button->isPressed()) { @@ -18,8 +18,14 @@ void c_simpleLed::runGame(void) } } -void c_simpleLed::initGame(void) +bool c_simpleLed::initGame(void) { + buttonlist = getButtonlist(); + if(buttonlist == NULL) + { + return false; + } + return true; //no init required } diff --git a/src/simpleled.h b/src/simpleled.h index dc9db85..8265b4c 100644 --- a/src/simpleled.h +++ b/src/simpleled.h @@ -1,18 +1,16 @@ -#ifndef SIMPLELEDH -#define SIMPLELEDH +#pragma once #include "led.h" #include "buttons.h" #include "vector" #include "game.h" - class c_simpleLed : public c_game { + std::vector *buttonlist; + public: - c_simpleLed(): c_game{simpleled} {} + c_simpleLed(e_ledcolor gamecolor): c_game{simpleled, gamecolor} {} void runGame(void); - void initGame(void); + bool initGame(void); void resetGame(void); }; - -#endif //SIMPLELEDH \ No newline at end of file