1111
1212class Adafruit_CharLCD :
1313
14+ # commands
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
23+
24+ # flags for display entry mode
25+ LCD_ENTRYRIGHT = 0x00
26+ LCD_ENTRYLEFT = 0x02
27+ LCD_ENTRYSHIFTINCREMENT = 0x01
28+ LCD_ENTRYSHIFTDECREMENT = 0x00
29+
30+ # flags for display on/off control
31+ LCD_DISPLAYON = 0x04
32+ LCD_DISPLAYOFF = 0x00
33+ LCD_CURSORON = 0x02
34+ LCD_CURSOROFF = 0x00
35+ LCD_BLINKON = 0x01
36+ LCD_BLINKOFF = 0x00
37+
38+ # flags for display/cursor shift
39+ LCD_DISPLAYMOVE = 0x08
40+ LCD_CURSORMOVE = 0x00
41+
42+ # flags for display/cursor shift
43+ LCD_DISPLAYMOVE = 0x08
44+ LCD_CURSORMOVE = 0x00
45+ LCD_MOVERIGHT = 0x04
46+ LCD_MOVELEFT = 0x00
47+
48+ # flags for function set
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+
57+
1458 def __init__ (self , pin_rs = 25 , pin_e = 24 , pins_db = [23 , 17 , 21 , 22 ]):
1559
1660 self .pin_rs = pin_rs
@@ -29,23 +73,134 @@ def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22]):
2973 self .write4bits (0x28 ) # 2 line 5x7 matrix
3074 self .write4bits (0x0C ) # turn cursor off 0x0E to enable cursor
3175 self .write4bits (0x06 ) # shift cursor right
76+
77+ self .displaycontrol = self .LCD_DISPLAYON | self .LCD_CURSOROFF | self .LCD_BLINKOFF
78+
79+ self .displayfunction = self .LCD_4BITMODE | self .LCD_1LINE | self .LCD_5x8DOTS
80+ self .displayfunction |= self .LCD_2LINE
81+
82+ """ Initialize to default text direction (for romance languages) """
83+ self .displaymode = self .LCD_ENTRYLEFT | self .LCD_ENTRYSHIFTDECREMENT
84+ self .write4bits (self .LCD_ENTRYMODESET | self .displaymode ) # set the entry mode
85+
3286 self .clear ()
87+
88+
89+ def begin (self , cols , lines ):
90+
91+ if (lines > 1 ):
92+ self .numlines = lines
93+ self .displayfunction |= self .LCD_2LINE
94+ self .currline = 0
95+
96+
97+ def home (self ):
98+
99+ self .write4bits (self .LCD_RETURNHOME ) # set cursor position to zero
100+ self .delayMicroseconds (2000 ) # this command takes a long time!
33101
34102
35103 def clear (self ):
36104
37- self .write4bits (0x01 ) # command to clear display
105+ self .write4bits (self .LCD_CLEARDISPLAY ) # command to clear display
106+ self .delayMicroseconds (2000 ) # 2000 microsecond sleep, clearing the display takes a long time
107+
108+
109+ def setCursor (self , col , row ):
110+
111+ self .row_offsets = [ 0x00 , 0x40 , 0x14 , 0x54 ]
112+
113+ if ( row > self .numlines ):
114+ row = self .numlines - 1 # we count rows starting w/0
115+
116+ self .write4bits (self .LCD_SETDDRAMADDR | (col + self .row_offsets [row ]))
117+
118+
119+ def noDisplay (self ):
120+ """ Turn the display off (quickly) """
121+
122+ self .displaycontrol &= ~ self .LCD_DISPLAYON
123+ self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
124+
125+
126+ def display (self ):
127+ """ Turn the display on (quickly) """
128+
129+ self .displaycontrol |= self .LCD_DISPLAYON
130+ self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
131+
132+
133+ def noCursor (self ):
134+ """ Turns the underline cursor on/off """
135+
136+ self .displaycontrol &= ~ self .LCD_CURSORON
137+ self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
138+
139+
140+ def cursor (self ):
141+ """ Cursor On """
142+
143+ self .displaycontrol |= self .LCD_CURSORON
144+ self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
145+
146+
147+ def noBlink (self ):
148+ """ Turn on and off the blinking cursor """
149+
150+ self .displaycontrol &= ~ self .LCD_BLINKON
151+ self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
152+
153+
154+ def noBlink (self ):
155+ """ Turn on and off the blinking cursor """
156+
157+ self .displaycontrol &= ~ self .LCD_BLINKON
158+ self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
159+
160+
161+ def DisplayLeft (self ):
162+ """ These commands scroll the display without changing the RAM """
163+
164+ self .write4bits (self .LCD_CURSORSHIFT | self .LCD_DISPLAYMOVE | self .LCD_MOVELEFT )
165+
166+
167+ def scrollDisplayRight (self ):
168+ """ These commands scroll the display without changing the RAM """
169+
170+ self .write4bits (self .LCD_CURSORSHIFT | self .LCD_DISPLAYMOVE | self .LCD_MOVERIGHT );
171+
172+
173+ def leftToRight (self ):
174+ """ This is for text that flows Left to Right """
175+
176+ self .displaymode |= self .LCD_ENTRYLEFT
177+ self .write4bits (self .LCD_ENTRYMODESET | self .displaymode );
178+
179+
180+ def rightToLeft (self ):
181+ """ This is for text that flows Right to Left """
182+ self .displaymode &= ~ self .LCD_ENTRYLEFT
183+ self .write4bits (self .LCD_ENTRYMODESET | self .displaymode )
184+
185+
186+ def autoscroll (self ):
187+ """ This will 'right justify' text from the cursor """
188+
189+ self .displaymode |= self .LCD_ENTRYSHIFTINCREMENT
190+ self .write4bits (self .LCD_ENTRYMODESET | self .displaymode )
191+
192+
193+ def noAutoscroll (self ):
194+ """ This will 'left justify' text from the cursor """
195+
196+ self .displaymode &= ~ self .LCD_ENTRYSHIFTINCREMENT
197+ self .write4bits (self .LCD_ENTRYMODESET | self .displaymode )
38198
39- # 2000 microsecond sleep, clearing the display takes a long time
40- sleep (.002 )
41- #self.delayMicroseconds(2000)
42199
43200 def write4bits (self , bits , char_mode = False ):
44201 """ Send command to LCD """
45202
46- # 1000 microseconds sleep
47- # sleep(.001)
48- self .delayMicroseconds (1000 )
203+ self .delayMicroseconds (1000 ) # 1000 microsecond sleep
49204
50205 bits = bin (bits )[2 :].zfill (8 )
51206
0 commit comments