Skip to content

Commit

Permalink
Fixing memory problems (Arduino)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ma-Pe-Ma committed Aug 18, 2022
1 parent 80dfc36 commit 2c7d651
Show file tree
Hide file tree
Showing 21 changed files with 108 additions and 84 deletions.
2 changes: 1 addition & 1 deletion DartsBoard/DartsBoard.ino
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void setup() {
Resources::initialize();
playerContainer.init();
gameContainer.init();
gameLogic.init();
gameLogic.init();

bluetooth.btSwitch = &gameLogic.androidMode;

Expand Down
47 changes: 24 additions & 23 deletions DartsBoard/src/Games/Cricket.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#include "Cricket.h"

Cricket::Cricket(DisplayContainer* displayContainer, PlayerContainer* playerContainer) : DartsGame(displayContainer, playerContainer) {
void Cricket::initialize(DisplayContainer* displayContainer, PlayerContainer* playerContainer) {
this->displayContainer = displayContainer;
this->playerContainer = playerContainer;

this->gameID = "CRICKET";
this->name = "Cricket";

this->initializeMaps();

scoreString = Resources::getTextByID(Resources::Text::cricketScore);
/*scoreString = Resources::getTextByID(Resources::Text::cricketScore);
noscoreString = Resources::getTextByID(Resources::Text::cricketNoscore);
cutthroatString = Resources::getTextByID(Resources::Text::cricketCutthroat);
Expand All @@ -18,31 +22,28 @@ Cricket::Cricket(DisplayContainer* displayContainer, PlayerContainer* playerCont
chaoticString = Resources::getTextByID(Resources::Text::cricketChaotic);
nrOfNrsString = Resources::getTextByID(Resources::Text::cricketNrOfNrs);
startingNrString = Resources::getTextByID(Resources::Text::cricketStartingNr);
startingNrString = Resources::getTextByID(Resources::Text::cricketStartingNr);*/
}

void Cricket::initializeMaps() {
typeMap = new SimpleMap<CricketType, String>(3);
typeMap->insert(score, String("SCORE"));
typeMap->insert(noscore, String("NOSCORE"));
typeMap->insert(cutthroat, String("CUTTHROAT"));

setMap = new SimpleMap<CricketNumberSet, String>(3);
setMap->insert(classicNumbers, String("CLASSIC"));
setMap->insert(allNumbers, String("ALL"));
setMap->insert(customNumbers, String("CUSTOM"));

customMap = new SimpleMap<CricketCustomSet, String>(3);
customMap->insert(interval, String("INTERVAL"));
customMap->insert(randomInterval, String("RANDOMINTERVAL"));
customMap->insert(chaotic, String("CHAOTIC"));
typeMap.insert(CricketType::score, String("SCORE"));
typeMap.insert(CricketType::noscore, String("NOSCORE"));
typeMap.insert(CricketType::cutthroat, String("CUTTHROAT"));

setMap.insert(CricketNumberSet::classicNumbers, String("CLASSIC"));
setMap.insert(CricketNumberSet::allNumbers, String("ALL"));
setMap.insert(CricketNumberSet::customNumbers, String("CUSTOM"));

customMap.insert(CricketCustomSet::interval, String("INTERVAL"));
customMap.insert(CricketCustomSet::randomInterval, String("RINTERVAL"));
customMap.insert(CricketCustomSet::chaotic, String("CHAOTIC"));
}

void Cricket::processConfig(JsonObject& message) {
cricketNr = message["CricketNr"].as<int>();
cricketType = typeMap->getKeyByValue(message["CricketType"]);
cricketNumberSet = setMap->getKeyByValue(message["CricketNumberSet"]);
cricketCustomSet = customMap->getKeyByValue(message["CricketCustomSet"]);
cricketType = typeMap.getKeyByValue(message["CricketType"]);
cricketNumberSet = setMap.getKeyByValue(message["CricketNumberSet"]);
cricketCustomSet = customMap.getKeyByValue(message["CricketCustomSet"]);
cricketStart = message["CricketStart"];
}

Expand Down Expand Up @@ -436,9 +437,9 @@ void Cricket::initializeGame() {

void Cricket::serializeConfigCustom(JsonObject& configObject) {
configObject["CricketNr"] = cricketNr;
configObject["CricketType"] = typeMap->getValueByKey(cricketType);
configObject["CricketNumberSet"] = setMap->getValueByKey(cricketNumberSet);
configObject["CricketCustomSet"] = customMap->getValueByKey(cricketCustomSet);
configObject["CricketType"] = typeMap.getValueByKey(cricketType);
configObject["CricketNumberSet"] = setMap.getValueByKey(cricketNumberSet);
configObject["CricketCustomSet"] = customMap.getValueByKey(cricketCustomSet);
configObject["CricketStart"] = cricketStart;

JsonObject mapObject = configObject.createNestedObject("MAP");
Expand Down
11 changes: 6 additions & 5 deletions DartsBoard/src/Games/Cricket.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class Cricket : public DartsGame {

int cricketMap[20] = {};

SimpleMap<CricketType, String>* typeMap;
SimpleMap<CricketNumberSet, String>* setMap;
SimpleMap<CricketCustomSet, String>* customMap;
SimpleMap<CricketType, String> typeMap;
SimpleMap<CricketNumberSet, String> setMap;
SimpleMap<CricketCustomSet, String> customMap;
void initializeMaps();

//used in custom game config
Expand Down Expand Up @@ -91,8 +91,9 @@ class Cricket : public DartsGame {
cricketScore->setCricketMap(scoreMap);
return cricketScore;
}

Cricket(DisplayContainer*, PlayerContainer*);
void initialize(DisplayContainer*, PlayerContainer*);

Cricket() : typeMap(3), setMap(3), customMap(3) {}
};

#endif
2 changes: 2 additions & 0 deletions DartsBoard/src/Games/DartsGame.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class DartsGame {
this->playerContainer = playerContainer;
}

DartsGame() {}

virtual void serializeConfigCustom(JsonObject&) = 0;
virtual void processConfig(JsonObject&) = 0;

Expand Down
11 changes: 7 additions & 4 deletions DartsBoard/src/Games/GameContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ GameContainer::GameContainer(DisplayContainer* displayContainer, PlayerContainer
}

void GameContainer::init() {
games = new DartsGame*[nrOfGames];
cricket.initialize(displayContainer, playerContainer);
games[0] = &cricket;

games[0] = new Cricket(displayContainer, playerContainer);
games[1] = new RoundTheClock(displayContainer, playerContainer);
games[2] = new X01(displayContainer, playerContainer);
roundTheClock.initialize(displayContainer, playerContainer);
games[1] = &roundTheClock;

x01.initialize(displayContainer, playerContainer);
games[2] = &x01;

dartsGame = games[chosenGameCursor];
}
Expand Down
6 changes: 5 additions & 1 deletion DartsBoard/src/Games/GameContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ class DisplayContainer;
class PlayerContainer;

class GameContainer {
DartsGame** games;
DartsGame* games[NUMBER_OF_GAMES];
int chosenGameCursor;
DartsGame* dartsGame;
DisplayContainer* displayContainer;
PlayerContainer* playerContainer;

Cricket cricket;
RoundTheClock roundTheClock;
X01 x01;
public:
GameContainer(DisplayContainer*, PlayerContainer*);
void init();
Expand Down
15 changes: 15 additions & 0 deletions DartsBoard/src/Games/RoundTheClock.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
#include "RoundTheClock.h"

void RoundTheClock::initialize(DisplayContainer* displayContainer, PlayerContainer* playerContainer) {
this->displayContainer = displayContainer;
this->playerContainer = playerContainer;

gameID = "RTC";
name = "Round The Clock";

/*sectorOnlyString = Resources::getTextByID(Resources::Text::rtcSector);
multiplierString = Resources::getTextByID(Resources::Text::rtcMultiplier);
multiplierWithPointsString = Resources::getTextByID(Resources::Text::rtcMultiplierWithPoints);
rtcClassicString = Resources::getTextByID(Resources::Text::rtcClassic);
shootOutString = Resources::getTextByID(Resources::Text::shootOut);*/
}

void RoundTheClock::serializeConfigCustom(JsonObject& configObject) {

}
Expand Down
16 changes: 4 additions & 12 deletions DartsBoard/src/Games/RoundTheClock.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,6 @@ class RoundTheClock : public DartsGame {
bool custom(Pair) override;

void initializeGame() override;

RoundTheClock(DisplayContainer* displayContainer, PlayerContainer* playerContainer) : DartsGame(displayContainer, playerContainer) {
gameID = "RTC";
name = "Round The Clock";

sectorOnlyString = Resources::getTextByID(Resources::Text::rtcSector);
multiplierString = Resources::getTextByID(Resources::Text::rtcMultiplier);
multiplierWithPointsString = Resources::getTextByID(Resources::Text::rtcMultiplierWithPoints);

rtcClassicString = Resources::getTextByID(Resources::Text::rtcClassic);
shootOutString = Resources::getTextByID(Resources::Text::shootOut);
}

void serializeConfigCustom(JsonObject&) override;
void processConfig(JsonObject&) override;
Expand All @@ -59,5 +47,9 @@ class RoundTheClock : public DartsGame {
rtcScore->setGameType(rtcType, rtcSubType);
return rtcScore;
}

void initialize(DisplayContainer*, PlayerContainer*);

RoundTheClock() {}
};
#endif
22 changes: 22 additions & 0 deletions DartsBoard/src/Games/X01.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
#include "X01.h"

void X01::initialize(DisplayContainer* displayContainer, PlayerContainer* playerContainer) {
this->displayContainer = displayContainer;
this->playerContainer = playerContainer;

gameID = "X01";
name = "X01";

scoreMap.insert(0, 301);
scoreMap.insert(1, 501);
scoreMap.insert(2, 701);
scoreMap.insert(3, 901);
scoreMap.insert(4, 1001);

/*simpleString = Resources::getTextByID(Resources::Text::x01Simple);
doubleString = Resources::getTextByID(Resources::Text::x01Double);
masterString = Resources::getTextByID(Resources::Text::x01Master);
inLabel = Resources::getTextByID(Resources::Text::x01In);
outLabel = Resources::getTextByID(Resources::Text::x01Out);
scoreLabel = Resources::getTextByID(Resources::Text::x01Score);*/
}

String X01::getInOutString(int cursor) {
if (cursor == 1) {
return doubleString;
Expand Down
23 changes: 4 additions & 19 deletions DartsBoard/src/Games/X01.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,15 @@ class X01 : public DartsGame {
void serializeConfigCustom(JsonObject&) override;
void processConfig(JsonObject&) override;

X01(DisplayContainer* displayContainer, PlayerContainer* playerContainer) : DartsGame(displayContainer, playerContainer) {
gameID = "X01";
name = "X01";

scoreMap.insert(0, 301);
scoreMap.insert(1, 501);
scoreMap.insert(2, 701);
scoreMap.insert(3, 901);
scoreMap.insert(4, 1001);

simpleString = Resources::getTextByID(Resources::Text::x01Simple);
doubleString = Resources::getTextByID(Resources::Text::x01Double);
masterString = Resources::getTextByID(Resources::Text::x01Master);

inLabel = Resources::getTextByID(Resources::Text::x01In);
outLabel = Resources::getTextByID(Resources::Text::x01Out);
scoreLabel = Resources::getTextByID(Resources::Text::x01Score);
}

AbstractScore* setProperScoreContainer() override {
X01Score* x01Score = new X01Score(displayContainer, playerContainer);
x01Score->setGameProperties(inCursor, outCursor, scoreMap.getValueByKey(scoreCursor), &inString, &outString);
return x01Score;
}

void initialize(DisplayContainer*, PlayerContainer*);

X01() {}
};

#endif
2 changes: 1 addition & 1 deletion DartsBoard/src/Hardware/AudioPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace AudioPlayer {

void play(Resources::AudioFile audioFile) {
//String fileName = "/res/audio/";
String fileName = Resources::getAudioFieByID(audioFile);
String fileName = Resources::getAudioFileByID(audioFile);
Serial.println("Playing file: " + fileName);
delay(200);

Expand Down
2 changes: 1 addition & 1 deletion DartsBoard/src/Hardware/DisplayContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ uint8_t DisplayContainer::showBMP(char *nm, int x, int y) {
ret = 0; // good render
}
bmpFile.close();
Serial.println("width: " + String(tft.width()) +", "+String(tft.height()));
//Serial.println("width: " + String(tft.width()) +", "+String(tft.height()));
return (ret);
}

Expand Down
6 changes: 3 additions & 3 deletions DartsBoard/src/Resource/Resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ namespace Resources {
textNameMap.insert(String("x01"), Text::x01);
textNameMap.insert(String("config"), Text::config);

textNameMap.insert(String("cricketScore"), Text::cricketScore);
/*textNameMap.insert(String("cricketScore"), Text::cricketScore);
textNameMap.insert(String("cricketNoscore"), Text::cricketNoscore);
textNameMap.insert(String("cricketCutthroat"), Text::cricketCutthroat);
Expand Down Expand Up @@ -143,7 +143,7 @@ namespace Resources {
textNameMap.insert(String("x01In"), Text::x01In);
textNameMap.insert(String("x01Out"), Text::x01Out);
textNameMap.insert(String("x01Score"), Text::x01Score);
textNameMap.insert(String("x01Score"), Text::x01Score);*/

String textPath = path + "text/" + textSetNameMap.getValueByKey(textSet) + ".txt";

Expand Down Expand Up @@ -180,7 +180,7 @@ namespace Resources {
return textMap.getValueByKey(text);
}

String getAudioFieByID(AudioFile audioFile) {
String getAudioFileByID(AudioFile audioFile) {
return audioMap.getValueByKey(audioFile);
}
}
2 changes: 1 addition & 1 deletion DartsBoard/src/Resource/Resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ namespace Resources {

extern SimpleMap<AudioFile, String> audioMap;
String getTextByID(Text text);
String getAudioFieByID(AudioFile audioFile);
String getAudioFileByID(AudioFile audioFile);
}

#endif
14 changes: 7 additions & 7 deletions DartsBoard/src/SimpleMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@

template<typename T, typename U>
class SimpleMap {
int currentSize = 0;
int maxSize;
uint8_t currentSize = 0;
uint8_t maxSize;

T* key;
U* value;

public:
SimpleMap(int maxSize) {
SimpleMap(uint8_t maxSize) {
this->maxSize = maxSize;

key = new T[maxSize];
value = new U[maxSize];
}

bool insert(T e1, U e2) {
if (currentSize < maxSize) {
key[currentSize] = e1;
key[currentSize] = e1;
value[currentSize] = e2;

currentSize++;
Expand All @@ -32,7 +32,7 @@ class SimpleMap {
}

U getValueByKey(T key) {
for (int i = 0; i < currentSize; i++) {
for (uint8_t i = 0; i < currentSize; i++) {
if (key == this->key[i] ) {
return this->value[i];
}
Expand All @@ -42,7 +42,7 @@ class SimpleMap {
}

T getKeyByValue(U value) {
for (int i = 0; i < currentSize; i++) {
for (uint8_t i = 0; i < currentSize; i++) {
if (value == this->value[i] ) {
return this->key[i];
}
Expand Down
2 changes: 1 addition & 1 deletion DartsBoard/src/States/AppStates/MainScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void MainScreen::start() {
//show top texts
int squareOffset = int(SCR_WIDTH * 0.04f);
getGameLogic()->displayContainer->write(squareOffset, squareOffset, RED, 3, "MPM");
getGameLogic()->displayContainer->writeRight(squareOffset, squareOffset, RED, 3, "Dartsi");
getGameLogic()->displayContainer->writeRight(squareOffset, squareOffset, RED, 3, "Darts");

//show icons at the bottom
getGameLogic()->displayContainer->showBMP("/res/img/offline.bmp", 0, SCR_HEIGHT - ICON_SIZE);
Expand Down
1 change: 0 additions & 1 deletion DartsBoard/src/States/GameLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ GameLogic::GameLogic(DisplayContainer* displayContainer, PlayerContainer* player
}

void GameLogic::init() {
states = new AppState*[NR_OF_APPSTATES];
states[0] = &mainScreen;
states[1] = &playerScreen;
states[2] = &gameSelectScreen;
Expand Down
Loading

0 comments on commit 2c7d651

Please sign in to comment.