Skip to content

Commit 51e3aa2

Browse files
committed
Added image function and font-based numbers example
1 parent 453b1bd commit 51e3aa2

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

adafruit_is31fl3731.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
CircuitPython driver for the IS31FL3731 charlieplex IC.
2828
2929
30-
* Author(s): Tony DiCola
30+
* Author(s): Tony DiCola, Melissa LeBlanc-Williams
3131
3232
Implementation Notes
3333
--------------------
@@ -336,6 +336,28 @@ def pixel(self, x, y, color=None, blink=None, frame=None):
336336
return None
337337
#pylint: enable-msg=too-many-arguments
338338

339+
def image(self, img, blink=None, frame=None):
340+
"""Set buffer to value of Python Imaging Library image. The image should
341+
be in 8-bit mode (L) and a size equal to the display size.
342+
343+
:param img: Python Imaging Library image
344+
:param blink: True to blink
345+
:param frame: the frame to set the image
346+
"""
347+
if img.mode != 'L':
348+
raise ValueError('Image must be in mode L.')
349+
imwidth, imheight = img.size
350+
if imwidth != self.width or imheight != self.height:
351+
raise ValueError('Image must be same dimensions as display ({0}x{1}).' \
352+
.format(self.width, self.height))
353+
# Grab all the pixels from the image, faster than getpixel.
354+
pixels = img.load()
355+
356+
# Iterate through the pixels
357+
for x in range(self.width): # yes this double loop is slow,
358+
for y in range(self.height): # but these displays are small!
359+
self.pixel(x, y, pixels[(x, y)], blink=blink, frame=frame)
360+
339361

340362
class CharlieWing(Matrix):
341363
"""Supports the Charlieplexed feather wing

examples/is31fl3731_pillow_numbers.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Example to utilize the Python Imaging Library (Pillow) and draw bitmapped text
3+
to 8 frames and then run autoplay on those frames.
4+
5+
Author(s): Melissa LeBlanc-Williams for Adafruit Industries
6+
"""
7+
8+
import board
9+
from PIL import Image, ImageDraw, ImageFont
10+
import adafruit_is31fl3731
11+
12+
i2c = board.I2C()
13+
14+
# uncomment line if you are using Adafruit 16x9 Charlieplexed PWM LED Matrix
15+
#display = adafruit_is31fl3731.Matrix(i2c)
16+
# uncomment line if you are using Adafruit 16x9 Charlieplexed PWM LED Matrix
17+
display = adafruit_is31fl3731.CharlieBonnet(i2c)
18+
19+
display.fill(0)
20+
21+
# 256 Color Grayscale Mode
22+
image = Image.new('L', (display.width, display.height))
23+
draw = ImageDraw.Draw(image)
24+
25+
# Load a font in 2 different sizes.
26+
font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 10)
27+
28+
# Load the text in each frame
29+
for x in range(8):
30+
draw.rectangle((0, 0, display.width, display.height), outline=0, fill=0)
31+
draw.text((x + 1, -2), str(x + 1), font=font, fill=32)
32+
display.image(image, frame=x)
33+
34+
display.autoplay(delay=500)

0 commit comments

Comments
 (0)