Skip to content

Commit 0f4ede8

Browse files
committed
Added Shift functions for matrix
1 parent 5b56a00 commit 0f4ede8

File tree

3 files changed

+103
-3
lines changed

3 files changed

+103
-3
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: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333

3434
class Matrix8x8(HT16K33):
3535
"""A single matrix."""
36+
columns = 8
37+
rows = 8
38+
3639
def pixel(self, x, y, color=None):
3740
"""Get or set the color of a given pixel."""
3841
if not 0 <= x <= 7:
@@ -50,8 +53,67 @@ def __setitem__(self, key, value):
5053
x, y = key
5154
self.pixel(x, y, value)
5255

56+
def shift_right(self, rotate=False):
57+
"""
58+
Shift all pixels right
59+
60+
:param rotate: (Optional) Rotate the shifted pixels to the left side (default=False)
61+
"""
62+
for y in range(0, self.rows):
63+
last_pixel = self[self.columns - 1, y] if rotate else 0
64+
for x in range(self.columns - 1, 0, -1):
65+
self[x, y] = self[x - 1, y]
66+
self[0, y] = last_pixel
67+
if self._auto_write:
68+
self.show()
69+
70+
def shift_left(self, rotate=False):
71+
"""
72+
Shift all pixels left
73+
74+
:param rotate: (Optional) Rotate the shifted pixels to the right side (default=False)
75+
"""
76+
for y in range(0, self.rows):
77+
last_pixel = self[0, y] if rotate else 0
78+
for x in range(0, self.columns - 1):
79+
self[x, y] = self[x + 1, y]
80+
self[self.columns - 1, y] = last_pixel
81+
if self._auto_write:
82+
self.show()
83+
84+
def shift_up(self, rotate=False):
85+
"""
86+
Shift all pixels up
87+
88+
:param rotate: (Optional) Rotate the shifted pixels to bottom (default=False)
89+
"""
90+
for x in range(0, self.columns):
91+
last_pixel = self[x, self.rows - 1] if rotate else 0
92+
for y in range(self.rows - 1, 0, -1):
93+
self[x, y] = self[x, y - 1]
94+
self[x, 0] = last_pixel
95+
if self._auto_write:
96+
self.show()
97+
98+
def shift_down(self, rotate=False):
99+
"""
100+
Shift all pixels down
101+
102+
:param rotate: (Optional) Rotate the shifted pixels to top (default=False)
103+
"""
104+
for x in range(0, self.columns):
105+
last_pixel = self[x, 0] if rotate else 0
106+
for y in range(0, self.rows - 1):
107+
self[x, y] = self[x, y + 1]
108+
self[x, self.rows - 1] = last_pixel
109+
if self._auto_write:
110+
self.show()
111+
53112
class Matrix16x8(Matrix8x8):
54113
"""The matrix wing."""
114+
columns = 16
115+
rows = 8
116+
55117
def pixel(self, x, y, color=None):
56118
"""Get or set the color of a given pixel."""
57119
if not 0 <= x <= 15:
@@ -63,7 +125,7 @@ def pixel(self, x, y, color=None):
63125
y += 8
64126
return super()._pixel(y, x, color)
65127

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

examples/ht16k33_matrix_simpletest.py

Lines changed: 40 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,47 @@
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)
76+

0 commit comments

Comments
 (0)