Files
Leo-led-truck/src/chainGame.cpp
Willem Oldemans 04b1235f1a firmware release 1.2
fixed MSboard, game class prep, power impr.
2021-03-29 11:10:57 +02:00

113 lines
2.2 KiB
C++

#ifndef UNIT_TEST
#ifdef ARDUINO
#include "chainGame.h"
uint8_t patternIndex = 0;
bool patternFlag = false;
//bool firstpattern = 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)
{
if (patternIndex < patternlength - 1)
{
patternIndex++;
}
else
{
patternIndex = 0;
}
}
void ResetChainGame(void)
{
patternIndex = 0;
patternFlag = false;
//firstpattern = false;
cheatbutton = 0;
cheatButtonFlag = false;
}
void HandleChainGame(bool newstate)
{
if (!patternFlag && !cheatButtonFlag)
{
if (buttonIsPressed(ledpattern[patternIndex]))
{
//pattern button pressed, turn on LED, set flag
turnOnLed(ledpattern[patternIndex]);
patternFlag = true;
cheatbutton = 0;
}
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;
}
}
if (!buttonIsPressed(ledpattern[patternIndex]))
{
// pattern switch is open, turn off pattern LED
turnOffLed(ledpattern[patternIndex]);
}
if (!buttonIsPressed(cheatbutton) && cheatButtonFlag)
{
// cheat switch is open, turn of cheat LED
turnOffLed(cheatbutton);
cheatButtonFlag = false;
cheatbutton = 0;
}
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
cheatbutton = 1;
}
else if (buttonIsPressed(5) && (cheatbutton == 0))
{
// cheatbutton 5 (momentary 2) was closed, set cheatbutton to 2
cheatbutton = 2;
}
else if (buttonIsPressed(6) && (cheatbutton == 0))
{
// cheatbutton 5 (momentary 3) was closed, set cheatbutton to 3
cheatbutton = 3;
}
}
#endif
#endif