Skip to content

Commit 417c80a

Browse files
committed
second example added flag "temp_in_fahrenheit"
In gVars class added flag "temp_in_fahrenheit". In second example modified algorithm to display the temperature sensor value in degrees Celsius or in degrees Fahrenheit depending the flag. See lines 211 , 667-670 and 725-726.
1 parent d52bbdf commit 417c80a

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

examples/hotplug_sensor_examples/displayio_layout_tablayout_hotplug_temp_sensor_datetime_fm_NTP_touch_and_gVars_test.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def __init__(self):
6969
25: "five_min_cnt",
7070
26: "next_NTP_sync_t1",
7171
27: "next_NTP_sync_t3",
72+
28: "temp_in_fahrenheit",
7273
}
7374

7475
self.gVars_rDict = {
@@ -100,6 +101,7 @@ def __init__(self):
100101
"five_min_cnt": 25,
101102
"next_NTP_sync_t1": 26,
102103
"next_NTP_sync_t3": 27,
104+
"temp_in_fahrenheit": 28,
103105
}
104106

105107
self.g_vars = {}
@@ -161,6 +163,7 @@ def clean(self):
161163
25: None,
162164
26: None,
163165
27: None,
166+
28: None,
164167
}
165168

166169
def list(self):
@@ -213,6 +216,7 @@ def list(self):
213216
myVars.write("five_min_cnt", 0)
214217
myVars.write("next_NTP_sync_t1", "Next NTP sync in ")
215218
myVars.write("next_NTP_sync_t3", " (mm:ss)")
219+
myVars.write("temp_in_fahrenheit", True)
216220
# nHH_old is used to check if the hour has changed.
217221
# If so we have to re-sync from NTP server
218222
# (if not using an external RTC)
@@ -645,17 +649,16 @@ def set_image(group, filename):
645649
glyphs = b' "(),-.0123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
646650
font_arial.load_glyphs(glyphs)
647651
font_arial.load_glyphs(("°",)) # a non-ascii character we need
648-
# gc.collect() # ADDED by @PaulskPt -- to prevent MemoryError - memory allocation failed,
652+
# font=font_term.collect() # ADDED by @PaulskPt --
653+
# to prevent MemoryError - memory allocation failed,
649654
# allocating 6444 bytes
650655

651656
pge2_group = 1
652657

653658

654-
"""
655-
If the temperature sensor has been disconnected,
659+
"""If the temperature sensor has been disconnected,
656660
this function will try to reconnect (test if the sensor is present by now)
657-
If reconnected this function creates the temp_sensor object
658-
"""
661+
If reconnected this function creates the temp_sensor object"""
659662

660663

661664
def connect_temp_sensor():
@@ -676,7 +679,10 @@ def connect_temp_sensor():
676679
print(t)
677680
print("temperature sensor connected")
678681
myVars.write("t0", "Temperature")
679-
myVars.write("t1", chr(186) + "C")
682+
if myVars.read("temp_in_fahrenheit"):
683+
myVars.write("t1", chr(186) + "F")
684+
else:
685+
myVars.write("t1", chr(186) + "C")
680686
myVars.write("t2", 27 * "_")
681687
else:
682688
print("no " + t)
@@ -686,10 +692,8 @@ def connect_temp_sensor():
686692
myVars.write("t2", None)
687693

688694

689-
"""
690-
If the external rtc has been disconnected,
691-
this function will try to reconnect (test if the external rtc is present by now)
692-
"""
695+
""" If the external rtc has been disconnected,
696+
this function will try to reconnect (test if the external rtc is present by now)"""
693697

694698

695699
def connect_rtc():
@@ -715,14 +719,12 @@ def connect_rtc():
715719
print("Failed to connect RTC")
716720

717721

718-
"""
719-
Function gets a value from the external temperature sensor
722+
"""Function gets a value from the external temperature sensor
720723
It only updates if the value has changed compared to the previous value
721724
A fixed text is set in pge4_lbl2.text. The variable temperature value is set in pge4_lbl3.text
722725
If no value obtained (for instance if the sensor is disconnected),
723726
the function sets the pge4_lbl to a default text and makes empty
724-
pge4_lbl2.text and pge4_lbl3.text
725-
"""
727+
pge4_lbl2.text and pge4_lbl3.text"""
726728

727729

728730
def get_temp():
@@ -732,6 +734,8 @@ def get_temp():
732734
if myVars.read("temp_sensor") is not None:
733735
try:
734736
temp = myVars.read("temp_sensor").temperature
737+
if myVars.read("temp_in_fahrenheit"):
738+
temp = (temp * 1.8) + 32
735739
t = "{:5.2f}{} ".format(temp, myVars.read("t1"))
736740
if my_debug and temp is not None and not myVars.read("temp_in_REPL"):
737741
myVars.write("temp_in_REPL", True)
@@ -767,7 +771,6 @@ def get_temp():
767771

768772
# Moved these six definitions outside handle_dt()
769773
# to correct pylint error 'too many variables'
770-
# dt_idxs = {0: "yy", 1:"mo", 2:"dd", 3:"hh", 4:"mm", 5:"ss"}
771774
dt_ridxs = {"yy": 0, "mo": 1, "dd": 2, "hh": 3, "mm": 4, "ss": 5}
772775

773776
# print("dict dt_ridxs =", dt_ridxs.keys())
@@ -843,14 +846,12 @@ def handle_dt(dt):
843846
return RetVal
844847

845848

846-
"""
847-
Function gets the date and time:
849+
"""Function gets the date and time:
848850
a) if an rtc is present from the rtc;
849851
b) if using online NTP pool server then get the date and time from the function time.localtime
850852
This time.localtime has before been set with data from the NTP server.
851853
In both cases the date and time will be set to the pge3_lbl, pge3_lbl12 and pge3_lbl3
852-
If no (valid) date and time has been received then a default text will be shown on the pge3_lbl
853-
"""
854+
If no (valid) date and time received then a default text will be shown on the pge3_lbl"""
854855

855856

856857
def get_dt():

0 commit comments

Comments
 (0)