2020年2月17日月曜日

Controlling MAX7219 8x8 Matrix LED with Raspberry Pi

昔々 Arduinoで試して死蔵してたMAX7219 と8x8 Matrix LEDを
Raspberry Piに繋げてみました。

3.3Vで動きましたね

配線
MAX7219 Pin Name Remarks RPi Pin RPi Function
1 VCC +5V Power      1 3.3V
2 GND Ground             6 GND
3 DIN Data In            19 GPIO 10 (MOSI)
4 CS Chip Select        24 GPIO 8 (SPI CE0)
5 CLK Clock             23 GPIO 11 (SPI CLK)

circuitPythonでの動作確認です。

# apt install python3-pip
# pip3 install --upgrade setuptools
# pip3 install RPI.GPIO
# pip3 install adafruit-blinka

# apt install python3-pil
# pip3 install adafruit-circuitpython-max7219

sample code get
# git clone https://github.com/adafruit/Adafruit_CircuitPython_max7219.git

[max7219_heike.py]

#!/usr/bin/env python3

import time
##from board import TX, RX, A1
from board import CE0
import board
import busio
import digitalio
from adafruit_max7219 import matrices
#-----------------------------
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont


def horizontal_scroll(image, padding=True):

        image_list = list()
        width = image.size[0]
        # Scroll into the blank image.
        if padding:
            for x in range(8):
                section = image.crop((0, 0, x, 8))
                display_section = create_blank_image()
                display_section.paste(section, (8 - x, 0, 8, 8))
                image_list.append(display_section)

        #Scroll across the input image.
        for x in range(8, width + 1):
            section = image.crop((x - 8, 0, x, 8))
            display_section = create_blank_image()
            display_section.paste(section, (0, 0, 8, 8))
            image_list.append(display_section)

        #Scroll out, leaving the blank image.
        if padding:
            for x in range(width - 7, width + 1):
                section = image.crop((x, 0, width, 8))
                display_section = create_blank_image()
                display_section.paste(section, (0, 0, 7 - (x - (width - 7)), 8))
                image_list.append(display_section)

        #Return the list of images created
        return image_list

def create_blank_image():
      return Image.new("RGB", (8, 8))


def image_gen(img):
        """Set buffer to value of Python Imaging Library image.  The image should
        be in 1 bit mode and a size equal to the display size."""
        imwidth, imheight = img.size
        # Grab all the pixels from the image, faster than getpixel.
        pixels = img.convert('1').load()
        # Iterate through the pixels
        for x in range(8):       # yes this double loop is slow,
            for y in range(8):  #  but these displays are small!
                matrix.pixel(x, y, pixels[(x, y)])
            matrix.show()

text = "ぎおんしょうじゃのかねのこえ しょぎょうむじょうのひびきあり さらそうじゅのはなのいろ じょう しゃひっすいのことわりをあらわす おごれるひともひさしからず ただはるのよのゆめのごとし たけきもの もついにはほろびぬ ひとえにかぜのまえのちりにおなじ"


text_len = len(text)
image = Image.new('1', (text_len*8, 8))
draw  = ImageDraw.Draw(image)
font  = ImageFont.truetype("/home/pi/font/misakifont/misaki_gothic.ttf", 8, encoding='unic')
draw.text((0,0), text, font=font, fill=255)

##mosi = TX
##clk = RX
##cs = digitalio.DigitalInOut(A1)
cs = digitalio.DigitalInOut(CE0)

##spi = busio.SPI(clk, MOSI=mosi)
spi = busio.SPI(board.SCLK, board.MOSI, board.MISO)

matrix = matrices.Matrix8x8(spi, cs)

matrix.fill(False)
#image_gen(matrix, image)

image_list = horizontal_scroll(image)

max_images = len(image_list)
while True:
    for i in range(0, max_images):
         image_gen(image_list[i])
#         time.sleep(0.5)



0 件のコメント: