From 0f4ede8f11921a4c00e062bf64aba60913525826 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Wed, 15 Jan 2020 10:22:00 -0800 Subject: [PATCH 1/5] Added Shift functions for matrix --- adafruit_ht16k33/ht16k33.py | 1 - adafruit_ht16k33/matrix.py | 64 ++++++++++++++++++++++++++- examples/ht16k33_matrix_simpletest.py | 41 ++++++++++++++++- 3 files changed, 103 insertions(+), 3 deletions(-) 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..3908847 100755 --- a/adafruit_ht16k33/matrix.py +++ b/adafruit_ht16k33/matrix.py @@ -33,6 +33,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 +53,67 @@ def __setitem__(self, key, value): x, y = key self.pixel(x, y, value) + def shift_right(self, rotate=False): + """ + Shift all pixels right + + :param rotate: (Optional) Rotate the shifted pixels to the left side (default=False) + """ + for y in range(0, self.rows): + last_pixel = self[self.columns - 1, y] if rotate else 0 + for x in range(self.columns - 1, 0, -1): + self[x, y] = self[x - 1, y] + self[0, y] = last_pixel + if self._auto_write: + self.show() + + def shift_left(self, rotate=False): + """ + Shift all pixels left + + :param rotate: (Optional) Rotate the shifted pixels to the right side (default=False) + """ + for y in range(0, self.rows): + last_pixel = self[0, y] if rotate else 0 + for x in range(0, self.columns - 1): + self[x, y] = self[x + 1, y] + self[self.columns - 1, y] = last_pixel + if self._auto_write: + self.show() + + def shift_up(self, rotate=False): + """ + Shift all pixels up + + :param rotate: (Optional) Rotate the shifted pixels to bottom (default=False) + """ + for x in range(0, self.columns): + last_pixel = self[x, self.rows - 1] if rotate else 0 + for y in range(self.rows - 1, 0, -1): + self[x, y] = self[x, y - 1] + self[x, 0] = last_pixel + if self._auto_write: + self.show() + + def shift_down(self, rotate=False): + """ + Shift all pixels down + + :param rotate: (Optional) Rotate the shifted pixels to top (default=False) + """ + for x in range(0, self.columns): + last_pixel = self[x, 0] if rotate else 0 + for y in range(0, self.rows - 1): + self[x, y] = self[x, y + 1] + self[x, self.rows - 1] = last_pixel + if self._auto_write: + self.show() + class Matrix16x8(Matrix8x8): """The matrix wing.""" + columns = 16 + rows = 8 + def pixel(self, x, y, color=None): """Get or set the color of a given pixel.""" if not 0 <= x <= 15: @@ -63,7 +125,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..8e6e680 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,47 @@ # 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) + From 86a20c7cb36efe8493b3b2de7368b82a5b95ad36 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Wed, 15 Jan 2020 10:31:05 -0800 Subject: [PATCH 2/5] Converted rows/columns to read-only property --- adafruit_ht16k33/matrix.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/adafruit_ht16k33/matrix.py b/adafruit_ht16k33/matrix.py index 3908847..dda6973 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,8 +32,8 @@ class Matrix8x8(HT16K33): """A single matrix.""" - columns = 8 - rows = 8 + _columns = 8 + _rows = 8 def pixel(self, x, y, color=None): """Get or set the color of a given pixel.""" @@ -109,10 +108,17 @@ def shift_down(self, rotate=False): if self._auto_write: self.show() + @property + def columns(self): + return self._columns + + @property + def rows(self): + return self._rows + class Matrix16x8(Matrix8x8): """The matrix wing.""" - columns = 16 - rows = 8 + _columns = 16 def pixel(self, x, y, color=None): """Get or set the color of a given pixel.""" From e2c290c6174f603ea7ced4801663b79105b07a59 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Wed, 15 Jan 2020 10:35:05 -0800 Subject: [PATCH 3/5] Added missing docstrings --- adafruit_ht16k33/matrix.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/adafruit_ht16k33/matrix.py b/adafruit_ht16k33/matrix.py index dda6973..e93ab61 100755 --- a/adafruit_ht16k33/matrix.py +++ b/adafruit_ht16k33/matrix.py @@ -110,10 +110,12 @@ def shift_down(self, rotate=False): @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): From d4b5b361a3c10f238bf2ab3c524306c913d0c6ab Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Wed, 15 Jan 2020 11:28:53 -0800 Subject: [PATCH 4/5] Linting + Generic shift function --- adafruit_ht16k33/matrix.py | 70 ++++++++++++++++----------- examples/ht16k33_matrix_simpletest.py | 1 - 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/adafruit_ht16k33/matrix.py b/adafruit_ht16k33/matrix.py index e93ab61..e711aca 100755 --- a/adafruit_ht16k33/matrix.py +++ b/adafruit_ht16k33/matrix.py @@ -52,33 +52,59 @@ def __setitem__(self, key, value): x, y = key self.pixel(x, y, value) - def shift_right(self, rotate=False): + def shift(self, x, y, rotate=False): """ - Shift all pixels right + Shift pixels by x and y :param rotate: (Optional) Rotate the shifted pixels to the left side (default=False) """ - for y in range(0, self.rows): - last_pixel = self[self.columns - 1, y] if rotate else 0 - for x in range(self.columns - 1, 0, -1): - self[x, y] = self[x - 1, y] - self[0, y] = last_pixel + 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() + + 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) """ - for y in range(0, self.rows): - last_pixel = self[0, y] if rotate else 0 - for x in range(0, self.columns - 1): - self[x, y] = self[x + 1, y] - self[self.columns - 1, y] = last_pixel - if self._auto_write: - self.show() + self.shift(-1, 0, rotate) def shift_up(self, rotate=False): """ @@ -86,13 +112,7 @@ def shift_up(self, rotate=False): :param rotate: (Optional) Rotate the shifted pixels to bottom (default=False) """ - for x in range(0, self.columns): - last_pixel = self[x, self.rows - 1] if rotate else 0 - for y in range(self.rows - 1, 0, -1): - self[x, y] = self[x, y - 1] - self[x, 0] = last_pixel - if self._auto_write: - self.show() + self.shift(0, 1, rotate) def shift_down(self, rotate=False): """ @@ -100,13 +120,7 @@ def shift_down(self, rotate=False): :param rotate: (Optional) Rotate the shifted pixels to top (default=False) """ - for x in range(0, self.columns): - last_pixel = self[x, 0] if rotate else 0 - for y in range(0, self.rows - 1): - self[x, y] = self[x, y + 1] - self[x, self.rows - 1] = last_pixel - if self._auto_write: - self.show() + self.shift(0, -1, rotate) @property def columns(self): diff --git a/examples/ht16k33_matrix_simpletest.py b/examples/ht16k33_matrix_simpletest.py index 8e6e680..f8b9627 100644 --- a/examples/ht16k33_matrix_simpletest.py +++ b/examples/ht16k33_matrix_simpletest.py @@ -73,4 +73,3 @@ for frame in range(0, 8): matrix.shift_up(True) time.sleep(0.05) - From 66e5f032badc8d7a527136ff78aded2328d59faf Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Wed, 15 Jan 2020 11:43:01 -0800 Subject: [PATCH 5/5] Disabling pylint message --- adafruit_ht16k33/matrix.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/adafruit_ht16k33/matrix.py b/adafruit_ht16k33/matrix.py index e711aca..101e63b 100755 --- a/adafruit_ht16k33/matrix.py +++ b/adafruit_ht16k33/matrix.py @@ -52,6 +52,7 @@ 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 @@ -88,7 +89,7 @@ def shift(self, x, y, rotate=False): self[col, self.rows - 1] = last_pixel if self._auto_write: self.show() - + #pylint: enable=too-many-branches def shift_right(self, rotate=False): """