Skip to content

Commit 2a0c6c8

Browse files
authored
Merge pull request #50 from makermelissa/matrix_scroll
Added Shift functions for matrix
2 parents 5b56a00 + 66e5f03 commit 2a0c6c8

File tree

3 files changed

+126
-4
lines changed

3 files changed

+126
-4
lines changed

adafruit_ht16k33/ht16k33.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ def __init__(self, i2c, address=0x70, auto_write=True):
5252
self.i2c_device = i2c_device.I2CDevice(i2c, address)
5353
self._temp = bytearray(1)
5454
self._buffer = bytearray(17)
55-
self._auto_write = None
5655
self._auto_write = auto_write
5756
self.fill(0)
5857
self._write_cmd(_HT16K33_OSCILATOR_ON)

adafruit_ht16k33/matrix.py

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@
2525
================
2626
2727
"""
28-
2928
from adafruit_ht16k33.ht16k33 import HT16K33
3029

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

3433
class Matrix8x8(HT16K33):
3534
"""A single matrix."""
35+
_columns = 8
36+
_rows = 8
37+
3638
def pixel(self, x, y, color=None):
3739
"""Get or set the color of a given pixel."""
3840
if not 0 <= x <= 7:
@@ -50,8 +52,91 @@ def __setitem__(self, key, value):
5052
x, y = key
5153
self.pixel(x, y, value)
5254

55+
#pylint: disable=too-many-branches
56+
def shift(self, x, y, rotate=False):
57+
"""
58+
Shift pixels by x and y
59+
60+
:param rotate: (Optional) Rotate the shifted pixels to the left side (default=False)
61+
"""
62+
if x > 0: # Shift Right
63+
for _ in range(x):
64+
for row in range(0, self.rows):
65+
last_pixel = self[self.columns - 1, row] if rotate else 0
66+
for col in range(self.columns - 1, 0, -1):
67+
self[col, row] = self[col - 1, row]
68+
self[0, row] = last_pixel
69+
elif x < 0: # Shift Left
70+
for _ in range(-x):
71+
for row in range(0, self.rows):
72+
last_pixel = self[0, row] if rotate else 0
73+
for col in range(0, self.columns - 1):
74+
self[col, row] = self[col + 1, row]
75+
self[self.columns - 1, row] = last_pixel
76+
if y > 0: # Shift Up
77+
for _ in range(y):
78+
for col in range(0, self.columns):
79+
last_pixel = self[col, self.rows - 1] if rotate else 0
80+
for row in range(self.rows - 1, 0, -1):
81+
self[col, row] = self[col, row - 1]
82+
self[col, 0] = last_pixel
83+
elif y < 0: # Shift Down
84+
for _ in range(-y):
85+
for col in range(0, self.columns):
86+
last_pixel = self[col, 0] if rotate else 0
87+
for row in range(0, self.rows - 1):
88+
self[col, row] = self[col, row + 1]
89+
self[col, self.rows - 1] = last_pixel
90+
if self._auto_write:
91+
self.show()
92+
#pylint: enable=too-many-branches
93+
94+
def shift_right(self, rotate=False):
95+
"""
96+
Shift all pixels right
97+
98+
:param rotate: (Optional) Rotate the shifted pixels to the left side (default=False)
99+
"""
100+
self.shift(1, 0, rotate)
101+
102+
def shift_left(self, rotate=False):
103+
"""
104+
Shift all pixels left
105+
106+
:param rotate: (Optional) Rotate the shifted pixels to the right side (default=False)
107+
"""
108+
self.shift(-1, 0, rotate)
109+
110+
def shift_up(self, rotate=False):
111+
"""
112+
Shift all pixels up
113+
114+
:param rotate: (Optional) Rotate the shifted pixels to bottom (default=False)
115+
"""
116+
self.shift(0, 1, rotate)
117+
118+
def shift_down(self, rotate=False):
119+
"""
120+
Shift all pixels down
121+
122+
:param rotate: (Optional) Rotate the shifted pixels to top (default=False)
123+
"""
124+
self.shift(0, -1, rotate)
125+
126+
@property
127+
def columns(self):
128+
"""Read-only property for number of columns"""
129+
return self._columns
130+
131+
@property
132+
def rows(self):
133+
"""Read-only property for number of rows"""
134+
return self._rows
135+
53136
class Matrix16x8(Matrix8x8):
54137
"""The matrix wing."""
138+
_columns = 16
139+
55140
def pixel(self, x, y, color=None):
56141
"""Get or set the color of a given pixel."""
57142
if not 0 <= x <= 15:
@@ -63,7 +148,7 @@ def pixel(self, x, y, color=None):
63148
y += 8
64149
return super()._pixel(y, x, color)
65150

66-
class MatrixBackpack16x8(Matrix8x8):
151+
class MatrixBackpack16x8(Matrix16x8):
67152
"""A double matrix backpack."""
68153
def pixel(self, x, y, color=None):
69154
"""Get or set the color of a given pixel."""

examples/ht16k33_matrix_simpletest.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# License: Public Domain
55

66
# Import all board pins.
7+
import time
78
import board
89
import busio
910

@@ -29,9 +30,46 @@
2930
# Clear the matrix.
3031
matrix.fill(0)
3132

32-
# Set a pixel in the origin 0,0 position.
33+
# Set a pixel in the origin 0, 0 position.
3334
matrix[0, 0] = 1
3435
# Set a pixel in the middle 8, 4 position.
3536
matrix[8, 4] = 1
3637
# Set a pixel in the opposite 15, 7 position.
3738
matrix[15, 7] = 1
39+
40+
time.sleep(2)
41+
42+
# Draw a Smiley Face
43+
for row in range(2, 6):
44+
matrix[row, 0] = 1
45+
matrix[row, 7] = 1
46+
47+
for column in range(2, 6):
48+
matrix[0, column] = 1
49+
matrix[7, column] = 1
50+
51+
matrix[1, 1] = 1
52+
matrix[1, 6] = 1
53+
matrix[6, 1] = 1
54+
matrix[6, 6] = 1
55+
matrix[2, 5] = 1
56+
matrix[5, 5] = 1
57+
matrix[2, 3] = 1
58+
matrix[5, 3] = 1
59+
matrix[3, 2] = 1
60+
matrix[4, 2] = 1
61+
62+
# Move the Smiley Face Around
63+
while True:
64+
for frame in range(0, 8):
65+
matrix.shift_right(True)
66+
time.sleep(0.05)
67+
for frame in range(0, 8):
68+
matrix.shift_down(True)
69+
time.sleep(0.05)
70+
for frame in range(0, 8):
71+
matrix.shift_left(True)
72+
time.sleep(0.05)
73+
for frame in range(0, 8):
74+
matrix.shift_up(True)
75+
time.sleep(0.05)

0 commit comments

Comments
 (0)