-
Notifications
You must be signed in to change notification settings - Fork 0
/
HD44780.py
281 lines (235 loc) · 9.31 KB
/
HD44780.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/python3
# -*- coding: utf-8 -*-
###########################################################################
#Filename :HD44780.py
#Description :i2c lcd library
#Author :Takao Akaki
#Website :https://raspberrypi.mongonta.com
#Update :2018/9/9
############################################################################
import smbus
import time
import configparser
import os
import mojimoji
# Define some device constants
LCD_CHR = 1 # Mode - Sending data
LCD_CMD = 0 # Mode - Sending command
LCD_LINE = [0x80, 0xC0, 0x94, 0xD4]
LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line
ENABLE = 0b00000100 # Enable bit
# Timing constants
E_PULSE = 0.0005
E_DELAY = 0.0005
#Open I2C interface
#bus = smbus.SMBus(0) # Rev 1 Pi uses 0
bus = smbus.SMBus(1) # Rev 2 Pi uses 1
class HD44780(object):
def __init__(self, configfile):
"""Initialize the LCD.
Parameters
----------
configfile : str
lcd config file name
Create config file under the conf folder
"""
dirname = os.path.dirname( os.path.abspath( __file__ ) )
self._configfile = dirname + '/conf/' + configfile
# When configfile doesn't exist, print error
self.loadconfig()
# shiftcontroll's variables initialize
self._shift = True # True:left False:right
self._shiftlen = 0
self._length = [0, 0, 0, 0]
def init(self):
"""Initialize LCD
"""
# Initialise display
self.lcd_byte(0x33,LCD_CMD) # 110011 Initialise
self.lcd_byte(0x32,LCD_CMD) # 110010 Initialise
self.lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
self.lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
self.lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
self.lcd_byte(0x01,LCD_CMD) # 000001 Clear display
time.sleep(E_DELAY)
def lcd_byte(self, bits, mode):
"""Send byte to LCD
Parameters
----------
bits : int
the data
mode : int
0 is data, 1 is command
"""
bits_high = mode | (bits & 0xF0) | self._backlight
bits_low = mode | ((bits<<4) & 0xF0) | self._backlight
# High bits
bus.write_byte(self._i2caddr, bits_high)
self.lcd_toggle_enable(bits_high)
# Low bits
bus.write_byte(self._i2caddr, bits_low)
self.lcd_toggle_enable(bits_low)
def lcd_toggle_enable(self,bits):
# Toggle enable
time.sleep(E_DELAY)
bus.write_byte(self._i2caddr, (bits | ENABLE))
time.sleep(E_PULSE)
bus.write_byte(self._i2caddr, (bits & ~ENABLE))
time.sleep(E_DELAY)
def lcd_string(self, message, line):
"""Send string to LCD
Parameters
----------
message : str
message data
line : int
LCD address for the line
"""
# Send string to display
# message = message.ljust(LCD_WIDTH," ")
self.lcd_byte(line, LCD_CMD)
for i in range(len(message)):
self.lcd_byte(ord(message[i]),LCD_CHR)
def set_lcd_i2caddress(self, i2caddr):
self._i2caddr(i2caddr)
def lshift(self):
""" Shift Display to the left by 1 char """
self.lcd_byte(0x18, LCD_CMD)
def rshift(self):
""" Shift Display to the right by 1 char """
self.lcd_byte(0x1C, LCD_CMD)
def message(self, message, lineno, readconfig=True):
"""Display characters on the LCD
Parameters
----------
message : str
String to display
lineno : int
LineNo to display
readconfig : bool
Read config file before display
Use this when you want to control the backlight
"""
if readconfig:
self.loadconfig()
if self._kanamode:
# include japanese kana characters
message = mojimoji.zen_to_han(message,\
kana=True,\
digit=True,\
ascii=True)
self._length[lineno - 1] = len(message)
message = mojimoji.han_to_zen(message,\
kana=True,\
digit=False,\
ascii=False)
self.lcd_string_kana(message, LCD_LINE[lineno - 1])
else:
# ascii only
self._length[lineno - 1] = len(message)
self.lcd_string(message, LCD_LINE[lineno - 1])
if (self._shiftmode != 0) and \
((max(self._length) - self._width) > 0) and \
(self._length.index(max(self._length)) == (lineno - 1)):
self.shiftcontroll(max(self._length))
def setbacklight(self, backlight=True):
"""Set value of backlight
Parameters
----------
backlight : bool
True is backlight on
False is backlight off
"""
if backlight:
self._backlight = 0x08
else:
self._backlight = 0x00
self.lcd_byte(0x00 ,LCD_CMD)
def shiftcontroll(self, messagelength):
"""Shift message when display message
Parameters
----------
messagelength : int
message length
"""
if self._shiftmode == 1:
self.lshift()
elif self._shiftmode == 2:
self.rshift()
elif self._shiftmode == 3:
excesslen = messagelength - self._width
if excesslen > 0:
if ((excesslen - self._shiftlen) > 0) and self._shift:
self.lshift()
self._shiftlen += 1
if self._shiftlen == excesslen:
self._shift = False
self._shiftlen = 0
else:
self.rshift()
self._shiftlen += 1
if self._shiftlen == excesslen:
self._shift = True
self._shiftlen = 0
def loadconfig(self):
"""Load config file"""
config = configparser.ConfigParser()
config.read( self._configfile )
self._i2caddr = int(config.get('lcd', 'i2c_address'), 0)
self._width = config.getint('lcd', 'width')
self._lines = config.getint('lcd', 'lines')
self._backlightflag = config.getboolean('lcd', 'backlight')
self._shiftmode = config.getint('lcd', 'shiftmode')
self._kanamode = config.getboolean('lcd', 'kanamode')
self.setbacklight(self._backlightflag)
def getwidth(self):
return self._width
def str2bool(self, boolstring):
"""Convert value when config file is read"""
return boolstring.lower() in ("yes", "true", "on", "1", "t")
def lcd_string_kana(self, message, line):
"""Called when displaying Kana characters
on the LCD when using them.
Parameters
----------
message : str
message data
line : int
LCD address for the line
"""
codes = u'線線線線線線線線線線線線線線線線 '\
u' !"#$%&()*+,-./0123456789:;<=>?@ABCDEFG'\
u'HIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{'\
u'|}→← '\
u' 。「」、・ヲァィゥェォャュョッーア'\
u'イウエオカキクケコサシスセソタチツテトナニヌネノハヒ'\
u'フヘホマミムメモヤユヨラリルレロワン゛゜αäβεμσρq√陰ι'\
u'×¢£nöpqθ∞ΩüΣπxν千万円÷ 塗'
dic ={u'ガ':u'カ゛',u'ギ':u'キ゛',u'グ':u'ク゛',\
u'ゲ':u'ケ゛',u'ゴ':u'コ゛',u'ザ':u'サ゛',\
u'ジ':u'シ゛',u'ズ':u'ス゛',u'ゼ':u'セ゛',\
u'ゾ':u'ソ゛',u'ダ':u'タ゛',u'ヂ':u'チ゛',\
u'ヅ':u'ツ゛',u'デ':u'テ゛',u'ド':u'ト゛',\
u'バ':u'ハ゛',u'ビ':u'ヒ゛',u'ブ':u'フ゛',\
u'ベ':u'ヘ゛',u'ボ':u'ホ゛',u'パ':u'ハ゜',\
u'ピ':u'ヒ゜',u'プ':u'フ゜',u'ペ':u'ヘ゜',\
u'ポ':u'ホ゜',u'℃':u'゜C'}
self.lcd_byte(line, LCD_CMD)
message2 = ''
for i in range(len(message)):
if (message[i] in dic.keys()):
message2 += dic[message[i]]
else:
message2 += message[i]
for i in range(len(message2)):
if message2[i] == ' ':
self.lcd_byte(ord(message2[i]), LCD_CHR)
elif (codes.find(message2[i]) >= 0):
self.lcd_byte(codes.find(message2[i]) + 1, LCD_CHR)
elif (codes_han.find(message2[i]) >= 0):
self.lcd_byte(codes.find(message2[i]) + 1, LCD_CHR)
elif (message2[i] != u' '):
self.lcd_byte('?', LCD_CHR)