Skip to content

Added Shift functions for matrix #50

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 5 commits into from
Jan 15, 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
1 change: 0 additions & 1 deletion adafruit_ht16k33/ht16k33.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
89 changes: 87 additions & 2 deletions adafruit_ht16k33/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@
================

"""

from adafruit_ht16k33.ht16k33 import HT16K33

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HT16K33.git"

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:
Expand All @@ -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:
Expand All @@ -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."""
Expand Down
40 changes: 39 additions & 1 deletion examples/ht16k33_matrix_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# License: Public Domain

# Import all board pins.
import time
import board
import busio

Expand All @@ -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)