Skip to content

Commit 48b6017

Browse files
committed
Use Future to obtain voltage of build HAT
1 parent 9cbb15d commit 48b6017

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

buildhat/hat.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""HAT handling functionality"""
22

3+
from concurrent.futures import Future
4+
35
from .devices import Device
46

57

@@ -45,11 +47,10 @@ def get_vin(self):
4547
:return: Voltage on the input power jack
4648
:rtype: float
4749
"""
50+
ftr = Future()
51+
Device._instance.vincond.append(ftr)
4852
Device._instance.write(b"vin\r")
49-
with Device._instance.vincond:
50-
Device._instance.vincond.wait()
51-
52-
return Device._instance.vin
53+
return ftr.result()
5354

5455
def _set_led(self, intmode):
5556
if isinstance(intmode, int) and intmode >= -1 and intmode <= 3:

buildhat/serinterface.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,9 @@ def __init__(self, firmware, signature, version, device="/dev/serial0", debug=Fa
8787
self.portcond = []
8888
self.pulsecond = []
8989
self.rampcond = []
90+
self.vincond = []
9091
self.fin = False
9192
self.running = True
92-
self.vincond = Condition()
93-
self.vin = None
9493
if debug:
9594
tmp = tempfile.NamedTemporaryFile(suffix=".log", prefix="buildhat-", delete=False)
9695
logging.basicConfig(filename=tmp.name, format='%(asctime)s %(message)s',
@@ -388,6 +387,5 @@ def runit():
388387

389388
if len(line) >= 5 and line[1] == "." and line.endswith(" V"):
390389
vin = float(line.split(" ")[0])
391-
self.vin = vin
392-
with self.vincond:
393-
self.vincond.notify()
390+
ftr = self.vincond.pop()
391+
ftr.set_result(vin)

0 commit comments

Comments
 (0)