Skip to content

Added image function and pillow demo for matrices #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions adafruit_ht16k33/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,22 @@ def shift_down(self, rotate=False):
"""
self.shift(0, -1, rotate)

def image(self, img):
"""Set buffer to value of Python Imaging Library image. The image should
be in 1 bit mode and a size equal to the display size."""
imwidth, imheight = img.size
if imwidth != self.columns or imheight != self.rows:
raise ValueError('Image must be same dimensions as display ({0}x{1}).' \
.format(self.columns, self.rows))
# Grab all the pixels from the image, faster than getpixel.
pixels = img.convert('1').load()
# Iterate through the pixels
for x in range(self.columns): # yes this double loop is slow,
for y in range(self.rows): # but these displays are small!
self.pixel(x, y, pixels[(x, y)])
if self._auto_write:
self.show()

@property
def columns(self):
"""Read-only property for number of columns"""
Expand Down Expand Up @@ -160,15 +176,21 @@ def pixel(self, x, y, color=None):

class Matrix8x8x2(Matrix8x8):
"""A bi-color matrix."""

LED_OFF = 0
LED_RED = 1
LED_GREEN = 2
LED_YELLOW = 3

def pixel(self, x, y, color=None):
"""Get or set the color of a given pixel."""
if not 0 <= x <= 7:
return None
if not 0 <= y <= 7:
return None
if color is not None:
super()._pixel(y, x, (color & 0x01))
super()._pixel(y + 8, x, (color >> 1) & 0x01)
super()._pixel(y, x, (color >> 1) & 0x01)
super()._pixel(y + 8, x, (color & 0x01))
else:
return super()._pixel(y, x) | super()._pixel(y + 8, x) << 1
return None
Expand All @@ -182,3 +204,27 @@ def fill(self, color):
self._set_buffer(i * 2 + 1, fill2)
if self._auto_write:
self.show()

def image(self, img):
"""Set buffer to value of Python Imaging Library image. The image should
be a size equal to the display size."""
imwidth, imheight = img.size
if imwidth != self.columns or imheight != self.rows:
raise ValueError('Image must be same dimensions as display ({0}x{1}).' \
.format(self.columns, self.rows))
# Grab all the pixels from the image, faster than getpixel.
pixels = img.convert('RGB').load()
# Iterate through the pixels
for x in range(self.columns): # yes this double loop is slow,
for y in range(self.rows): # but these displays are small!
if pixels[(x, y)] == (255, 0, 0):
self.pixel(x, y, self.LED_RED)
elif pixels[(x, y)] == (0, 255, 0):
self.pixel(x, y, self.LED_GREEN)
elif pixels[(x, y)] == (255, 255, 0):
self.pixel(x, y, self.LED_YELLOW)
else:
# Unknown color, default to LED off.
self.pixel(x, y, self.LED_OFF)
if self._auto_write:
self.show()
43 changes: 43 additions & 0 deletions examples/ht16k33_matrix_pillow_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Basic example of drawing an image
# This example and library is meant to work with Adafruit CircuitPython API.
#
# This example is for use on (Linux) computers that are using CPython with
# Adafruit Blinka to support CircuitPython libraries. CircuitPython does
# not support PIL/pillow (python imaging library)!
#
# Author: Melissa LeBlanc-Williams
# License: Public Domain

# Import all board pins.
import board
import busio
from PIL import Image

# Import the HT16K33 LED matrix module.
from adafruit_ht16k33 import matrix

# Create the I2C interface.
i2c = busio.I2C(board.SCL, board.SDA)

# Create the matrix class.
# This creates a 16x8 matrix:
mtrx = matrix.Matrix16x8(i2c)
# Or this creates a 16x8 matrix backpack:
#mtrx = matrix.MatrixBackpack16x8(i2c)
# Or this creates a 8x8 matrix:
#mtrx = matrix.Matrix8x8(i2c)
# Or this creates a 8x8 bicolor matrix:
#mtrx = matrix.Matrix8x8x2(i2c)
# Finally you can optionally specify a custom I2C address of the HT16k33 like:
#mtrx = matrix.Matrix16x8(i2c, address=0x70)

if isinstance(mtrx, matrix.Matrix8x8x2):
image = Image.open("squares-color.png")
elif isinstance(mtrx, matrix.Matrix16x8):
image = Image.open("squares-mono-16x8.png")
else:
image = Image.open("squares-mono-8x8.png")

# Clear the matrix
mtrx.fill(0)
mtrx.image(image)
2 changes: 2 additions & 0 deletions examples/ht16k33_matrix_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
time.sleep(2)

# Draw a Smiley Face
matrix.fill(0)

for row in range(2, 6):
matrix[row, 0] = 1
matrix[row, 7] = 1
Expand Down