-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathled_backpacks.py
executable file
·181 lines (170 loc) · 8.66 KB
/
led_backpacks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# Raspberry Pi Physical Dashboard LED Backpack Widgets
# Author: Tony DiCola
#
# The MIT License (MIT)
#
# Copyright (c) 2016 Adafruit Industries
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from Adafruit_LED_Backpack import AlphaNum4, BicolorBargraph24, SevenSegment
import widget
class AlphaNum4Widget(widget.Widget):
"""Quad alpha-numeric LED backpack widget. Can display 4 character text
and numeric values.
"""
def __init__(self, address='0x70', brightness='15', decimal_digits='0',
justify_right='True'):
"""Create an instance of the quad alpha-numeric display widget. Can pass
in the following optional parameters to control the widget (note all
parameters are strings as they are parsed from config files):
- address: I2C address, default is 0x70
- brightness: Brightness of the display, can be a value from 0 to 15
with 15 being the brightest. The default is 15.
- decimal_digits: Number of digits to show after decimal point, default
is 0.
- justify_right: Justify numeric display to the right side if true (the
default). Set to false to justify to the left side.
"""
# Setup display and initial state.
self._display = AlphaNum4.AlphaNum4(address=int(address, 0))
self._display.begin()
self._display.set_brightness(int(brightness))
self._decimal_digits = int(decimal_digits)
self._justify_right = self.parse_bool(justify_right)
# Clear the display
self._display.clear()
self._display.write_display()
def set_value(self, value):
"""Set the value of the display. Must pass in a string with either a
floating point value (note that only values -999 to 9999 will display),
or up to 4 alpha-numeric characters.
"""
# Check if the value can be converted to a float and displayed as such.
self._display.clear()
try:
value = float(value)
self._display.print_float(value, decimal_digits=self._decimal_digits,
justify_right=self._justify_right)
except ValueError:
# Value isn't a float, so display the first 4 characters.
self._display.print_str(value[0:4], justify_right=self._justify_right)
self._display.write_display()
class BicolorBargraph24Widget(widget.Widget):
"""Bi-color 24 count bar graph LED backpack widget. Can display a bar graph
with 24 LEDs. Set the value to a percentage of the LEDs to illuminate, like
50.0 to turn on half of the LEDs. Values in the range 0-100 will light up
green, 200-300 will light up yellow, and 400-500 will light up red.
"""
def __init__(self, address='0x70', brightness='15', justify_right='True'):
"""Create an instance of the bi-color bar graph widget. Can pass in the
following optional parameters to control the widget (note all
parameters are strings as they are parsed from config files):
- address: I2C address, default is 0x70
- brightness: Brightness of the display, can be a value from 0 to 15
with 15 being the brightest. The default is 15.
- justify_right: Justify numeric display to the right side if true (the
default). Set to false to justify to the left side.
"""
# Setup display and initial state.
self._display = BicolorBargraph24.BicolorBargraph24(address=int(address, 0))
self._display.begin()
self._display.set_brightness(int(brightness))
self._justify_right = self.parse_bool(justify_right)
# Clear the display
self._display.clear()
self._display.write_display()
def set_value(self, value):
"""Set the value of the display. Pass a string with a floating point
value to set the bargraph with the specified percent of LEDs enabled.
Passing a value in the range 0-100% will use green LEDs, 200-300% will
use yellow LEDs, and 400-500% will use red LEDs.
"""
# Get the floating point value and determine the range for the color.
value = float(value)
if 0.0 <= value <= 100.0:
color = BicolorBargraph24.GREEN
elif 200.0 <= value <= 300.0:
color = BicolorBargraph24.YELLOW
value -= 200.0 # Normalize value to be within 0-100.
elif 400.0 <= value <= 500.0:
color = BicolorBargraph24.RED
value -= 400.0 # Normalize value to be within 0-100.
else:
raise RuntimeError('Unexpected bar graph value, must be in range of 0-100, 200-300, or 400-500!')
# Light up the appropriate percent of LEDs.
if self._justify_right:
# Start from right and move left.
positions = range(23, -1, -1)
else:
# Start from left and move right.
positions = range(24)
self._display.clear()
for i, pos in enumerate(positions):
if (i+1.0)/24.0*100.0 <= value:
self._display.set_bar(pos, color)
self._display.write_display()
class SevenSegmentWidget(widget.Widget):
"""Seven segment LED backpack widget. Can display simple numeric values
in the range of -999 to 9999.
"""
def __init__(self, address='0x70', brightness='15', decimal_digits='0',
justify_right='True', invert='False'):
"""Create an instance of the seven segment display widget. Can pass in
the following optional parameters to control the widget (note all
parameters are strings as they are parsed from config files):
- address: I2C address, default is 0x70
- brightness: Brightness of the display, can be a value from 0 to 15
with 15 being the brightest. The default is 15.
- decimal_digits: Number of digits to show after decimal point, default
is 0.
- justify_right: Justify numeric display to the right side if true (the
default). Set to false to justify to the left side.
- invert: Vertically flip the display if true. Default is false. Note
that when flipped the decimal points will be at the top!
"""
# Setup display and initial state.
self._display = SevenSegment.SevenSegment(address=int(address, 0))
self._display.begin()
self._display.set_brightness(int(brightness))
if self.parse_bool(invert):
self._display.set_invert(True)
self._decimal_digits = int(decimal_digits)
self._justify_right = self.parse_bool(justify_right)
# Clear the display
self._display.clear()
self._display.write_display()
def set_value(self, value):
"""Set the value of the display. Must pass in a string with either a
floating point value (note that only values -999 to 9999 will display),
or 'colon_on' to turn on the colon or 'colon_off' to turn off the colon.
"""
value = value.lower()
# Check if the value is a command to turn on/off the colon.
if value == 'colon_on':
self._display.set_colon(True)
elif value == 'colon_off':
self._display.set_colon(False)
else:
# Otherwise assume the value is a number and attempt to set the
# display to show it.
self._display.clear()
value = float(value)
self._display.print_float(value, decimal_digits=self._decimal_digits,
justify_right=self._justify_right)
self._display.write_display()