updated to gameclass merged with v1.1 power
This commit is contained in:
@@ -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)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
|
||||||
|
|||||||
@@ -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
|
||||||
190
src/game.cpp
190
src/game.cpp
@@ -2,53 +2,221 @@
|
|||||||
#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;
|
||||||
|
uint64_t GameSelectTimer = 0;
|
||||||
|
e_state currentState = state_init;
|
||||||
|
|
||||||
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
41
src/game.h
41
src/game.h
@@ -1,12 +1,15 @@
|
|||||||
#ifndef GAMEH
|
#ifndef GAMEH
|
||||||
#define GAMEH
|
#define GAMEH
|
||||||
|
|
||||||
|
#define GAMESELECTTIMEOUT 10000 // 10sec * 1000ms
|
||||||
|
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
|
none,
|
||||||
simpleled,
|
simpleled,
|
||||||
chaingame,
|
chaingame,
|
||||||
magicswitchboard,
|
magicswitchboard
|
||||||
detectled
|
|
||||||
} e_game;
|
} e_game;
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
@@ -14,22 +17,44 @@ typedef enum
|
|||||||
disabled,
|
disabled,
|
||||||
init,
|
init,
|
||||||
active
|
active
|
||||||
} e_state;
|
} e_gamestate;
|
||||||
|
|
||||||
|
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
state_init,
|
||||||
|
state_idle,
|
||||||
|
state_play
|
||||||
|
}e_state;
|
||||||
|
|
||||||
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);
|
||||||
|
//void HandleGameSelectTimeout(void);
|
||||||
|
//void HandleGameIdle( void );
|
||||||
|
//void setGameState(e_state newstate);
|
||||||
|
//e_state getCurrentState( void );
|
||||||
|
void handleGames();
|
||||||
|
|
||||||
|
|
||||||
#endif //GAMEH
|
#endif //GAMEH
|
||||||
@@ -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();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
184
src/main.cpp
184
src/main.cpp
@@ -1,202 +1,26 @@
|
|||||||
#ifndef UNIT_TEST
|
|
||||||
|
|
||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
#include "board.h"
|
#include "board.h"
|
||||||
#include "chainGame.h"
|
|
||||||
#include "detectled.h"
|
|
||||||
#include "magicSwitchBoard.h"
|
|
||||||
#include "simpleled.h"
|
|
||||||
#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
|
|
||||||
|
|
||||||
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()
|
void setup()
|
||||||
{
|
{
|
||||||
initLeds();
|
initLeds();
|
||||||
initButtons();
|
initButtons();
|
||||||
initPower();
|
initPower();
|
||||||
|
initGames();
|
||||||
initDetectLed();
|
|
||||||
initMagicSwitchBoard();
|
|
||||||
initSimpleLed();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
handleButtons();
|
handleButtons();
|
||||||
HandlePower();
|
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
|
|
||||||
@@ -17,8 +17,9 @@ void initBattery(void)
|
|||||||
{
|
{
|
||||||
#ifdef VBATTPIN
|
#ifdef VBATTPIN
|
||||||
#ifdef MEAS_EN
|
#ifdef MEAS_EN
|
||||||
|
battery.onDemand(MEAS_EN, LOW);
|
||||||
battery.begin(VBATTREF, (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
|
||||||
battery.onDemand(MEAS_EN, HIGH);
|
|
||||||
#else
|
#else
|
||||||
battery.begin(VBATTREF, (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
|
#endif
|
||||||
@@ -62,9 +63,11 @@ bool handleBattery(void)
|
|||||||
uint64_t currentmillis = millis();
|
uint64_t currentmillis = millis();
|
||||||
if (currentmillis - delay_timer > BATTERYMEASUREDELAY)
|
if (currentmillis - delay_timer > BATTERYMEASUREDELAY)
|
||||||
{
|
{
|
||||||
// uint16_t vbatt = battery.voltage();
|
uint16_t vbatt = battery.voltage();
|
||||||
// if (vbatt < VBATTMIN)
|
if (vbatt < VBATTMIN)
|
||||||
// {
|
{
|
||||||
|
//turnOnAllLed();
|
||||||
|
}
|
||||||
|
|
||||||
delay_timer = currentmillis;
|
delay_timer = currentmillis;
|
||||||
// delay(5000);
|
// delay(5000);
|
||||||
@@ -125,11 +128,11 @@ void handlePowerState(void)
|
|||||||
turnOnLed(1);
|
turnOnLed(1);
|
||||||
powerstate = poweringOn2;
|
powerstate = poweringOn2;
|
||||||
}
|
}
|
||||||
else if (buttonPower.pressedFor(1000))
|
else if (buttonPower.pressedFor(800))
|
||||||
{
|
{
|
||||||
turnOnLed(2);
|
turnOnLed(2);
|
||||||
}
|
}
|
||||||
else if (buttonPower.pressedFor(500))
|
else if (buttonPower.pressedFor(100))
|
||||||
{
|
{
|
||||||
turnOnLed(3);
|
turnOnLed(3);
|
||||||
}
|
}
|
||||||
@@ -152,7 +155,7 @@ void handlePowerState(void)
|
|||||||
break;
|
break;
|
||||||
case on:
|
case on:
|
||||||
{
|
{
|
||||||
if (buttonPower.pressedFor(500))
|
if (buttonPower.pressedFor(100))
|
||||||
{
|
{
|
||||||
powerstate = poweringOff;
|
powerstate = poweringOff;
|
||||||
turnOnAllLed();
|
turnOnAllLed();
|
||||||
@@ -169,16 +172,16 @@ void handlePowerState(void)
|
|||||||
break;
|
break;
|
||||||
case poweringOff:
|
case poweringOff:
|
||||||
{
|
{
|
||||||
if (buttonPower.pressedFor(3000))
|
if (buttonPower.pressedFor(POWERBUTTONDELAY))
|
||||||
{
|
{
|
||||||
turnOffLed(1);
|
turnOffLed(1);
|
||||||
powerstate = poweringOff2;
|
powerstate = poweringOff2;
|
||||||
}
|
}
|
||||||
else if (buttonPower.pressedFor(2000))
|
else if (buttonPower.pressedFor(950))
|
||||||
{
|
{
|
||||||
turnOffLed(2);
|
turnOffLed(2);
|
||||||
}
|
}
|
||||||
else if (buttonPower.pressedFor(1000))
|
else if (buttonPower.pressedFor(450))
|
||||||
{
|
{
|
||||||
turnOffLed(3);
|
turnOffLed(3);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
}
|
}
|
||||||
@@ -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
|
||||||
Reference in New Issue
Block a user