2020年1月27日月曜日

Controlling 16x8 Matrix LED with Raspberry Pi






































Raspberry Piに8x8 Matrix LED 2個を繋げてみた


制御chip HT16K33





















matrix 8x8 led  788ASY  *2
(昔 aitendoで入手した奴かな?!)
ブレッドボードに横並びで16x8に見えるように配置
(pinは上下8pinづつになりますな)

配線

LED pin   HT16K33
16        C7    (左led, 右led 共通)
15        C6
11        C5
6         C4
10        C3
4         C2
3         C1
13        C0

5         A0, A8  (左led, 右led)
2         A1, A9
7         A2, A10
1         A3, A11
12        A4, A12
8         A5, A13
14        A6, A14
9         A7, A15


試しに、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-ht16k33


ValueError: No I2C device at address: 70  となる
/boot/config.txt でi2c bus 追加してた場合

# 4 i2c add
### dtoverlay=i2c-gpio,i2c_gpio_sda=0,i2c_gpio_scl=1,i2c_gpio_delay_us=2,bus=3
で、OK!!(コメント化)
busの指定が無いのが問題だけどね。CircuitPythonだからね。


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


動作確認 code
[matrix16x8_heike_scroll.py]

#!/usr/bin/env python3

import time
import board
import busio

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

from adafruit_ht16k33 import matrix

def horizontal_scroll(image, padding=True):

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

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

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

        #Return the list of images created
        return image_list

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

# Create the I2C interface.
i2c = busio.I2C(board.SCL, board.SDA)

# Create the matrix class.
matrix = matrix.Matrix16x8(i2c, address=0x70)

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,1), text, font=font, fill=255)
new_image = image.transpose(Image.FLIP_TOP_BOTTOM)

image_list = horizontal_scroll(new_image)

max_images = len(image_list)
while True:
    for i in range(0, max_images):
         matrix.image(image_list[i])





2020年1月2日木曜日

enable I2C for CentOS on Raspberry Pi

i2c 7segを繋ごうと思ったら、CentOSが入ってた。
# uname -a
Linux rpi2 4.1.11-v7+ #822 SMP PREEMPT Fri Oct 23 16:22:18 BST 2015 armv7l armv7l armv7l GNU/Linux

# cat /etc/centos-release
CentOS Linux release 7.7.1908 (AltArch)

i2cの設定がされてなかった。。。
以下設定内容
/boot/config.txt
dtparam=i2c1=on
dtparam=i2c_arm=on

/etc/modules-load.d/i2c.conf
i2c-bcm2708
i2c-dev

# yum install -y i2c-tools

で、見えた
# i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- UU -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: 70 -- -- -- -- -- -- --

python script 動いた

# cat ./ht16k33_7seg_cpu-temp.py
#!/usr/bin/env python

import time

from Adafruit_LED_Backpack import SevenSegment

def get_cpu_temp():
    with open("/sys/class/thermal/thermal_zone0/temp", "r") as f:
        t=f.read()
    return int(t)/1000

segment = SevenSegment.SevenSegment(address=0x70)

segment.begin()

print "Press CTRL+Z to exit"

while(True):
  cpu_temp=get_cpu_temp()
#  print cpu_temp
  segment.clear()
  segment.set_digit(0, int(cpu_temp / 10))
  segment.set_digit(1, cpu_temp % 10, decimal=True)
  segment.set_digit(2, 'C')
  segment.set_digit(3, '-')
  segment.set_colon(0)

  segment.write_display()

  time.sleep(0.25)