Skip to content

Commit 66fabe5

Browse files
author
James Foster
authored
v21.07.2 (#217)
- Memory cleanup to avoid stack/heap collision (fix #186 ?) - Remove many uses of String - Add View Free Memory menu option
1 parent 9f4b7d4 commit 66fabe5

29 files changed

Lines changed: 132 additions & 48 deletions
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
brkval
2+
bss

src/Devices/EthernetServer_TC.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class EthernetServer_TC : public EthernetServer {
1717
// class methods
1818
static EthernetServer_TC* instance();
1919
// instance methods
20-
virtual String className() const {
20+
const char* className() const {
2121
return "EthernetServer_TC";
2222
}
2323
void handleRequest();

src/TankControllerLib.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "UIState/MainMenu.h"
2020
#include "UIState/UIState.h"
2121

22-
const char TANK_CONTROLLER_VERSION[] = "21.07.1";
22+
const char TANK_CONTROLLER_VERSION[] = "21.07.2";
2323

2424
// ------------ Class Methods ------------
2525
/**
@@ -86,6 +86,23 @@ void TankControllerLib::blink() {
8686
}
8787
}
8888

89+
// https://github.com/maniacbug/MemoryFree/blob/master/MemoryFree.cpp
90+
size_t TankControllerLib::freeMemory() {
91+
#ifdef MOCK_PINS_COUNT
92+
int *__brkval = 0;
93+
int __bss_end = 0;
94+
#else
95+
extern int *__brkval;
96+
extern int __bss_end;
97+
#endif
98+
int topOfStack;
99+
100+
if ((size_t)__brkval == 0) {
101+
return ((size_t)&topOfStack) - ((size_t)&__bss_end);
102+
}
103+
return ((size_t)&topOfStack) - ((size_t)__brkval);
104+
}
105+
89106
/**
90107
* Is the current UIState one that should disable controls?
91108
* We don't want to turn on the heat/chill if the temperature probe is out of the tank!
@@ -174,6 +191,7 @@ void TankControllerLib::setup() {
174191
wdt_enable(WDTO_8S);
175192
serial("TankControllerLib::setup()");
176193
SD_TC::instance()->printRootDirectory();
194+
serial("Free memory = %i", freeMemory());
177195
}
178196

179197
/**

src/TankControllerLib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class TankControllerLib {
1616

1717
// instance methods
1818
bool isInCalibration();
19+
size_t freeMemory();
1920
void loop();
2021
void serialEvent();
2122
void serialEvent1();

src/UIState/CalibrationManagement.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ class CalibrationManagement : public UIState {
1515
return "CalibrationManagement";
1616
}
1717
const char* prompt() {
18-
return "Cal Management ";
18+
return "Cal Management";
1919
};
2020
};

src/UIState/EnablePID.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class EnablePID : public NumCollectorState {
1818
}
1919
float getCurrentValue();
2020
const char* prompt() {
21-
return "PID 1:on; 9:off ";
21+
return "PID 1:on; 9:off";
2222
};
2323
void setValue(float value);
2424
};

src/UIState/MainMenu.cpp

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "ResetLCDScreen.h"
1212
#include "SeeDeviceAddress.h"
1313
#include "SeeDeviceUptime.h"
14+
#include "SeeFreeMemory.h"
1415
#include "SeeGoogleMins.h"
1516
#include "SeeLogFile.h"
1617
#include "SeePHSlope.h"
@@ -30,28 +31,29 @@
3031
#include "TemperatureCalibration.h"
3132

3233
MainMenu::MainMenu(TankControllerLib *tc) : UIState(tc) {
33-
viewMenus[VIEW_GOOGLE_MINS] = String("View Google mins");
34-
viewMenus[VIEW_ADDRESS] = String("View IP and MAC ");
35-
viewMenus[VIEW_LOG_FILE] = String("View log file ");
36-
viewMenus[VIEW_PID] = String("View PID ");
37-
viewMenus[VIEW_PH_SLOPE] = String("View pH slope ");
38-
viewMenus[VIEW_TANK_ID] = String("View tank ID ");
39-
viewMenus[VIEW_TIME] = String("View time ");
40-
viewMenus[VIEW_VERSION] = String("View version ");
34+
viewMenus[VIEW_GOOGLE_MINS] = "View Google mins";
35+
viewMenus[VIEW_ADDRESS] = "View IP and MAC";
36+
viewMenus[VIEW_FREE_MEMORY] = "View free memory";
37+
viewMenus[VIEW_LOG_FILE] = "View log file";
38+
viewMenus[VIEW_PID] = "View PID";
39+
viewMenus[VIEW_PH_SLOPE] = "View pH slope";
40+
viewMenus[VIEW_TANK_ID] = "View tank ID";
41+
viewMenus[VIEW_TIME] = "View time";
42+
viewMenus[VIEW_VERSION] = "View version";
4143

42-
setMenus[SET_CALIBRATION] = String("pH calibration ");
43-
setMenus[SET_CALIBRATION_CLEAR] = String("Clear pH calibra");
44-
setMenus[SET_CHILL_OR_HEAT] = String("Set chill/heat ");
45-
setMenus[SET_GOOGLE_MINS] = String("Set Google mins ");
46-
setMenus[SET_PH] = String("Set pH target ");
47-
setMenus[SET_KD] = String("Set KD ");
48-
setMenus[SET_KI] = String("Set KI ");
49-
setMenus[SET_KP] = String("Set KP ");
50-
setMenus[SET_PID_ON_OFF] = String("PID on/off ");
51-
setMenus[SET_TANK_ID] = String("Set Tank ID ");
52-
setMenus[SET_TEMP_CALIBRATION] = String("Temp calibration");
53-
setMenus[SET_TEMPERATURE] = String("Set temperature ");
54-
setMenus[SET_TIME] = String("Set date/time ");
44+
setMenus[SET_CALIBRATION] = "pH calibration";
45+
setMenus[SET_CALIBRATION_CLEAR] = "Clear pH calibra";
46+
setMenus[SET_CHILL_OR_HEAT] = "Set chill/heat";
47+
setMenus[SET_GOOGLE_MINS] = "Set Google mins";
48+
setMenus[SET_PH] = "Set pH target";
49+
setMenus[SET_KD] = "Set KD";
50+
setMenus[SET_KI] = "Set KI";
51+
setMenus[SET_KP] = "Set KP";
52+
setMenus[SET_PID_ON_OFF] = "PID on/off";
53+
setMenus[SET_TANK_ID] = "Set Tank ID";
54+
setMenus[SET_TEMP_CALIBRATION] = "Temp calibration";
55+
setMenus[SET_TEMPERATURE] = "Set temperature";
56+
setMenus[SET_TIME] = "Set date/time";
5557
}
5658

5759
/**
@@ -140,6 +142,9 @@ void MainMenu::selectView() {
140142
case VIEW_ADDRESS:
141143
this->setNextState(static_cast<UIState *>(new SeeDeviceAddress(tc)));
142144
break;
145+
case VIEW_FREE_MEMORY:
146+
this->setNextState(static_cast<UIState *>(new SeeFreeMemory(tc)));
147+
break;
143148
case VIEW_LOG_FILE:
144149
this->setNextState(static_cast<UIState *>(new SeeLogFile(tc)));
145150
break;
@@ -236,13 +241,13 @@ void MainMenu::loop() {
236241
if (level2 == -1) {
237242
LiquidCrystal_TC::instance()->writeLine("View settings", 0);
238243
} else {
239-
LiquidCrystal_TC::instance()->writeLine(viewMenus[level2].c_str(), 0);
244+
LiquidCrystal_TC::instance()->writeLine(viewMenus[level2], 0);
240245
}
241246
} else {
242247
if (level2 == -1) {
243248
LiquidCrystal_TC::instance()->writeLine("Change settings ", 0);
244249
} else {
245-
LiquidCrystal_TC::instance()->writeLine(setMenus[level2].c_str(), 0);
250+
LiquidCrystal_TC::instance()->writeLine(setMenus[level2], 0);
246251
}
247252
}
248253
LiquidCrystal_TC::instance()->writeLine("<4 ^2 8v 6>", 1);

src/UIState/MainMenu.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class MainMenu : public UIState {
2121
VIEW_GOOGLE_MINS,
2222
VIEW_ADDRESS,
2323
VIEW_VERSION,
24+
VIEW_FREE_MEMORY,
2425
VIEW_COMMAND_COUNT // always last
2526
};
2627
enum SetCommands {
@@ -45,7 +46,7 @@ class MainMenu : public UIState {
4546
return "MainMenu";
4647
}
4748
const char* prompt() {
48-
return "Main Menu ";
49+
return "Main Menu";
4950
};
5051
bool isMainMenu() {
5152
return true;
@@ -55,8 +56,8 @@ class MainMenu : public UIState {
5556
private:
5657
int16_t level1 = 0;
5758
int16_t level2 = -1;
58-
String viewMenus[VIEW_COMMAND_COUNT];
59-
String setMenus[SET_COMMAND_COUNT];
59+
const char* viewMenus[VIEW_COMMAND_COUNT];
60+
const char* setMenus[SET_COMMAND_COUNT];
6061
void left();
6162
void right();
6263
void up();

src/UIState/PHCalibrationHigh.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class PHCalibrationHigh : public PHCalibration {
1414
return "PHCalibrationHigh";
1515
}
1616
const char* prompt() {
17-
return "pH-Highpoint ";
17+
return "pH-Highpoint";
1818
};
1919
void setValue(float value);
2020
};

src/UIState/PHCalibrationLow.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class PHCalibrationLow : public PHCalibration {
1414
return "PHCalibrationLow";
1515
}
1616
const char* prompt() {
17-
return "pH-Lowpoint ";
17+
return "pH-Lowpoint";
1818
};
1919
void setValue(float value);
2020
};

0 commit comments

Comments
 (0)