Compare commits
1 Commits
2e5caea417
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 54c326c784 |
@@ -19,9 +19,6 @@ lib_ldf_mode = deep+
|
||||
board = nucleo_l031K6
|
||||
build_flags =
|
||||
-DHARDWAREVERSION=11
|
||||
; -fexceptions
|
||||
; build_unflags =
|
||||
; -fno-exceptions
|
||||
|
||||
; [env:LedBoardV10]
|
||||
; board = STM32L031K6
|
||||
|
||||
@@ -16,118 +16,59 @@ void c_chaingame::nextPattern(void)
|
||||
|
||||
void c_chaingame::runGame(void)
|
||||
{
|
||||
switch (state)
|
||||
//check if we are idle
|
||||
if (!patternFlag && !cheatButtonFlag)
|
||||
{
|
||||
case cg_reset:
|
||||
{
|
||||
// clear all vars
|
||||
turnOnAllLed();
|
||||
cheatbutton = 0;
|
||||
playNextTimer = 0;
|
||||
patternIndex = 0;
|
||||
state = cg_idle;
|
||||
}
|
||||
break;
|
||||
case cg_idle:
|
||||
{
|
||||
updateCheatButton();
|
||||
turnOffAllLed();
|
||||
// wait for button
|
||||
|
||||
if (buttonIsPressed(ledpattern[patternIndex]) && (cheatbutton == 0))
|
||||
{
|
||||
state = cg_play;
|
||||
}
|
||||
// -> cg_cheat
|
||||
else if (buttonIsPressed(cheatbutton))
|
||||
{
|
||||
state = cg_cheat;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case cg_play:
|
||||
{
|
||||
// enable pattern LED and wait for all switches OFF
|
||||
if (buttonIsPressed(ledpattern[patternIndex]))
|
||||
{
|
||||
//pattern button pressed, turn on LED, set flag
|
||||
turnOnLed(ledpattern[patternIndex]);
|
||||
}
|
||||
else
|
||||
{
|
||||
turnOffLed(ledpattern[patternIndex]);
|
||||
}
|
||||
if (!anybutton())
|
||||
{
|
||||
// -> cg_play_next
|
||||
turnOffAllLed();
|
||||
playNextTimer = millis();
|
||||
state = cg_play_next;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case cg_play_next:
|
||||
{
|
||||
uint32_t timeNow = millis();
|
||||
if (anybutton())
|
||||
{
|
||||
// switched on again, return to cg_play state
|
||||
state = cg_play;
|
||||
}
|
||||
if (timeNow - playNextTimer > PLAYNEXTTIMEOUT)
|
||||
{
|
||||
// increase patterm counter after 3sec timeout -> cg_idle
|
||||
nextPattern();
|
||||
state = cg_idle;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case cg_cheat:
|
||||
{
|
||||
// enable CHEAT LED on
|
||||
if (buttonIsPressed(cheatbutton))
|
||||
{
|
||||
turnOnLed(cheatbutton);
|
||||
}
|
||||
else
|
||||
{
|
||||
turnOffLed(cheatbutton);
|
||||
}
|
||||
if (!anybutton())
|
||||
{
|
||||
turnOffAllLed();
|
||||
playNextTimer = millis();
|
||||
state = cg_cheat_next;
|
||||
}
|
||||
// wait for all switches off
|
||||
// -> cg_idle
|
||||
}
|
||||
break;
|
||||
case cg_cheat_next:
|
||||
{
|
||||
uint32_t timeNow = millis();
|
||||
if (anybutton())
|
||||
{
|
||||
// switched on again, return to cg_play state
|
||||
state = cg_cheat;
|
||||
}
|
||||
if (timeNow - playNextTimer > PLAYNEXTTIMEOUT)
|
||||
{
|
||||
// increase patterm counter after 3sec timeout -> cg_idle
|
||||
patternFlag = true;
|
||||
cheatbutton = 0;
|
||||
state = cg_idle;
|
||||
}
|
||||
else if (buttonIsPressed(cheatbutton))
|
||||
{
|
||||
// cheatbutton pressed, turn on cheat led, set flag
|
||||
turnOnLed(cheatbutton);
|
||||
cheatButtonFlag = true;
|
||||
}
|
||||
else if (anybutton())
|
||||
{
|
||||
// if any other button is pressed, clear cheat button
|
||||
//cheatbutton = 0;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
{
|
||||
state = cg_reset;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void c_chaingame::updateCheatButton(void)
|
||||
{
|
||||
// check cheatbuttons
|
||||
// check if the actual pattern switch is off
|
||||
if (!buttonIsPressed(ledpattern[patternIndex]))
|
||||
{
|
||||
// pattern switch is open, turn off pattern LED
|
||||
turnOffLed(ledpattern[patternIndex]);
|
||||
}
|
||||
|
||||
// check if the game is in cheat mode and cheat switch is off
|
||||
if (!buttonIsPressed(cheatbutton) && cheatButtonFlag)
|
||||
{
|
||||
// cheat switch is open, turn of cheat LED
|
||||
turnOffLed(cheatbutton);
|
||||
cheatButtonFlag = false;
|
||||
cheatbutton = 0;
|
||||
}
|
||||
|
||||
// if all siwtches are off turn all LEDs off
|
||||
if (!anybutton())
|
||||
{
|
||||
//all switches are open, turn off all LEDs
|
||||
turnOffAllLed();
|
||||
if (patternFlag)
|
||||
{
|
||||
// pattern LED was triggerd, reset flag, move to next pattern
|
||||
patternFlag = false;
|
||||
nextPattern();
|
||||
}
|
||||
}
|
||||
|
||||
//check cheatbuttons
|
||||
if (buttonIsPressed(4) && (cheatbutton == 0))
|
||||
{
|
||||
// cheatbutton 4 (momentary 1) was closed, set cheatbutton to 1
|
||||
@@ -144,13 +85,12 @@ void c_chaingame::updateCheatButton(void)
|
||||
cheatbutton = 3;
|
||||
}
|
||||
}
|
||||
|
||||
bool c_chaingame::initGame(void)
|
||||
{
|
||||
// patternFlag = false;
|
||||
// cheatbutton = 0;
|
||||
// cheatButtonFlag = false;
|
||||
state = cg_reset;
|
||||
patternIndex = 0;
|
||||
patternFlag = false;
|
||||
cheatbutton = 0;
|
||||
cheatButtonFlag = false;
|
||||
return true;
|
||||
}
|
||||
void c_chaingame::resetGame(void)
|
||||
|
||||
@@ -5,35 +5,20 @@
|
||||
#include "buttons.h"
|
||||
#include "game.h"
|
||||
|
||||
#define PLAYNEXTTIMEOUT 1500 // 3sec * 1000ms
|
||||
|
||||
typedef enum
|
||||
{
|
||||
cg_idle,
|
||||
cg_play,
|
||||
cg_play_next,
|
||||
cg_cheat,
|
||||
cg_cheat_next,
|
||||
cg_reset
|
||||
} cg_states;
|
||||
|
||||
class c_chaingame : public c_game
|
||||
{
|
||||
private:
|
||||
uint8_t patternIndex;
|
||||
//bool patternFlag = false;
|
||||
bool patternFlag = false;
|
||||
uint16_t cheatbutton = 0;
|
||||
//bool cheatButtonFlag = false;
|
||||
bool cheatButtonFlag = false;
|
||||
uint16_t ledpattern[4] = {1, 3, 1, 2};
|
||||
int patternlength = sizeof(ledpattern) / sizeof(ledpattern[0]);
|
||||
cg_states state = cg_reset;
|
||||
uint32_t playNextTimer=0;
|
||||
|
||||
void nextPattern(void);
|
||||
void updateCheatButton(void);
|
||||
|
||||
public:
|
||||
c_chaingame(e_ledcolor gamecolor) : c_game{chaingame, gamecolor} {}
|
||||
c_chaingame(e_ledcolor gamecolor): c_game{chaingame, gamecolor} {}
|
||||
void runGame(void);
|
||||
bool initGame(void);
|
||||
void resetGame(void);
|
||||
@@ -42,4 +27,5 @@ public:
|
||||
// void HandleChainGame( bool newstate );
|
||||
// void ResetChainGame(void);
|
||||
|
||||
#endif // CHAINGAMEH
|
||||
|
||||
#endif //CHAINGAMEH
|
||||
72
src/led.cpp
72
src/led.cpp
@@ -71,78 +71,6 @@ void setAllLeds( bool state)
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t lasttime = 0;
|
||||
bool laststate = false;
|
||||
|
||||
void blinkAllLeds(void)
|
||||
{
|
||||
uint32_t timeNow = millis();
|
||||
if(timeNow - lasttime > BLINKINTERVAL)
|
||||
{
|
||||
if(laststate)
|
||||
{
|
||||
turnOnAllLed();
|
||||
laststate = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
turnOffAllLed();
|
||||
laststate = true;
|
||||
}
|
||||
lasttime = timeNow;
|
||||
}
|
||||
}
|
||||
|
||||
void blinkLed(e_ledcolor blinkled)
|
||||
{
|
||||
uint32_t timeNow = millis();
|
||||
if(timeNow - lasttime > BLINKINTERVAL)
|
||||
{
|
||||
if(laststate)
|
||||
{
|
||||
turnOnLed(blinkled);
|
||||
laststate = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
turnOffLed(blinkled);
|
||||
laststate = true;
|
||||
}
|
||||
lasttime = timeNow;
|
||||
}
|
||||
}
|
||||
//e_ledcolor lastLed = e_ledcolor::YELLOW;
|
||||
|
||||
// void runningLeds(void)
|
||||
// {
|
||||
// uint32_t timeNow = millis();
|
||||
// if(timeNow - lasttime > BLINKINTERVAL)
|
||||
// {
|
||||
// switch (lastLed)
|
||||
// {
|
||||
// case e_ledcolor::YELLOW:
|
||||
// turnOffAllLed();
|
||||
// turnOnLed(YELLOW);
|
||||
// lastLed = e_ledcolor::RED;
|
||||
// break;
|
||||
// case e_ledcolor::RED:
|
||||
// turnOffAllLed();
|
||||
// turnOnLed(RED);
|
||||
// lastLed = e_ledcolor::GREEN;
|
||||
// break;
|
||||
// case e_ledcolor::GREEN:
|
||||
// turnOffAllLed();
|
||||
// turnOnLed(GREEN);
|
||||
// lastLed = e_ledcolor::YELLOW;
|
||||
// break;
|
||||
// default:
|
||||
// break;
|
||||
// }
|
||||
// lasttime = timeNow;
|
||||
|
||||
// }
|
||||
// }
|
||||
|
||||
//#############################################
|
||||
//# leds functions #
|
||||
//#############################################
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
#include "vector"
|
||||
#include "board.h"
|
||||
|
||||
#define BLINKINTERVAL 200
|
||||
|
||||
enum e_ledcolor
|
||||
{
|
||||
YELLOW,
|
||||
@@ -99,8 +97,6 @@ void turnOnLed(uint16_t index);
|
||||
void turnOffAllLed(void);
|
||||
void turnOnAllLed(void);
|
||||
void setAllLeds( bool state);
|
||||
void blinkAllLeds(void);
|
||||
void blinkLed(e_ledcolor blinkled);
|
||||
|
||||
|
||||
#endif //LEDH
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "Arduino.h"
|
||||
#include "arduino.h"
|
||||
#include "board.h"
|
||||
#include "buttons.h"
|
||||
#include "led.h"
|
||||
|
||||
Reference in New Issue
Block a user