Skip to content

Commit e05ede7

Browse files
committed
fix: annoying memory leak bug
1 parent 7593f41 commit e05ede7

4 files changed

Lines changed: 36 additions & 65 deletions

File tree

.vscode/settings.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"files.associations": {
3+
"array": "cpp",
4+
"deque": "cpp",
5+
"string": "cpp",
6+
"unordered_map": "cpp",
7+
"vector": "cpp",
8+
"string_view": "cpp",
9+
"initializer_list": "cpp"
10+
}
11+
}

fossa/100_config.ino

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ void printDefaultValues() {
9191
Serial.println("Charge: " + String(charge) + "%");
9292
Serial.println("Max amount: " + String(maxAmount));
9393
Serial.println("Max before reset: " + String(maxBeforeReset));
94-
convertStringToFloatArray(coinAmounts.c_str(), coinAmountFloat);
95-
convertStringToIntArray(billAmounts.c_str(), billAmountInt);
94+
convertToFloatArray(coinAmounts.c_str(), coinAmountFloat);
95+
convertToFloatArray(billAmounts.c_str(), billAmountFloat);
9696
Serial.println("Coin amounts: " + String(coinAmounts));
9797
Serial.println("Bill amounts: " + String(billAmounts));
9898
}
@@ -157,10 +157,12 @@ void readFiles() {
157157
}
158158

159159
billAmounts = getJsonValue(doc, "config_bill_ints");
160-
convertStringToIntArray(billAmounts.c_str(), billAmountInt);
160+
convertToFloatArray(billAmounts.c_str(), billAmountFloat);
161+
Serial.println(billAmountFloat[0]);
161162

162163
coinAmounts = getJsonValue(doc, "config_coin_floats");
163-
convertStringToFloatArray(coinAmounts.c_str(), coinAmountFloat);
164+
convertToFloatArray(coinAmounts.c_str(), coinAmountFloat);
165+
Serial.println(billAmountFloat[0]);
164166

165167
String langConfig = getJsonValue(doc, "config_lang");
166168
if (langConfig != "") {

fossa/102_helpers.ino

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -48,44 +48,16 @@ void splitSettings(String str) {
4848
Serial.println("currencyATM: " + currencyATM);
4949
}
5050

51-
void convertStringToFloatArray(const char* str, float* floatArray) {
52-
char buffer[30]; // Temporary buffer to hold each substring
53-
int index = 0; // Index for the float array
54-
int bufferIndex = 0; // Index for the buffer
51+
void convertToFloatArray(const char* str, float* floatArray) {
52+
char temp[50];
53+
strncpy(temp, str, sizeof(temp)); // copy input to mutable buffer
54+
temp[sizeof(temp) - 1] = '\0'; // ensure null-termination
5555

56-
for (int i = 0; str[i] != '\0'; i++) {
57-
if (str[i] == ',') { // When a comma is found
58-
buffer[bufferIndex] = '\0'; // Null-terminate the buffer string
59-
floatArray[index] = atof(buffer); // Convert buffer to float and store in array
60-
index++; // Move to the next position in float array
61-
bufferIndex = 0; // Reset buffer index
62-
} else {
63-
buffer[bufferIndex++] = str[i]; // Copy characters to buffer
64-
}
65-
}
66-
67-
// Don't forget to convert the last number in the string
68-
buffer[bufferIndex] = '\0'; // Null-terminate the buffer
69-
floatArray[index] = atof(buffer); // Convert buffer to float
70-
}
56+
int index = 0;
57+
char* token = strtok(temp, ",");
7158

72-
void convertStringToIntArray(const char* str, int* intArray) {
73-
char buffer[20]; // Temporary buffer to hold each substring
74-
int index = 0; // Index for the integer array
75-
int bufferIndex = 0; // Index for the buffer
76-
77-
for (int i = 0; str[i] != '\0'; i++) {
78-
if (str[i] == ',') { // When a comma is found
79-
buffer[bufferIndex] = '\0'; // Null-terminate the buffer string
80-
intArray[index] = atoi(buffer); // Convert buffer to int and store in array
81-
index++; // Move to the next position in integer array
82-
bufferIndex = 0; // Reset buffer index
83-
} else {
84-
buffer[bufferIndex++] = str[i]; // Copy characters to buffer
85-
}
59+
while (token != NULL && index < 10) {
60+
floatArray[index++] = strtof(token, NULL); // convert substring to float
61+
token = strtok(NULL, ",");
8662
}
87-
88-
// Don't forget to convert the last number in the string
89-
buffer[bufferIndex] = '\0'; // Null-terminate the buffer
90-
intArray[index] = atoi(buffer); // Convert buffer to int
91-
}
63+
}

fossa/fossa.ino

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232

3333
bool hardcoded = HARDCODED; // set to true if you want to use the above hardcoded settings
3434
bool printerBool = false;
35-
int billAmountInt[10];
36-
float coinAmountFloat[6];
35+
float billAmountFloat[10];
36+
float coinAmountFloat[10];
3737

3838
///////////////////////////////////////////////////
3939
///////////////////////////////////////////////////
@@ -60,7 +60,7 @@ int maxBeforeResetTally;
6060
int bills;
6161
float coins;
6262
float total;
63-
int billAmountSize = sizeof(billAmountInt) / sizeof(int);
63+
float billAmountSize = sizeof(billAmountFloat) / sizeof(float);
6464
float coinAmountSize = sizeof(coinAmountFloat) / sizeof(float);
6565
int moneyTimer = 0;
6666
bool waitForTap = true;
@@ -92,6 +92,7 @@ void setup() {
9292
tft.setRotation(1);
9393
tft.invertDisplay(false);
9494
printMessage("", "Loading..", "", TFT_WHITE, TFT_BLACK);
95+
printMessage("", "Loading..", "", TFT_BLACK, TFT_WHITE);
9596

9697
// wait few secods for tap to start config mode
9798
while (waitForTap && total < 100) {
@@ -117,21 +118,6 @@ void setup() {
117118
SerialPort1.begin(300, SERIAL_8N2, BILL_TX, BILL_RX);
118119
SerialPort2.begin(4800, SERIAL_8N1, COIN_TX);
119120
printerSerial.begin(9600);
120-
printer.begin();
121-
printer.wake();
122-
printer.setDefault();
123-
printer.justify('C');
124-
printer.feed(3);
125-
printer.boldOn();
126-
printer.setSize('L');
127-
printer.println("Printer Connected :)");
128-
printer.println("");
129-
printer.println("");
130-
printer.println("");
131-
printer.println("");
132-
printer.println("");
133-
printer.sleep();
134-
135121
pinMode(COIN_INHIBIT, OUTPUT);
136122
}
137123

@@ -153,10 +139,10 @@ void loop() {
153139
Serial.println("Coin acceptor connected");
154140
}
155141
moneyTimerFun();
156-
Serial.println(total);
157-
Serial.println(maxBeforeResetTally);
142+
Serial.println("total" + String(total));
143+
Serial.println("maxBeforeResetTally" + String(maxBeforeResetTally));
158144
maxBeforeResetTally = maxBeforeResetTally + (total / 100);
159-
Serial.println(maxBeforeResetTally);
145+
Serial.println("maxBeforeResetTally" + String(maxBeforeResetTally));
160146
makeLNURL();
161147
qrShowCodeLNURL(scanMeT);
162148
}
@@ -180,9 +166,9 @@ void moneyTimerFun() {
180166
int x = SerialPort1.read();
181167
for (int i = 0; i < billAmountSize; i++) {
182168
if ((i + 1) == x) {
183-
bills = bills + billAmountInt[i];
169+
bills = bills + billAmountFloat[i];
184170
total = (coins + bills);
185-
printMessage(billAmountInt[i] + currencyATM, totalT + String(total) + currencyATM, tapScreenT, TFT_WHITE, TFT_BLACK);
171+
printMessage(billAmountFloat[i] + currencyATM, totalT + String(total) + currencyATM, tapScreenT, TFT_WHITE, TFT_BLACK);
186172
}
187173
}
188174
}

0 commit comments

Comments
 (0)