Skip to content

Commit 51e69d6

Browse files
committed
Merge branch 'jakekarnes42-master'
2 parents 472e84f + cbe532e commit 51e69d6

File tree

4 files changed

+225
-97
lines changed

4 files changed

+225
-97
lines changed

Adafruit_LED_Backpack/BicolorMatrix8x8.py

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
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
22-
21+
from Matrix8x8 import Matrix8x8
2322

2423
# Color values as convenient globals.
2524
# This is a bitmask value where the first bit is green, and the second bit is
@@ -30,49 +29,49 @@
3029
YELLOW = 3
3130

3231

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

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)
35+
def __init__(self, **kwargs):
36+
"""Initialize display. All arguments will be passed to the HT16K33 class
37+
initializer, including optional I2C address and bus number parameters.
38+
"""
39+
super(BicolorMatrix8x8, self).__init__(**kwargs)
4140

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

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

Adafruit_LED_Backpack/Matrix8x8.py

Lines changed: 151 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,157 @@
1919
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2020
# THE SOFTWARE.
2121
import HT16K33
22+
import Image
23+
import time
2224

2325

2426
class Matrix8x8(HT16K33.HT16K33):
25-
"""Single color 8x8 matrix LED backpack display."""
26-
27-
def __init__(self, **kwargs):
28-
"""Initialize display. All arguments will be passed to the HT16K33 class
29-
initializer, including optional I2C address and bus number parameters.
30-
"""
31-
super(Matrix8x8, self).__init__(**kwargs)
32-
33-
def set_pixel(self, x, y, value):
34-
"""Set pixel at position x, y to the given value. X and Y should be values
35-
of 0 to 8. Value should be 0 for off and non-zero for on.
36-
"""
37-
if x < 0 or x > 7 or y < 0 or y > 7:
38-
# Ignore out of bounds pixels.
39-
return
40-
self.set_led(y*16+((x+7)%8), value)
41-
42-
def set_image(self, image):
43-
"""Set display buffer to Python Image Library image. Image will be converted
44-
to 1 bit color and non-zero color values will light the LEDs.
45-
"""
46-
imwidth, imheight = image.size
47-
if imwidth != 8 or imheight != 8:
48-
raise ValueError('Image must be an 8x8 pixels in size.')
49-
# Convert image to 1 bit color and grab all the pixels.
50-
pix = image.convert('1').load()
51-
# Loop through each pixel and write the display buffer pixel.
52-
for x in [0, 1, 2, 3, 4, 5, 6, 7]:
53-
for y in [0, 1, 2, 3, 4, 5, 6, 7]:
54-
color = pix[(x, y)]
55-
# Handle the color of the pixel, off or on.
56-
if color == 0:
57-
self.set_pixel(x, y, 0)
58-
else:
59-
self.set_pixel(x, y, 1)
27+
"""Single color 8x8 matrix LED backpack display."""
28+
29+
def __init__(self, **kwargs):
30+
"""Initialize display. All arguments will be passed to the HT16K33 class
31+
initializer, including optional I2C address and bus number parameters.
32+
"""
33+
super(Matrix8x8, self).__init__(**kwargs)
34+
35+
def set_pixel(self, x, y, value):
36+
"""Set pixel at position x, y to the given value. X and Y should be values
37+
of 0 to 8. Value should be 0 for off and non-zero for on.
38+
"""
39+
if x < 0 or x > 7 or y < 0 or y > 7:
40+
# Ignore out of bounds pixels.
41+
return
42+
self.set_led(y * 16 + ((x + 7) % 8), value)
43+
44+
def set_image(self, image):
45+
"""Set display buffer to Python Image Library image. Image will be converted
46+
to 1 bit color and non-zero color values will light the LEDs.
47+
"""
48+
imwidth, imheight = image.size
49+
if imwidth != 8 or imheight != 8:
50+
raise ValueError('Image must be an 8x8 pixels in size.')
51+
# Convert image to 1 bit color and grab all the pixels.
52+
pix = image.convert('1').load()
53+
# Loop through each pixel and write the display buffer pixel.
54+
for x in [0, 1, 2, 3, 4, 5, 6, 7]:
55+
for y in [0, 1, 2, 3, 4, 5, 6, 7]:
56+
color = pix[(x, y)]
57+
# Handle the color of the pixel, off or on.
58+
if color == 0:
59+
self.set_pixel(x, y, 0)
60+
else:
61+
self.set_pixel(x, y, 1)
62+
63+
def create_blank_image(self):
64+
return Image.new("RGB", (8, 8))
65+
66+
67+
def horizontal_scroll(self, image, padding=True):
68+
"""Returns a list of images which appear to scroll from left to right
69+
across the input image when displayed on the LED matrix in order.
70+
71+
The input image is not limited to being 8x8. If the input image is
72+
larger than this, then all columns will be scrolled through but only
73+
the top 8 rows of pixels will be displayed.
74+
75+
Keyword arguments:
76+
image -- The image to scroll across.
77+
padding -- If True, the animation will begin with a blank screen and the
78+
input image will scroll into the blank screen one pixel column at a
79+
time. Similarly, after scrolling across the whole input image, the
80+
end of the image will scroll out of a blank screen one column at a
81+
time. If this is not True, then only the input image will be scroll
82+
across without beginning or ending with "whitespace."
83+
(Default = True)
84+
"""
85+
86+
image_list = list()
87+
width = image.size[0]
88+
# Scroll into the blank image.
89+
if padding:
90+
for x in range(8):
91+
section = image.crop((0, 0, x, 8))
92+
display_section = self.create_blank_image()
93+
display_section.paste(section, (8 - x, 0, 8, 8))
94+
image_list.append(display_section)
95+
96+
#Scroll across the input image.
97+
for x in range(8, width + 1):
98+
section = image.crop((x - 8, 0, x, 8))
99+
display_section = self.create_blank_image()
100+
display_section.paste(section, (0, 0, 8, 8))
101+
image_list.append(display_section)
102+
103+
#Scroll out, leaving the blank image.
104+
if padding:
105+
for x in range(width - 7, width + 1):
106+
section = image.crop((x, 0, width, 8))
107+
display_section = self.create_blank_image()
108+
display_section.paste(section, (0, 0, 7 - (x - (width - 7)), 8))
109+
image_list.append(display_section)
110+
111+
#Return the list of images created
112+
return image_list
113+
114+
def vertical_scroll(self, image, padding=True):
115+
"""Returns a list of images which appear to scroll from top to bottom
116+
down the input image when displayed on the LED matrix in order.
117+
118+
The input image is not limited to being 8x8. If the input image is
119+
largerthan this, then all rows will be scrolled through but only the
120+
left-most 8 columns of pixels will be displayed.
121+
122+
Keyword arguments:
123+
image -- The image to scroll down.
124+
padding -- If True, the animation will begin with a blank screen and the
125+
input image will scroll into the blank screen one pixel row at a
126+
time. Similarly, after scrolling down the whole input image, the end
127+
of the image will scroll out of a blank screen one row at a time.
128+
If this is not True, then only the input image will be scroll down
129+
without beginning or ending with "whitespace." (Default = True)
130+
"""
131+
132+
image_list = list()
133+
height = image.size[1]
134+
# Scroll into the blank image.
135+
if padding:
136+
for y in range(8):
137+
section = image.crop((0, 0, 8, y))
138+
display_section = self.create_blank_image()
139+
display_section.paste(section, (0, 8 - y, 8, 8))
140+
image_list.append(display_section)
141+
142+
#Scroll across the input image.
143+
for y in range(8, height + 1):
144+
section = image.crop((0, y - 8, 8, y))
145+
display_section = self.create_blank_image()
146+
display_section.paste(section, (0, 0, 8, 8))
147+
image_list.append(display_section)
148+
149+
#Scroll out, leaving the blank image.
150+
if padding:
151+
for y in range(height - 7, height + 1):
152+
section = image.crop((0, y, 8, height))
153+
display_section = self.create_blank_image()
154+
display_section.paste(section, (0, 0, 8, 7 - (y - (height - 7))))
155+
image_list.append(display_section)
156+
157+
#Return the list of images created
158+
return image_list
159+
160+
def animate(self, images, delay=.25):
161+
"""Displays each of the input images in order, pausing for "delay"
162+
seconds after each image.
163+
164+
Keyword arguments:
165+
image -- An iterable collection of Image objects.
166+
delay -- How many seconds to wait after displaying an image before
167+
displaying the next one. (Default = .25)
168+
"""
169+
for image in images:
170+
# Draw the image on the display buffer.
171+
self.set_image(image)
172+
173+
# Draw the buffer to the display hardware.
174+
self.write_display()
175+
time.sleep(delay)

examples/bicolor_matrix8x8_test.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2020
# THE SOFTWARE.
2121
import time
22-
2322
import Image
2423
import ImageDraw
2524

@@ -38,18 +37,18 @@
3837
# Run through each color and pixel.
3938
# Iterate through all colors.
4039
for c in [BicolorMatrix8x8.RED, BicolorMatrix8x8.GREEN, BicolorMatrix8x8.YELLOW]:
41-
# Iterate through all positions x and y.
42-
for x in range(8):
43-
for y in range(8):
44-
# Clear the display buffer.
45-
display.clear()
46-
# Set pixel at position i, j to appropriate color.
47-
display.set_pixel(x, y, c)
48-
# Write the display buffer to the hardware. This must be called to
49-
# update the actual display LEDs.
50-
display.write_display()
51-
# Delay for a quarter second.
52-
time.sleep(0.25)
40+
# Iterate through all positions x and y.
41+
for x in range(8):
42+
for y in range(8):
43+
# Clear the display buffer.
44+
display.clear()
45+
# Set pixel at position i, j to appropriate color.
46+
display.set_pixel(x, y, c)
47+
# Write the display buffer to the hardware. This must be called to
48+
# update the actual display LEDs.
49+
display.write_display()
50+
# Delay for a quarter second.
51+
time.sleep(0.25)
5352

5453
# Draw some colored shapes using the Python Imaging Library.
5554

@@ -63,17 +62,31 @@
6362
draw = ImageDraw.Draw(image)
6463

6564
# Draw a filled yellow rectangle with red outline.
66-
draw.rectangle((0,0,7,7), outline=(255,0,0), fill=(255,255,0))
65+
draw.rectangle((0, 0, 7, 7), outline=(255, 0, 0), fill=(255, 255, 0))
6766

6867
# Draw an X with two green lines.
69-
draw.line((1,1,6,6), fill=(0,255,0))
70-
draw.line((1,6,6,1), fill=(0,255,0))
68+
draw.line((1, 1, 6, 6), fill=(0, 255, 0))
69+
draw.line((1, 6, 6, 1), fill=(0, 255, 0))
7170

7271
# Draw the image on the display buffer.
7372
display.set_image(image)
7473

7574
# Draw the buffer to the display hardware.
7675
display.write_display()
7776

77+
# Pause for 5 seconds
78+
time.sleep(5)
79+
80+
# Clear the screen again.
81+
display.clear()
82+
display.set_image(display.create_blank_image())
83+
84+
# Make the same image scrollable
85+
scrollable = display.horizontal_scroll(image)
86+
87+
# Animate the scrollable image
88+
display.animate(scrollable)
89+
90+
7891
# See the SSD1306 library for more examples of using the Python Imaging Library
7992
# such as drawing text: https://github.com/adafruit/Adafruit_Python_SSD1306

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from setuptools import setup, find_packages
44

55
setup(name = 'Adafruit_LED_Backpack',
6-
version = '1.5.0',
6+
version = '1.6.0',
77
author = 'Tony DiCola',
88
author_email = 'tdicola@adafruit.com',
99
description = 'Library to control LED backpack displays such as 8x8 single and bi-color matrices, bargraphs, 7 segment, and 14 segment displays.',

0 commit comments

Comments
 (0)