Determination of unique colors and send values to display

pull/14/head
Dennis Bücker 1 year ago
parent 084434e0db
commit 845660d26e
  1. 148
      src/EnterColorSequence.cpp
  2. 1
      src/MCUInfo.h
  3. 33
      src/SimonDisplay.cpp
  4. 16
      src/SimonDisplay.h
  5. 4
      src/SimonSays.cpp
  6. 5
      src/SimonSays.h

@ -13,14 +13,115 @@ Mistake: Red screen. Game will restart with level one. Counter with failed games
Success: Nothing until final success: Show a green screen for a short time.
*/
/*
// qsort requires you to create a sort function
int sort_desc(const void *cmp1, const void *cmp2)
{
// Need to cast the void * to int *
int a = *((int *)cmp1);
int b = *((int *)cmp2);
// The comparison
return a > b ? -1 : (a < b ? 1 : 0);
// A simpler, probably faster way:
//return b - a;
}
*/
EnterColorSequence::EnterColorSequence() :
inputStarted{false}
inputStarted{false},
inputSequencePosition{0},
inputActiveColor{0},
numberOfUniqueColors{0}, // Will be determined from the sequence
arrayOfUniqueColors{0,0,0,0,0,0,0,0,0,0} // length of MCUInfo::maxColorArrayLength
{}
void EnterColorSequence::enter(SimonSays & simonSays){
// Reset game to no input yet
inputStarted = false;
inputSequencePosition = 0;
inputActiveColor = 0;
// To display inital green screen
simonSays.simonDisplay.setDisplayMode(0);
// Determine the number of unique
// Generate array with only active colors
int activeSequenceColors[simonSays.currentSequenceLength];
for (int i=0;i<simonSays.currentSequenceLength;i++)
{
activeSequenceColors[i] = simonSays.currentSequence[i];
}
/*
// Debug unsorted Array
Serial.print("UNsorted Array: ");
for (int i = 0;i<simonSays.currentSequenceLength;i++)
{
Serial.print(activeSequenceColors[i]);
Serial.print(" ");
}
Serial.println();
*/
// Sort the array (high to low)
// qsort(activeSequenceColors, simonSays.currentSequenceLength, sizeof(activeSequenceColors[0]), sort_desc);
// Sorting is not necessary at this time
/*
// Debug sorted Array
Serial.print("Sorted Array: ");
for (int i = 0; i < simonSays.currentSequenceLength; i++)
{
Serial.print(activeSequenceColors[i]);
Serial.print(" ");
}
Serial.println();
*/
// Determine the unique elements in the array (for display) and their number
for (int i = 0; i<MCUInfo::maxColorArrayLength; i++)
{
arrayOfUniqueColors[i]=99999; // set array to high value that is not in color sequence
}
//int arrayOfUniqueColors[MCUInfo::maxColorArrayLength];
numberOfUniqueColors = 0;
int i, k;
for (i = 0; i < simonSays.currentSequenceLength; i++)
{
// Iterate through active color sequence
int flag = 0;
for (k = 0; k <= numberOfUniqueColors; k++)
{
// iterate through saved elements if it is a duplicate
if (activeSequenceColors[i] == arrayOfUniqueColors[k])
{
// Serial.println("set flag");
flag = 1; // this is a duplicat
break;
}
}
if (flag !=1){
// add only if it is no duplicat and advance counter
arrayOfUniqueColors[numberOfUniqueColors++] = activeSequenceColors[i]; // assign to unique elements array
}
}
// Debug print outcome
Serial.print("Unique elements: ");
for (int i = 0; i < numberOfUniqueColors; i++)
{
Serial.print(arrayOfUniqueColors[i]);
Serial.print(" ");
}
Serial.println();
Serial.print("Number of unique elements: ");
Serial.println(numberOfUniqueColors);
}
void EnterColorSequence::leave(SimonSays &simonSays)
@ -36,6 +137,31 @@ void EnterColorSequence::update(SimonSays &simonSays)
if (M5.Btn.wasPressed())
{
inputStarted = true;
// Switch to correct color selection state
switch (numberOfUniqueColors)
{
case 2:
simonSays.simonDisplay.setDisplayMode(2);
break;
case 3:
simonSays.simonDisplay.setDisplayMode(3);
break;
case 4:
simonSays.simonDisplay.setDisplayMode(4);
break;
case 5:
simonSays.simonDisplay.setDisplayMode(5);
break;
case 6:
simonSays.simonDisplay.setDisplayMode(6);
break;
default:
simonSays.simonDisplay.setDisplayMode(0);
break;
}
simonSays.simonDisplay.setActiveColor(inputActiveColor);
}
else
{
@ -46,12 +172,28 @@ void EnterColorSequence::update(SimonSays &simonSays)
// check for short press
if (M5.Btn.wasReleased())
{
simonSays.simonDisplay.displayOneColor(CRGB::Red);
inputActiveColor = (inputActiveColor + 1) % numberOfUniqueColors;
simonSays.simonDisplay.setActiveColor(arrayOfUniqueColors[inputActiveColor]);
Serial.print("Input position: ");
Serial.println(inputActiveColor);
Serial.print("Current color: ");
Serial.println(arrayOfUniqueColors[inputActiveColor]);
// Debug print outcome
Serial.print("Unique elements: ");
for (int i = 0; i < numberOfUniqueColors; i++)
{
Serial.print(arrayOfUniqueColors[i]);
Serial.print(" ");
}
Serial.println();
Serial.print("Number of unique elements: ");
Serial.println(numberOfUniqueColors);
}
// check for long press
if (M5.Btn.wasReleaseFor(MCUInfo::longButtonPress))
{
simonSays.simonDisplay.displayOneColor(CRGB::Orange);
simonSays.simonDisplay.setActiveColor(5);
}
}

@ -7,5 +7,6 @@ struct MCUInfo
static constexpr float longButtonPress = 700; // millisecond
static constexpr int maxColorArrayLength = 10; //maximal array length
};

@ -22,8 +22,8 @@ SimonDisplay::SimonDisplay(M5Atom &_m5) :
&threeColorSelection,
&fourColorSelection,
&fiveColorSelection,
&sixColorSelection
}
&sixColorSelection},
colorsToDisplay{0, 1, 2, 3, 4, 5, 6, 7, 8, 8}
{}
void SimonDisplay::begin()
@ -52,6 +52,8 @@ void SimonDisplay::begin()
void SimonDisplay::update()
{
modeList[currentMode]->update(*this);
// Serial.print("Display update of mode ");
// Serial.println(currentMode);
}
void SimonDisplay::setDisplayMode(unsigned int _mode)
@ -60,10 +62,18 @@ void SimonDisplay::setDisplayMode(unsigned int _mode)
currentMode = _mode % numberOfModes;
}
void SimonDisplay::setActiveColor(int _highlightColor)
void SimonDisplay::setColorsToDisplay(int _colorsToDisplay[10])
{
for (int i = 0; i < (sizeof(colorsToDisplay) / sizeof(colorsToDisplay[0])); i++)
{
colorsToDisplay[i] = _colorsToDisplay[i];
}
}
void SimonDisplay::setActiveColor(int _activeColor)
{
activeColor=_highlightColor;
//this->displayOneColor(baseColors[currentColor]); // One could include the highlight bool here
activeColor=_activeColor;
}
void SimonDisplay::displayOneColor(CRGB _color)
@ -87,22 +97,35 @@ void CustomDisplay::update(SimonDisplay & simonDisplay)
void OneColorSelection::update(SimonDisplay &simonDisplay)
{
simonDisplay.displayOneColor(simonDisplay.activeColor);
}
void TwoColorSelection::update(SimonDisplay &simonDisplay)
{
// Only test
simonDisplay.displayOneColorById(simonDisplay.activeColor);
// Serial.println("TwoColorSelectionUpdate");
}
void ThreeColorSelection::update(SimonDisplay &simonDisplay)
{
// Only test
simonDisplay.displayOneColorById(simonDisplay.activeColor);
// Serial.println("TwoColorSelectionUpdate");
}
void FourColorSelection::update(SimonDisplay &simonDisplay)
{
// Only test
simonDisplay.displayOneColorById(simonDisplay.activeColor);
// Serial.println("TwoColorSelectionUpdate");
}
void FiveColorSelection::update(SimonDisplay &simonDisplay)
{
// Only test
simonDisplay.displayOneColorById(simonDisplay.activeColor);
// Serial.println("TwoColorSelectionUpdate");
}
void SixColorSelection::update(SimonDisplay &simonDisplay)

@ -21,12 +21,14 @@ class CustomDisplay final : public DisplayMode
class OneColorSelection final : public DisplayMode {
public:
void update(SimonDisplay &simonDisplay) override;
};
class TwoColorSelection final : public DisplayMode
{
public:
void update(SimonDisplay &simonDisplay) override;
};
class ThreeColorSelection final : public DisplayMode
@ -62,8 +64,9 @@ class SimonDisplay {
void begin();
void update();
void setActiveColor(int _highlightColor); // The color ID is highlighed. Applicable only for screens with more than 1 color.
//void setColorSequence(int _colorSequence[10]);
void setActiveColor(int _activeColor); // The color ID is highlighed. Applicable only for screens with more than 1 color.
void setColorsToDisplay(int _colorsToDisplay[10]); // only the first $numberOfColors will be in the display, depending on display mode
void setDisplayMode(unsigned int _mode);
void setNumberColors(int _numberOfColors);
void setColors();
@ -84,8 +87,13 @@ class SimonDisplay {
friend class SingleColor;
friend class CustomDisplay;
friend class OneColorSelection;
friend class TwoColorSelection;
friend class ThreeColorSelection;
friend class FourColorSelection;
friend class FiveColorSelection;
friend class SixColorSelection;
DisplayMode * modeList[7];
int currentSequence[10];
int colorsToDisplay[10];
};

@ -30,7 +30,7 @@ void SimonSays::update() {
void SimonSays::begin() {
// Some starting logic
currentSequenceLength = 9; // change this adjusting to level
currentSequenceLength = 4; // change this adjusting to level
generateNewSequence(); // This will generate a new random sequence.
stateList[currentState]->enter(*this);
Serial.println(sizeof(stateList) / sizeof(stateList[0]));
@ -54,7 +54,7 @@ void SimonSays::goToNextPhase() {
void SimonSays::generateNewSequence() {
int maxId = 8;
// TODO: Make random sequence
int newSequence[10] = {0,1,2,3,4,5,6,7,8,8};
int newSequence[10] = {0,1,1,0,3,4,5,6,7,8};
int length = sizeof(currentSequence)/sizeof(currentSequence[0]);
for (int i = 0; i < length; i++) {
currentSequence[i] = newSequence[i];

@ -2,6 +2,7 @@
#include <Arduino.h>
#include "SimonDisplay.h"
#include "MCUInfo.h"
// Introduce main class
class SimonSays;
@ -39,6 +40,10 @@ class EnterColorSequence final : public GameState {
private:
int inputStarted;
int inputSequencePosition; // will track advancement of the input
int inputActiveColor; // will track selected color
int numberOfUniqueColors; // Will be determined from the sequence
int arrayOfUniqueColors[MCUInfo::maxColorArrayLength]; // will contain an array of all unique colors
};

Loading…
Cancel
Save