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": { "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,13 +4,14 @@
#include "Arduino.h" #include "Arduino.h"
#include "JC_Button.h" #include "JC_Button.h"
std::vector<c_button *> buttonlist; std::vector<c_button *> buttonlist;
c_button button1(SWITCH1, YELLOW); c_button button1(SWITCH1, YELLOW, 1);
c_button button2(SWITCH2, RED); c_button button2(SWITCH2, RED, 2);
c_button button3(SWITCH3, GREEN); 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)
{ {
@@ -23,9 +24,11 @@ void initButtons( void )
buttonbegin(&button1); buttonbegin(&button1);
buttonbegin(&button2); buttonbegin(&button2);
buttonbegin(&button3); buttonbegin(&button3);
buttonbegin(&button4);
buttonbegin(&button5);
buttonbegin(&button6);
} }
void handleButtons(void) void handleButtons(void)
{ {
for (auto &&i : buttonlist) for (auto &&i : buttonlist)
@@ -47,6 +50,32 @@ bool anybutton(void)
return false; return false;
} }
bool buttonIsPressed(e_ledcolor index)
{
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) c_button *getButton(unsigned int index)
{ {
if (index > buttonlist.size()) if (index > buttonlist.size())
@@ -65,4 +94,10 @@ c_button* getButton(e_ledcolor color)
return button; return button;
} }
} }
return NULL;
}
std::vector<c_button *> *getButtonlist(void)
{
return &buttonlist;
} }

View File

@@ -9,10 +9,14 @@
class c_button : public ToggleButton class c_button : public ToggleButton
{ {
const e_ledcolor _color; const e_ledcolor _color;
const uint8_t _index;
public: 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;} e_ledcolor getColor( void ){return _color;}
uint8_t getIndex( void ) {return _index;}
}; };
@@ -20,5 +24,12 @@ bool anybutton(void);
void initButtons(void); void initButtons(void);
void handleButtons(void); void handleButtons(void);
c_button *getButton(unsigned int index); 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 #endif //BUTTONSH

View File

@@ -1,22 +1,30 @@
#include "chainGame.h" #include "chainGame.h"
#include "Arduino.h" #include "Arduino.h"
//#include "JC_Button.h"
#include "buttons.h" #include "buttons.h"
extern c_button button1; // extern c_button button1;
extern c_button button2; // extern c_button button2;
extern c_button button3; // extern c_button button3;
uint8_t patternIndex = 0; uint8_t patternIndex = 0;
uint8_t nextPatternIndex = 1;
bool patternFlag = false; bool patternFlag = false;
bool firstpattern = false; bool firstpattern = false;
uint8_t ledpattern[4][3] = { // uint8_t ledpattern[4][3] = {
{1, 0, 0}, // {1, 0, 0},
{0, 0, 1}, // {0, 0, 1},
{1, 0, 0}, // {1, 0, 0},
{0, 1, 0}, // {0, 1, 0},
}; // };
e_ledcolor ledpattern[4] =
{
YELLOW,
GREEN,
YELLOW,
RED};
int patternlength = sizeof(ledpattern) / sizeof(ledpattern[0]); int patternlength = sizeof(ledpattern) / sizeof(ledpattern[0]);
void nextPattern(void) void nextPattern(void)
@@ -33,56 +41,75 @@ void nextPattern(void)
void HandleChainGame(void) void HandleChainGame(void)
{ {
if ((button1.isPressed()) | (button3.isPressed())) if (buttonIsPressed(ledpattern[patternIndex]))
{ {
if (!patternFlag) turnOnLed(ledpattern[patternIndex]);
{
//button detected, increase pattern
if (!firstpattern)
{
firstpattern = true;
}
else
{
nextPattern();
}
patternFlag = true; patternFlag = true;
} }
} else if (!buttonIsPressed(ledpattern[patternIndex]))
if (button2.isPressed())
{ {
if (!patternFlag) 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; patternFlag = false;
nextPattern();
} }
turnOffLed(ledpattern[patternIndex]);
}
else
{
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; 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 # //# 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) 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); c_ledport port(pin, analogpin, index, invert);
@@ -36,15 +77,44 @@ void c_leds::begin(void)
} }
void c_leds::turnOnLed(e_ledcolor color) void c_leds::turnOnLed(e_ledcolor color)
{
if (verifyLed(color))
{ {
getLed(color)->turnOn(); getLed(color)->turnOn();
} }
}
void c_leds::turnOnLed(uint16_t index)
{
if (verifyLed(index))
{
getLed(index)->turnOn();
}
}
void c_leds::turnOffLed(e_ledcolor color) void c_leds::turnOffLed(e_ledcolor color)
{
if (verifyLed(color))
{ {
getLed(color)->turnOff(); getLed(color)->turnOff();
} }
}
void c_leds::turnOffLed(uint16_t index)
{
if (verifyLed(index))
{
getLed(index)->turnOff();
}
}
void c_leds::turnAllOff(void)
{
for (auto &&port : v_ledports)
{
port.turnOff();
}
}
c_ledport *c_leds::getLed(e_ledcolor color) c_ledport *c_leds::getLed(e_ledcolor color)
{ {
@@ -59,19 +129,44 @@ c_ledport* c_leds::getLed( e_ledcolor color )
} }
} }
} }
return NULL;
} }
void c_leds::turnOnLed(uint16_t index) c_ledport *c_leds::getLed(uint16_t index)
{
}
void c_leds::turnAllOff(void)
{ {
for (auto &&port : v_ledports) 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;
}
//############################################# //#############################################
//# c_ledport functions # //# c_ledport functions #
@@ -109,7 +204,6 @@ uint16_t c_ledport::ledRead(void)
return analogRead(_analogPin); return analogRead(_analogPin);
} }
//############################################# //#############################################
//# c_led functions # //# c_led functions #
//############################################# //#############################################
@@ -134,3 +228,12 @@ bool c_led::checkcolor(e_ledcolor color)
} }
return false; 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 "arduino.h"
#include "vector" #include "vector"
enum e_ledcolor
enum e_ledcolor{ {
YELLOW,
RED, RED,
GREEN, GREEN,
YELLOW, YELLOW2,
RED2,
GREEN2,
NONE NONE
}; };
@@ -34,6 +37,7 @@ public:
uint16_t ledRead(void); uint16_t ledRead(void);
uint16_t getIndex(void) { return _index; }
}; };
class c_led class c_led
@@ -43,12 +47,12 @@ class c_led
const uint16_t _index; const uint16_t _index;
public: public:
c_led(e_ledcolor color, uint16_t value, uint16_t index) c_led(e_ledcolor color, uint16_t value, uint16_t index)
: _color(color), _value(value), _index(index){}; : _color(color), _value(value), _index(index){};
bool checkThreshold(uint16_t value); bool checkThreshold(uint16_t value);
bool checkcolor(e_ledcolor color); bool checkcolor(e_ledcolor color);
bool checkIndex(uint16_t index);
}; };
class c_leds class c_leds
@@ -59,20 +63,33 @@ class c_leds
public: public:
c_leds(){}; c_leds(){};
void init();
void AddLed(uint32_t pin, uint32_t analogpin, uint16_t index, uint16_t value, e_ledcolor color, bool invert); void AddLed(uint32_t pin, uint32_t analogpin, uint16_t index, uint16_t value, e_ledcolor color, bool invert);
void begin(void); void begin(void);
void turnOnLed(e_ledcolor color); void turnOnLed(e_ledcolor color);
void turnOffLed(e_ledcolor color);
void turnOnLed(uint16_t index); void turnOnLed(uint16_t index);
void turnOffLed(e_ledcolor color);
void turnOffLed(uint16_t index);
c_ledport *getLed(e_ledcolor color); 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); void turnAllOff(void);
}; };
c_leds *getledlist(void); c_leds *getledlist(void);
void initLeds(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 #endif //LEDH

View File

@@ -1,22 +1,21 @@
#include <Arduino.h> #include <Arduino.h>
//#include "buttons.h"
#include "board.h" #include "board.h"
#include "chainGame.h" #include "chainGame.h"
#include "detectled.h" #include "detectled.h"
#include "magicSwitchBoard.h" #include "magicSwitchBoard.h"
#include "simpleled.h"
#include "buttons.h" #include "buttons.h"
//#include "JC_Button.h"
#include "led.h" #include "led.h"
#define TIMEOUT 15000 // 15sec * 1000ms
extern c_button button1; #define GAMESELECTTIMEOUT 7000 // 7sec * 1000ms
extern c_button button2;
extern c_button button3;
typedef enum typedef enum
{ {
none, none,
sleep, sleep,
idle,
SimpleLed,
ChainGame, ChainGame,
magicSwitchBoard, magicSwitchBoard,
detectLED, detectLED,
@@ -24,34 +23,114 @@ typedef enum
} game; } game;
game currentGame = none; game currentGame = none;
game nextGame = none;
uint8_t gameState = 0; 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() void setup()
{ {
pinMode(LED1, OUTPUT); initLeds();
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();
initButtons(); initButtons();
initDetectLed(); initDetectLed();
initSimpleLed();
} }
void loop() void loop()
{ {
handleButtons(); handleButtons();
//handleSystemTimeout(); //HandleTimeOut();
HandleGameSelectTimeout();
switch (currentGame) switch (currentGame)
{ {
case idle:
{
HandleIdle();
}
break;
case SimpleLed:
{
handleSimpleLed();
}
break;
case magicSwitchBoard: case magicSwitchBoard:
{ {
handleMagicSwitchBoard(); handleMagicSwitchBoard();
@@ -73,30 +152,29 @@ void loop()
case none: case none:
{ {
currentGame = ChainGame; currentGame = SimpleLed;
digitalWrite(LED1, 0); if (buttonIsPressed(YELLOW))
digitalWrite(LED2, 0);
digitalWrite(LED3, 0);
if (button1.isPressed())
{ {
digitalWrite(LED1, 1); currentGame = ChainGame;
turnOnLed(YELLOW);
} }
if (button2.isPressed()) if (buttonIsPressed(RED))
{ {
currentGame = magicSwitchBoard; currentGame = magicSwitchBoard;
digitalWrite(LED2, 1); turnOnLed(RED);
} }
if (button3.isPressed()) if (buttonIsPressed(GREEN))
{ {
digitalWrite(LED3, 1);
currentGame = detectLED; currentGame = detectLED;
turnOnLed(GREEN);
} }
while (anybutton()); while (anybutton())
;
turnOffAllLed();
} }
break; 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