Skip to content

Commit 842bcc2

Browse files
reorder device functions alphabetically and new view temp feature
1 parent c1cec4a commit 842bcc2

5 files changed

Lines changed: 185 additions & 84 deletions

File tree

src/devices/thermal_control.py

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -29,62 +29,47 @@ def __init__(self, titrator):
2929
self._amplitude = 0.0
3030
self._period_in_seconds = 0
3131

32-
def get_heat(self, default):
33-
"""
34-
Get the heat setting from EEPROM
35-
"""
36-
if self._heat is None:
37-
return default
38-
return self._heat
39-
40-
def set_heat(self, value):
32+
def get_amplitude(self):
4133
"""
42-
Set the heat setting in EEPROM
34+
Get the amplitude for the pH function.
4335
"""
44-
self._heat = value
36+
return self._amplitude
4537

4638
def get_base_thermal_target(self):
4739
"""
4840
Get the base thermal target
4941
"""
5042
return self._base_thermal_target
5143

52-
def set_base_thermal_target(self, value):
53-
"""
54-
Set the base thermal target
55-
"""
56-
self._base_thermal_target = value
57-
5844
def get_current_thermal_target(self):
5945
"""
6046
Get the current thermal target
6147
"""
6248
return self._current_thermal_target
6349

64-
def set_current_thermal_target(self, value):
50+
def get_heat(self, default):
6551
"""
66-
Set the current thermal target
52+
Get the heat setting from EEPROM
6753
"""
68-
self._current_thermal_target = value
54+
if self._heat is None:
55+
return default
56+
return self._heat
6957

70-
def get_thermal_function_type(self):
58+
def get_period_in_seconds(self):
7159
"""
72-
Get the current thermal function type.
60+
Get the period in seconds for the pH function.
7361
"""
74-
return self._thermal_function_type
62+
return self._period_in_seconds
7563

76-
def set_thermal_function_type(self, function_type):
64+
def get_ramp_time_end(self):
7765
"""
78-
Set the current thermal function type.
66+
Get the ramp time end in seconds.
7967
"""
80-
if function_type in (
81-
ThermalControl.FLAT_TYPE,
82-
ThermalControl.RAMP_TYPE,
83-
ThermalControl.SINE_TYPE,
84-
):
85-
self._thermal_function_type = function_type
86-
else:
87-
raise ValueError("Invalid thermal function type")
68+
return (
69+
self._ramp_time_end_seconds
70+
if self._thermal_function_type != ThermalControl.FLAT_TYPE
71+
else 0
72+
)
8873

8974
def get_ramp_time_start(self):
9075
"""
@@ -96,15 +81,35 @@ def get_ramp_time_start(self):
9681
else 0
9782
)
9883

99-
def get_ramp_time_end(self):
84+
def get_thermal_function_type(self):
10085
"""
101-
Get the ramp time end in seconds.
86+
Get the current thermal function type.
10287
"""
103-
return (
104-
self._ramp_time_end_seconds
105-
if self._thermal_function_type != ThermalControl.FLAT_TYPE
106-
else 0
107-
)
88+
return self._thermal_function_type
89+
90+
def set_amplitude(self, amplitude):
91+
"""
92+
Set the amplitude for the pH function.
93+
"""
94+
self._amplitude = amplitude
95+
96+
def set_base_thermal_target(self, value):
97+
"""
98+
Set the base thermal target
99+
"""
100+
self._base_thermal_target = value
101+
102+
def set_current_thermal_target(self, value):
103+
"""
104+
Set the current thermal target
105+
"""
106+
self._current_thermal_target = value
107+
108+
def set_heat(self, value):
109+
"""
110+
Set the heat setting in EEPROM
111+
"""
112+
self._heat = value
108113

109114
def set_ramp_duration_hours(self, new_ph_ramp_duration):
110115
"""
@@ -133,24 +138,6 @@ def set_ramp_duration_hours(self, new_ph_ramp_duration):
133138
self._thermal_function_type = ThermalControl.FLAT_TYPE
134139
print("Set ramp time to 0")
135140

136-
def get_amplitude(self):
137-
"""
138-
Get the amplitude for the pH function.
139-
"""
140-
return self._amplitude
141-
142-
def set_amplitude(self, amplitude):
143-
"""
144-
Set the amplitude for the pH function.
145-
"""
146-
self._amplitude = amplitude
147-
148-
def get_period_in_seconds(self):
149-
"""
150-
Get the period in seconds for the pH function.
151-
"""
152-
return self._period_in_seconds
153-
154141
def set_sine_amplitude_and_hours(self, amplitude, period_in_hours):
155142
"""
156143
Set the amplitude and period (in hours) for the sine wave pH function.
@@ -161,3 +148,16 @@ def set_sine_amplitude_and_hours(self, amplitude, period_in_hours):
161148
self._thermal_function_type = ThermalControl.SINE_TYPE
162149
else:
163150
raise ValueError("Amp and period !> than 0.")
151+
152+
def set_thermal_function_type(self, function_type):
153+
"""
154+
Set the current thermal function type.
155+
"""
156+
if function_type in (
157+
ThermalControl.FLAT_TYPE,
158+
ThermalControl.RAMP_TYPE,
159+
ThermalControl.SINE_TYPE,
160+
):
161+
self._thermal_function_type = function_type
162+
else:
163+
raise ValueError("Invalid thermal function type")

src/devices/thermal_probe.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,42 @@ def __init__(self, eeprom):
2525
self.first_time = True
2626
self.last_time = 0
2727

28-
def get_thermal_correction(self):
29-
"""
30-
Get the thermal correction value from EEPROM
31-
"""
32-
return float(self._correction)
33-
3428
def clear_thermal_correction(self):
3529
"""
3630
Clear the thermal correction value in EEPROM
3731
"""
3832
self._correction = 0
3933

40-
def set_thermal_correction(self, value):
34+
def get_raw_temperature(self):
4135
"""
42-
Set the thermal correction value in EEPROM
36+
Simulate reading the raw temperature from a sensor.
37+
In a real implementation, this method would interface with hardware.
4338
"""
44-
self._correction = value
39+
# Placeholder for actual sensor reading logic
40+
return 25.0 # return thermo.temperature(RTDnominal, refResistor);
41+
42+
def get_running_average(self):
43+
"""
44+
Return the corrected running average within the range of 00.00-99.99
45+
"""
46+
temperature = self.get_uncorrected_running_average() + self._correction
47+
if temperature < 0.0:
48+
temperature = 0.0
49+
elif temperature > 99.99:
50+
temperature = 99.99
51+
return temperature
52+
53+
def get_thermal_correction(self):
54+
"""
55+
Get the thermal correction value from EEPROM
56+
"""
57+
return float(self._correction)
4558

4659
def get_uncorrected_running_average(self):
4760
"""
4861
Calculate the uncorrected running average of temperature readings.
4962
"""
50-
current_time = time.time() # Get current time in seconds
63+
current_time = time.time()
5164
if (
5265
self.first_time or self.last_time + 1 <= current_time
5366
): # Check if 1 second has passed
@@ -66,21 +79,8 @@ def get_uncorrected_running_average(self):
6679
valid_readings = self.history[: self.history_index + 1]
6780
return sum(valid_readings) / len(valid_readings)
6881

69-
def get_running_average(self):
70-
"""
71-
Return the corrected running average within the range of 00.00-99.99
72-
"""
73-
temperature = self.get_uncorrected_running_average() + self._correction
74-
if temperature < 0.0:
75-
temperature = 0.0
76-
elif temperature > 99.99:
77-
temperature = 99.99
78-
return temperature
79-
80-
def get_raw_temperature(self):
82+
def set_thermal_correction(self, value):
8183
"""
82-
Simulate reading the raw temperature from a sensor.
83-
In a real implementation, this method would interface with hardware.
84+
Set the thermal correction value in EEPROM
8485
"""
85-
# Placeholder for actual sensor reading logic
86-
return 25.0 # return thermo.temperature(RTDnominal, refResistor);
86+
self._correction = value

src/ui_state/main_menu.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from src.ui_state.view_menu.view_ph_calibration import ViewPHCalibration
3434
from src.ui_state.view_menu.view_pid_constants import ViewPIDConstants
3535
from src.ui_state.view_menu.view_tank_id import ViewTankID
36+
from src.ui_state.view_menu.view_thermal import ViewThermal
3637
from src.ui_state.view_menu.view_thermal_correction import (
3738
ViewThermalCorrection,
3839
)
@@ -61,6 +62,7 @@ def __init__(self, titrator):
6162
"View pH slope",
6263
"View PID",
6364
"View tank ID",
65+
"View temp",
6466
"View temp cal",
6567
"View time",
6668
"View version",
@@ -95,6 +97,7 @@ def __init__(self, titrator):
9597
ViewPHCalibration, # View pH slope
9698
ViewPIDConstants, # View PID constants
9799
ViewTankID, # View Tank ID
100+
ViewThermal, # View Temperature
98101
ViewThermalCorrection, # View Thermal Correction
99102
ViewTime, # View Time
100103
ViewVersion, # View Version
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
The file to hold the View Thermal class
3+
"""
4+
5+
from src.devices.library import Keypad
6+
from src.ui_state.ui_state import UIState
7+
8+
9+
class ViewThermal(UIState):
10+
"""
11+
This is a class for the ViewThermal state of the Tank Controller
12+
"""
13+
14+
def __init__(self, titrator, previous_state=None):
15+
"""
16+
The constructor for the ViewThermal class
17+
"""
18+
super().__init__(titrator)
19+
self.previous_state = previous_state
20+
21+
def loop(self):
22+
"""
23+
The loop function for the ViewThermal class
24+
"""
25+
self.titrator.lcd.print("Avg Raw", line=1)
26+
average = self.titrator.thermal_probe.get_running_average()
27+
raw = self.titrator.thermal_probe.get_raw_temperature()
28+
self.titrator.lcd.print(
29+
f"{average} {raw}", line=2
30+
)
31+
32+
def handle_key(self, key):
33+
"""
34+
The handle_key function for the ViewThermal class
35+
"""
36+
if key in [Keypad.KEY_4, Keypad.KEY_D]:
37+
self._set_next_state(self.previous_state, True)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
The file to test the View Thermal class
3+
"""
4+
5+
from unittest import mock
6+
7+
from src.devices.library import LiquidCrystal
8+
from src.titrator import Titrator
9+
from src.ui_state.main_menu import MainMenu
10+
from src.ui_state.ui_state import UIState
11+
from src.ui_state.view_menu.view_thermal import ViewThermal
12+
13+
14+
class MockPreviousState(UIState):
15+
"""
16+
A mock previous state for testing purposes
17+
"""
18+
19+
def __init__(self, titrator):
20+
super().__init__(titrator)
21+
22+
23+
@mock.patch.object(LiquidCrystal, "print")
24+
def test_view_thermal_loop(print_mock):
25+
"""
26+
The function to test ViewThermal's loop function
27+
"""
28+
titrator = Titrator()
29+
titrator.thermal_probe.get_running_average = mock.Mock(return_value="25.5")
30+
titrator.thermal_probe.get_raw_temperature = mock.Mock(return_value="26.0")
31+
32+
state = ViewThermal(titrator, MainMenu(titrator))
33+
34+
state.loop()
35+
36+
print_mock.assert_any_call("Avg Raw", line=1)
37+
print_mock.assert_any_call("25.5 26.0", line=2)
38+
39+
40+
def test_handle_key_4():
41+
"""
42+
The function to test the back handle key
43+
"""
44+
titrator = Titrator()
45+
46+
titrator.state = ViewThermal(titrator, MockPreviousState(titrator))
47+
48+
titrator.state.handle_key("4")
49+
assert isinstance(titrator.state, MockPreviousState)
50+
51+
52+
def test_handle_key_d():
53+
"""
54+
The function to test the reset handle keys
55+
"""
56+
titrator = Titrator()
57+
58+
titrator.state = ViewThermal(titrator, MockPreviousState(titrator))
59+
60+
titrator.state.handle_key("D")
61+
assert isinstance(titrator.state, MockPreviousState)

0 commit comments

Comments
 (0)