Skip to content

Commit 1a003c7

Browse files
mrmcwethytannewt
authored andcommitted
Updated MAX7219 to be compatible with CircuitPython (#1)
1 parent 04669e9 commit 1a003c7

File tree

6 files changed

+434
-101
lines changed

6 files changed

+434
-101
lines changed

adafruit_max7219.py

Lines changed: 0 additions & 90 deletions
This file was deleted.

adafruit_max7219/bcddigits.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2017 Dan Halbert
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+
24+
"""
25+
:mod:`adafruit_max7219.bcddigits.BCDDigits`
26+
====================================================
27+
"""
28+
from adafruit_max7219 import max7219
29+
30+
_DECODEMODE = const(9)
31+
_SCANLIMIT = const(11)
32+
_SHUTDOWN = const(12)
33+
_DISPLAYTEST = const(15)
34+
35+
class BCDDigits(max7219.MAX7219):
36+
"""
37+
Basic support for display on a 7-Segment BCD display controlled
38+
by a Max7219 chip using SPI.
39+
"""
40+
def __init__(self, spi, cs, nDigits=1):
41+
"""
42+
:param object spi: an spi busio or spi bitbangio object
43+
:param ~digitalio.DigitalInOut cs: digital in/out to use as chip select signal
44+
:param int nDigits: number of led 7-segment digits; default 1; max 8
45+
"""
46+
self.nD = nDigits
47+
super().__init__(self.nD, 8 ,spi ,cs)
48+
49+
def init_display(self):
50+
51+
for cmd, data in (
52+
(_SHUTDOWN, 0),
53+
(_DISPLAYTEST, 0),
54+
(_SCANLIMIT, 7),
55+
(_DECODEMODE, (2**self.nD)-1),
56+
(_SHUTDOWN, 1),
57+
):
58+
self.write_cmd(cmd, data)
59+
60+
self.clear_all()
61+
self.show()
62+
63+
def set_digit(self, d, v):
64+
"""
65+
set one digit in the display
66+
:param int d: the digit position; zero-based
67+
:param int v: integer ranging from 0->15
68+
"""
69+
d = self.nD - d - 1
70+
for i in range(4):
71+
#print('digit {} pixel {} value {}'.format(d,i+4,v & 0x01))
72+
self.pixel(d,i,v & 0x01)
73+
v >>= 1
74+
75+
def set_digits(self, s, ds):
76+
"""
77+
set the display from a list
78+
:param int s: digit to start display zero-based
79+
:param list ds: list of integer values ranging from 0->15
80+
"""
81+
for d in ds:
82+
#print('set digit {} start {}'.format(d,start))
83+
self.set_digit(s,d)
84+
s += 1
85+
86+
def show_dot(self,d, col=None):
87+
"""
88+
set the decimal point for a digit
89+
:param int d: the digit to set the decimal point zero-based
90+
:param int col: value > zero lights the decimal point, else unlights the point
91+
"""
92+
if d < self.nD and d >= 0:
93+
#print('set dot {} = {}'.format((self.nD - d -1),col))
94+
self.pixel(self.nD-d-1, 7,col)
95+
96+
def clear_all(self):
97+
"""
98+
clear all digits and decimal points
99+
"""
100+
self.fill(1)
101+
for i in range(self.nD):
102+
self.show_dot(i)
103+
104+
def show_str(self,s,str):
105+
"""
106+
displays a numeric str in the display. shows digits 0-9, -, and .
107+
:param int s: start position to show the numeric string
108+
:param string str: the numeric string
109+
"""
110+
ci = s
111+
for i in range (len(str)):
112+
c = str[i]
113+
# print('c {}'.format(c))
114+
v = 0x0f # assume blank
115+
if c >= '0' and c<='9':
116+
v = int(c)
117+
elif c == '-':
118+
v = 10 # minus sign
119+
elif c == '.':
120+
self.show_dot(ci-1,1)
121+
continue
122+
self.set_digit(ci,v)
123+
ci += 1
124+
125+
def show_help(self, s):
126+
"""
127+
display the word HELP in the display
128+
:param int s: start position to show HELP
129+
"""
130+
digits = [12,11,13,14]
131+
self.set_digits(s,digits)
132+

adafruit_max7219/matrices.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2017 Dan Halbert
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+
24+
"""
25+
:mod:`adafruit_max7219.matrices.Matrix8x8`
26+
====================================================
27+
"""
28+
from adafruit_max7219 import max7219
29+
30+
_DECODEMODE = const(9)
31+
_SCANLIMIT = const(11)
32+
_SHUTDOWN = const(12)
33+
_DISPLAYTEST = const(15)
34+
35+
class Matrix8x8(max7219.MAX7219):
36+
def __init__(self, spi, cs):
37+
"""
38+
:param object spi: an spi busio or spi bitbangio object
39+
:param ~digitalio.DigitalInOut cs: digital in/out to use as chip select signal
40+
"""
41+
super().__init__(8,8,spi,cs)
42+
43+
def init_display(self):
44+
for cmd, data in (
45+
(_SHUTDOWN, 0),
46+
(_DISPLAYTEST, 0),
47+
(_SCANLIMIT, 7),
48+
(_DECODEMODE, 0),
49+
(_SHUTDOWN, 1),
50+
):
51+
self.write_cmd(cmd, data)
52+
53+
self.fill(0)
54+
self.show()
55+
56+
def text(self, str, x, y, col=1):
57+
"""
58+
draw text in the 8x8 matrix.
59+
"""
60+
self.framebuf.text(str, x, y, col)
61+
62+
def clear_all(self):
63+
"""
64+
unlights all matrix leds
65+
"""
66+
self.fill(0)
67+

0 commit comments

Comments
 (0)