Skip to content

Commit ab9ff67

Browse files
authored
Merge pull request #51 from makermelissa/matrix_scroll
Added image function and pillow demo for matrices
2 parents 2a0c6c8 + c4a781a commit ab9ff67

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

adafruit_ht16k33/matrix.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,22 @@ def shift_down(self, rotate=False):
123123
"""
124124
self.shift(0, -1, rotate)
125125

126+
def image(self, img):
127+
"""Set buffer to value of Python Imaging Library image. The image should
128+
be in 1 bit mode and a size equal to the display size."""
129+
imwidth, imheight = img.size
130+
if imwidth != self.columns or imheight != self.rows:
131+
raise ValueError('Image must be same dimensions as display ({0}x{1}).' \
132+
.format(self.columns, self.rows))
133+
# Grab all the pixels from the image, faster than getpixel.
134+
pixels = img.convert('1').load()
135+
# Iterate through the pixels
136+
for x in range(self.columns): # yes this double loop is slow,
137+
for y in range(self.rows): # but these displays are small!
138+
self.pixel(x, y, pixels[(x, y)])
139+
if self._auto_write:
140+
self.show()
141+
126142
@property
127143
def columns(self):
128144
"""Read-only property for number of columns"""
@@ -160,15 +176,21 @@ def pixel(self, x, y, color=None):
160176

161177
class Matrix8x8x2(Matrix8x8):
162178
"""A bi-color matrix."""
179+
180+
LED_OFF = 0
181+
LED_RED = 1
182+
LED_GREEN = 2
183+
LED_YELLOW = 3
184+
163185
def pixel(self, x, y, color=None):
164186
"""Get or set the color of a given pixel."""
165187
if not 0 <= x <= 7:
166188
return None
167189
if not 0 <= y <= 7:
168190
return None
169191
if color is not None:
170-
super()._pixel(y, x, (color & 0x01))
171-
super()._pixel(y + 8, x, (color >> 1) & 0x01)
192+
super()._pixel(y, x, (color >> 1) & 0x01)
193+
super()._pixel(y + 8, x, (color & 0x01))
172194
else:
173195
return super()._pixel(y, x) | super()._pixel(y + 8, x) << 1
174196
return None
@@ -182,3 +204,27 @@ def fill(self, color):
182204
self._set_buffer(i * 2 + 1, fill2)
183205
if self._auto_write:
184206
self.show()
207+
208+
def image(self, img):
209+
"""Set buffer to value of Python Imaging Library image. The image should
210+
be a size equal to the display size."""
211+
imwidth, imheight = img.size
212+
if imwidth != self.columns or imheight != self.rows:
213+
raise ValueError('Image must be same dimensions as display ({0}x{1}).' \
214+
.format(self.columns, self.rows))
215+
# Grab all the pixels from the image, faster than getpixel.
216+
pixels = img.convert('RGB').load()
217+
# Iterate through the pixels
218+
for x in range(self.columns): # yes this double loop is slow,
219+
for y in range(self.rows): # but these displays are small!
220+
if pixels[(x, y)] == (255, 0, 0):
221+
self.pixel(x, y, self.LED_RED)
222+
elif pixels[(x, y)] == (0, 255, 0):
223+
self.pixel(x, y, self.LED_GREEN)
224+
elif pixels[(x, y)] == (255, 255, 0):
225+
self.pixel(x, y, self.LED_YELLOW)
226+
else:
227+
# Unknown color, default to LED off.
228+
self.pixel(x, y, self.LED_OFF)
229+
if self._auto_write:
230+
self.show()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Basic example of drawing an image
2+
# This example and library is meant to work with Adafruit CircuitPython API.
3+
#
4+
# This example is for use on (Linux) computers that are using CPython with
5+
# Adafruit Blinka to support CircuitPython libraries. CircuitPython does
6+
# not support PIL/pillow (python imaging library)!
7+
#
8+
# Author: Melissa LeBlanc-Williams
9+
# License: Public Domain
10+
11+
# Import all board pins.
12+
import board
13+
import busio
14+
from PIL import Image
15+
16+
# Import the HT16K33 LED matrix module.
17+
from adafruit_ht16k33 import matrix
18+
19+
# Create the I2C interface.
20+
i2c = busio.I2C(board.SCL, board.SDA)
21+
22+
# Create the matrix class.
23+
# This creates a 16x8 matrix:
24+
mtrx = matrix.Matrix16x8(i2c)
25+
# Or this creates a 16x8 matrix backpack:
26+
#mtrx = matrix.MatrixBackpack16x8(i2c)
27+
# Or this creates a 8x8 matrix:
28+
#mtrx = matrix.Matrix8x8(i2c)
29+
# Or this creates a 8x8 bicolor matrix:
30+
#mtrx = matrix.Matrix8x8x2(i2c)
31+
# Finally you can optionally specify a custom I2C address of the HT16k33 like:
32+
#mtrx = matrix.Matrix16x8(i2c, address=0x70)
33+
34+
if isinstance(mtrx, matrix.Matrix8x8x2):
35+
image = Image.open("squares-color.png")
36+
elif isinstance(mtrx, matrix.Matrix16x8):
37+
image = Image.open("squares-mono-16x8.png")
38+
else:
39+
image = Image.open("squares-mono-8x8.png")
40+
41+
# Clear the matrix
42+
mtrx.fill(0)
43+
mtrx.image(image)

examples/ht16k33_matrix_simpletest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
time.sleep(2)
4141

4242
# Draw a Smiley Face
43+
matrix.fill(0)
44+
4345
for row in range(2, 6):
4446
matrix[row, 0] = 1
4547
matrix[row, 7] = 1

0 commit comments

Comments
 (0)