updated to gameclass merged with v1.1 power

This commit is contained in:
2021-05-09 22:07:01 +02:00
parent f19e7b79f4
commit ef9d84b09d
12 changed files with 378 additions and 297 deletions

View File

@@ -4,12 +4,12 @@
std::vector<c_button *> 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)
{

View File

@@ -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);

View File

@@ -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

View File

@@ -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

View File

@@ -2,53 +2,221 @@
#include <vector>
#include "simpleled.h"
#include "magicSwitchBoard.h"
#include "chainGame.h"
std::vector<c_game *> gameslist;
uint64_t GameSelectTimer = 0;
e_state currentState = state_init;
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);
}
}
void HandleGameSelectTimeout(void)
{
uint64_t currentmillis = millis();
// yellow && red && green all on
if (allButtons())
{
//all buttons pressed, wait for next game
if (!GameSelectTimer)
{
GameSelectTimer = currentmillis;
}
else
{
//check timeout
if (currentmillis - GameSelectTimer > GAMESELECTTIMEOUT)
{
currentState = state_idle;
GameSelectTimer = 0;
}
}
}
else
{
//no gameselect sequence initiated
GameSelectTimer = currentmillis;
}
}
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))
{
//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);
}
//wait for all buttons to be switched off
if (!anybutton())
{
activateGame(nextGame);
currentState = state_play;
}
}
void setGameState(e_state newstate)
{
currentState = state_play;
}
e_state getCurrentState( void )
{
return currentState;
}
void handleGames( void )
{
HandleGameSelectTimeout();
switch (getCurrentState())
{
case state_idle:
{
HandleGameIdle();
}
break;
case state_play:
{
runGames();
}
break;
case state_init:
{
delay(1000);
turnOffAllLed();
activateGame(simpleled);
if (buttonIsPressed(GREEN))
{
activateGame(chaingame);
turnOnLed(GREEN);
}
if (buttonIsPressed(RED))
{
activateGame(magicswitchboard);
turnOnLed(RED);
}
if (buttonIsPressed(YELLOW))
{
activateGame(simpleled);
turnOnLed(YELLOW);
}
//wait for all buttons idle
while (anybutton())
{
}
turnOffAllLed();
setGameState(state_play);
}
break;
}
}

View File

@@ -1,12 +1,15 @@
#ifndef GAMEH
#define GAMEH
#define GAMESELECTTIMEOUT 10000 // 10sec * 1000ms
typedef enum
{
none,
simpleled,
chaingame,
magicswitchboard,
detectled
magicswitchboard
} e_game;
typedef enum
@@ -14,22 +17,44 @@ typedef enum
disabled,
init,
active
} e_state;
} e_gamestate;
typedef enum
{
state_init,
state_idle,
state_play
}e_state;
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);
//void HandleGameSelectTimeout(void);
//void HandleGameIdle( void );
//void setGameState(e_state newstate);
//e_state getCurrentState( void );
void handleGames();
#endif //GAMEH

View File

@@ -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();
}

View File

@@ -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);

View File

@@ -1,202 +1,26 @@
#ifndef UNIT_TEST
#include "Arduino.h"
#include "board.h"
#include "chainGame.h"
#include "detectled.h"
#include "magicSwitchBoard.h"
#include "simpleled.h"
#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;
game currentGame = none;
game nextGame = none;
bool newstate = false;
uint8_t gameState = 0;
uint64_t GameSelectTimer = 0;
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))
{
//prepare for next game
nextGame = ChainGame;
ResetChainGame();
turnOffLed(YELLOW);
}
//red button first released
if (buttonIsPressed(YELLOW) && !buttonIsPressed(RED) && buttonIsPressed(GREEN) & (nextGame == none))
{
//prepare for next game
nextGame = magicSwitchBoard;
turnOffLed(RED);
}
//green button first released
if (buttonIsPressed(YELLOW) && buttonIsPressed(RED) && !buttonIsPressed(GREEN) & (nextGame == none))
{
//prepare for next game
nextGame = SimpleLed;
turnOffLed(GREEN);
}
//wait for all buttons to be switched off
if (!anybutton())
{
setNewState(nextGame);
nextGame = none;
}
}
void HandleGameSelectTimeout(void)
{
uint64_t currentmillis = millis();
// yellow && red && green all on
if (buttonIsPressed(YELLOW) && buttonIsPressed(RED) && buttonIsPressed(GREEN))
{
//all buttons pressed, wait for next game
if (!GameSelectTimer)
{
GameSelectTimer = currentmillis;
}
else
{
//check timeout
if (currentmillis - GameSelectTimer > GAMESELECTTIMEOUT)
{
setNewState(idle);
GameSelectTimer = 0;
}
}
}
else
{
//no gameselect sequence initiated
GameSelectTimer = currentmillis;
}
}
void setup()
{
initLeds();
initButtons();
initPower();
initDetectLed();
initMagicSwitchBoard();
initSimpleLed();
initGames();
}
void loop()
{
handleButtons();
HandlePower();
if(getPowerState() != on)
if(getPowerState() == on)
{
return;
handleGames();
}
HandleGameSelectTimeout();
switch (currentGame)
{
case idle:
{
HandleIdle(newstate);
newstate = false;
}
break;
case SimpleLed:
{
handleSimpleLed(newstate);
newstate = false;
}
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:
{
delay(1000);
turnOffAllLed();
if (buttonIsPressed(GREEN) && currentGame == none)
{
setNewState(ChainGame);
turnOnLed(GREEN);
}
if (buttonIsPressed(RED) && currentGame == none)
{
setNewState(magicSwitchBoard);
turnOnLed(RED);
}
if (buttonIsPressed(YELLOW) && currentGame == none)
{
setNewState(SimpleLed);
turnOnLed(YELLOW);
}
if (currentGame == none)
{
setNewState(SimpleLed);
}
//wait for all buttons idle
while (anybutton())
{
}
turnOffAllLed();
}
break;
}
}
#endif
}

View File

@@ -17,8 +17,9 @@ void initBattery(void)
{
#ifdef VBATTPIN
#ifdef MEAS_EN
battery.onDemand(MEAS_EN, LOW);
battery.begin(VBATTREF, (R12 + R13) / R13); //R1 = 220K, R2 = 100K, factor = (R1+R2)/R2
battery.onDemand(MEAS_EN, HIGH);
#else
battery.begin(VBATTREF, (R12 + R13) / R13); //R1 = 220K, R2 = 100K, factor = (R1+R2)/R2
#endif
@@ -62,9 +63,11 @@ bool handleBattery(void)
uint64_t currentmillis = millis();
if (currentmillis - delay_timer > BATTERYMEASUREDELAY)
{
// uint16_t vbatt = battery.voltage();
// if (vbatt < VBATTMIN)
// {
uint16_t vbatt = battery.voltage();
if (vbatt < VBATTMIN)
{
//turnOnAllLed();
}
delay_timer = currentmillis;
// delay(5000);
@@ -125,11 +128,11 @@ void handlePowerState(void)
turnOnLed(1);
powerstate = poweringOn2;
}
else if (buttonPower.pressedFor(1000))
else if (buttonPower.pressedFor(800))
{
turnOnLed(2);
}
else if (buttonPower.pressedFor(500))
else if (buttonPower.pressedFor(100))
{
turnOnLed(3);
}
@@ -152,7 +155,7 @@ void handlePowerState(void)
break;
case on:
{
if (buttonPower.pressedFor(500))
if (buttonPower.pressedFor(100))
{
powerstate = poweringOff;
turnOnAllLed();
@@ -169,16 +172,16 @@ void handlePowerState(void)
break;
case poweringOff:
{
if (buttonPower.pressedFor(3000))
if (buttonPower.pressedFor(POWERBUTTONDELAY))
{
turnOffLed(1);
powerstate = poweringOff2;
}
else if (buttonPower.pressedFor(2000))
else if (buttonPower.pressedFor(950))
{
turnOffLed(2);
}
else if (buttonPower.pressedFor(1000))
else if (buttonPower.pressedFor(450))
{
turnOffLed(3);
}

View File

@@ -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<c_button *> 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
}

View File

@@ -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