diff --git a/adafruit_ht16k33/ht16k33.py b/adafruit_ht16k33/ht16k33.py index b60317e..f0c4643 100644 --- a/adafruit_ht16k33/ht16k33.py +++ b/adafruit_ht16k33/ht16k33.py @@ -52,7 +52,6 @@ def __init__(self, i2c, address=0x70, auto_write=True): self.i2c_device = i2c_device.I2CDevice(i2c, address) self._temp = bytearray(1) self._buffer = bytearray(17) - self._auto_write = None self._auto_write = auto_write self.fill(0) self._write_cmd(_HT16K33_OSCILATOR_ON) diff --git a/adafruit_ht16k33/matrix.py b/adafruit_ht16k33/matrix.py index a9fa7ac..101e63b 100755 --- a/adafruit_ht16k33/matrix.py +++ b/adafruit_ht16k33/matrix.py @@ -25,7 +25,6 @@ ================ """ - from adafruit_ht16k33.ht16k33 import HT16K33 __version__ = "0.0.0-auto.0" @@ -33,6 +32,9 @@ class Matrix8x8(HT16K33): """A single matrix.""" + _columns = 8 + _rows = 8 + def pixel(self, x, y, color=None): """Get or set the color of a given pixel.""" if not 0 <= x <= 7: @@ -50,8 +52,91 @@ def __setitem__(self, key, value): x, y = key self.pixel(x, y, value) + #pylint: disable=too-many-branches + def shift(self, x, y, rotate=False): + """ + Shift pixels by x and y + + :param rotate: (Optional) Rotate the shifted pixels to the left side (default=False) + """ + if x > 0: # Shift Right + for _ in range(x): + for row in range(0, self.rows): + last_pixel = self[self.columns - 1, row] if rotate else 0 + for col in range(self.columns - 1, 0, -1): + self[col, row] = self[col - 1, row] + self[0, row] = last_pixel + elif x < 0: # Shift Left + for _ in range(-x): + for row in range(0, self.rows): + last_pixel = self[0, row] if rotate else 0 + for col in range(0, self.columns - 1): + self[col, row] = self[col + 1, row] + self[self.columns - 1, row] = last_pixel + if y > 0: # Shift Up + for _ in range(y): + for col in range(0, self.columns): + last_pixel = self[col, self.rows - 1] if rotate else 0 + for row in range(self.rows - 1, 0, -1): + self[col, row] = self[col, row - 1] + self[col, 0] = last_pixel + elif y < 0: # Shift Down + for _ in range(-y): + for col in range(0, self.columns): + last_pixel = self[col, 0] if rotate else 0 + for row in range(0, self.rows - 1): + self[col, row] = self[col, row + 1] + self[col, self.rows - 1] = last_pixel + if self._auto_write: + self.show() + #pylint: enable=too-many-branches + + def shift_right(self, rotate=False): + """ + Shift all pixels right + + :param rotate: (Optional) Rotate the shifted pixels to the left side (default=False) + """ + self.shift(1, 0, rotate) + + def shift_left(self, rotate=False): + """ + Shift all pixels left + + :param rotate: (Optional) Rotate the shifted pixels to the right side (default=False) + """ + self.shift(-1, 0, rotate) + + def shift_up(self, rotate=False): + """ + Shift all pixels up + + :param rotate: (Optional) Rotate the shifted pixels to bottom (default=False) + """ + self.shift(0, 1, rotate) + + def shift_down(self, rotate=False): + """ + Shift all pixels down + + :param rotate: (Optional) Rotate the shifted pixels to top (default=False) + """ + self.shift(0, -1, rotate) + + @property + def columns(self): + """Read-only property for number of columns""" + return self._columns + + @property + def rows(self): + """Read-only property for number of rows""" + return self._rows + class Matrix16x8(Matrix8x8): """The matrix wing.""" + _columns = 16 + def pixel(self, x, y, color=None): """Get or set the color of a given pixel.""" if not 0 <= x <= 15: @@ -63,7 +148,7 @@ def pixel(self, x, y, color=None): y += 8 return super()._pixel(y, x, color) -class MatrixBackpack16x8(Matrix8x8): +class MatrixBackpack16x8(Matrix16x8): """A double matrix backpack.""" def pixel(self, x, y, color=None): """Get or set the color of a given pixel.""" diff --git a/examples/ht16k33_matrix_simpletest.py b/examples/ht16k33_matrix_simpletest.py index 8656c07..f8b9627 100644 --- a/examples/ht16k33_matrix_simpletest.py +++ b/examples/ht16k33_matrix_simpletest.py @@ -4,6 +4,7 @@ # License: Public Domain # Import all board pins. +import time import board import busio @@ -29,9 +30,46 @@ # Clear the matrix. matrix.fill(0) -# Set a pixel in the origin 0,0 position. +# Set a pixel in the origin 0, 0 position. matrix[0, 0] = 1 # Set a pixel in the middle 8, 4 position. matrix[8, 4] = 1 # Set a pixel in the opposite 15, 7 position. matrix[15, 7] = 1 + +time.sleep(2) + +# Draw a Smiley Face +for row in range(2, 6): + matrix[row, 0] = 1 + matrix[row, 7] = 1 + +for column in range(2, 6): + matrix[0, column] = 1 + matrix[7, column] = 1 + +matrix[1, 1] = 1 +matrix[1, 6] = 1 +matrix[6, 1] = 1 +matrix[6, 6] = 1 +matrix[2, 5] = 1 +matrix[5, 5] = 1 +matrix[2, 3] = 1 +matrix[5, 3] = 1 +matrix[3, 2] = 1 +matrix[4, 2] = 1 + +# Move the Smiley Face Around +while True: + for frame in range(0, 8): + matrix.shift_right(True) + time.sleep(0.05) + for frame in range(0, 8): + matrix.shift_down(True) + time.sleep(0.05) + for frame in range(0, 8): + matrix.shift_left(True) + time.sleep(0.05) + for frame in range(0, 8): + matrix.shift_up(True) + time.sleep(0.05)