Skip to content

Commit 8763a0b

Browse files
committed
Changing BicolorMatrix8x8 class to extend Matrix8x8.
1 parent aea6f6b commit 8763a0b

File tree

1 file changed

+45
-44
lines changed

1 file changed

+45
-44
lines changed

Adafruit_LED_Backpack/BicolorMatrix8x8.py

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,61 +18,62 @@
1818
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1919
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2020
# THE SOFTWARE.
21-
import HT16K33
2221

2322

2423
# Color values as convenient globals.
2524
# This is a bitmask value where the first bit is green, and the second bit is
2625
# red. If both bits are set the color is yellow (red + green light).
26+
from Matrix8x8 import Matrix8x8
27+
2728
OFF = 0
2829
GREEN = 1
2930
RED = 2
3031
YELLOW = 3
3132

3233

33-
class BicolorMatrix8x8(HT16K33.HT16K33):
34-
"""Bi-color 8x8 matrix LED backpack display."""
34+
class BicolorMatrix8x8(Matrix8x8):
35+
"""Bi-color 8x8 matrix LED backpack display."""
3536

36-
def __init__(self, **kwargs):
37-
"""Initialize display. All arguments will be passed to the HT16K33 class
38-
initializer, including optional I2C address and bus number parameters.
39-
"""
40-
super(BicolorMatrix8x8, self).__init__(**kwargs)
37+
def __init__(self, **kwargs):
38+
"""Initialize display. All arguments will be passed to the HT16K33 class
39+
initializer, including optional I2C address and bus number parameters.
40+
"""
41+
super(BicolorMatrix8x8, self).__init__(**kwargs)
4142

42-
def set_pixel(self, x, y, value):
43-
"""Set pixel at position x, y to the given value. X and Y should be values
44-
of 0 to 8. Value should be OFF, GREEN, RED, or YELLOW.
45-
"""
46-
if x < 0 or x > 7 or y < 0 or y > 7:
47-
# Ignore out of bounds pixels.
48-
return
49-
# Set green LED based on 1st bit in value.
50-
self.set_led(y*16+x, 1 if value & GREEN > 0 else 0)
51-
# Set red LED based on 2nd bit in value.
52-
self.set_led(y*16+x+8, 1 if value & RED > 0 else 0)
43+
def set_pixel(self, x, y, value):
44+
"""Set pixel at position x, y to the given value. X and Y should be values
45+
of 0 to 8. Value should be OFF, GREEN, RED, or YELLOW.
46+
"""
47+
if x < 0 or x > 7 or y < 0 or y > 7:
48+
# Ignore out of bounds pixels.
49+
return
50+
# Set green LED based on 1st bit in value.
51+
self.set_led(y * 16 + x, 1 if value & GREEN > 0 else 0)
52+
# Set red LED based on 2nd bit in value.
53+
self.set_led(y * 16 + x + 8, 1 if value & RED > 0 else 0)
5354

54-
def set_image(self, image):
55-
"""Set display buffer to Python Image Library image. Red pixels (r=255,
56-
g=0, b=0) will map to red LEDs, green pixels (r=0, g=255, b=0) will map to
57-
green LEDs, and yellow pixels (r=255, g=255, b=0) will map to yellow LEDs.
58-
All other pixel values will map to an unlit LED value.
59-
"""
60-
imwidth, imheight = image.size
61-
if imwidth != 8 or imheight != 8:
62-
raise ValueError('Image must be an 8x8 pixels in size.')
63-
# Convert image to RGB and grab all the pixels.
64-
pix = image.convert('RGB').load()
65-
# Loop through each pixel and write the display buffer pixel.
66-
for x in [0, 1, 2, 3, 4, 5, 6, 7]:
67-
for y in [0, 1, 2, 3, 4, 5, 6, 7]:
68-
color = pix[(x, y)]
69-
# Handle the color of the pixel.
70-
if color == (255, 0, 0):
71-
self.set_pixel(x, y, RED)
72-
elif color == (0, 255, 0):
73-
self.set_pixel(x, y, GREEN)
74-
elif color == (255, 255, 0):
75-
self.set_pixel(x, y, YELLOW)
76-
else:
77-
# Unknown color, default to LED off.
78-
self.set_pixel(x, y, OFF)
55+
def set_image(self, image):
56+
"""Set display buffer to Python Image Library image. Red pixels (r=255,
57+
g=0, b=0) will map to red LEDs, green pixels (r=0, g=255, b=0) will map to
58+
green LEDs, and yellow pixels (r=255, g=255, b=0) will map to yellow LEDs.
59+
All other pixel values will map to an unlit LED value.
60+
"""
61+
imwidth, imheight = image.size
62+
if imwidth != 8 or imheight != 8:
63+
raise ValueError('Image must be an 8x8 pixels in size.')
64+
# Convert image to RGB and grab all the pixels.
65+
pix = image.convert('RGB').load()
66+
# Loop through each pixel and write the display buffer pixel.
67+
for x in [0, 1, 2, 3, 4, 5, 6, 7]:
68+
for y in [0, 1, 2, 3, 4, 5, 6, 7]:
69+
color = pix[(x, y)]
70+
# Handle the color of the pixel.
71+
if color == (255, 0, 0):
72+
self.set_pixel(x, y, RED)
73+
elif color == (0, 255, 0):
74+
self.set_pixel(x, y, GREEN)
75+
elif color == (255, 255, 0):
76+
self.set_pixel(x, y, YELLOW)
77+
else:
78+
# Unknown color, default to LED off.
79+
self.set_pixel(x, y, OFF)

0 commit comments

Comments
 (0)