|
| 1 | +#---------------------------------------------------------------- |
| 2 | +# Author: Chris Crumpacker |
| 3 | +# Date: May 2013 |
| 4 | +# |
| 5 | +# Heavily modified version of the "message" function from Adafruit's |
| 6 | +# CharLCD code as written for the RaspberryPi LCD Plate. This handles |
| 7 | +# the end of row/line So that it is actually is cut off and does not |
| 8 | +# overflow into the buffer on the 16x2 displays and worse onto |
| 9 | +# line 3 on the 20x4 displays |
| 10 | +# |
| 11 | +# All orginal functionality is retained with the addition of handling the |
| 12 | +# \n's Cariage returns for all 4 lines on the 20x4 |
| 13 | +# |
| 14 | +# When calling the message function you can add a "mode" variable to the end |
| 15 | +# to describe how to handle strings over the length of the display. |
| 16 | +# |
| 17 | +# MODE: |
| 18 | +# 0 or empty = Normal handling as it was from Adafruit |
| 19 | +# 1 = Truncates the string right at the display's limit |
| 20 | +# 2 = Truncates the string 3 short of the limit and adds an elipse |
| 21 | +# |
| 22 | +# Future plans will be to both handle proper cariage returns and to |
| 23 | +# cut at the spaces as to not leave partial words. |
| 24 | +# |
| 25 | +# Orginal was written by Adafruit Industries. |
| 26 | +# Under MIT license. |
| 27 | +# |
| 28 | +# "This is essentially a complete rewrite, but the calling syntax |
| 29 | +# and constants are based on code from lrvick and LiquidCrystal. |
| 30 | +# lrvic - https://github.com/lrvick/raspi-hd44780/blob/master/hd44780.py |
| 31 | +# LiquidCrystal - https://github.com/arduino/Arduino/blob/master/libraries/LiquidCrystal/LiquidCrystal.cpp" |
| 32 | +#---------------------------------------------------------------- |
| 33 | + |
| 34 | +from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate |
| 35 | + |
| 36 | +lcd = Adafruit_CharLCDPlate() |
| 37 | + |
| 38 | +class CrumpLCD_EoL_Handling(Adafruit_CharLCDPlate): |
| 39 | + |
| 40 | + def __init__(self, cols=20, rows=4): # Defaulted to 20x4 displays |
| 41 | + self.numrows = rows |
| 42 | + self.numcols = cols # Added a var for the column count to act as the line length limit (say that 10x fast) |
| 43 | + |
| 44 | + |
| 45 | + def message(self, text, limitMode = 0): |
| 46 | + """ Send string to LCD. Newline wraps to next line""" |
| 47 | + lines = str(text).split('\n') # Split at newline(s) |
| 48 | + for i, line in enumerate(lines): # For each substring... |
| 49 | + if i == 1: # If newline(s), |
| 50 | + lcd.write(0xC0) # set DDRAM address to 2nd line |
| 51 | + elif i == 2: |
| 52 | + lcd.write(0x94) |
| 53 | + elif i >= 3: |
| 54 | + lcd.write(0xD4) |
| 55 | + """Now depending on the limit mode set by the function call this will handle """ |
| 56 | + lineLength = len(line) |
| 57 | + limit = self.numcols |
| 58 | + if limitMode == 0: |
| 59 | + lcd.write(line, True) |
| 60 | + elif lineLength >= limit and limitMode == 1: |
| 61 | + '''With the limit mode set to 1 the line is truncated |
| 62 | + at the number of columns available on the display''' |
| 63 | + limitedLine = line[0:self.numcols] |
| 64 | + lcd.write(limitedLine, True) |
| 65 | + elif lineLength >= limit and limitMode == 2: |
| 66 | + '''With the limit mode set to 2 the line is truncated |
| 67 | + at the number of columns minus 3 to add in an elipse''' |
| 68 | + limitedLine = line[0:self.numcols-3]+'...' |
| 69 | + lcd.write(limitedLine, True) |
| 70 | + elif lineLength >= limit and limitMode == 3: |
| 71 | + '''Future todo, add in proper line after line cariage return''' |
| 72 | + print lines |
| 73 | + else: |
| 74 | + lcd.write(line, True) |
| 75 | + |
| 76 | +#!/usr/bin/python |
| 77 | + |
| 78 | +if __name__ == '__main__': |
| 79 | + from time import sleep |
| 80 | + |
| 81 | + numcolumns = 20 |
| 82 | + numrows = 4 |
| 83 | + |
| 84 | + eol = CrumpLCD_EoL_Handling(numcolumns, numrows) |
| 85 | + |
| 86 | + lcd.backlight(lcd.ON) |
| 87 | + lcd.begin(numcolumns, numrows) |
| 88 | + |
| 89 | + eol.message("CrumpLCD\nEnd of Line Handling\nWith Forced\nCarriage Returns") |
| 90 | + sleep(2) |
| 91 | + |
| 92 | + lcd.clear() |
| 93 | + eol.message("Short String") |
| 94 | + sleep(2) |
| 95 | + |
| 96 | + lcd.clear() |
| 97 | + eol.message("Longer string then can't fit in one line",0) |
| 98 | + sleep(2) |
| 99 | + |
| 100 | + lcd.clear() |
| 101 | + eol.message("Longer string then can't fit in one line",1) |
| 102 | + sleep(2) |
| 103 | + |
| 104 | + lcd.clear() |
| 105 | + eol.message("Longer string then can't fit in one line",2) |
| 106 | + sleep(2) |
0 commit comments