3.56KiB; Python | 2019-09-04 18:22:20+02 | SLOC 92
1
"""
2
MicroPython max7219 cascadable 8x8 LED matrix driver
3
https://github.com/mcauser/micropython-max7219
4
5
MIT License
6
Copyright (c) 2017 Mike Causer
7
8
Permission is hereby granted, free of charge, to any person obtaining a copy
9
of this software and associated documentation files (the "Software"), to deal
10
in the Software without restriction, including without limitation the rights
11
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
copies of the Software, and to permit persons to whom the Software is
13
furnished to do so, subject to the following conditions:
14
15
The above copyright notice and this permission notice shall be included in all
16
copies or substantial portions of the Software.
17
18
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
SOFTWARE.
25
"""
26
27
from micropython import const
28
import framebuf
29
30
_NOOP = const(0)
31
_DIGIT0 = const(1)
32
_DECODEMODE = const(9)
33
_INTENSITY = const(10)
34
_SCANLIMIT = const(11)
35
_SHUTDOWN = const(12)
36
_DISPLAYTEST = const(15)
37
38
class Matrix8x8:
39
    def __init__(self, spi, cs, num):
40 1
␣␣␣␣␣␣␣␣
41
        self.spi = spi
42
        self.cs = cs
43
        self.cs.init(cs.OUT, True)
44
        self.buffer = bytearray(8 * num)
45
        self.num = num
46
        fb = framebuf.FrameBuffer(self.buffer, 8 * num, 8, framebuf.MONO_HLSB)
47
        self.framebuf = fb
48
        # Provide methods for accessing FrameBuffer graphics primitives. This is a workround
49
        # because inheritance from a native class is currently unsupported.
50
        # http://docs.micropython.org/en/latest/pyboard/library/framebuf.html
51
        self.fill = fb.fill  # (col)
52
        self.pixel = fb.pixel # (x, y[, c])
53
        self.hline = fb.hline  # (x, y, w, col)
54
        self.vline = fb.vline  # (x, y, h, col)
55
        self.line = fb.line  # (x1, y1, x2, y2, col)
56
        self.rect = fb.rect  # (x, y, w, h, col)
57
        self.fill_rect = fb.fill_rect  # (x, y, w, h, col)
58
        self.text = fb.text  # (string, x, y, col=1)
59
        self.scroll = fb.scroll  # (dx, dy)
60
        self.blit = fb.blit  # (fbuf, x, y[, key])
61
        self.init()
62
63
    def _write(self, command, data):
64
        self.cs(0)
65
        for m in range(self.num):
66
            self.spi.write(bytearray([command, data]))
67
        self.cs(1)
68
69
    def init(self):
70
        for command, data in (
71
            (_SHUTDOWN, 0),
72
            (_DISPLAYTEST, 0),
73
            (_SCANLIMIT, 7),
74
            (_DECODEMODE, 0),
75
            (_SHUTDOWN, 1),
76
        ):
77
            self._write(command, data)
78
79
    def brightness(self, value):
80
        if not 0 <= value <= 15:
81
            raise ValueError("Brightness out of range")
82
        self._write(_INTENSITY, value)
83
84
    def show(self):
85
        for y in range(8):
86
            self.cs(0)
87
            for m in range(self.num):
88
                self.spi.write(bytearray([_DIGIT0 + y, self.buffer[(y * self.num) + m]]))
89
            self.cs(1)
90
91
92 1
from machine import Pin, SPI
93 2
import time
94
95
spi = SPI(1, baudrate=10000000, polarity=1, phase=0, sck=Pin(21), mosi=Pin(16))
96
ss = Pin(17, Pin.OUT)
97
display = Matrix8x8(spi, ss, 1)
98
display.brightness(15)
99
100
text = "Alerta"
101
display.text(text,0,0,1)
102
display.show()
103
while True:
104
  for i in range(len(text)):
105
    display = Matrix8x8(spi, ss, i+1)
106
    display.text(text,0,0,1)
107
    time.sleep(1)
108
    display.show()