Skip to content

Commit 22383fd

Browse files
author
James Foster
authored
Show temperature control in idle screen (#226)
* Finish the idle screen display of the control status (fix #216).
1 parent e24ba2e commit 22383fd

7 files changed

Lines changed: 137 additions & 93 deletions

File tree

.github/actions/spelling/expect.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ testfile
134134
timeinfo
135135
Tmp
136136
TODO
137+
toupper
137138
typedef
138139
uint
139140
undef

src/Devices/Serial_TC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void Serial_TC::vprintf(const char *format, va_list args) {
5656
}
5757
#ifdef MOCK_PINS_COUNT
5858
#if DEBUG
59-
// std::cout << "\tSERIAL: " << buffer << std::endl;
59+
std::cout << "\tSERIAL: " << buffer << std::endl;
6060
#endif
6161
#endif
6262
}

src/Devices/TemperatureControl.cpp

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,16 @@ void TemperatureControl::enableHeater(bool flag) {
4242
* protected constructor
4343
*/
4444
TemperatureControl::TemperatureControl() {
45+
COUT("TemperatureControl()");
4546
targetTemperature = EEPROM_TC::instance()->getTemp();
4647
if (isnan(targetTemperature)) {
4748
targetTemperature = DEFAULT_TEMPERATURE;
4849
EEPROM_TC::instance()->setTemp(targetTemperature);
4950
}
5051
pinMode(TEMP_CONTROL_PIN, OUTPUT);
51-
pinValue = TURN_SOLENOID_OFF;
52-
digitalWrite(TEMP_CONTROL_PIN, pinValue);
53-
serial("%s with target temperature of %5.2f C", this->isHeater() ? "Heater" : "Chiller", targetTemperature);
52+
digitalWrite(TEMP_CONTROL_PIN, TURN_SOLENOID_OFF);
53+
serial("%s starts with solenoid off with target temperature of %5.2f C", this->isHeater() ? "Heater" : "Chiller",
54+
targetTemperature);
5455
}
5556

5657
/**
@@ -60,6 +61,10 @@ bool TemperatureControl::isHeater() {
6061
return true;
6162
}
6263

64+
bool TemperatureControl::isOn() {
65+
return digitalRead(TEMP_CONTROL_PIN) == TURN_SOLENOID_ON;
66+
}
67+
6368
/**
6469
* set target temperature and save in EEPROM
6570
*/
@@ -73,35 +78,48 @@ void TemperatureControl::setTargetTemperature(float newTemperature) {
7378

7479
void Chiller::updateControl(float currentTemperature) {
7580
uint32_t currentMillis = millis();
81+
COUT("Chiller::updateControl(" << currentTemperature << ") at " << currentMillis);
82+
if (currentMillis < previousMillis) {
83+
COUT("Reset previousMillis from " << previousMillis << " to 0");
84+
previousMillis = 0; // reset if clock went backwards (typical during tests)
85+
}
7686
// pause 30 seconds between switching chiller on and off to prevent damage to chiller
77-
if (currentMillis - previousMillis >= TIME_INTERVAL) {
78-
bool newValue = pinValue;
87+
if (currentMillis - previousMillis < TIME_INTERVAL) {
88+
COUT("Chiller update at " << currentMillis << " ignored due to update at " << previousMillis);
89+
} else {
90+
bool oldValue = digitalRead(TEMP_CONTROL_PIN);
91+
bool newValue;
7992
previousMillis = currentMillis;
8093
// if in calibration, turn unit off
8194
if (TankControllerLib::instance()->isInCalibration()) {
8295
newValue = TURN_SOLENOID_OFF;
96+
COUT("Chiller should be off");
8397
}
8498
// if the observed temperature is above the set-point range turn on the chiller
8599
else if (currentTemperature >= targetTemperature + DELTA) {
86100
newValue = TURN_SOLENOID_ON;
101+
COUT("Chiller should be on");
87102
}
88103
// if the observed temperature is below the set-point range turn off the chiller
89104
else if (currentTemperature <= targetTemperature - DELTA) {
90105
newValue = TURN_SOLENOID_OFF;
106+
COUT("Chiller should be off");
107+
} else {
108+
newValue = oldValue;
91109
}
92-
if (newValue != pinValue) {
93-
pinValue = newValue;
94-
DateTime_TC::now().printToSerial();
110+
if (newValue != oldValue) {
95111
uint32_t currentMS = millis();
96-
serial("chiller turned %s after %lu ms", pinValue ? "off" : "on", currentMS - lastSwitchMS);
112+
serial("chiller turned %s at %lu after %lu ms", newValue ? "off" : "on", currentMS, currentMS - lastSwitchMS);
97113
lastSwitchMS = currentMS;
98-
digitalWrite(TEMP_CONTROL_PIN, pinValue);
114+
digitalWrite(TEMP_CONTROL_PIN, newValue);
99115
}
100116
}
101117
}
102118

103119
void Heater::updateControl(float currentTemperature) {
104-
bool newValue = pinValue;
120+
COUT("Heater::updateControl(" << currentTemperature);
121+
bool oldValue = digitalRead(TEMP_CONTROL_PIN);
122+
bool newValue;
105123
// if in calibration, turn unit off
106124
if (TankControllerLib::instance()->isInCalibration()) {
107125
newValue = TURN_SOLENOID_OFF;
@@ -113,13 +131,14 @@ void Heater::updateControl(float currentTemperature) {
113131
// if the observed temperature is above the temperature set-point range turn off the heater
114132
else if (currentTemperature >= targetTemperature + DELTA) {
115133
newValue = TURN_SOLENOID_OFF;
134+
} else {
135+
newValue = oldValue;
136+
COUT("Heater update at " << millis() << " ignored due to recent change in state");
116137
}
117-
if (newValue != pinValue) {
118-
pinValue = newValue;
119-
DateTime_TC::now().printToSerial();
138+
if (newValue != oldValue) {
120139
uint32_t currentMS = millis();
121-
serial("heater turned %s after %lu ms", pinValue ? "off" : "on", currentMS - lastSwitchMS);
140+
serial("heater turned %s at %lu after %lu ms", newValue ? "off" : "on", currentMS, currentMS - lastSwitchMS);
122141
lastSwitchMS = currentMS;
123-
digitalWrite(TEMP_CONTROL_PIN, pinValue);
142+
digitalWrite(TEMP_CONTROL_PIN, newValue);
124143
}
125144
}

src/Devices/TemperatureControl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ class TemperatureControl {
1818
const float DELTA = 0.05;
1919
uint32_t lastSwitchMS = 0;
2020
float targetTemperature;
21-
bool pinValue = TURN_SOLENOID_OFF;
2221
TemperatureControl();
2322

2423
public:
@@ -30,6 +29,7 @@ class TemperatureControl {
3029
return targetTemperature;
3130
}
3231
virtual bool isHeater();
32+
bool isOn();
3333
void setTargetTemperature(float newTemperature);
3434
virtual void updateControl(float currentTemperature) = 0;
3535
};

src/UIState/MainMenu.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,13 @@ void MainMenu::idle() {
230230
} else if (99.99 < temp) {
231231
temp = 99.99;
232232
}
233-
snprintf(output, sizeof(output), "T%c%5.2f %c %5.2f", equals, temp, (tempControl->isHeater() ? 'H' : 'C'),
234-
tempControl->getTargetTemperature());
233+
char status = tempControl->isHeater() ? 'h' : 'c';
234+
if (tempControl->isOn()) {
235+
status = toupper(status);
236+
// status -= 'a' - 'A'; // convert to uppercase
237+
}
238+
239+
snprintf(output, sizeof(output), "T%c%5.2f %c %5.2f", equals, temp, status, tempControl->getTargetTemperature());
235240
LiquidCrystal_TC::instance()->writeLine(output, 1);
236241
}
237242

test/MenuTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ unittest_teardown() {
3939

4040
unittest(MainMenu) {
4141
assertEqual("pH=0.000 8.100", lc->getLines().at(0));
42-
assertEqual("T=12.23 H 15.75 ", lc->getLines().at(1));
42+
assertEqual("T=12.23 h 15.75 ", lc->getLines().at(1));
4343
delay(1000);
4444
tc->loop();
4545
assertEqual("pH 0.000 8.100", lc->getLines().at(0));

0 commit comments

Comments
 (0)