Skip to content

Commit 0a783b2

Browse files
authored
Merge pull request #29 from makermelissa/master
Added 8x16 LED Matrix FeatherWing
2 parents 44eb33e + 09e0267 commit 0a783b2

File tree

4 files changed

+263
-0
lines changed

4 files changed

+263
-0
lines changed
+193
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2019 Melissa LeBlanc-Williams for Adafruit Industries LLC
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
"""
23+
`adafruit_featherwing.matrix_featherwing`
24+
====================================================
25+
26+
Helper for using the `Adafruit 8x16 LED Matrix FeatherWing
27+
<https://www.adafruit.com/product/3155>`_.
28+
29+
* Author(s): Melissa LeBlanc-Williams
30+
"""
31+
32+
__version__ = "0.0.0-auto.0"
33+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git"
34+
35+
import adafruit_ht16k33.matrix as matrix
36+
from adafruit_featherwing import shared
37+
38+
class MatrixFeatherWing:
39+
"""Class representing an `Adafruit 8x16 LED Matrix FeatherWing
40+
<https://www.adafruit.com/product/3155>`_.
41+
42+
Automatically uses the feather's I2C bus."""
43+
def __init__(self, address=0x70):
44+
self._matrix = matrix.Matrix16x8(shared.I2C_BUS, address)
45+
self._matrix.auto_write = False
46+
self.columns = 16
47+
self.rows = 8
48+
self._auto_write = True
49+
50+
def __getitem__(self, key):
51+
"""
52+
Get the current value of a pixel
53+
"""
54+
x, y = key
55+
return self.pixel(x, y)
56+
57+
def __setitem__(self, key, value):
58+
"""
59+
Turn a pixel off or on
60+
"""
61+
x, y = key
62+
self.pixel(x, y, value)
63+
self._update()
64+
65+
def _update(self):
66+
"""
67+
Update the Display automatically if auto_write is set to True
68+
"""
69+
if self._auto_write:
70+
self._matrix.show()
71+
72+
def pixel(self, x, y, color=None):
73+
"""
74+
Turn a pixel on or off or retrieve a pixel value
75+
76+
:param int x: The pixel row
77+
:param int y: The pixel column
78+
:param color: Whether to turn the pixel on or off
79+
:type color: int or bool
80+
"""
81+
value = self._matrix.pixel(x, y, color)
82+
self._update()
83+
return value
84+
85+
def show(self):
86+
"""
87+
Update the Pixels. This is only needed if auto_write is set to False
88+
This can be very useful for more advanced graphics effects.
89+
"""
90+
self._matrix.show()
91+
92+
def fill(self, fill):
93+
"""
94+
Turn all pixels on or off
95+
96+
:param bool fill: True turns all pixels on, False turns all pixels off
97+
98+
"""
99+
if isinstance(fill, bool):
100+
self._matrix.fill(1 if fill else 0)
101+
self._update()
102+
else:
103+
raise ValueError('Must set to either True or False.')
104+
105+
def shift_right(self, rotate=False):
106+
"""
107+
Shift all pixels right
108+
109+
:param rotate: (Optional) Rotate the shifted pixels to the left side (default=False)
110+
"""
111+
for y in range(0, self.rows):
112+
last_pixel = self._matrix[self.columns - 1, y] if rotate else 0
113+
for x in range(self.columns - 1, 0, -1):
114+
self._matrix[x, y] = self._matrix[x - 1, y]
115+
self._matrix[0, y] = last_pixel
116+
self._update()
117+
118+
def shift_left(self, rotate=False):
119+
"""
120+
Shift all pixels left
121+
122+
:param rotate: (Optional) Rotate the shifted pixels to the right side (default=False)
123+
"""
124+
for y in range(0, self.rows):
125+
last_pixel = self._matrix[0, y] if rotate else 0
126+
for x in range(0, self.columns - 1):
127+
self._matrix[x, y] = self._matrix[x + 1, y]
128+
self._matrix[self.columns - 1, y] = last_pixel
129+
self._update()
130+
131+
def shift_up(self, rotate=False):
132+
"""
133+
Shift all pixels up
134+
135+
:param rotate: (Optional) Rotate the shifted pixels to bottom (default=False)
136+
"""
137+
for x in range(0, self.columns):
138+
last_pixel = self._matrix[x, self.rows - 1] if rotate else 0
139+
for y in range(self.rows - 1, 0, -1):
140+
self._matrix[x, y] = self._matrix[x, y - 1]
141+
self._matrix[x, 0] = last_pixel
142+
self._update()
143+
144+
def shift_down(self, rotate=False):
145+
"""
146+
Shift all pixels down
147+
148+
:param rotate: (Optional) Rotate the shifted pixels to top (default=False)
149+
"""
150+
for x in range(0, self.columns):
151+
last_pixel = self._matrix[x, 0] if rotate else 0
152+
for y in range(0, self.rows - 1):
153+
self._matrix[x, y] = self._matrix[x, y + 1]
154+
self._matrix[x, self.rows - 1] = last_pixel
155+
self._update()
156+
157+
@property
158+
def auto_write(self):
159+
"""
160+
Whether or not we are automatically updating
161+
If set to false, be sure to call show() to update
162+
"""
163+
return self._auto_write
164+
165+
@auto_write.setter
166+
def auto_write(self, write):
167+
if isinstance(write, bool):
168+
self._auto_write = write
169+
170+
@property
171+
def blink_rate(self):
172+
"""
173+
Blink Rate returns the current rate that the pixels blink.
174+
0 = Not Blinking
175+
1-3 = Successively slower blink rates
176+
"""
177+
return self._matrix.blink_rate
178+
179+
@blink_rate.setter
180+
def blink_rate(self, rate):
181+
self._matrix.blink_rate = rate
182+
183+
@property
184+
def brightness(self):
185+
"""
186+
Brightness returns the current display brightness.
187+
0-15 = Dimmest to Brightest Setting
188+
"""
189+
return self._matrix.brightness
190+
191+
@brightness.setter
192+
def brightness(self, brightness):
193+
self._matrix.brightness = brightness

docs/api.rst

+3
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@
2121

2222
.. automodule:: adafruit_featherwing.gps_featherwing
2323
:members:
24+
25+
.. automodule:: adafruit_featherwing.matrix_featherwing
26+
:members:

docs/examples.rst

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ Ensure your device works with this simple test.
3535
:caption: examples/featherwing_gps_simpletest.py
3636
:linenos:
3737

38+
.. literalinclude:: ../examples/featherwing_matrix_simpletest.py
39+
:caption: examples/featherwing_matrix_simpletest.py
40+
:linenos:
41+
3842
Other Tests
3943
------------
4044

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""
2+
This example will demonstrate some graphic effects and then
3+
draw a smiley face and shift it around the display
4+
"""
5+
import time
6+
from adafruit_featherwing import matrix_featherwing
7+
8+
matrix = matrix_featherwing.MatrixFeatherWing()
9+
10+
# Create a Fade-in Effect
11+
matrix.brightness = 0
12+
matrix.fill(True)
13+
for level in range(0, 16):
14+
matrix.brightness = level
15+
time.sleep(0.1)
16+
17+
# Show the different Blink Rates
18+
for level in range(3, -1, -1):
19+
matrix.blink_rate = level
20+
time.sleep(4)
21+
22+
# Create a Fade-out Effect
23+
for level in range(15, -1, -1):
24+
matrix.brightness = level
25+
time.sleep(0.1)
26+
matrix.fill(False)
27+
28+
# Reset the brightness to full
29+
matrix.brightness = 15
30+
31+
# Clear the Screen
32+
matrix.fill(False)
33+
34+
# Draw a Smiley Face
35+
for row in range(2, 6):
36+
matrix[row, 0] = 1
37+
matrix[row, 7] = 1
38+
39+
for column in range(2, 6):
40+
matrix[0, column] = 1
41+
matrix[7, column] = 1
42+
43+
matrix[1, 1] = 1
44+
matrix[1, 6] = 1
45+
matrix[6, 1] = 1
46+
matrix[6, 6] = 1
47+
matrix[2, 5] = 1
48+
matrix[5, 5] = 1
49+
matrix[2, 3] = 1
50+
matrix[5, 3] = 1
51+
matrix[3, 2] = 1
52+
matrix[4, 2] = 1
53+
54+
# Move the Smiley Face Around
55+
while True:
56+
for frame in range(0, 8):
57+
matrix.shift_right()
58+
for frame in range(0, 8):
59+
matrix.shift_down(True)
60+
for frame in range(0, 8):
61+
matrix.shift_left()
62+
for frame in range(0, 8):
63+
matrix.shift_up(True)

0 commit comments

Comments
 (0)