Last active
August 28, 2017 15:34
-
-
Save t114/157cab837411323153add1b125249ca5 to your computer and use it in GitHub Desktop.
入力されたテキストをTG12864B-02WWBVに表示させる
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import wiringpi as w | |
import sys | |
import time | |
from PIL import Image, ImageDraw, ImageFont | |
FONT_SIZE = 14 | |
MARGIN_L = 1 | |
MARGIN_TOP = 1 | |
LCD_WIDTH = 128 | |
LCD_HEIGHT = 64 | |
#LCD表示用ビットマップイメージ作成 | |
def make_textbmp(text, filename="display.bmp"): | |
filename = filename if filename.lower().endswith(".bmp") else filename + ".bmp" | |
# 入力された文字列を液晶に表示できる幅で配列にする | |
lines = line_feed(text) | |
# フォントの指定。引数は順に「フォントのパス」「フォントのサイズ」「エンコード」 | |
# メソッド名からも分かるようにTure Typeのフォントを指定する | |
font = ImageFont.truetype('/home/pi/kochi-gothic-subst.ttf', FONT_SIZE, encoding='unic') | |
image = Image.new('1', (LCD_WIDTH, LCD_HEIGHT), '#ffffff') | |
for idx, line in enumerate(lines): | |
draw = ImageDraw.Draw(image) | |
# 日本語の文字を入れてみる | |
# 引数は順に「(文字列の左上のx座標, 文字列の左上のy座標)」「フォントの指定」「文字色」 | |
draw.text((MARGIN_L, MARGIN_TOP + (FONT_SIZE * idx) ), line, font = font, fill='#000000') | |
# 液晶のピンがある側を上にしたい場合ROTATE_90、逆にしたい場合ROTATE_270を指定する | |
image = image.transpose(Image.ROTATE_90) | |
image.save(filename, 'BMP') | |
return filename | |
def is_ascii(char): | |
return char and ord(char) < 128 | |
def line_feed(string): | |
lines = [] | |
length = 0 | |
new_line = "" | |
for char in string: | |
# 全角は半角の2文字分として換算 | |
length += 1 if is_ascii(char) else 2 | |
if ((length * FONT_SIZE/2) + MARGIN_L) <= LCD_WIDTH: | |
new_line += char | |
else: | |
lines.append(new_line) | |
new_line = char | |
length = 1 | |
if ((len(lines) + 1) * FONT_SIZE) > LCD_HEIGHT: | |
break | |
else: | |
lines.append(new_line) | |
return lines | |
# データ線以外のポート定義 | |
# データ線はw.digitalWriteByteコマンドを使いたいので | |
# GPIOの0~7番に接続する。 | |
# ※ R/WはL時書き込みモードなので接続しない。 | |
# もし万一読み込みモードに設定してしまった場合 | |
# パイが焼けてしまうので絶対に繋がない。 | |
LCD_RS = 13 # データ・コマンド切り替え信号 | |
LCD_EN = 12 # イネーブル信号 | |
LCD_C1 = 10 # コントローラ切り替え信号(右半分) | |
LCD_C2 = 11 # コントローラ切り替え信号(左半分) | |
LCD_RE = 14 # リセット信号 | |
# BMPデータ転送関数 | |
# 128x8バイトの配列を「末尾 ex: array[127][7]」から転送する | |
def lcd_bmp(BMPData): | |
lcd_set(1,0,0x3E);lcd_set(2,0,0x3E); | |
# 転送 BMPファイルは左下から記録されているので後ろから読み込む | |
for d in range(0, 128): | |
for n in range(0, 8): | |
lcd_set(1+(d>=64),0,0x40+(d%64));lcd_set(1+(d>=64),0,0xB8+n);lcd_set(1+(d>=64),1,BMPData[127-d][7-n]); | |
lcd_set(1,0,0x3F);lcd_set(2,0,0x3F); | |
# LCD制御命令 | |
# CntSel=コントローラセレクト | |
# 1 =CS1 2 =CS2 | |
# rs=データ・コマンド切り替え | |
# 0 =コマンド 1 =データ | |
# Data = 8bit制御データ | |
def lcd_set(CntSel, rs, Data): | |
chip_sel = [LCD_C1, LCD_C2] | |
w.digitalWrite(chip_sel[0],0);w.digitalWrite(chip_sel[1],0); | |
w.digitalWriteByte(0); | |
w.digitalWrite(LCD_EN,0);#time.sleep(0.00045); | |
w.digitalWrite(LCD_RS,rs);#time.sleep(0.002); | |
w.digitalWrite(chip_sel[CntSel-1],1); | |
w.digitalWrite(LCD_EN,1);#time.sleep(0.00045); | |
int_data = Data if isinstance(Data, int) else ord(Data) | |
w.digitalWriteByte(int_data); | |
#time.sleep(0.002); | |
w.digitalWrite(LCD_EN,0); | |
w.digitalWrite(LCD_RS,0); | |
w.digitalWriteByte(0); | |
w.digitalWrite(chip_sel[0],0);w.digitalWrite(chip_sel[1],0); | |
def lcd_display(display_filename): | |
if w.wiringPiSetup() < 0: | |
print("GPIO Err...") | |
#データbit用ポートを出力に設定 | |
for i in range(0, 8): | |
w.pinMode(i,w.OUTPUT) | |
#time.sleep(0.002) | |
# データ以外のポートを出力に設定 | |
w.pinMode(LCD_RS,w.OUTPUT) | |
w.pinMode(LCD_EN,w.OUTPUT) | |
w.pinMode(LCD_C1,w.OUTPUT) | |
w.pinMode(LCD_C2,w.OUTPUT) | |
w.pinMode(LCD_RE,w.OUTPUT) | |
w.digitalWrite(LCD_RE,1) | |
#time.sleep(0.05) | |
w.digitalWrite(LCD_RE,0) | |
#time.sleep(0.05) | |
w.digitalWrite(LCD_RE,1) | |
# 液晶初期化 | |
lcd_set(1,0,0xC0);lcd_set(1,0,0x3F); | |
lcd_set(2,0,0xC0);lcd_set(2,0,0x3F); | |
# モノクロ2値 1bit 64x128サイズのBMPファイル読み込み | |
# 液晶の入力端子がある方を「下」にした場合 | |
# Windows標準のペイントで「右90度回転」させればOK。 | |
# 入力端子を「上」にした場合は「左90度回転」 | |
BMPData = [] | |
f = open(display_filename, "rb") | |
# 読み込み ファイル形式が(1bit BMP 64x128px)だと前提して | |
# 色々と端折ってデータ読み込み。↑の形式じゃないとエラー | |
buf = f.read() | |
for i in range(0, 128): | |
BMPData.append(buf[62+(i*8):62+(i*8)+8]) | |
f.close() | |
# 読み込んだデータを転送 | |
lcd_bmp(BMPData); | |
if __name__ == '__main__': | |
args = sys.argv | |
if len(args) != 2: | |
sys.exit | |
#display_filename = make_textbmp(args[1]) #Python3.5系 | |
display_filename = make_textbmp(args[1].decode("utf-8")) #Python2.7系 | |
lcd_display(display_filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/t114/c6943200803a920c7295ce94c8219e98 を
モノクロビットマップイメージでなく、テキストを表示するように改造
'/home/pi/kochi-gothic-subst.ttf'を任意のフォントファイルに変更後、
python lcd_display.py 表示させたい文字
で液晶に文字が表示される