@@ -8,6 +8,7 @@ class Hat:
8
8
"""Allows enumeration of devices which are connected to the hat
9
9
"""
10
10
def __init__ (self , device = None ):
11
+ self .led_status = - 1
11
12
if not device :
12
13
Device ._setup ()
13
14
else :
@@ -46,5 +47,67 @@ def get_vin(self):
46
47
47
48
return Device ._instance .vin
48
49
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
+
49
112
def _close (self ):
50
113
Device ._instance .shutdown ()
0 commit comments