Skip to content

Commit a7289c4

Browse files
committed
Adding End of Line handling and support for 20x4 displays
I was using this code with a RPi and the MCP230xx without the plate and found a need to update it with support for 20x4 (or any size that runs the HD44780 really) This also helps with some of the odd carriage return things. with more than 2 lines. See my test script for demos
1 parent 501c1ce commit a7289c4

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

Adafruit_CharLCDPlate/Adafruit_CharLCDPlate.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,9 @@ def write(self, value, char_mode=False):
257257
# ----------------------------------------------------------------------
258258
# Utility methods
259259

260-
def begin(self, cols, lines):
260+
def begin(self, cols = 16, lines = 2):
261261
self.currline = 0
262+
self.cols = cols
262263
self.numlines = lines
263264
self.clear()
264265

@@ -404,13 +405,35 @@ def createChar(self, location, bitmap):
404405
self.write(self.LCD_SETDDRAMADDR)
405406

406407

407-
def message(self, text):
408-
""" Send string to LCD. Newline wraps to second line"""
409-
lines = str(text).split('\n') # Split at newline(s)
410-
for i, line in enumerate(lines): # For each substring...
411-
if i > 0: # If newline(s),
412-
self.write(0xC0) # set DDRAM address to 2nd line
413-
self.write(line, True) # Issue substring
408+
def message(self, text, limitMode = 0):
409+
""" Send string to LCD. Newline wraps to next line"""
410+
lines = str(text).split('\n') # Split at newline(s)
411+
for i, line in enumerate(lines): # For each substring...
412+
if i == 1: # If newline(s),
413+
self.write(0xC0) # set DDRAM address to 2nd line
414+
elif i == 2:
415+
self.write(0x94)
416+
elif i >= 3:
417+
self.write(0xD4)
418+
"""Now depending on the limit mode set by the function call this will handle """
419+
lineLength = len(line)
420+
limit = self.numcols
421+
if limitMode <= 0:
422+
self.write(line, True)
423+
elif lineLength >= limit and limitMode == 1:
424+
'''With the limit mode set to 1 the line is truncated
425+
at the number of columns available on the display'''
426+
limitedLine = line[0:self.numcols]
427+
self.write(limitedLine, True)
428+
elif lineLength >= limit and limitMode == 2:
429+
'''With the limit mode set to 2 the line is truncated
430+
at the number of columns minus 3 to add in an elipse'''
431+
limitedLine = line[0:self.numcols-3]+'...'
432+
self.write(limitedLine, True)
433+
elif lineLength >= limit and limitMode >= 3:
434+
'''Future todo, add in proper, "line after line" cariage return'''
435+
else:
436+
self.write(line, True)
414437

415438

416439
def backlight(self, color):

0 commit comments

Comments
 (0)