modified games
This commit is contained in:
45
.vscode/settings.json
vendored
45
.vscode/settings.json
vendored
@@ -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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,28 +4,31 @@
|
|||||||
#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 )
|
|
||||||
{
|
{
|
||||||
thisbutton->begin();
|
thisbutton->begin();
|
||||||
buttonlist.push_back(thisbutton);
|
buttonlist.push_back(thisbutton);
|
||||||
}
|
}
|
||||||
|
|
||||||
void initButtons( void )
|
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,22 +50,54 @@ bool anybutton(void)
|
|||||||
return false;
|
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 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 button;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<c_button *> *getButtonlist(void)
|
||||||
|
{
|
||||||
|
return &buttonlist;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -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;
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
121
src/led.cpp
121
src/led.cpp
@@ -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);
|
||||||
@@ -37,16 +78,45 @@ 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
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)
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
//#############################################
|
//#############################################
|
||||||
@@ -109,7 +204,6 @@ uint16_t c_ledport::ledRead(void)
|
|||||||
return analogRead(_analogPin);
|
return analogRead(_analogPin);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//#############################################
|
//#############################################
|
||||||
//# c_led functions #
|
//# c_led functions #
|
||||||
//#############################################
|
//#############################################
|
||||||
@@ -128,7 +222,16 @@ bool c_led::checkThreshold(uint16_t value)
|
|||||||
|
|
||||||
bool c_led::checkcolor(e_ledcolor color)
|
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 true;
|
||||||
}
|
}
|
||||||
|
|||||||
63
src/led.h
63
src/led.h
@@ -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
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -22,18 +25,19 @@ class c_ledport
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
c_ledport(uint32_t pin, uint32_t analogpin, uint16_t index, bool invert = false)
|
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
|
class c_led
|
||||||
@@ -42,13 +46,13 @@ class c_led
|
|||||||
const uint16_t _value;
|
const uint16_t _value;
|
||||||
const uint16_t _index;
|
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)
|
bool checkThreshold(uint16_t value);
|
||||||
:_color(color), _value(value), _index(index) {};
|
|
||||||
|
|
||||||
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
|
||||||
@@ -56,23 +60,36 @@ class c_leds
|
|||||||
std::vector<c_ledport> v_ledports;
|
std::vector<c_ledport> v_ledports;
|
||||||
std::vector<c_led> v_leds;
|
std::vector<c_led> v_leds;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
c_leds(){};
|
c_leds(){};
|
||||||
|
|
||||||
void AddLed( uint32_t pin, uint32_t analogpin, uint16_t index, uint16_t value, e_ledcolor color, bool invert );
|
void init();
|
||||||
void begin( void );
|
|
||||||
|
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 turnOnLed(e_ledcolor color);
|
||||||
void turnOffLed(e_ledcolor color);
|
|
||||||
|
|
||||||
void turnOnLed(uint16_t index);
|
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
|
#endif //LEDH
|
||||||
144
src/main.cpp
144
src/main.cpp
@@ -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
28
src/simpleled.cpp
Normal 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
8
src/simpleled.h
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#ifndef SIMPLELEDH
|
||||||
|
#define SIMPLELEDH
|
||||||
|
|
||||||
|
void initSimpleLed( void );
|
||||||
|
void handleSimpleLed( void );
|
||||||
|
|
||||||
|
|
||||||
|
#endif //SIMPLELEDH
|
||||||
Reference in New Issue
Block a user