次のページを参考にしました。
Grove - LCD RGB Backlight
ベースシールドは次のものを使いました。
Stackable Shield for Raspberry Pi 2.0
プログラム
display_lcd.py
#! /usr/bin/python3
#
# display_lcd.py
#
# ------------------------------------------------------------------
import sys
import time
import lcd_lib
# ------------------------------------------------------------------
sys.stderr.write("*** start ***\n")
count = 0
while True:
text = 'count = ' + str(count)
lcd_lib.setText_norefresh(text)
time.sleep(1)
count += 1
#
sys.stderr.write("*** end ***\n")
# ------------------------------------------------------------------
lcd_lib.py
#
# lcd_lib.py
#
# ------------------------------------------------------------------
import time
import smbus
DISPLAY_RGB_ADDR = 0x62
DISPLAY_TEXT_ADDR = 0x3e
bus = smbus.SMBus(1)
# ------------------------------------------------------------------
def textCommand(cmd):
bus.write_byte_data(DISPLAY_TEXT_ADDR,0x80,cmd)
# ------------------------------------------------------------------
def setText_norefresh(text):
textCommand(0x02) # return home
time.sleep(.05)
textCommand(0x08 | 0x04) # display on, no cursor
textCommand(0x28) # 2 lines
time.sleep(.05)
count = 0
row = 0
while len(text) < 32: #clears the rest of the screen
text += ' '
for c in text:
if c == '\n' or count == 16:
count = 0
row += 1
if row == 2:
break
textCommand(0xc0)
if c == '\n':
continue
count += 1
bus.write_byte_data(DISPLAY_TEXT_ADDR,0x40,ord(c))
#
# ------------------------------------------------------------------
次のバージョンで確認しました。
$ python3 --version
Python 3.7.3