Introduction¶
CircuitPython driver for the MAX7219 LED matrix driver chip.
See here for the equivalent MicroPython driver.
Dependencies¶
This driver depends on:
Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading the Adafruit library and driver bundle.
Usage Example¶
adafruit_max7219.Matrix8x8 Example¶
from adafruit_max7219 import matrices
from board import TX, RX, A2
import busio
import digitalio
import time
clk = RX
din = TX
cs = digitalio.DigitalInOut(A2)
spi = busio.SPI(clk, MOSI=din)
display = matrices.Matrix8x8(spi, cs)
while True:
display.brightness(3)
display.fill(1)
display.pixel(3, 3)
display.pixel(3, 4)
display.pixel(4, 3)
display.pixel(4, 4)
display.show()
time.sleep(3.0)
display.clear_all()
s = 'Hello, World!'
for c in range(len(s)*8):
display.fill(0)
display.text(s,-c,0)
display.show()
time.sleep(0.25)
adafruit_max7219.BCDDigits Example¶
from adafruit_max7219 import bcddigits
from board import TX, RX, A2
import bitbangio
import digitalio
clk = RX
din = TX
cs = digitalio.DigitalInOut(A2)
spi = bitbangio.SPI(clk, MOSI=din)
display = bcddigits.BCDDigits(spi, cs, nDigits=8)
display.clear_all()
display.show_str(0,'{:9.2f}'.format(-1234.56))
display.show()
Contributing¶
Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.
Building locally¶
To build this library locally you’ll need to install the circuitpython-build-tools package.
python3 -m venv .env
source .env/bin/activate
pip install circuitpython-build-tools
Once installed, make sure you are in the virtual environment:
source .env/bin/activate
Then run the build:
circuitpython-build-bundles --filename_prefix adafruit-circuitpython-max7219 --library_location .
Sphinx documentation¶
Sphinx is used to build the documentation based on rST files and comments in the code. First, install dependencies (feel free to reuse the virtual environment from above):
python3 -m venv .env
source .env/bin/activate
pip install Sphinx sphinx-rtd-theme
Now, once you have the virtual environment activated:
cd docs
sphinx-build -E -W -b html . _build/html
This will output the documentation to docs/_build/html
. Open the index.html in your browser to
view them. It will also (due to -W) error out on any warning like Travis will. This is a good way to
locally verify it will pass.
Table of Contents¶
Simple test¶
Ensure your device works with this simple test.
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 | import time
from board import TX, RX, A1
import busio
import digitalio
from adafruit_max7219 import matrices
mosi = TX
clk = RX
cs = digitalio.DigitalInOut(A1)
spi = busio.SPI(clk, MOSI=mosi)
matrix = matrices.Matrix8x8(spi, cs)
while True:
print("Cycle start")
# all lit up
matrix.fill(True)
matrix.show()
time.sleep(0.5)
# all off
matrix.fill(False)
matrix.show()
time.sleep(0.5)
# one column of leds lit
for i in range(8):
matrix.pixel(1, i, 1)
matrix.show()
time.sleep(0.5)
# now scroll the column to the right
for j in range(8):
matrix.scroll(1, 0)
matrix.show()
time.sleep(0.5)
# show a string one character at a time
adafruit = 'Adafruit'
for char in adafruit:
matrix.fill(0)
matrix.text(char, 0, 0)
matrix.show()
time.sleep(1.0)
# scroll the last character off the display
for i in range(8):
matrix.scroll(-1, 0)
matrix.show()
time.sleep(0.5)
# scroll a string across the display
for pixel_position in range(len(adafruit) * 8):
matrix.fill(0)
matrix.text(adafruit, -pixel_position, 0)
matrix.show()
time.sleep(0.25)
|
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 80 81 82 83 84 85 86 | import time
import random
from board import TX, RX, A1
import busio
import digitalio
from adafruit_max7219 import bcddigits
mosi = TX
clk = RX
cs = digitalio.DigitalInOut(A1)
spi = busio.SPI(clk, MOSI=mosi)
leds = bcddigits.BCDDigits(spi, cs, nDigits=8)
while True:
# clear display and dim 0
leds.brightness(0)
leds.clear_all()
# place 8-digit number on display
value = 12345678
leds.show_str(0, '{:8}'.format(value))
leds.show()
# increase the brightness slowly
for i in range(16):
leds.brightness(i)
time.sleep(0.5)
leds.brightness(3)
# show "-HELP-90" on display
leds.show_str(6, '90') # show 90 starting at position 6
leds.set_digit(0, 10) # show - at position 0
leds.set_digit(1, 12) # show H at position 1
leds.set_digit(2, 11) # show E at position 2
leds.set_digit(3, 13) # show L at position 3
leds.set_digit(4, 14) # show P at position 4
leds.set_digit(5, 10) # show - at position 5
leds.show()
time.sleep(1.0)
leds.clear_all()
leds.brightness(5)
# set the two dots and two 4-digit numbers
leds.show_dot(2, 1)
leds.show_dot(6, 1)
leds.show_str(0, ' 72.5')
leds.show_str(4, '-10.8')
leds.show()
time.sleep(1.0)
leds.brightness(10)
leds.clear_all()
# show a 4 character numeric string
leds.show_str(0, ' 0')
leds.show()
time.sleep(1.0)
leds.clear_all()
# show 0->8
for digit in range(8):
leds.set_digit(digit, digit)
leds.show()
time.sleep(1.0)
# show random 8-digit numbers via show_str
for _ in range(10):
number = random.uniform(-1.0, 1.0)
number *= 10000.0
number_string = '{:9.3f}'.format(number)
leds.clear_all()
leds.show_str(0, number_string)
leds.show()
time.sleep(1.0)
# show the help string
leds.clear_all()
leds.show_help(2)
leds.show()
time.sleep(1.0)
|
adafruit_max7219.max7219
- MAX7219 LED Matrix/Digit Display Driver¶
CircuitPython library to support MAX7219 LED Matrix/Digit Display Driver. This library supports the use of the MAX7219-based display in CircuitPython, either an 8x8 matrix or a 8 digit 7-segment numeric display.
See Also¶
- matrices.Maxtrix8x8 is a class support an 8x8 led matrix display
- bcddigits.BCDDigits is a class that support the 8 digit 7-segment display
Beware that most CircuitPython compatible hardware are 3.3v logic level! Make sure that the input pin is 5v tolerant.
- Author(s): Michael McWethy
Implementation Notes¶
Hardware:
- Adafruit MAX7219CNG LED Matrix/Digit Display Driver - MAX7219 (Product ID: 453)
Software and Dependencies:
- Adafruit CircuitPython firmware for the ESP8622 and M0-based boards: https://github.com/adafruit/circuitpython/releases
- Adafruit’s Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
Notes: #. Datasheet: https://cdn-shop.adafruit.com/datasheets/MAX7219.pdf
-
class
adafruit_max7219.max7219.
MAX7219
(width, height, spi, cs, *, baudrate=8000000, polarity=0, phase=0)[source]¶ MAX2719 - driver for displays based on max719 chip_select
Parameters: - width (int) – the number of pixels wide
- height (int) – the number of pixels high
- spi (object) – an spi busio or spi bitbangio object
- chip_select (DigitalInOut) – digital in/out to use as chip select signal
- baudrate – for SPIDevice baudrate (default 8000000)
- polarity – for SPIDevice polarity (default 0)
- phase – for SPIDevice phase (default 0)
-
brightness
(value)[source]¶ Controls the brightness of the display.
Parameters: value (int) – 0->15 dimmest to brightest
-
fill
(bit_value)[source]¶ Fill the display buffer.
Parameters: bit_value (int) – value > 0 set the buffer bit, else clears the buffer bit
adafruit_max7219.matrices.Matrix8x8
¶
-
class
adafruit_max7219.matrices.
Matrix8x8
(spi, cs)[source]¶ Driver for a 8x8 LED matrix based on the MAX7219 chip.
Parameters: - spi (object) – an spi busio or spi bitbangio object
- cs (DigitalInOut) – digital in/out to use as chip select signal
adafruit_max7219.bcddigits.BCDDigits
¶
-
class
adafruit_max7219.bcddigits.
BCDDigits
(spi, cs, nDigits=1)[source]¶ Basic support for display on a 7-Segment BCD display controlled by a Max7219 chip using SPI.
Parameters: - spi (object) – an spi busio or spi bitbangio object
- cs (DigitalInOut) – digital in/out to use as chip select signal
- nDigits (int) – number of led 7-segment digits; default 1; max 8