Browse Source

Add state machine states for further modes.

pull/14/head
Dennis Bücker 2 weeks ago
parent
commit
884c44fae3
7 changed files with 85 additions and 16 deletions
  1. +34
    -0
      src/EnterColorSequence.cpp
  2. +3
    -2
      src/ShowColorSequence.cpp
  3. +10
    -0
      src/SimonDisplay.cpp
  4. +1
    -0
      src/SimonDisplay.h
  5. +18
    -0
      src/SimonSays.cpp
  6. +13
    -10
      src/SimonSays.h
  7. +6
    -4
      src/main.cpp

+ 34
- 0
src/EnterColorSequence.cpp View File

@ -0,0 +1,34 @@
#include "SimonSays.h"
#include <FastLED.h> // For color management
/* This mode will show multiple Colors on the screen. Number of colors is defined by level.
(Initial: Green display is shown and color sequence entering is starting with short/long button press) --> Idea for later
Navigation:
Short button press: next color
Long button press (and release): select color
Mistake: Red screen. Game will restart with level one. Counter with failed games increased.
Success: Nothing until final success: Show a green screen for a short time.
*/
EnterColorSequence::EnterColorSequence() :
inputStarted{false}
{}
void EnterColorSequence::enter(SimonSays & simonSays){
// Reset game to no input yet
inputStarted = false;
}
void EnterColorSequence::leave(SimonSays &simonSays)
{
}
void EnterColorSequence::update(SimonSays &simonSays)
{
if (!inputStarted) {
simonSays.simonDisplay.displayOneColor(CRGB::Green);
Serial.println("Game not started condition.");
}
}

+ 3
- 2
src/ShowColorSequence.cpp View File

@ -31,8 +31,9 @@ void ShowColorSequence::update(SimonSays & simonSays)
positionDisplay += 1;
if (positionDisplay >= simonSays.currentSequenceLength)
{
positionDisplay = 0;
simonSays.goToNextPhase();
return;
}
ticksTillNextColor=100;
ticksTillNextColor=100; //TODO: Set this by selected level
}
}

+ 10
- 0
src/SimonDisplay.cpp View File

@ -45,10 +45,12 @@ void SimonDisplay::update()
void SimonDisplay::setActiveColor(int _highlightColor)
{
currentColor=_highlightColor;
//displayOneColor(baseColors[currentColor]); // One could include the highlight bool here
}
void SimonDisplay::drawSingleColor(int colorId, bool hightlightBool)
{
// Legacy, delete lateron
for (int i = 0; i <= 25; i++)
{
if (hightlightBool){
@ -61,6 +63,14 @@ void SimonDisplay::drawSingleColor(int colorId, bool hightlightBool)
}
}
void SimonDisplay::displayOneColor(CRGB color)
{
for (int i = 0; i <= 25; i++)
{
M5.dis.drawpix(i, color);
}
}
void SingleColor::update(SimonDisplay & simonDisplay)
{
// Display single current color


+ 1
- 0
src/SimonDisplay.h View File

@ -28,6 +28,7 @@ class SimonDisplay {
void setColorSequence(int _colorSequence[10]);
void setDisplayMode(int _mode);
void setNumberColors(int _numberOfColors);
void displayOneColor(CRGB color);
private:


+ 18
- 0
src/SimonSays.cpp View File

@ -24,6 +24,8 @@ SimonSays::SimonSays(SimonDisplay & _simonDisplay):
void SimonSays::update() {
stateList[currentState]->update(*this);
// Serial.print("Current phase is: ");
// Serial.println(currentState);
}
void SimonSays::begin() {
@ -31,6 +33,22 @@ void SimonSays::begin() {
currentSequenceLength = 9; // change this adjusting to level
generateNewSequence(); // This will generate a new random sequence.
stateList[currentState]->enter(*this);
Serial.println(sizeof(stateList) / sizeof(stateList[0]));
}
void SimonSays::goToNextPhase() {
// This advances to the next phase (which is a state)..
// Serial.print("Leave state ");
// Serial.print(currentState);
stateList[currentState] -> leave(*this);
int numberOfStates = sizeof(stateList) / sizeof(stateList[0]);
currentState = (currentState + 1) % numberOfStates; // Advance to next phase and go to first one if at end
stateList[currentState] -> enter(*this);
// Serial.print(" and enter state ");
// Serial.println(currentState);
// Serial.print("Number of states: ");
// Serial.println(numberOfStates);
}
void SimonSays::generateNewSequence() {


+ 13
- 10
src/SimonSays.h View File

@ -11,8 +11,8 @@ class GameState {
public:
virtual ~GameState() {}
virtual void enter(SimonSays & simonSays) {}
virtual void update(SimonSays & simonSays) {}
virtual void leave(SimonSays & simonSays) const {}
virtual void update(SimonSays & simonSays) {}
virtual void leave(SimonSays & simonSays) {}
};
// Game classes build from this
@ -21,8 +21,8 @@ class ShowColorSequence final : public GameState {
public:
ShowColorSequence();
void enter(SimonSays &simonSays) override;
void update(SimonSays &simonSays) override;
void leave(SimonSays &simonSays) const override {};
void update(SimonSays &simonSays) override;
void leave(SimonSays &simonSays) override {};
private:
int positionDisplay;
@ -31,10 +31,14 @@ class ShowColorSequence final : public GameState {
};
class EnterColorSequence final : public GameState {
public:
void enter(SimonSays &simonSays) override {};
void update(SimonSays &simonSays) override {};
void leave(SimonSays &simonSays) const override {};
public:
EnterColorSequence();
void enter(SimonSays &simonSays) override;
void update(SimonSays &simonSays) override;
void leave(SimonSays &simonSays) override;
private:
int inputStarted;
};
@ -52,8 +56,7 @@ public:
// possible functions to call
void begin();
void selectNext();
void set();
void goToNextPhase();
void update();
// Game logic


+ 6
- 4
src/main.cpp View File

@ -27,9 +27,9 @@ void setBuff(uint8_t Rdata, uint8_t Gdata, uint8_t Bdata)
}
}
SimonDisplay simonDisplay(M5);
SimonDisplay SimonDisplay(M5);
SimonSays Simon(simonDisplay);
SimonSays Simon(SimonDisplay);
uint32_t timeOfLastTick = 0;
@ -38,7 +38,7 @@ void setup()
M5.begin(true, false, true);
delay(10);
simonDisplay.begin();
SimonDisplay.begin();
Simon.begin();
// Initialize ticks
@ -57,7 +57,9 @@ void loop()
timeOfLastTick = time;
Simon.update();
simonDisplay.update();
SimonDisplay.update();
M5.update();
}


Loading…
Cancel
Save