2020年2月8日土曜日

Controlling 16x8 Matrix LED x2 (32x8) with Raspberry Pi










https://make-muda.net/2015/01/2414/

さんのホームページを参考にして、

HT16K33を使用した、8x8 matrix led
4個でのスクロールを試してみた


[sample script]

#!/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

# Referenced https://make-muda.net/2015/01/2414/
def horizontal_multi_scroll(matrix, image, total=1, number=0):
        padding=True
        image_list = list()
        width = image.size[0]

        for x in range(matrix.columns * number):
          display_section = create_blank_image(matrix)
          image_list.append(display_section)

        # Scroll into the blank image.
        if padding:
            for x in range(matrix.columns):
                section = image.crop((0, 0, x, matrix.rows))
                display_section = create_blank_image(matrix)
                display_section.paste(section, (matrix.columns - x, 0, matrix.columns, matrix.rows))
                image_list.append(display_section)

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

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

        for x in range(matrix.columns * (total - (number + 1))):
          display_section = create_blank_image(matrix)
          image_list.append(display_section)


        #Return the list of images created
        return image_list

def create_blank_image(matrix):
      return Image.new("RGB", (matrix.columns, matrix.rows))



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

# Create the matrix class.
matrixx=[]
i2c_address=(0x71, 0x70,)
for addr in i2c_address:
  matrixx.append(matrix.Matrix16x8(i2c, address=addr))

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_listx = []
max_addr=len(i2c_address)
for i in range(0, max_addr):
  image_listx.append( horizontal_multi_scroll(matrixx[0], new_image, max_addr, i))

max_images = len(image_listx[0])
while True:
    for i in range(0, max_images):
         for j, m in enumerate(matrixx, 0):
            m.image(image_listx[j][i])


i2c_address=(0x71, 0x70,)の部分を
i2c_address=(0x70,) とかにすると
16x8 で動きますね

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)

2019年12月23日月曜日

0.56 inch 4-Digit 7-Segment Display w/I2C(HT16K33) Backpack - Red on a Raspberry Pi

Raspberry Piに、HT16K33 I2C制御の 7 segをつけてみた。
(cheep¥339でした)
つなぎ方はネットに色々あるので省略(I2C)
VCCは3.3V接続です。


# i2cdetect -y 1

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- 3c -- -- --
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: 70 -- -- -- -- -- -- --

I2Cアドレスは、デフォルト0x70ですね
ジャンパで、0x71-0x77に変更可能なようです。





































































ライブラリは、Adafruit_Python_LED_Backpackを使わせて頂きました。


2019年12月21日土曜日

TM1637 7 segment display on a Raspberry Pi

Raspberry Piに、TM1637制御の 7 segをつけてみた。
(cheep¥200以下でした)
つなぎ方はネットに色々あるので省略(疑似I2Cってところ)
VCCは3.3V接続です。






























































ライブラリは、
https://raspberrytips.nl/tm1637-4-digit-led-display-raspberry-pi/
の方の
https://raspberrytips.nl/files/tm1637.py
のを拝借して、アルファベット表示も可能なように変更

[tm1637_ab.py]
# https://raspberrytips.nl/tm1637-4-digit-led-display-raspberry-pi/
# by shinshinshin

import sys
import os
import time
import RPi.GPIO as IO

IO.setwarnings(False)
IO.setmode(IO.BCM)

HexDigits = {
             "0":0x3f,
             "1":0x06,
             "2":0x5b,
             "3":0x4f,
             "4":0x66,
             "5":0x6d,
             "6":0x7d,
             "7":0x07,
             "8":0x7f,
             "9":0x6f,
             "A":0x77,
             "B":0x7c,
             "C":0x39,
             "D":0x5e,
             "E":0x79,
             "F":0x71,
             "G":0x3D,
             "H":0x74,
             "I":0x04,
             "J":0x0E,
             "K":0x70,
             "L":0x38,
             "M":0x54,
             "N":0x37,
             "O":0x5C,
             "P":0x73,
             "Q":0x67,
             "R":0x50,
             "S":0x65,
             "T":0x78,
             "U":0x3E,
             "V":0x1C,
             "W":0x52,
             "X":0x76,
             "Y":0x6E,
             "Z":0x49,
             " ":0x00,
             "-":0x40,
             "|":0x36,
             }


ADDR_AUTO = 0x40
ADDR_FIXED = 0x44
STARTADDR = 0xC0
BRIGHT_DARKEST = 0
BRIGHT_TYPICAL = 2
BRIGHT_HIGHEST = 7
OUTPUT = IO.OUT
INPUT = IO.IN
LOW = IO.LOW
HIGH = IO.HIGH

class TM1637:
        __doublePoint = False
        __Clkpin = 0
        __Datapin = 0
        __brightnes = BRIGHT_TYPICAL;
        __currentData = [0,0,0,0];

        def __init__( self, pinClock, pinData, brightnes ):
                self.__Clkpin = pinClock
                self.__Datapin = pinData
                self.__brightnes = brightnes;
                IO.setup(self.__Clkpin,OUTPUT)
                IO.setup(self.__Datapin,OUTPUT)
        # end  __init__

        def Clear(self):
                b = self.__brightnes;
                point = self.__doublePoint;
                self.__brightnes = 0;
                self.__doublePoint = False;
                data = [0x7F,0x7F,0x7F,0x7F];
                self.Show(data);
                self.__brightnes = b;                           # restore saved brightnes
                self.__doublePoint = point;
        # end  Clear

        def ShowInt(self, i):
                s = str(i)
                self.Clear()
                for i in range(0,len(s)):
                        self.Show1(i, int(s[i]))

        def Show( self, data ):
                for i in range(0,4):
                        self.__currentData[i] = data[i];

                self.start();
                self.writeByte(ADDR_AUTO);
                self.stop();
                self.start();
                self.writeByte(STARTADDR);
                for i in range(0,4):
                        self.writeByte(self.coding(data[i]));
                self.stop();
                self.start();
                self.writeByte(0x88 + self.__brightnes);
                self.stop();
        # end  Show

        def SetBrightnes(self, brightnes):              # brightnes 0...7
                if( brightnes > 7 ):
                        brightnes = 7;
                elif( brightnes < 0 ):
                        brightnes = 0;

                if( self.__brightnes != brightnes):
                        self.__brightnes = brightnes;
                        self.Show(self.__currentData);
                # end if
        # end  SetBrightnes

        def ShowDoublepoint(self, on):                  # shows or hides the doublepoint
                if( self.__doublePoint != on):
                        self.__doublePoint = on;
                        self.Show(self.__currentData);
                # end if
        # end  ShowDoublepoint

        def writeByte( self, data ):
                for i in range(0,8):
                        IO.output( self.__Clkpin, LOW)
                        if(data & 0x01):
                                IO.output( self.__Datapin, HIGH)
                        else:
                                IO.output( self.__Datapin, LOW)
                        data = data >> 1
                        IO.output( self.__Clkpin, HIGH)
                #endfor

                # wait for ACK
                IO.output( self.__Clkpin, LOW)
                IO.output( self.__Datapin, HIGH)
                IO.output( self.__Clkpin, HIGH)
                IO.setup(self.__Datapin, INPUT)

                while(IO.input(self.__Datapin)):
                        time.sleep(0.001)
                        if( IO.input(self.__Datapin)):
                                IO.setup(self.__Datapin, OUTPUT)
                                IO.output( self.__Datapin, LOW)
                                IO.setup(self.__Datapin, INPUT)
                        #endif
                # endwhile
                IO.setup(self.__Datapin, OUTPUT)
        # end writeByte

        def start(self):
                IO.output( self.__Clkpin, HIGH) # send start signal to TM1637
                IO.output( self.__Datapin, HIGH)
                IO.output( self.__Datapin, LOW)
                IO.output( self.__Clkpin, LOW)
        # end start

        def stop(self):
                IO.output( self.__Clkpin, LOW)
                IO.output( self.__Datapin, LOW)
                IO.output( self.__Clkpin, HIGH)
                IO.output( self.__Datapin, HIGH)
        # end stop

        def coding(self, data):
                if( self.__doublePoint ):
                        pointData = 0x80
                else:
                        pointData = 0;

                if(data == 0x7F):
                        data = 0
                else:
#
# by shinshinshin start
#                       data = HexDigits[data] + pointData;
                        if data in HexDigits:
                           data = HexDigits[data] + pointData;
                        else:
                           data = HexDigits[" "] + pointData;
# by shinshinshin end
                return data
        # end coding

# end class TM1637_ab

[tm1637_ab_time_temp.py]

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# https://raspberrytips.nl

import sys
import time
import re
import datetime
import RPi.GPIO as GPIO
import tm1637_ab

def get_temp():
#  DEVID="28-000003613615"
  DEVID="28-000000ffe755"
  for line in open('/sys/bus/w1/devices/' + DEVID + '/w1_slave', 'r'):
     m = re.findall(r't=(?P<temp>\d+)\s+', line)
     if m:
       return [int(m[0])/1000, (int(m[0])%1000/10)]
     else:
        m = ''

#CLK -> GPIO23 (Pin 16)
#Di0 -> GPIO24 (Pin 18)

##Display = tm1637.TM1637(23,24,tm1637.BRIGHT_TYPICAL)
Display = tm1637_ab.TM1637(16,12,tm1637_ab.BRIGHT_TYPICAL)

Display.Clear()
Display.SetBrightnes(1)

while(True):

#### current time

   now = datetime.datetime.now()
   hour = now.hour
   minute = now.minute
   second = now.second
   currenttime = [ str(hour / 10), str(hour % 10), str(minute / 10), str(minute % 10) ]

   Display.SetBrightnes(7)

   Display.Show(currenttime)
   Display.ShowDoublepoint(1)

   time.sleep(2)

#### temp

   temp=get_temp()
   currenttemp = [ str(int(temp[0] / 10)), str(temp[0] % 10), "C", "-"]

   Display.SetBrightnes(3)
   Display.Show(currenttemp)
   Display.ShowDoublepoint(0)

   time.sleep(1)






2019年12月1日日曜日

Raspberry Pi 3 Model A+ case w/Fan

Raspberry Pi 3 Model A+ 用のFan装着可能なケースってなかなか
ないんですよね。3A+が人気なさそうなので。

AliExpressのを調達してみました。Fanも付いてました。
china post経由でワンコイン