Skip to content

Commit 772bcf6

Browse files
Add ph calibration states
1 parent a0d81f3 commit 772bcf6

8 files changed

Lines changed: 398 additions & 12 deletions

src/ui_state/set_menu/set_ph_calibration.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
The file to hold the PHCalibration class
33
"""
44

5+
from src.ui_state.set_menu.set_ph_calibration_mid import (
6+
PHCalibrationHigher,
7+
PHCalibrationMid,
8+
PHCalibrationOnly,
9+
)
510
from src.ui_state.ui_state import UIState
611

712

@@ -28,14 +33,15 @@ def handle_key(self, key):
2833
if key == "1":
2934
self.titrator.lcd.print("1-pt pH calib...", line=2)
3035
self.return_to_main_menu(ms_delay=3000)
36+
self._set_next_state(PHCalibrationOnly(self.titrator, self), True)
3137

3238
if key == "2":
3339
self.titrator.lcd.print("2 Point Cali", line=2)
34-
self.return_to_main_menu(ms_delay=3000)
40+
self._set_next_state(PHCalibrationHigher(self.titrator, self), True)
3541

3642
if key == "3":
3743
self.titrator.lcd.print("3 Point Cali", line=2)
38-
self.return_to_main_menu(ms_delay=3000)
44+
self._set_next_state(PHCalibrationMid(self.titrator, self), True)
3945

4046
if key == "4":
4147
self._set_next_state(self.previous_state, True)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
The file to hold the Set pH Calibration Highpoint class
3+
"""
4+
5+
from src.ui_state.set_menu.set_ph_calibration_low import PHCalibrationLow
6+
from src.ui_state.user_value import UserValue
7+
8+
9+
class PHCalibrationHigh(UserValue):
10+
"""
11+
UI state to set the pH Calibration Highpoint.
12+
Uses UserValue's keypad flow: implement get_label and save_value.
13+
"""
14+
15+
def __init__(self, titrator, previous_state=None):
16+
super().__init__(titrator, previous_state)
17+
self.previous_state = previous_state
18+
self.value = str(self.titrator.ph_probe._highpoint_calibration or "")
19+
20+
def get_label(self):
21+
"""
22+
Returns the label for the user value input.
23+
"""
24+
return "High buffer pH"
25+
26+
def save_value(self):
27+
"""
28+
Saves the entered pH Calibration Highpoint to the pH probe.
29+
"""
30+
self.titrator.ph_probe.set_highpoint_calibration(float(self.value))
31+
32+
self.titrator.lcd.print(
33+
f"High = {self.titrator.ph_probe._highpoint_calibration}", line=2
34+
)
35+
self._set_next_state(PHCalibrationLow(self.titrator, self), True)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
The file to hold the Set pH Calibration Lowpoint class
3+
"""
4+
5+
from src.ui_state.user_value import UserValue
6+
7+
8+
class PHCalibrationLower(UserValue):
9+
"""
10+
Docstring for PHCalibrationLower
11+
"""
12+
13+
def __init__(self, titrator, previous_state=None):
14+
super().__init__(titrator, previous_state)
15+
self.previous_state = previous_state
16+
self.value = str(self.titrator.ph_probe._lowpoint_calibration or "")
17+
18+
def get_label(self):
19+
"""
20+
Returns the label for the user value input.
21+
"""
22+
return "Lower buffer pH"
23+
24+
def save_value(self):
25+
"""
26+
Saves the entered pH Calibration Lowpoint to the pH probe.
27+
"""
28+
self.titrator.ph_probe._lowpoint_calibration = float(self.value)
29+
30+
self.titrator.lcd.print(
31+
f"Low = {self.titrator.ph_probe._lowpoint_calibration}", line=2
32+
)
33+
self.return_to_main_menu(ms_delay=3000)
34+
35+
36+
class PHCalibrationLow(UserValue):
37+
"""
38+
UI state to set the pH Calibration Lowpoint.
39+
Uses UserValue's keypad flow: implement get_label and save_value.
40+
"""
41+
42+
def __init__(self, titrator, previous_state=None):
43+
super().__init__(titrator, previous_state)
44+
self.previous_state = previous_state
45+
self.value = str(self.titrator.ph_probe._lowpoint_calibration or "")
46+
47+
def get_label(self):
48+
"""
49+
Returns the label for the user value input.
50+
"""
51+
return "Low buffer pH"
52+
53+
def save_value(self):
54+
"""
55+
Saves the entered pH Calibration Lowpoint to the pH probe.
56+
"""
57+
self.titrator.ph_probe.set_lowpoint_calibration(float(self.value))
58+
59+
self.titrator.lcd.print(
60+
f"Low = {self.titrator.ph_probe._lowpoint_calibration}", line=2
61+
)
62+
self.return_to_main_menu(ms_delay=3000)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"""
2+
The file to hold the Set pH Calibration Midpoint class
3+
"""
4+
5+
from src.ui_state.set_menu.set_ph_calibration_high import PHCalibrationHigh
6+
from src.ui_state.set_menu.set_ph_calibration_low import PHCalibrationLower
7+
from src.ui_state.user_value import UserValue
8+
9+
10+
class PHCalibrationOnly(UserValue):
11+
"""
12+
UI state to set the pH Calibration Midpoint.
13+
Uses UserValue's keypad flow: implement get_label and save_value.
14+
"""
15+
16+
def __init__(self, titrator, previous_state=None):
17+
super().__init__(titrator, previous_state)
18+
self.previous_state = previous_state
19+
self.value = str(self.titrator.ph_probe._midpoint_calibration or "")
20+
21+
def get_label(self):
22+
"""
23+
Returns the label for the user value input.
24+
"""
25+
return "Buffer pH"
26+
27+
def save_value(self):
28+
"""
29+
Saves the entered pH Calibration Midpoint to the pH probe.
30+
"""
31+
self.titrator.ph_probe._midpoint_calibration = float(self.value)
32+
33+
self.titrator.lcd.print(
34+
f"Buffer = {self.titrator.ph_probe._midpoint_calibration}", line=2
35+
)
36+
self.return_to_main_menu(ms_delay=3000)
37+
38+
39+
class PHCalibrationHigher(UserValue):
40+
"""
41+
Docstring for PHCalibrationHigher
42+
"""
43+
44+
def __init__(self, titrator, previous_state=None):
45+
super().__init__(titrator, previous_state)
46+
self.previous_state = previous_state
47+
self.value = str(self.titrator.ph_probe._midpoint_calibration or "")
48+
49+
def get_label(self):
50+
"""
51+
Returns the label for the user value input.
52+
"""
53+
return "Higher buffer pH"
54+
55+
def save_value(self):
56+
"""
57+
Saves the entered pH Calibration Midpoint to the pH probe.
58+
"""
59+
self.titrator.ph_probe._midpoint_calibration = float(self.value)
60+
61+
self.titrator.lcd.print(
62+
f"Mid = {self.titrator.ph_probe._midpoint_calibration}", line=2
63+
)
64+
self._set_next_state(PHCalibrationLower(self.titrator, self), True)
65+
66+
67+
class PHCalibrationMid(UserValue):
68+
"""
69+
UI state to set the pH Calibration Midpoint.
70+
Uses UserValue's keypad flow: implement get_label and save_value.
71+
"""
72+
73+
def __init__(self, titrator, previous_state=None):
74+
super().__init__(titrator, previous_state)
75+
self.previous_state = previous_state
76+
self.value = str(self.titrator.ph_probe._midpoint_calibration or "")
77+
78+
def get_label(self):
79+
"""
80+
Returns the label for the user value input.
81+
"""
82+
return "Mid buffer pH"
83+
84+
def save_value(self):
85+
"""
86+
Saves the entered pH Calibration Midpoint to the pH probe.
87+
"""
88+
self.titrator.ph_probe._midpoint_calibration = float(self.value)
89+
90+
self.titrator.lcd.print(
91+
f"Mid = {self.titrator.ph_probe._midpoint_calibration}", line=2
92+
)
93+
self._set_next_state(PHCalibrationHigh(self.titrator, self), True)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
The file to test the PHCalibrationHigh 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.set_menu.set_ph_calibration_high import PHCalibrationHigh
10+
from src.ui_state.set_menu.set_ph_calibration_low import PHCalibrationLow
11+
from src.ui_state.ui_state import UIState
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_ph_calibration_high(print_mock):
25+
"""
26+
Test the PHCalibrationHigh state.
27+
"""
28+
titrator = Titrator()
29+
state = PHCalibrationHigh(titrator, MockPreviousState(titrator))
30+
31+
state.loop()
32+
print_mock.assert_any_call("High buffer pH", line=1)
33+
34+
state.value = "10.0"
35+
state.save_value()
36+
assert titrator.ph_probe._highpoint_calibration == 10.0
37+
print_mock.assert_any_call("High = 10.0", line=2)
38+
39+
assert isinstance(titrator.state, PHCalibrationLow)
40+
assert isinstance(titrator.state.previous_state, PHCalibrationHigh)
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 PHCalibrationLow 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.set_menu.set_ph_calibration_low import (
11+
PHCalibrationLow,
12+
PHCalibrationLower,
13+
)
14+
from src.ui_state.ui_state import UIState
15+
16+
17+
class MockPreviousState(UIState):
18+
"""
19+
A mock previous state for testing purposes
20+
"""
21+
22+
def __init__(self, titrator):
23+
super().__init__(titrator)
24+
25+
26+
@mock.patch.object(LiquidCrystal, "print")
27+
def test_ph_calibration_lower(print_mock):
28+
"""
29+
Test the PHCalibrationLower state.
30+
"""
31+
titrator = Titrator()
32+
state = PHCalibrationLower(titrator, MockPreviousState(titrator))
33+
34+
state.loop()
35+
print_mock.assert_any_call("Lower buffer pH", line=1)
36+
37+
state.value = "3.5"
38+
state.save_value()
39+
assert titrator.ph_probe._lowpoint_calibration == 3.5
40+
print_mock.assert_any_call("Low = 3.5", line=2)
41+
42+
assert isinstance(titrator.state.next_state, MainMenu)
43+
44+
45+
@mock.patch.object(LiquidCrystal, "print")
46+
def test_ph_calibration_low(print_mock):
47+
"""
48+
Test the PHCalibrationLow state.
49+
"""
50+
titrator = Titrator()
51+
state = PHCalibrationLow(titrator, MockPreviousState(titrator))
52+
53+
state.loop()
54+
print_mock.assert_any_call("Low buffer pH", line=1)
55+
56+
state.value = "4.0"
57+
state.save_value()
58+
assert titrator.ph_probe._lowpoint_calibration == 4.0
59+
print_mock.assert_any_call("Low = 4.0", line=2)
60+
61+
assert isinstance(titrator.state.next_state, MainMenu)

0 commit comments

Comments
 (0)