Skip to content

Commit d4b5b36

Browse files
committed
Linting + Generic shift function
1 parent e2c290c commit d4b5b36

File tree

2 files changed

+42
-29
lines changed

2 files changed

+42
-29
lines changed

adafruit_ht16k33/matrix.py

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -52,61 +52,75 @@ def __setitem__(self, key, value):
5252
x, y = key
5353
self.pixel(x, y, value)
5454

55-
def shift_right(self, rotate=False):
55+
def shift(self, x, y, rotate=False):
5656
"""
57-
Shift all pixels right
57+
Shift pixels by x and y
5858
5959
:param rotate: (Optional) Rotate the shifted pixels to the left side (default=False)
6060
"""
61-
for y in range(0, self.rows):
62-
last_pixel = self[self.columns - 1, y] if rotate else 0
63-
for x in range(self.columns - 1, 0, -1):
64-
self[x, y] = self[x - 1, y]
65-
self[0, y] = last_pixel
61+
if x > 0: # Shift Right
62+
for _ in range(x):
63+
for row in range(0, self.rows):
64+
last_pixel = self[self.columns - 1, row] if rotate else 0
65+
for col in range(self.columns - 1, 0, -1):
66+
self[col, row] = self[col - 1, row]
67+
self[0, row] = last_pixel
68+
elif x < 0: # Shift Left
69+
for _ in range(-x):
70+
for row in range(0, self.rows):
71+
last_pixel = self[0, row] if rotate else 0
72+
for col in range(0, self.columns - 1):
73+
self[col, row] = self[col + 1, row]
74+
self[self.columns - 1, row] = last_pixel
75+
if y > 0: # Shift Up
76+
for _ in range(y):
77+
for col in range(0, self.columns):
78+
last_pixel = self[col, self.rows - 1] if rotate else 0
79+
for row in range(self.rows - 1, 0, -1):
80+
self[col, row] = self[col, row - 1]
81+
self[col, 0] = last_pixel
82+
elif y < 0: # Shift Down
83+
for _ in range(-y):
84+
for col in range(0, self.columns):
85+
last_pixel = self[col, 0] if rotate else 0
86+
for row in range(0, self.rows - 1):
87+
self[col, row] = self[col, row + 1]
88+
self[col, self.rows - 1] = last_pixel
6689
if self._auto_write:
6790
self.show()
6891

92+
93+
def shift_right(self, rotate=False):
94+
"""
95+
Shift all pixels right
96+
97+
:param rotate: (Optional) Rotate the shifted pixels to the left side (default=False)
98+
"""
99+
self.shift(1, 0, rotate)
100+
69101
def shift_left(self, rotate=False):
70102
"""
71103
Shift all pixels left
72104
73105
:param rotate: (Optional) Rotate the shifted pixels to the right side (default=False)
74106
"""
75-
for y in range(0, self.rows):
76-
last_pixel = self[0, y] if rotate else 0
77-
for x in range(0, self.columns - 1):
78-
self[x, y] = self[x + 1, y]
79-
self[self.columns - 1, y] = last_pixel
80-
if self._auto_write:
81-
self.show()
107+
self.shift(-1, 0, rotate)
82108

83109
def shift_up(self, rotate=False):
84110
"""
85111
Shift all pixels up
86112
87113
:param rotate: (Optional) Rotate the shifted pixels to bottom (default=False)
88114
"""
89-
for x in range(0, self.columns):
90-
last_pixel = self[x, self.rows - 1] if rotate else 0
91-
for y in range(self.rows - 1, 0, -1):
92-
self[x, y] = self[x, y - 1]
93-
self[x, 0] = last_pixel
94-
if self._auto_write:
95-
self.show()
115+
self.shift(0, 1, rotate)
96116

97117
def shift_down(self, rotate=False):
98118
"""
99119
Shift all pixels down
100120
101121
:param rotate: (Optional) Rotate the shifted pixels to top (default=False)
102122
"""
103-
for x in range(0, self.columns):
104-
last_pixel = self[x, 0] if rotate else 0
105-
for y in range(0, self.rows - 1):
106-
self[x, y] = self[x, y + 1]
107-
self[x, self.rows - 1] = last_pixel
108-
if self._auto_write:
109-
self.show()
123+
self.shift(0, -1, rotate)
110124

111125
@property
112126
def columns(self):

examples/ht16k33_matrix_simpletest.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,3 @@
7373
for frame in range(0, 8):
7474
matrix.shift_up(True)
7575
time.sleep(0.05)
76-

0 commit comments

Comments
 (0)