BeagleBoneからI2C液晶を制御するライブラリをpythonで作りました。
Raspberry PiでI2C液晶を制御するコードを参考にしています。
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
秋月電子で販売されている
I2C接続小型LCDモジュール 8x2行
用のスクリプトです。
ストロベリーリナックスで販売されている
I2C低電圧キャラクタ液晶モジュール(16x2行)
も動かせると思います。
"""
import smbus # sudo apt-get install python-smbus
import time
class i2cLCD:
i2c = smbus.SMBus(2)
addr = 0x3e
lineCount=2
charCount=8
cursorEnable=0
blinkEnable=0
def _writeControlByte(self,val):
self.i2c.write_byte_data(self.addr,0,val)
def _writeByte(self,val):
self.i2c.write_byte_data(self.addr,0x40,val)
def _IS0(self):# Instruction Set 0
self._writeControlByte(0x38)# function set(IS=0)
def _IS1(self):#Instruction set 1
self._writeControlByte(0x39)# function set(IS=1)
def initialize(self):
self._IS0()
self._IS1()
self._writeControlByte(0x14)# internal osc
self._writeControlByte(0x70)#contrast
self._writeControlByte(0x56)#icon contrast
self._writeControlByte(0x6c)# follower control
time.sleep(0.2)
self.clear()
def clear(self):
self._writeControlByte(0x0F) # Display,cursor on
self._writeControlByte(0x01) # Clear Display
self._writeControlByte(0x06) # Entry Mode to normal
time.sleep(0.2)
def setCursor(self,cursorEnable,blinkEnable):
c=0x0
b=0x0
if(cursorEnable):c=0x02
if(blinkEnable):b=0x01
mode=0x0C|c|b
self._writeControlByte(mode)
self._writeControlByte(0x06)
def puts(self, msg):
for c in msg:
self._writeByte(ord(c))
def moveTo(self, line, col):
if(line<0): line=0
if(col<0): col=0
if(line>=self.lineCount): line=self.lineCount-1
if(col>=self.charCount): col=self.charCount-1
line=line*0x40
self._writeControlByte(0x80 | line | col)
def scroll(self,msg):
self.clear()
self.moveTo(0,0)
self.puts(msg)
sz=len(msg)
self._IS0()
time.sleep(1)
for i in range(sz):
self._writeControlByte(0x18)# Display left shift
time.sleep(0.5)
for i in range(sz):
self._writeControlByte(0x1C)#Display right shift
self.clear()
def doubleHeightScroll(self,msg):
self.clear()
self.moveTo(0,0)
self._writeControlByte(0x34)#double hight mode
self.puts(msg)
self.setCursor(0,0)
sz=len(msg)
time.sleep(1)
for i in range(sz):
self._writeControlByte(0x18)# Display left shift
time.sleep(0.5)
for i in range(sz):
self._writeControlByte(0x1C)#Display right shift
self.clear()
self.setCursor(0,0)
def setCGRam(self,cgAddr,values):
#0x40が SET CGRAM Address
#6ビットのうち上位3ビットがCGRAMのアドレス(0-5まで)
#下位3ビットがCGの上から何行目かを表します。
self._IS0()
if(cgAddr>5):
cgAddr=5
print('CG addr must 0 to 5 ')
lcd._writeControlByte(0x40|cgAddr<<3)#set CGAddr
for val in values:
lcd._writeByte(val)
binval=format(val,'08b')
binval=binval.replace('0',' ')
binval=binval.replace('1','#')
#print binval
if __name__ == "__main__":
lcd = i2cLCD()
lcd.initialize()
lcd._IS0()
lcd.doubleHeightScroll('Welcome to BeagleBone!')
Ubuntu13.04のKernelは3.8なので仮想CAPEを登録する作業が必要です。
sudo -s
cd /sys/devices/bone_capemgr.7 (数字は環境によって異なります)
echo BB-I2C1 > slots
dmesg | grep capemgr
データシートを見ると、縦2文字分を使用して1文字を表示するDouble Heightモードがあったので
使ってみました。
また、カスタムアイコンを6つまで作れるので、4文字または6文字分を使って漢字を表示できるかな?と思い実験をしましたが、文字間のタテヨコの空白部分がどうしても気になってしまう形になってしまい、無理があると判断しました。漢字だと隙間ナシで16x16は無いとダメみたいですね。
もうちょっと高解像度の液晶を探そうかな。