-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlcd_driver.py
202 lines (162 loc) · 5.56 KB
/
lcd_driver.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
# -*- coding: utf-8 -*-
from . import i2c_lib
from time import sleep
from . import locale_class
### BIT PATTERNS ###
# Commands
LCD_CLEARDISPLAY = 0b00000001
LCD_RETURNHOME = 0b00000010
LCD_ENTRYMODESET = 0b00000100
LCD_DISPLAYCONTROL = 0b00001000
LCD_CURSORSHIFT = 0b00010000
LCD_FUNCTIONSET = 0b00100000
LCD_SETCGRAMADDR = 0b01000000
LCD_SETDDRAMADDR = 0b10000000
# Flags for display entry mode
LCD_ENTRYRIGHT = 0b00000000
LCD_ENTRYLEFT = 0b00000010
LCD_ENTRYSHIFTINCREMENT = 0b00000001
LCD_ENTRYSHIFTDECREMENT = 0b00000000
# Flags for display on/off control
LCD_DISPLAYON = 0b00000100
LCD_DISPLAYOFF = 0b00000000
LCD_CURSORON = 0b00000010
LCD_CURSOROFF = 0b00000000
LCD_BLINKON = 0b00000001
LCD_BLINKOFF = 0b00000000
# Flags for display/cursor shift
LCD_DISPLAYMOVE = 0b00001000
LCD_CURSORMOVE = 0b00000000
LCD_MOVERIGHT = 0b00000100
LCD_MOVELEFT = 0b00000000
# Flags for function set
LCD_8BITMODE = 0b00010000
LCD_4BITMODE = 0b00000000
LCD_2LINE = 0b00001000
LCD_1LINE = 0b00000000
LCD_5x10DOTS = 0b00000100
LCD_5x8DOTS = 0b00000000
### Bits ###
RS_ON = 0b00000001 # send command (instead of char)
RS_OFF = 0b00000000
RW_ON = 0b00000010
RW_OFF = 0b00000000
EN_ON = 0b00000100
EN_OFF = 0b00000000
BL_ON = 0b00001000
BL_OFF = 0b00000000
class LCD(object):
def __init__(self, address = 0x27, default_sleep_time = 0.00004, backlight = True, locale = None):
# backlight mode
# we have to start with that as it is required for the following commands
self.backlight_bit = BL_OFF
if backlight:
self.backlight_bit = BL_ON
if locale is None:
self.locale = locale_class.LocaleClass()
else:
self.locale = locale
self.bus = i2c_lib.i2c_device(address)
self.default_sleep_time = default_sleep_time
# init LCD with 4 bit
self.init_4_bit()
# define custom chars
self.def_custom_chars()
# init LCD mode
self.init_display_mode()
def init_4_bit(self):
sleep(0.015) # wait 150 ms
self.write_cmd(0b00000011)
sleep(0.0041) # wait 4,1 ms
self.write_cmd(0b00000011)
sleep(0.0001) # wait 100 µs
self.write_cmd(0b00000011)
# set to 4 bit mode now
self.write_cmd(0b00000010)
def init_display_mode(self):
# 2 lines, 5x8 dots
self.write_cmd(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE)
# turn display off
self.write_cmd(LCD_DISPLAYCONTROL | LCD_DISPLAYOFF)
# clear display
self.clear_display()
# cursor move right, no shift
self.write_cmd(LCD_ENTRYMODESET | LCD_ENTRYLEFT)
# turn display on
self.write_cmd(LCD_DISPLAYCONTROL | LCD_DISPLAYON | LCD_CURSOROFF)
def def_custom_chars(self):
# raise error if too many custom characters are defined
if (len(self.locale.custom_chars) > 8):
raise ValueError("Too many custom characters (only 8 allowed)!")
# define the index of the custom chars
custom_char_index = 0
# set "cursor" at the start of the CGRAM
pos = LCD_SETCGRAMADDR | 0b0000000 # this is the start of the CGRAM
self.write_cmd(pos)
# write custom characters to CGRAM
for c in self.locale.custom_chars.keys():
char_shape = self.locale.custom_chars[c]
if len(char_shape) != 8:
raise ValueError("One of the custom chars is not defined correctly. Each custom char must consist of 8 bytes (representing 8 lines).")
# store shape in CGRAM
for b in char_shape:
self.write_byte(b, rs = RS_ON)
# store defined char to custom_chars dict
self.locale.locale_chars[c] = custom_char_index
custom_char_index += 1
def clear_display(self):
# usually a longer sleep time is required
self.write_cmd(LCD_CLEARDISPLAY, sleep_time = 0.0007) # for my display it takes about 7000 ms - please check for your display
def set_position(self, line, column):
# position commands always have the first bit set (1xxxxxxx)
pos = int(LCD_SETDDRAMADDR)
# first set the line
line_pos = 0
if line == 1:
line_pos = 0
elif line == 2:
line_pos = 64
elif line == 3:
line_pos = 20
elif line == 4:
line_pos = 84
# now set the column
pos += line_pos + column
# write it to the display
self.write_cmd(pos)
def backlight(self, state):
if state:
self.backlight_bit = BL_ON
else:
self.backlight_bit = BL_OFF
def replace_locale_chars(self, char):
if char in self.locale.locale_chars:
return self.locale.locale_chars[char]
else:
return ord(char)
def write_byte(self, byte, rs, sleep_time = None):
if sleep_time is None:
sleep_time = self.default_sleep_time
# write high nibble
self.send_nibble(nibble = byte & 0b11110000, rw = RW_OFF, rs = rs)
# write low nibble
self.send_nibble(nibble = (byte << 4) & 0b11110000, rw = RW_OFF, rs = rs)
sleep(sleep_time) # sleep (default 40 µs)
def send_nibble(self, nibble, rw, rs):
# bits:
# cccc bl en rw rs
# cccc: nibble (4 bits)
# bl (backlight, 1 bit, low: off, high: on)
# en (1 bit, sequence: high - low - high)
# rw (1 bit, low: write, high: read)
# rs (1 bit, low: command, high: character)
self.bus.write(nibble | EN_ON | rw | rs | self.backlight_bit) # enable high
self.bus.write(nibble | EN_OFF | rw | rs | self.backlight_bit) # enable low
def write_cmd(self, cmd, sleep_time = None):
self.write_byte(cmd, rs = RS_OFF, sleep_time = sleep_time)
def write_char(self, char):
char_number = self.replace_locale_chars(char)
self.write_byte(char_number, rs = RS_ON)
def write(self, string):
for c in string:
self.write_char(c)