Skip to content

Commit 6c53704

Browse files
authored
Allow the BuildHAT LEDs to be turned on and off (#127)
* Expose BuildHAT LEDs
1 parent 001ecb6 commit 6c53704

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

buildhat/hat.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Hat:
88
"""Allows enumeration of devices which are connected to the hat
99
"""
1010
def __init__(self, device=None):
11+
self.led_status = -1
1112
if not device:
1213
Device._setup()
1314
else:
@@ -46,5 +47,67 @@ def get_vin(self):
4647

4748
return Device._instance.vin
4849

50+
def _set_led(self, intmode):
51+
if isinstance(intmode, int) and intmode >= -1 and intmode <= 3:
52+
self.led_status = intmode
53+
Device._instance.write("ledmode {}\r".format(intmode).encode())
54+
55+
def set_leds(self, color="voltage"):
56+
"""Sets the two LEDs on or off on the BuildHAT. By default
57+
the color depends on the input voltage with green being nominal at around 8V
58+
(The fastest time the LEDs can be perceptually toggled is around 0.025 seconds)
59+
:param color: orange, green, both, off, or voltage (default)
60+
"""
61+
if color == "orange":
62+
self._set_led(1)
63+
elif color == "green":
64+
self._set_led(2)
65+
elif color == "both":
66+
self._set_led(3)
67+
elif color == "off":
68+
self._set_led(0)
69+
elif color == "voltage":
70+
self._set_led(-1)
71+
else:
72+
return
73+
74+
def orange_led(self, status=True):
75+
"""Turn the BuildHAT's orange LED on or off
76+
:param status: True to turn it on, False to turn it off
77+
"""
78+
if status:
79+
if self.led_status == 3 or self.led_status == 1:
80+
# already on
81+
return
82+
elif self.led_status == 2:
83+
self._set_led(3)
84+
# off or default
85+
else:
86+
self._set_led(1)
87+
else:
88+
if self.led_status == 1 or self.led_status == -1:
89+
self._set_led(0)
90+
elif self.led_status == 3:
91+
self._set_led(2)
92+
93+
def green_led(self, status=True):
94+
"""Turn the BuildHAT's green LED on or off
95+
:param status: True to turn it on, False to turn it off
96+
"""
97+
if status:
98+
if self.led_status == 3 or self.led_status == 2:
99+
# already on
100+
return
101+
elif self.led_status == 1:
102+
self._set_led(3)
103+
# off or default
104+
else:
105+
self._set_led(2)
106+
else:
107+
if self.led_status == 2 or self.led_status == -1:
108+
self._set_led(0)
109+
elif self.led_status == 3:
110+
self._set_led(1)
111+
49112
def _close(self):
50113
Device._instance.shutdown()

0 commit comments

Comments
 (0)