-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatrix7219.py
79 lines (69 loc) · 1.77 KB
/
matrix7219.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
original version by Radomir Dopieralski
https://bitbucket.org/thesheep/micropython-max7219/
modified according to ideas from jezdean
https://github.com/microbit-playground/matrix7seg
"""
from microbit import spi
_NOOP = 0
_DIGIT0 = 1
_DIGIT1 = 2
_DIGIT2 = 3
_DIGIT3 = 4
_DIGIT4 = 5
_DIGIT5 = 6
_DIGIT6 = 7
_DIGIT7 = 8
_DECODEMODE = 9
_INTENSITY = 10
_SCANLIMIT = 11
_SHUTDOWN = 12
_DISPLAYTEST = 15
"""
sample usage
import microbit
import matrix7219
display = matrix7219.Matrix8x8(microbit.spi, microbit.pin0)
display.fill(True)
display.pixel(2, 2, False)
display.show()
"""
class Matrix8x8:
def __init__(self, spi, cs):
self.spi = spi
self.cs = cs
self.buffer = bytearray(8)
spi.init()
self.init()
def _register(self, command, data):
# write to display
self.cs.write_digital(0)
self.spi.write(bytearray([command, data]))
self.cs.write_digital(1)
def init(self):
for command, data in (
(_SHUTDOWN, 0),
(_DISPLAYTEST, 0),
(_SCANLIMIT, 7),
(_DECODEMODE, 0),
(_SHUTDOWN, 1),
):
self._register(command, data)
def brightness(self, value):
if not 0<= value <= 15:
raise ValueError("Brightness out of range")
self._register(_INTENSITY, value)
def fill(self, color):
data = 0xff if color else 0x00
for y in range(8):
self.buffer[y] = data
def pixel(self, x, y, color=None):
if color is None:
return bool(self.buffer[y] & 1 << x)
elif color:
self.buffer[y] |= 1 << x
else:
self.buffer[y] &= ~(1 << x)
def show(self):
for y in range(8):
self._register(_DIGIT0 + y, self.buffer[y])