diff --git a/README.rst b/README.rst index aaa33a7..383b1cb 100644 --- a/README.rst +++ b/README.rst @@ -73,6 +73,7 @@ adafruit_max7219.Matrix8x8 Example spi = busio.SPI(clk, MOSI=din) display = matrices.Matrix8x8(spi, cs) + display.rotation(1) #change display direction while True: display.brightness(3) @@ -124,3 +125,14 @@ Documentation ============= For information on building library documentation, please check out `this guide `_. + + +Additons +============= + +1. Add rotation func + +.. code-block:: python + + matrix = matrices.Matrix8x8(spi, cs) + matrix.rotation(2) diff --git a/adafruit_max7219/matrices.py b/adafruit_max7219/matrices.py index 955e050..f32c127 100644 --- a/adafruit_max7219/matrices.py +++ b/adafruit_max7219/matrices.py @@ -6,9 +6,11 @@ `adafruit_max7219.matrices.Matrix8x8` ==================================================== """ +import time from micropython import const from adafruit_max7219 import max7219 + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MAX7219.git" @@ -26,8 +28,8 @@ class Matrix8x8(max7219.MAX7219): :param ~digitalio.DigitalInOut cs: digital in/out to use as chip select signal """ - def __init__(self, spi, cs): - super().__init__(8, 8, spi, cs) + def __init__(self, spi, cs, num=1): + super().__init__(8, 8, spi, cs, num=num) def init_display(self): for cmd, data in ( @@ -37,7 +39,7 @@ def init_display(self): (_DECODEMODE, 0), (_SHUTDOWN, 1), ): - self.write_cmd(cmd, data) + self._write([cmd, data] * self.num) self.fill(0) self.show() @@ -58,3 +60,20 @@ def clear_all(self): Clears all matrix leds. """ self.fill(0) + + def display_str(self, data, delay=1): + """ + Display string on led matrix by matrix length + + :param str: string that can be of any length. + :param delay: transfer time from one screen to another screen. default value is 1s + + """ + i = -1 + for char in data: + i += 1 + self.fill(0) + self.text(char, 1, 0) + self.show_char_position(i % self.num) + if i % self.num == self.num - 1: + time.sleep(delay) diff --git a/adafruit_max7219/max7219.py b/adafruit_max7219/max7219.py index b0458f6..a665ed7 100644 --- a/adafruit_max7219/max7219.py +++ b/adafruit_max7219/max7219.py @@ -52,6 +52,8 @@ _DIGIT0 = const(1) _INTENSITY = const(10) +MAX7219_REG_NOOP = 0x0 + class MAX7219: """ @@ -66,7 +68,7 @@ class MAX7219: """ def __init__( - self, width, height, spi, cs, *, baudrate=8000000, polarity=0, phase=0 + self, width, height, spi, cs, *, baudrate=8000000, polarity=0, phase=0, num=1 ): self._chip_select = cs @@ -81,6 +83,7 @@ def __init__( self.width = width self.height = height + self.num = num self.init_display() @@ -90,6 +93,7 @@ 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: @@ -108,6 +112,7 @@ def show(self, number=1, t_num=1): 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) @@ -115,6 +120,7 @@ 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 @@ -153,3 +159,29 @@ def rotation(self, direction): :param direction:set int to change display direction, value 0 (default), 1, 2, 3 """ self.framebuf.rotation = direction + + def _write(self, data): + """ + Send the bytes (which should comprise of alternating command, data values) + over the SPI device. + + :param data: command collections + """ + + self._chip_select.value = False + with self._spi_device as my_spi_device: + my_spi_device.write(bytes(data)) + self._chip_select.value = True + + def show_char_position(self, position=0): + """ + write data to the position that is one of multi led matrix + + :param position: the position of matrix, value begin 0. + + """ + for ypos in range(8): + self._write( + [_DIGIT0 + ypos, self._buffer[ypos]] + + ([MAX7219_REG_NOOP, 0] * (position)) + ) diff --git a/examples/max7219_disaplay_string_multi_matrix.py b/examples/max7219_disaplay_string_multi_matrix.py new file mode 100644 index 0000000..930b49f --- /dev/null +++ b/examples/max7219_disaplay_string_multi_matrix.py @@ -0,0 +1,20 @@ +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-License-Identifier: MIT + + +import board +import busio +import digitalio +from adafruit_max7219 import matrices + +clk = board.IO4 +din = board.IO2 +cs = digitalio.DigitalInOut(board.IO3) + +spi = busio.SPI(clk, MOSI=din) +display = matrices.Matrix8x8(spi, cs, 4) +display.framebuf.rotation = 1 # rotate screen + +display.clear_all() +while True: + display.display_str("abc7568123456789asdfghj", 2)