diff --git a/adafruit_ht16k33/matrix.py b/adafruit_ht16k33/matrix.py index 101e63b..c8f4832 100755 --- a/adafruit_ht16k33/matrix.py +++ b/adafruit_ht16k33/matrix.py @@ -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""" @@ -160,6 +176,12 @@ 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: @@ -167,8 +189,8 @@ def pixel(self, x, y, color=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 @@ -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() diff --git a/examples/ht16k33_matrix_pillow_image.py b/examples/ht16k33_matrix_pillow_image.py new file mode 100644 index 0000000..38da454 --- /dev/null +++ b/examples/ht16k33_matrix_pillow_image.py @@ -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) diff --git a/examples/ht16k33_matrix_simpletest.py b/examples/ht16k33_matrix_simpletest.py index f8b9627..f993928 100644 --- a/examples/ht16k33_matrix_simpletest.py +++ b/examples/ht16k33_matrix_simpletest.py @@ -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