Merge pull request 'longPressFeedback' (#14) from longPressFeedback into main

Reviewed-on: #14
pull/15/head
laenan 1 year ago
commit 8c3bcd130a
  1. 9
      src/DisplayModeLayouts.h
  2. 136
      src/EnterColorSequence.cpp
  3. 116
      src/SimonSays.cpp
  4. 4
      src/SimonSays.h

@ -18,6 +18,15 @@ static unsigned const int DisplayLayoutX[25] =
1, 0, 0, 0, 1,
};
static unsigned const int DisplayLayoutODot[25] =
{
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 1, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1,
};
static unsigned const int DisplayLayoutCheckmark[25] =
{
0, 0, 0, 0, 0,

@ -15,27 +15,12 @@ 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},
inputSequencePosition{0},
inputActiveColor{0},
levelCompleted{false},
levelSuccess{false},
numberOfUniqueColors{0}, // Will be determined from the sequence
arrayOfUniqueColors{0,0,0,0,0,0,0,0,0,0} // length of MCUInfo::maxColorArrayLength
levelSuccess{false}
{}
void EnterColorSequence::enter(SimonSays & simonSays){
@ -47,91 +32,13 @@ void EnterColorSequence::enter(SimonSays & simonSays){
inputSequencePosition = 0;
inputActiveColor = 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, but makes highlight sequence in good order
/*
// 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);
// give information to display module
simonSays.simonDisplay.setNumberColors(numberOfUniqueColors);
simonSays.simonDisplay.setNumberColors(simonSays.numberOfUniqueColors);
simonSays.simonDisplay.setActiveColor(inputActiveColor);
simonSays.simonDisplay.setColorsToDisplay(arrayOfUniqueColors);
simonSays.simonDisplay.setColorsToDisplay(simonSays.arrayOfUniqueColors);
// Switch to correct color selection state
switch (numberOfUniqueColors)
switch (simonSays.numberOfUniqueColors)
{
case 1:
simonSays.simonDisplay.setDisplayMode(1);
@ -171,15 +78,15 @@ void EnterColorSequence::update(SimonSays &simonSays)
if (M5.Btn.wasReleased())
{
// Advance the input color
inputActiveColor = (inputActiveColor + 1) % numberOfUniqueColors;
inputActiveColor = (inputActiveColor + 1) % simonSays.numberOfUniqueColors;
// Debug help to see where input is in sequence and if next input is correct.
Serial.print("Next requested color is ");
Serial.print(simonSays.currentSequence[inputSequencePosition]);
Serial.print(" and current selected color is ");
Serial.print(arrayOfUniqueColors[inputActiveColor]);
Serial.print(simonSays.arrayOfUniqueColors[inputActiveColor]);
Serial.print(" an input click would be: ");
if (arrayOfUniqueColors[inputActiveColor] == simonSays.currentSequence[inputSequencePosition])
if (simonSays.arrayOfUniqueColors[inputActiveColor] == simonSays.currentSequence[inputSequencePosition])
{
Serial.println("CORRECT.");
}
@ -190,9 +97,22 @@ void EnterColorSequence::update(SimonSays &simonSays)
}
// Update the color to display every tick for failsave
simonSays.simonDisplay.setActiveColor(arrayOfUniqueColors[inputActiveColor]);
simonSays.simonDisplay.setActiveColor(simonSays.arrayOfUniqueColors[inputActiveColor]);
// check for long press
// check for long press for indication, if long enough
if (M5.Btn.wasPressedFor(MCUInfo::longButtonPress))
{
// this will run immediatly when button press is long enough
// Go to custom display mode (will be resetted with button release)
simonSays.simonDisplay.setDisplayMode(0);
// show white O with dot
simonSays.simonDisplay.displayDisplayLayout(DisplayLayoutODot, true, 2, 10, 9); // 2 colors: highlight colors, 9: white, 10:black, // needs custom mode
//Serial.println("Button press was long enough.");
}
// check for long press and release
if (M5.Btn.wasReleaseFor(MCUInfo::longButtonPress))
{
// Check if selected color was correct !-> loose condition + restart
@ -200,19 +120,25 @@ void EnterColorSequence::update(SimonSays &simonSays)
// check if sequence ready
// --> advance level
if (arrayOfUniqueColors[inputActiveColor] == simonSays.currentSequence[inputSequencePosition]) {
// Reset display mode.
simonSays.simonDisplay.setDisplayMode(simonSays.numberOfUniqueColors);
if (simonSays.arrayOfUniqueColors[inputActiveColor] == simonSays.currentSequence[inputSequencePosition])
{
// show green flash as correct input, maybe enter this earlier after pressdown already
// Advance sequence counter
inputSequencePosition++;
// Check for game End condition
if (inputSequencePosition == simonSays.currentSequenceLength) {
if (inputSequencePosition == simonSays.currentSequenceLength)
{
levelCompleted = true;
levelSuccess = true;
}
}
else {
else
{
// An error was made, this will go to error mode
levelCompleted = true;
levelSuccess = false;

@ -1,7 +1,18 @@
#include "SimonSays.h"
// Anonymous namespace for states
// 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;
}
// Anonymous namespace for states
namespace {
ShowColorSequence showColorSequence;
EnterColorSequence enterColorSequence;
@ -20,6 +31,8 @@ SimonSays::SimonSays(SimonDisplay & _simonDisplay):
currentSequenceLength{1},
currentLevel{0},
currentSpeed{100},
numberOfUniqueColors{0}, // Will be determined from the sequence
arrayOfUniqueColors{0,0,0,0,0,0,0,0,0,0}, // length of MCUInfo::maxColorArrayLength
maxLevel{MCUInfo::maxLevel},
gameLifes{MCUInfo::initialGameLifes},
gameFinishedStatus{false},
@ -140,18 +153,101 @@ void SimonSays::generateNewSequence() {
break;
}
Serial.print("Generated sequence ");
for (int i = 0; i < currentSequenceLength; i++)
// Repeat generation until enough colors are generated for level.
// Initially reset number of unique colors
numberOfUniqueColors = 0; // otherwise no new sequence might be generated
while (numberOfUniqueColors < numberOfColors)
{
Serial.print("Generated sequence ");
for (int i = 0; i < currentSequenceLength; i++)
{
currentSequence[i] = random(numberOfColors);
Serial.print(currentSequence[i]);
Serial.print(" ");
}
Serial.print(" with ");
Serial.print(currentSequenceLength);
Serial.print(" members for level ");
Serial.print(currentLevel);
Serial.println(".");
// Generate array with only active colors
int activeSequenceColors[currentSequenceLength];
for (int i = 0; i < currentSequenceLength; i++)
{
activeSequenceColors[i] = 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, currentSequenceLength, sizeof(activeSequenceColors[0]), sort_desc);
// Sorting is not necessary at this time, but makes highlight sequence in good order
/*
// 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 < 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++)
{
currentSequence[i] = random(numberOfColors);
Serial.print(currentSequence[i]);
Serial.print(arrayOfUniqueColors[i]);
Serial.print(" ");
}
Serial.print(" with ");
Serial.print(currentSequenceLength);
Serial.print(" members for level ");
Serial.print(currentLevel);
Serial.println(".");
Serial.println();
Serial.print("Number of unique elements: ");
Serial.println(numberOfUniqueColors);
}
void SimonSays::reportError()

@ -47,8 +47,6 @@ class EnterColorSequence final : public GameState {
bool levelCompleted; // color entering is finished?
bool levelSuccess; // This indicates if level was finished successfull
int ticksTillNextLevel; // time specified how long checkmark is shown
int numberOfUniqueColors; // Will be determined from the sequence
int arrayOfUniqueColors[MCUInfo::maxColorArrayLength]; // will contain an array of all unique colors
};
class GameResultState final : public GameState
@ -89,6 +87,8 @@ public:
int currentSequenceLength;
int currentLevel;
int currentSpeed;
int numberOfUniqueColors;
int arrayOfUniqueColors[MCUInfo::maxColorArrayLength];
int maxLevel;
int gameLifes;
bool gameFinishedStatus;

Loading…
Cancel
Save