[PYTHON] Use the graphic LCD as a character LCD that can also display Chinese characters on the Rasberry Pi

If you want to display characters on Raspberry Pi, the easiest way is to display them on the character LCD. However, in the case of character LCD, visibility is poor because the size of characters and the characters that can be used are limited (generally only half-width alphanumeric characters and half-width characters). (Even if it is displayed in half-width kana, it cannot be read at once ...) Therefore, the purpose of this time is to use the graphic liquid crystal as the character liquid crystal.

Execution result

TL;DR -Prepare the LCD according to Control of graphic LCD with Raspberry Pi 2 (1) Preparation. --Prepare a ttf format font file -[** here **] clone the source (https://gist.github.com/t114/157cab837411323153add1b125249ca5) --How to use'/home/pi/kochi-gothic-subst.ttf' After changing to any font file prepared python lcd_display.py" Characters you want to display "

environment

Graphic LCD preparation

At the time of purchase, there are no pins attached to the LCD board, so you can actually use it by soldering the pins or connecting it to the Raspberry Pi. Please refer to the following page of sakura87.net for a very detailed explanation. I was very helpful. Control of graphic LCD with Raspberry Pi 2 (1) Preparation

Image display

When the LCD is ready, let's output a monochrome bitmap image to the LCD. The procedure is basically explained in great detail on the following page of sakura87.net, so please refer to that. I was very helpful. (2nd time) Control of graphic LCD with Raspberry Pi 2 (2) Control

Translate glcd.c to python

I personally like Python, so I translated it into Python.

Because it is long, it is saved in ** here Gist **.

If there is no wiring pi, install it with the following command.

sudo pip install wiringpi

With the work up to this point, the monochrome bitmap image specified by the following command can be displayed on the TG12864B-02WWBV.

python glcd.py test.bmp

Bitmap image creation

Now that we've been able to display a monochrome image, it's time to create a monochrome bitmap image from the text. It is almost the same as the reference source below, and the difference is that the image size, monochrome bitmap specification, and rotation by 90 degrees before saving. I tried using Japanese with PIL

make_bmp.py


#!/usr/bin/env python
# -*- encoding:utf8 -*-
from PIL import Image, ImageDraw, ImageFont
#Font specification. The arguments are "font path", "font size", and "encoding" in order.
#Specify the True Type font as you can see from the method name
font = ImageFont.truetype('/home/pi/kochi-gothic-subst.ttf', 16, encoding='unic')
image = Image.new('1', (128, 64), '#ffffff')
draw = ImageDraw.Draw(image)
#Try to insert Japanese characters
#The arguments are in order "(Top left x coordinate of the string,Upper left y coordinate of the string)"Specify font" "Text color"
draw.text((1, 1), u'Hello', font = font, fill='#000000')
#If you want the side with the LCD pin to face up ROTATE_90
image = image.transpose(Image.ROTATE_90)
image.save('hello.bmp', 'BMP')

Based on the above

The result of adding / modifying the text line feed processing + α is saved in ** here Gist **.

Command to display at the beginning


python lcd_display.py "August 28, 2017 The weather is sunny and the temperature is 30 ℃"

You can change the size of the displayed characters by changing'FONT_SIZE = xx' at the beginning of the source. The font type can be specified in the place of'/home/pi/kochi-gothic-subst.ttf'.

reference

-Control of graphic LCD with Raspberry Pi 2 (1) Preparation -Control of graphic LCD with Raspberry Pi 2 (2) Control -I tried using Japanese with PIL

Afterword

Since TG12864B-02WWBV is connected in parallel, GPIO is blocked a lot, so I want an inexpensive liquid crystal that can be used with serial connection. It's usually expensive or small, and you can't find a reasonable one. Well, at the moment, I don't have any specific intention to do anything with this LCD, so I hope. .. .. (It is inconvenient that Qiita cannot embed Gist)

Recommended Posts

Use the graphic LCD as a character LCD that can also display Chinese characters on the Rasberry Pi
[Don't refer to 04.02.17] Display the temperature sensor on a real-time graph with rasberry pi 3.
[Python] I made a function that can also use regular expressions that replace character strings all at once.
Use the Grove sensor on the Raspberry Pi
Find a building on Google Earth that is the same height as Shingodzilla
[Hackathon] About making a tool that can be CD on Raspberry Pi [Convenient tool]