Skip to content

Add rotation function to max7219.py #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -124,3 +125,17 @@ Documentation
=============

For information on building library documentation, please check out `this guide <https://learn.adafruit.com/creating-and-sharing-a-circuitpython-library/sharing-our-docs-on-readthedocs#sphinx-5-1>`_.


Additons
=============

1. Add rotation func

.. code-block:: python

matrix = matrices.Matrix8x8(spi, cs)
matrix.rotation(2)



24 changes: 21 additions & 3 deletions adafruit_max7219/matrices.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"""
from micropython import const
from adafruit_max7219 import max7219
import time

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MAX7219.git"
Expand All @@ -45,8 +46,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 (
Expand All @@ -56,7 +57,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()
Expand All @@ -77,3 +78,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)
39 changes: 38 additions & 1 deletion adafruit_max7219/max7219.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
_DIGIT0 = const(1)
_INTENSITY = const(10)

MAX7219_REG_NOOP = 0x0



class MAX7219:
"""
Expand All @@ -81,7 +84,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
Expand All @@ -96,6 +99,8 @@ def __init__(

self.width = width
self.height = height
self.num = num


self.init_display()

Expand Down Expand Up @@ -149,3 +154,35 @@ def write_cmd(self, cmd, data):
self._chip_select.value = False
with self._spi_device as my_spi_device:
my_spi_device.write(bytearray([cmd, data]))

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


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)))
16 changes: 16 additions & 0 deletions examples/max7219_disaplay_string_multi_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from adafruit_max7219 import matrices
import board
import busio
import digitalio

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)