diff --git a/adafruit_max7219/max7219.py b/adafruit_max7219/max7219.py index ff30c32..9e5dea1 100644 --- a/adafruit_max7219/max7219.py +++ b/adafruit_max7219/max7219.py @@ -1,5 +1,6 @@ # SPDX-FileCopyrightText: 2016 Philip R. Moyer for Adafruit Industries # SPDX-FileCopyrightText: 2016 Radomir Dopieralski for Adafruit Industries +# SPDX-FileCopyrightText: 2021 bluejazzCHN for Adafruit Industries # # SPDX-License-Identifier: MIT @@ -24,7 +25,7 @@ -------------------- **Hardware:** -* Adafruit `MAX7219CNG LED Matrix/Digit Display Driver - +* `Adafruit MAX7219CNG LED Matrix/Digit Display Driver - MAX7219 `_ (Product ID: 453) **Software and Dependencies:** @@ -32,7 +33,8 @@ * 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 +* Adafruit's Bus Device library: + https://github.com/adafruit/Adafruit_CircuitPython_BusDevice **Notes:** #. Datasheet: https://cdn-shop.adafruit.com/datasheets/MAX7219.pdf @@ -54,7 +56,6 @@ class MAX7219: """ MAX2719 - driver for displays based on max719 chip_select - :param int width: the number of pixels wide :param int height: the number of pixels high :param object spi: an spi busio or spi bitbangio object @@ -89,24 +90,24 @@ def init_display(self): def brightness(self, value): """ Controls the brightness of the display. - :param int value: 0->15 dimmest to brightest """ if not 0 <= value <= 15: raise ValueError("Brightness out of range") self.write_cmd(_INTENSITY, value) - def show(self): + def show(self, number=1, t_num=1): """ Updates the display. + :param int number: which one is in the cascaded matrixs, default is 1. + :param int t_num: total number of cascaded Matrixs,default is 1. """ for ypos in range(8): - self.write_cmd(_DIGIT0 + ypos, self._buffer[ypos]) + self.write_cmd(_DIGIT0 + ypos, self._buffer[ypos], number, t_num) def fill(self, bit_value): """ Fill the display buffer. - :param int bit_value: value > 0 set the buffer bit, else clears the buffer bit """ self.framebuf.fill(bit_value) @@ -114,7 +115,6 @@ def fill(self, bit_value): def pixel(self, xpos, ypos, bit_value=None): """ Set one buffer bit - :param xpos: x position to set bit :param ypos: y position to set bit :param int bit_value: value > 0 sets the buffer bit, else clears the buffer bit @@ -126,10 +126,29 @@ def scroll(self, delta_x, delta_y): """Srcolls the display using delta_x,delta_y.""" self.framebuf.scroll(delta_x, delta_y) - def write_cmd(self, cmd, data): + def write_cmd(self, cmd, data, number=1, t_num=1): # pylint: disable=no-member - """Writes a command to spi device.""" + """Writes a command to spi device. + :param int number: whichi one is in the cascaded matrixs, default is 1. + :param int t_num: total number of cascaded Matrixs,default is 1. + """ # print('cmd {} data {}'.format(cmd,data)) self._chip_select.value = False + + # send Noop to ones behind number Matrix with self._spi_device as my_spi_device: + for i in range(number, t_num): + my_spi_device.write(bytearray([0, 0])) + my_spi_device.write(bytearray([cmd, data])) + + # send Noop to all before number, if you want to know why, please ref to MAX7219.pdf. + for _ in range(0, number - 1): + my_spi_device.write(bytearray([0, 0])) + + def rotation(self, direction): + """ + Set display direction + :param direction:set int to change display direction, value 0 (default), 1, 2, 3 + """ + self.framebuf.rotation = direction diff --git a/examples/max7219_cascade.py b/examples/max7219_cascade.py new file mode 100644 index 0000000..23f5203 --- /dev/null +++ b/examples/max7219_cascade.py @@ -0,0 +1,42 @@ +# SPDX-FileCopyrightText: 2021 bluejazzCHN for Adafruit Industries +# SPDX-License-Identifier: MIT + +import time +import random +from board import TX, RX, A2 +import busio +import digitalio +from adafruit_max7219 import matrices + +clk = RX +din = TX +cs = digitalio.DigitalInOut(A2) + +spi = busio.SPI(clk, MOSI=din) +display = matrices.Matrix8x8(spi, cs) + +while True: + display.clear_all() + s = "Hello, World!" + + # Demo One: cascaded matrix with two max7219 + # show random char in string s on the the first of cascaded matrixs. + # show scroll string s on the second of cascaded matrixs. + display.text(s[random.randint(0, len(s) - 1)], 0, 0) + display.show(1, 2) + + for c in range(len(s) * 8): + display.fill(0) + display.text(s, -c, 0) + display.show(2, 2) + time.sleep(0.25) + + # Demo two: scroll string s on the cascaded matrix with two max7219 + for c in range(len(s) * 8): + display.fill(0) + display.text(s, -c, 0) + display.show(1, 2) + display.fill(0) + display.text(s, -c + 8, 0) + display.show(2, 2) + time.sleep(0.25)