modified games

This commit is contained in:
willem oldemans
2020-12-08 18:38:35 +01:00
parent 43fcb87efc
commit 32ae7afe35
9 changed files with 489 additions and 139 deletions

45
.vscode/settings.json vendored
View File

@@ -1,5 +1,48 @@
{
"files.associations": {
"vector": "cpp"
"vector": "cpp",
"*.tcc": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"unordered_map": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp"
}
}

View File

@@ -4,28 +4,31 @@
#include "Arduino.h"
#include "JC_Button.h"
std::vector<c_button *> buttonlist;
c_button button1(SWITCH1, YELLOW);
c_button button2(SWITCH2, RED);
c_button button3(SWITCH3, GREEN);
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);
void buttonbegin( c_button *thisbutton )
void buttonbegin(c_button *thisbutton)
{
thisbutton->begin();
buttonlist.push_back(thisbutton);
}
void initButtons( void )
void initButtons(void)
{
buttonbegin(&button1);
buttonbegin(&button2);
buttonbegin(&button3);
buttonbegin(&button4);
buttonbegin(&button5);
buttonbegin(&button6);
}
void handleButtons(void)
{
for (auto &&i : buttonlist)
@@ -47,22 +50,54 @@ bool anybutton(void)
return false;
}
c_button* getButton(unsigned int index)
bool buttonIsPressed(e_ledcolor index)
{
if(index > buttonlist.size())
c_button *thisbutton = getButton(index);
if (thisbutton == NULL)
{
return false;
}
else
{
return thisbutton->isPressed();
}
}
bool buttonWasPressed(e_ledcolor index)
{
c_button *thisbutton = getButton(index);
if (thisbutton == NULL)
{
return false;
}
else
{
return thisbutton->wasPressed();
}
}
c_button *getButton(unsigned int index)
{
if (index > buttonlist.size())
{
return NULL;
}
return buttonlist[index-1];
return buttonlist[index - 1];
}
c_button* getButton(e_ledcolor color)
c_button *getButton(e_ledcolor color)
{
for(auto&& button : buttonlist)
for (auto &&button : buttonlist)
{
if(button->getColor() == color)
if (button->getColor() == color)
{
return button;
}
}
return NULL;
}
std::vector<c_button *> *getButtonlist(void)
{
return &buttonlist;
}

View File

@@ -9,10 +9,14 @@
class c_button : public ToggleButton
{
const e_ledcolor _color;
const uint8_t _index;
public:
c_button(uint8_t pin, e_ledcolor color) : ToggleButton(pin), _color(color){}
c_button(uint8_t pin, e_ledcolor color, uint8_t index)
: ToggleButton(pin), _color(color), _index(index) {}
e_ledcolor getColor( void ){return _color;}
uint8_t getIndex( void ) {return _index;}
};
@@ -20,5 +24,12 @@ bool anybutton(void);
void initButtons(void);
void handleButtons(void);
c_button *getButton(unsigned int index);
c_button *getButton(e_ledcolor index);
std::vector<c_button *>* getButtonlist(void);
bool buttonIsPressed(e_ledcolor index);
bool buttonWasPressed(e_ledcolor index);
#endif //BUTTONSH

View File

@@ -1,22 +1,30 @@
#include "chainGame.h"
#include "Arduino.h"
//#include "JC_Button.h"
#include "buttons.h"
extern c_button button1;
extern c_button button2;
extern c_button button3;
// extern c_button button1;
// extern c_button button2;
// extern c_button button3;
uint8_t patternIndex = 0;
uint8_t nextPatternIndex = 1;
bool patternFlag = false;
bool firstpattern = false;
uint8_t ledpattern[4][3] = {
{1, 0, 0},
{0, 0, 1},
{1, 0, 0},
{0, 1, 0},
};
// uint8_t ledpattern[4][3] = {
// {1, 0, 0},
// {0, 0, 1},
// {1, 0, 0},
// {0, 1, 0},
// };
e_ledcolor ledpattern[4] =
{
YELLOW,
GREEN,
YELLOW,
RED};
int patternlength = sizeof(ledpattern) / sizeof(ledpattern[0]);
void nextPattern(void)
@@ -33,56 +41,75 @@ void nextPattern(void)
void HandleChainGame(void)
{
if ((button1.isPressed()) | (button3.isPressed()))
if (buttonIsPressed(ledpattern[patternIndex]))
{
if (!patternFlag)
{
//button detected, increase pattern
if (!firstpattern)
{
firstpattern = true;
}
else
{
nextPattern();
}
patternFlag = true;
}
turnOnLed(ledpattern[patternIndex]);
patternFlag = true;
}
if (button2.isPressed())
else if (!buttonIsPressed(ledpattern[patternIndex]))
{
if (!patternFlag)
if (patternFlag)
{
if (!firstpattern)
{
firstpattern = true;
nextPattern();
}
else
{
//second input, skip a pattern
nextPattern();
nextPattern();
}
patternFlag = true;
patternFlag = false;
nextPattern();
}
}
if (anybutton() && patternFlag)
{
//write pattern to the LEDs
digitalWrite(LED1, ledpattern[patternIndex][0]);
digitalWrite(LED2, ledpattern[patternIndex][1]);
digitalWrite(LED3, ledpattern[patternIndex][2]);
turnOffLed(ledpattern[patternIndex]);
}
else
{
//leds off
digitalWrite(LED1, 0);
digitalWrite(LED2, 0);
digitalWrite(LED3, 0);
patternFlag = false;
turnOffAllLed();
}
// if (buttonIsPressed(YELLOW) | buttonIsPressed(RED) | buttonIsPressed(GREEN))
// {
// if (!patternFlag)
// {
// //button detected, increase pattern
// if (!firstpattern)
// {
// firstpattern = true;
// }
// else
// {
// nextPattern();
// }
// patternFlag = true;
// }
// }
// if (buttonIsPressed(RED))
// {
// if (!patternFlag)
// {
// if (!firstpattern)
// {
// firstpattern = true;
// nextPattern();
// }
// else
// {
// //second input, skip a pattern
// nextPattern();
// nextPattern();
// }
// patternFlag = true;
// }
// }
// if (anybutton() && patternFlag)
// {
// //write pattern to the LEDs
// digitalWrite(LED1, ledpattern[patternIndex][0]);
// digitalWrite(LED2, ledpattern[patternIndex][1]);
// digitalWrite(LED3, ledpattern[patternIndex][2]);
// }
// else
// {
// //leds off
// digitalWrite(LED1, 0);
// digitalWrite(LED2, 0);
// digitalWrite(LED3, 0);
// patternFlag = false;
// }
}

View File

@@ -14,10 +14,51 @@ c_leds *getledlist(void)
return &ledlist;
}
void initLeds(void)
{
ledlist.init();
ledlist.AddLed(LED1, DETECT1, 1, 844, YELLOW, false);
ledlist.AddLed(LED2, DETECT2, 2, 512, RED, false);
ledlist.AddLed(LED3, DETECT3, 2, 92, GREEN, false);
ledlist.begin();
}
void turnOnLed(e_ledcolor color)
{
ledlist.turnOnLed(color);
}
void turnOffLed(uint16_t index)
{
ledlist.turnOffLed(index);
}
void turnOnLed(uint16_t index)
{
ledlist.turnOnLed(index);
}
void turnOffLed(e_ledcolor color)
{
ledlist.turnOffLed(color);
}
void turnOffAllLed()
{
ledlist.turnAllOff();
}
//#############################################
//# leds functions #
//#############################################
void c_leds::init(void)
{
v_ledports.clear();
v_leds.clear();
}
void c_leds::AddLed(uint32_t pin, uint32_t analogpin, uint16_t index, uint16_t value, e_ledcolor color, bool invert)
{
c_ledport port(pin, analogpin, index, invert);
@@ -37,16 +78,45 @@ void c_leds::begin(void)
void c_leds::turnOnLed(e_ledcolor color)
{
getLed(color)->turnOn();
if (verifyLed(color))
{
getLed(color)->turnOn();
}
}
void c_leds::turnOnLed(uint16_t index)
{
if (verifyLed(index))
{
getLed(index)->turnOn();
}
}
void c_leds::turnOffLed(e_ledcolor color)
{
getLed(color)->turnOff();
if (verifyLed(color))
{
getLed(color)->turnOff();
}
}
void c_leds::turnOffLed(uint16_t index)
{
if (verifyLed(index))
{
getLed(index)->turnOff();
}
}
c_ledport* c_leds::getLed( e_ledcolor color )
void c_leds::turnAllOff(void)
{
for (auto &&port : v_ledports)
{
port.turnOff();
}
}
c_ledport *c_leds::getLed(e_ledcolor color)
{
for (auto &&port : v_ledports)
{
@@ -59,18 +129,43 @@ c_ledport* c_leds::getLed( e_ledcolor color )
}
}
}
return NULL;
}
void c_leds::turnOnLed(uint16_t index)
{
}
void c_leds::turnAllOff(void)
c_ledport *c_leds::getLed(uint16_t index)
{
for (auto &&port : v_ledports)
{
port.turnOff();
if (port.getIndex() == index)
{
return &port;
}
}
return NULL;
}
bool c_leds::verifyLed(e_ledcolor color)
{
for (auto &&thisled : v_leds)
{
if (thisled.checkcolor(color))
{
return true;
}
}
return false;
}
bool c_leds::verifyLed(uint16_t index)
{
for (auto &&thisled : v_leds)
{
if (thisled.checkIndex(index))
{
return true;
}
}
return false;
}
//#############################################
@@ -109,7 +204,6 @@ uint16_t c_ledport::ledRead(void)
return analogRead(_analogPin);
}
//#############################################
//# c_led functions #
//#############################################
@@ -128,9 +222,18 @@ bool c_led::checkThreshold(uint16_t value)
bool c_led::checkcolor(e_ledcolor color)
{
if(_color == color)
if (_color == color)
{
return true;
}
return false;
}
}
bool c_led::checkIndex(uint16_t index)
{
if (_index == index)
{
return true;
}
return false;
}

View File

@@ -4,11 +4,14 @@
#include "arduino.h"
#include "vector"
enum e_ledcolor{
enum e_ledcolor
{
YELLOW,
RED,
GREEN,
YELLOW,
YELLOW2,
RED2,
GREEN2,
NONE
};
@@ -22,18 +25,19 @@ class c_ledport
public:
c_ledport(uint32_t pin, uint32_t analogpin, uint16_t index, bool invert = false)
:_pin(pin), _analogPin(analogpin), _index(index), _invert(invert){};
: _pin(pin), _analogPin(analogpin), _index(index), _invert(invert){};
void begin( void );
void begin(void);
void turnOn( void );
void turnOn(void);
void turnOff( void );
void turnOff(void);
void writeLed( bool state );
void writeLed(bool state);
uint16_t ledRead( void );
uint16_t ledRead(void);
uint16_t getIndex(void) { return _index; }
};
class c_led
@@ -42,37 +46,50 @@ class c_led
const uint16_t _value;
const uint16_t _index;
public:
public:
c_led(e_ledcolor color, uint16_t value, uint16_t index)
: _color(color), _value(value), _index(index){};
c_led(e_ledcolor color,uint16_t value, uint16_t index)
:_color(color), _value(value), _index(index) {};
bool checkThreshold( uint16_t value);
bool checkThreshold(uint16_t value);
bool checkcolor(e_ledcolor color);
bool checkIndex(uint16_t index);
};
class c_leds
{
std::vector<c_ledport> v_ledports;
std::vector<c_led> v_leds;
public:
public:
c_leds(){};
void AddLed( uint32_t pin, uint32_t analogpin, uint16_t index, uint16_t value, e_ledcolor color, bool invert );
void begin( void );
void init();
void AddLed(uint32_t pin, uint32_t analogpin, uint16_t index, uint16_t value, e_ledcolor color, bool invert);
void begin(void);
void turnOnLed(e_ledcolor color);
void turnOffLed(e_ledcolor color);
void turnOnLed(uint16_t index);
c_ledport* getLed(e_ledcolor color);
void turnOffLed(e_ledcolor color);
void turnOffLed(uint16_t index);
void turnAllOff( void );
c_ledport *getLed(e_ledcolor color);
c_ledport *getLed(uint16_t index);
bool verifyLed(e_ledcolor color);
bool verifyLed(uint16_t index);
void turnAllOff(void);
};
c_leds *getledlist(void);
void initLeds(void);
c_leds* getledlist( void );
void turnOnLed(e_ledcolor color);
void turnOffLed(e_ledcolor color);
void turnOffLed(uint16_t index);
void turnOnLed(uint16_t index);
void turnOffAllLed();
#endif //LEDH

View File

@@ -1,22 +1,21 @@
#include <Arduino.h>
//#include "buttons.h"
#include "board.h"
#include "chainGame.h"
#include "detectled.h"
#include "magicSwitchBoard.h"
#include "simpleled.h"
#include "buttons.h"
//#include "JC_Button.h"
#include "led.h"
extern c_button button1;
extern c_button button2;
extern c_button button3;
#define TIMEOUT 15000 // 15sec * 1000ms
#define GAMESELECTTIMEOUT 7000 // 7sec * 1000ms
typedef enum
{
none,
sleep,
idle,
SimpleLed,
ChainGame,
magicSwitchBoard,
detectLED,
@@ -24,34 +23,114 @@ typedef enum
} game;
game currentGame = none;
game nextGame = none;
uint8_t gameState = 0;
uint64_t lasttimeOut = 0;
uint64_t GameSelectTimer = 0;
void HandleIdle(void)
{
//green button first released
if (buttonIsPressed(YELLOW) && !buttonIsPressed(RED) && buttonIsPressed(GREEN) && (nextGame == none))
{
//prepare for next game
nextGame = ChainGame;
}
//yellow button first released
if (buttonIsPressed(YELLOW) && buttonIsPressed(RED) && !buttonIsPressed(GREEN) & (nextGame == none))
{
//prepare for next game
nextGame = magicSwitchBoard;
}
//wait for all buttons to be switched off
if (!anybutton())
{
currentGame = nextGame;
nextGame = none;
}
}
void HandleGameSelectTimeout(void)
{
uint64_t currentmillis = millis();
if(buttonIsPressed(YELLOW) && buttonIsPressed(RED) && buttonIsPressed(GREEN))
//if (yellow && red && green)
{
//all buttons pressed, wait for next game
if (!GameSelectTimer)
{
GameSelectTimer = currentmillis;
}
else
{
//check timeout
if (currentmillis - GameSelectTimer > GAMESELECTTIMEOUT)
{
currentGame = idle;
GameSelectTimer = 0;
}
}
}
else
{
//no gameselect sequence initiated
GameSelectTimer = currentmillis;
}
}
// void HandleTimeOut(void)
// {
// uint64_t currentmillis = millis();
// if (!lasttimeOut)
// {
// lasttimeOut = currentmillis;
// }
// //check if lastTime is initialized or timeout expired
// if ((currentmillis - lasttimeOut > TIMEOUT))
// {
// //handle timeout
// }
// else
// {
// if (anybutton())
// {
// //game in progress, update timer
// lasttimeOut = currentmillis;
// }
// }
// }
void setup()
{
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(SYS_WKUP3, INPUT);
c_leds *ledlist = getledlist();
ledlist->AddLed(LED1, DETECT1, 1, 844, YELLOW, false);
ledlist->AddLed(LED2, DETECT2, 2, 512, RED, false);
ledlist->AddLed(LED3, DETECT3, 2, 92, GREEN, false);
ledlist->begin();
initLeds();
initButtons();
initDetectLed();
initSimpleLed();
}
void loop()
{
handleButtons();
//handleSystemTimeout();
//HandleTimeOut();
HandleGameSelectTimeout();
switch (currentGame)
{
case idle:
{
HandleIdle();
}
break;
case SimpleLed:
{
handleSimpleLed();
}
break;
case magicSwitchBoard:
{
handleMagicSwitchBoard();
@@ -73,30 +152,29 @@ void loop()
case none:
{
currentGame = ChainGame;
digitalWrite(LED1, 0);
digitalWrite(LED2, 0);
digitalWrite(LED3, 0);
if (button1.isPressed())
currentGame = SimpleLed;
if (buttonIsPressed(YELLOW))
{
digitalWrite(LED1, 1);
currentGame = ChainGame;
turnOnLed(YELLOW);
}
if (button2.isPressed())
if (buttonIsPressed(RED))
{
currentGame = magicSwitchBoard;
digitalWrite(LED2, 1);
turnOnLed(RED);
}
if (button3.isPressed())
if (buttonIsPressed(GREEN))
{
digitalWrite(LED3, 1);
currentGame = detectLED;
turnOnLed(GREEN);
}
while (anybutton());
while (anybutton())
;
turnOffAllLed();
}
break;
}
}

28
src/simpleled.cpp Normal file
View File

@@ -0,0 +1,28 @@
#include "simpleled.h"
#include "led.h"
#include "buttons.h"
#include "vector"
extern std::vector<c_button *> buttonlist;
bool status = false;
void initSimpleLed(void)
{
status = true;
}
void handleSimpleLed(void)
{
for (auto &&button : buttonlist)
{
if (button->isPressed())
{
turnOnLed(button->getColor());
}
else
{
turnOffLed(button->getColor());
}
}
}

8
src/simpleled.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef SIMPLELEDH
#define SIMPLELEDH
void initSimpleLed( void );
void handleSimpleLed( void );
#endif //SIMPLELEDH