8
8
9
9
from time import sleep
10
10
11
- class Adafruit_CharLCD :
11
+
12
+ class Adafruit_CharLCD (object ):
12
13
13
14
# commands
14
- LCD_CLEARDISPLAY = 0x01
15
- LCD_RETURNHOME = 0x02
16
- LCD_ENTRYMODESET = 0x04
17
- LCD_DISPLAYCONTROL = 0x08
18
- LCD_CURSORSHIFT = 0x10
19
- LCD_FUNCTIONSET = 0x20
20
- LCD_SETCGRAMADDR = 0x40
21
- LCD_SETDDRAMADDR = 0x80
15
+ LCD_CLEARDISPLAY = 0x01
16
+ LCD_RETURNHOME = 0x02
17
+ LCD_ENTRYMODESET = 0x04
18
+ LCD_DISPLAYCONTROL = 0x08
19
+ LCD_CURSORSHIFT = 0x10
20
+ LCD_FUNCTIONSET = 0x20
21
+ LCD_SETCGRAMADDR = 0x40
22
+ LCD_SETDDRAMADDR = 0x80
22
23
23
24
# flags for display entry mode
24
- LCD_ENTRYRIGHT = 0x00
25
- LCD_ENTRYLEFT = 0x02
26
- LCD_ENTRYSHIFTINCREMENT = 0x01
27
- LCD_ENTRYSHIFTDECREMENT = 0x00
25
+ LCD_ENTRYRIGHT = 0x00
26
+ LCD_ENTRYLEFT = 0x02
27
+ LCD_ENTRYSHIFTINCREMENT = 0x01
28
+ LCD_ENTRYSHIFTDECREMENT = 0x00
28
29
29
30
# flags for display on/off control
30
- LCD_DISPLAYON = 0x04
31
- LCD_DISPLAYOFF = 0x00
32
- LCD_CURSORON = 0x02
33
- LCD_CURSOROFF = 0x00
34
- LCD_BLINKON = 0x01
35
- LCD_BLINKOFF = 0x00
31
+ LCD_DISPLAYON = 0x04
32
+ LCD_DISPLAYOFF = 0x00
33
+ LCD_CURSORON = 0x02
34
+ LCD_CURSOROFF = 0x00
35
+ LCD_BLINKON = 0x01
36
+ LCD_BLINKOFF = 0x00
36
37
37
38
# flags for display/cursor shift
38
- LCD_DISPLAYMOVE = 0x08
39
- LCD_CURSORMOVE = 0x00
39
+ LCD_DISPLAYMOVE = 0x08
40
+ LCD_CURSORMOVE = 0x00
40
41
41
42
# flags for display/cursor shift
42
- LCD_DISPLAYMOVE = 0x08
43
- LCD_CURSORMOVE = 0x00
44
- LCD_MOVERIGHT = 0x04
45
- LCD_MOVELEFT = 0x00
43
+ LCD_DISPLAYMOVE = 0x08
44
+ LCD_CURSORMOVE = 0x00
45
+ LCD_MOVERIGHT = 0x04
46
+ LCD_MOVELEFT = 0x00
46
47
47
48
# flags for function set
48
- LCD_8BITMODE = 0x10
49
- LCD_4BITMODE = 0x00
50
- LCD_2LINE = 0x08
51
- LCD_1LINE = 0x00
52
- LCD_5x10DOTS = 0x04
53
- LCD_5x8DOTS = 0x00
54
-
55
-
56
-
57
- def __init__ (self , pin_rs = 25 , pin_e = 24 , pins_db = [23 , 17 , 21 , 22 ], GPIO = None ):
58
- # Emulate the old behavior of using RPi.GPIO if we haven't been given
59
- # an explicit GPIO interface to use
60
- if not GPIO :
61
- import RPi .GPIO as GPIO
62
- self .GPIO = GPIO
49
+ LCD_8BITMODE = 0x10
50
+ LCD_4BITMODE = 0x00
51
+ LCD_2LINE = 0x08
52
+ LCD_1LINE = 0x00
53
+ LCD_5x10DOTS = 0x04
54
+ LCD_5x8DOTS = 0x00
55
+
56
+ def __init__ (self , pin_rs = 25 , pin_e = 24 , pins_db = [23 , 17 , 21 , 22 ], GPIO = None ):
57
+ # Emulate the old behavior of using RPi.GPIO if we haven't been given
58
+ # an explicit GPIO interface to use
59
+ if not GPIO :
60
+ import RPi .GPIO as GPIO
61
+ GPIO .setwarnings (False )
62
+ self .GPIO = GPIO
63
63
self .pin_rs = pin_rs
64
64
self .pin_e = pin_e
65
65
self .pins_db = pins_db
@@ -71,190 +71,140 @@ def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], GPIO = None):
71
71
for pin in self .pins_db :
72
72
self .GPIO .setup (pin , GPIO .OUT )
73
73
74
- self .write4bits (0x33 ) # initialization
75
- self .write4bits (0x32 ) # initialization
76
- self .write4bits (0x28 ) # 2 line 5x7 matrix
77
- self .write4bits (0x0C ) # turn cursor off 0x0E to enable cursor
78
- self .write4bits (0x06 ) # shift cursor right
74
+ self .write4bits (0x33 ) # initialization
75
+ self .write4bits (0x32 ) # initialization
76
+ self .write4bits (0x28 ) # 2 line 5x7 matrix
77
+ self .write4bits (0x0C ) # turn cursor off 0x0E to enable cursor
78
+ self .write4bits (0x06 ) # shift cursor right
79
79
80
- self .displaycontrol = self .LCD_DISPLAYON | self .LCD_CURSOROFF | self .LCD_BLINKOFF
80
+ self .displaycontrol = self .LCD_DISPLAYON | self .LCD_CURSOROFF | self .LCD_BLINKOFF
81
81
82
- self .displayfunction = self .LCD_4BITMODE | self .LCD_1LINE | self .LCD_5x8DOTS
83
- self .displayfunction |= self .LCD_2LINE
82
+ self .displayfunction = self .LCD_4BITMODE | self .LCD_1LINE | self .LCD_5x8DOTS
83
+ self .displayfunction |= self .LCD_2LINE
84
84
85
- """ Initialize to default text direction (for romance languages) """
86
- self .displaymode = self .LCD_ENTRYLEFT | self .LCD_ENTRYSHIFTDECREMENT
87
- self .write4bits (self .LCD_ENTRYMODESET | self .displaymode ) # set the entry mode
85
+ # Initialize to default text direction (for romance languages)
86
+ self .displaymode = self .LCD_ENTRYLEFT | self .LCD_ENTRYSHIFTDECREMENT
87
+ self .write4bits (self .LCD_ENTRYMODESET | self .displaymode ) # set the entry mode
88
88
89
89
self .clear ()
90
90
91
-
92
91
def begin (self , cols , lines ):
93
-
94
- if (lines > 1 ):
95
- self .numlines = lines
96
- self .displayfunction |= self .LCD_2LINE
97
-
92
+ if (lines > 1 ):
93
+ self .numlines = lines
94
+ self .displayfunction |= self .LCD_2LINE
98
95
99
96
def home (self ):
100
-
101
- self .write4bits (self .LCD_RETURNHOME ) # set cursor position to zero
102
- self .delayMicroseconds (3000 ) # this command takes a long time!
103
-
97
+ self .write4bits (self .LCD_RETURNHOME ) # set cursor position to zero
98
+ self .delayMicroseconds (3000 ) # this command takes a long time!
104
99
105
100
def clear (self ):
106
-
107
- self .write4bits (self .LCD_CLEARDISPLAY ) # command to clear display
108
- self .delayMicroseconds (3000 ) # 3000 microsecond sleep, clearing the display takes a long time
109
-
101
+ self .write4bits (self .LCD_CLEARDISPLAY ) # command to clear display
102
+ self .delayMicroseconds (3000 ) # 3000 microsecond sleep, clearing the display takes a long time
110
103
111
104
def setCursor (self , col , row ):
105
+ self .row_offsets = [0x00 , 0x40 , 0x14 , 0x54 ]
106
+ if row > self .numlines :
107
+ row = self .numlines - 1 # we count rows starting w/0
108
+ self .write4bits (self .LCD_SETDDRAMADDR | (col + self .row_offsets [row ]))
112
109
113
- self .row_offsets = [ 0x00 , 0x40 , 0x14 , 0x54 ]
114
-
115
- if ( row > self .numlines ):
116
- row = self .numlines - 1 # we count rows starting w/0
117
-
118
- self .write4bits (self .LCD_SETDDRAMADDR | (col + self .row_offsets [row ]))
119
-
120
-
121
- def noDisplay (self ):
122
- """ Turn the display off (quickly) """
123
-
124
- self .displaycontrol &= ~ self .LCD_DISPLAYON
125
- self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
126
-
110
+ def noDisplay (self ):
111
+ """ Turn the display off (quickly) """
112
+ self .displaycontrol &= ~ self .LCD_DISPLAYON
113
+ self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
127
114
128
115
def display (self ):
129
- """ Turn the display on (quickly) """
130
-
131
- self .displaycontrol |= self .LCD_DISPLAYON
132
- self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
133
-
116
+ """ Turn the display on (quickly) """
117
+ self .displaycontrol |= self .LCD_DISPLAYON
118
+ self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
134
119
135
120
def noCursor (self ):
136
- """ Turns the underline cursor off """
137
-
138
- self .displaycontrol &= ~ self .LCD_CURSORON
139
- self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
140
-
121
+ """ Turns the underline cursor off """
122
+ self .displaycontrol &= ~ self .LCD_CURSORON
123
+ self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
141
124
142
125
def cursor (self ):
143
- """ Turns the underline cursor on """
144
-
145
- self .displaycontrol |= self .LCD_CURSORON
146
- self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
147
-
126
+ """ Turns the underline cursor on """
127
+ self .displaycontrol |= self .LCD_CURSORON
128
+ self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
148
129
149
130
def noBlink (self ):
150
- """ Turn the blinking cursor off """
151
-
152
- self .displaycontrol &= ~ self .LCD_BLINKON
153
- self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
154
-
131
+ """ Turn the blinking cursor off """
132
+ self .displaycontrol &= ~ self .LCD_BLINKON
133
+ self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
155
134
156
135
def blink (self ):
157
- """ Turn the blinking cursor on """
158
-
159
- self .displaycontrol |= self .LCD_BLINKON
160
- self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
161
-
136
+ """ Turn the blinking cursor on """
137
+ self .displaycontrol |= self .LCD_BLINKON
138
+ self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
162
139
163
140
def DisplayLeft (self ):
164
- """ These commands scroll the display without changing the RAM """
165
-
166
- self .write4bits (self .LCD_CURSORSHIFT | self .LCD_DISPLAYMOVE | self .LCD_MOVELEFT )
167
-
141
+ """ These commands scroll the display without changing the RAM """
142
+ self .write4bits (self .LCD_CURSORSHIFT | self .LCD_DISPLAYMOVE | self .LCD_MOVELEFT )
168
143
169
144
def scrollDisplayRight (self ):
170
- """ These commands scroll the display without changing the RAM """
171
-
172
- self .write4bits (self .LCD_CURSORSHIFT | self .LCD_DISPLAYMOVE | self .LCD_MOVERIGHT );
173
-
145
+ """ These commands scroll the display without changing the RAM """
146
+ self .write4bits (self .LCD_CURSORSHIFT | self .LCD_DISPLAYMOVE | self .LCD_MOVERIGHT )
174
147
175
148
def leftToRight (self ):
176
- """ This is for text that flows Left to Right """
177
-
178
- self .displaymode |= self .LCD_ENTRYLEFT
179
- self .write4bits (self .LCD_ENTRYMODESET | self .displaymode );
180
-
149
+ """ This is for text that flows Left to Right """
150
+ self .displaymode |= self .LCD_ENTRYLEFT
151
+ self .write4bits (self .LCD_ENTRYMODESET | self .displaymode )
181
152
182
153
def rightToLeft (self ):
183
- """ This is for text that flows Right to Left """
184
- self .displaymode &= ~ self .LCD_ENTRYLEFT
185
- self .write4bits (self .LCD_ENTRYMODESET | self .displaymode )
186
-
154
+ """ This is for text that flows Right to Left """
155
+ self .displaymode &= ~ self .LCD_ENTRYLEFT
156
+ self .write4bits (self .LCD_ENTRYMODESET | self .displaymode )
187
157
188
158
def autoscroll (self ):
189
- """ This will 'right justify' text from the cursor """
190
-
191
- self .displaymode |= self .LCD_ENTRYSHIFTINCREMENT
192
- self .write4bits (self .LCD_ENTRYMODESET | self .displaymode )
193
-
194
-
195
- def noAutoscroll (self ):
196
- """ This will 'left justify' text from the cursor """
197
-
198
- self .displaymode &= ~ self .LCD_ENTRYSHIFTINCREMENT
199
- self .write4bits (self .LCD_ENTRYMODESET | self .displaymode )
159
+ """ This will 'right justify' text from the cursor """
160
+ self .displaymode |= self .LCD_ENTRYSHIFTINCREMENT
161
+ self .write4bits (self .LCD_ENTRYMODESET | self .displaymode )
200
162
163
+ def noAutoscroll (self ):
164
+ """ This will 'left justify' text from the cursor """
165
+ self .displaymode &= ~ self .LCD_ENTRYSHIFTINCREMENT
166
+ self .write4bits (self .LCD_ENTRYMODESET | self .displaymode )
201
167
202
168
def write4bits (self , bits , char_mode = False ):
203
169
""" Send command to LCD """
204
-
205
- self .delayMicroseconds (1000 ) # 1000 microsecond sleep
206
-
207
- bits = bin (bits )[2 :].zfill (8 )
208
-
170
+ self .delayMicroseconds (1000 ) # 1000 microsecond sleep
171
+ bits = bin (bits )[2 :].zfill (8 )
209
172
self .GPIO .output (self .pin_rs , char_mode )
210
-
211
173
for pin in self .pins_db :
212
174
self .GPIO .output (pin , False )
213
-
214
175
for i in range (4 ):
215
176
if bits [i ] == "1" :
216
177
self .GPIO .output (self .pins_db [::- 1 ][i ], True )
217
-
218
- self .pulseEnable ()
219
-
178
+ self .pulseEnable ()
220
179
for pin in self .pins_db :
221
180
self .GPIO .output (pin , False )
222
-
223
- for i in range (4 ,8 ):
181
+ for i in range (4 , 8 ):
224
182
if bits [i ] == "1" :
225
183
self .GPIO .output (self .pins_db [::- 1 ][i - 4 ], True )
226
-
227
- self .pulseEnable ()
228
-
184
+ self .pulseEnable ()
229
185
230
186
def delayMicroseconds (self , microseconds ):
231
- seconds = microseconds / float (1000000 ) # divide microseconds by 1 million for seconds
232
- sleep (seconds )
233
-
187
+ seconds = microseconds / float (1000000 ) # divide microseconds by 1 million for seconds
188
+ sleep (seconds )
234
189
235
190
def pulseEnable (self ):
236
- self .GPIO .output (self .pin_e , False )
237
- self .delayMicroseconds (1 ) # 1 microsecond pause - enable pulse must be > 450ns
238
- self .GPIO .output (self .pin_e , True )
239
- self .delayMicroseconds (1 ) # 1 microsecond pause - enable pulse must be > 450ns
240
- self .GPIO .output (self .pin_e , False )
241
- self .delayMicroseconds (1 ) # commands need > 37us to settle
242
-
191
+ self .GPIO .output (self .pin_e , False )
192
+ self .delayMicroseconds (1 ) # 1 microsecond pause - enable pulse must be > 450ns
193
+ self .GPIO .output (self .pin_e , True )
194
+ self .delayMicroseconds (1 ) # 1 microsecond pause - enable pulse must be > 450ns
195
+ self .GPIO .output (self .pin_e , False )
196
+ self .delayMicroseconds (1 ) # commands need > 37us to settle
243
197
244
198
def message (self , text ):
245
199
""" Send string to LCD. Newline wraps to second line"""
246
-
247
200
for char in text :
248
201
if char == '\n ' :
249
- self .write4bits (0xC0 ) # next line
202
+ self .write4bits (0xC0 ) # next line
250
203
else :
251
- self .write4bits (ord (char ),True )
204
+ self .write4bits (ord (char ), True )
252
205
253
206
254
207
if __name__ == '__main__' :
255
-
256
208
lcd = Adafruit_CharLCD ()
257
-
258
209
lcd .clear ()
259
210
lcd .message (" Adafruit 16x2\n Standard LCD" )
260
-
0 commit comments