Skip to content
This repository was archived by the owner on Sep 30, 2019. It is now read-only.

Commit 24fd2f4

Browse files
committed
Resolve #43 and resolve #44 by adding 20x4 LCD support and 20x4 example.
1 parent 349fbb2 commit 24fd2f4

File tree

2 files changed

+180
-4
lines changed

2 files changed

+180
-4
lines changed

Adafruit_CharLCDPlate/Adafruit_CharLCDPlate.py

+24-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from Adafruit_I2C import Adafruit_I2C
1212
from time import sleep
1313

14+
1415
class Adafruit_CharLCDPlate(Adafruit_I2C):
1516

1617
# ----------------------------------------------------------------------
@@ -72,6 +73,13 @@ class Adafruit_CharLCDPlate(Adafruit_I2C):
7273
LCD_MOVERIGHT = 0x04
7374
LCD_MOVELEFT = 0x00
7475

76+
# Line addresses for up to 4 line displays. Maps line number to DDRAM address for line.
77+
LINE_ADDRESSES = { 1: 0xC0, 2: 0x94, 3: 0xD4 }
78+
79+
# Truncation constants for message function truncate parameter.
80+
NO_TRUNCATE = 0
81+
TRUNCATE = 1
82+
TRUNCATE_ELLIPSIS = 2
7583

7684
# ----------------------------------------------------------------------
7785
# Constructor
@@ -265,6 +273,7 @@ def write(self, value, char_mode=False):
265273
def begin(self, cols, lines):
266274
self.currline = 0
267275
self.numlines = lines
276+
self.numcols = cols
268277
self.clear()
269278

270279

@@ -409,13 +418,24 @@ def createChar(self, location, bitmap):
409418
self.write(self.LCD_SETDDRAMADDR)
410419

411420

412-
def message(self, text):
421+
def message(self, text, truncate=NO_TRUNCATE):
413422
""" Send string to LCD. Newline wraps to second line"""
414423
lines = str(text).split('\n') # Split at newline(s)
415424
for i, line in enumerate(lines): # For each substring...
416-
if i > 0: # If newline(s),
417-
self.write(0xC0) # set DDRAM address to 2nd line
418-
self.write(line, True) # Issue substring
425+
address = self.LINE_ADDRESSES.get(i, None)
426+
if address is not None: # If newline(s),
427+
self.write(address) # set DDRAM address to line
428+
# Handle appropriate truncation if requested.
429+
linelen = len(line)
430+
if truncate == self.TRUNCATE and linelen > self.numcols:
431+
# Hard truncation of line.
432+
self.write(line[0:self.numcols], True)
433+
elif truncate == self.TRUNCATE_ELLIPSIS and linelen > self.numcols:
434+
# Nicer truncation with ellipses.
435+
self.write(line[0:self.numcols-3] + '...', True)
436+
else:
437+
self.write(line, True)
438+
419439

420440

421441
def backlight(self, color):

Adafruit_CharLCDPlate/LCDtest_20x4.py

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#!/usr/bin/python
2+
3+
#----------------------------------------------------------------
4+
# Author: Chris Crumpacker
5+
# Date: May 2013
6+
#
7+
# A demo of some of the built in helper functions of
8+
# the Adafruit_CharLCDPlate.py This is 20x4 display specific.
9+
#
10+
# Using Adafruit_CharLCD code with the I2C and MCP230xx code aswell
11+
#----------------------------------------------------------------
12+
13+
numcolumns = 20
14+
numrows = 4
15+
16+
from time import sleep
17+
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
18+
19+
lcd = Adafruit_CharLCDPlate()
20+
21+
lcd.begin(numcolumns, numrows)
22+
23+
lcd.backlight(lcd.ON)
24+
lcd.message("LCD 20x4\nDemonstration")
25+
sleep(2)
26+
27+
while True:
28+
#Text on each line alone.
29+
lcd.clear()
30+
lcd.setCursor(0,0)
31+
lcd.message("Line 1")
32+
sleep(1)
33+
34+
lcd.clear()
35+
lcd.setCursor(0,1)
36+
lcd.message("Line 2")
37+
sleep(1)
38+
39+
lcd.clear()
40+
lcd.setCursor(0,2)
41+
lcd.message("Line 3")
42+
sleep(1)
43+
44+
lcd.clear()
45+
lcd.setCursor(0,3)
46+
lcd.message("Line 4")
47+
sleep(1)
48+
49+
# Using the "\n" new line marker
50+
lcd.clear()
51+
lcd.setCursor(0,0)
52+
lcd.message("Line 1")
53+
sleep(1)
54+
55+
lcd.clear()
56+
lcd.setCursor(0,0)
57+
lcd.message("Line 1\nLine 2")
58+
sleep(1)
59+
60+
lcd.clear()
61+
lcd.setCursor(0,0)
62+
lcd.message("Line 1\nLine 2\nLine 3")
63+
sleep(1)
64+
65+
lcd.clear()
66+
lcd.setCursor(0,0)
67+
lcd.message("Line 1\nLine 2\nLine 3\nLine 4")
68+
sleep(1)
69+
70+
# Auto line limiting by length as to not overflow the display
71+
# This is line by line and does not to any caraige returns
72+
lcd.clear()
73+
lcd.setCursor(0,0)
74+
lcd.message("This String is 33 Characters long", lcd.TRUNCATE)
75+
sleep(2)
76+
77+
lcd.clear()
78+
lcd.setCursor(0,0)
79+
lcd.message("This String has ellipsis", lcd.TRUNCATE_ELLIPSIS)
80+
sleep(2)
81+
82+
#Scroll text to the right
83+
messageToPrint = "Scrolling Right"
84+
i=0
85+
while i<20:
86+
lcd.clear()
87+
lcd.setCursor(0,0)
88+
suffix = " " * i
89+
lcd.message(suffix + messageToPrint, lcd.TRUNCATE)
90+
sleep(.25)
91+
i += 1
92+
93+
# Scroll test in from the Left
94+
messageToPrint = "Scrolling Left"
95+
i=20
96+
while i>=0:
97+
lcd.clear()
98+
lcd.setCursor(0,0)
99+
suffix = " " * i
100+
lcd.message(suffix + messageToPrint, lcd.TRUNCATE)
101+
sleep(.25)
102+
i -= 1
103+
sleep(2)
104+
105+
# Printing text backwards, NOT right justified
106+
lcd.clear()
107+
lcd.setCursor(0,0)
108+
lcd.message("Right to left:")
109+
lcd.setCursor(10,1)
110+
lcd.rightToLeft()
111+
lcd.message("Testing")
112+
sleep(2)
113+
114+
# Printing normally from the middle of the line
115+
lcd.clear()
116+
lcd.setCursor(0,0)
117+
lcd.message("Left to Right:")
118+
lcd.setCursor(10,1)
119+
lcd.message("Testing")
120+
sleep(2)
121+
122+
# Enabling the cursor and having it blink
123+
lcd.clear()
124+
lcd.setCursor(0,0)
125+
lcd.cursor()
126+
lcd.blink()
127+
lcd.message("Cursor is blinking")
128+
lcd.setCursor(0,1)
129+
sleep(3)
130+
lcd.noCursor()
131+
lcd.noBlink()
132+
133+
# Turning the backlight off and showing a simple count down
134+
lcd.clear()
135+
lcd.setCursor(0,0)
136+
lcd.message("Backlight off in")
137+
lcd.setCursor(0,3)
138+
lcd.message("Back on in 3sec")
139+
lcd.setCursor(17,0) #Reseting the cursor here keeps us from having to clear the screen, this over writes the previous character
140+
lcd.message("3")
141+
sleep(1)
142+
143+
lcd.setCursor(17,0)
144+
lcd.message("2")
145+
sleep(1)
146+
147+
lcd.setCursor(17,0)
148+
lcd.message("1")
149+
sleep(1)
150+
151+
lcd.backlight(lcd.OFF)
152+
lcd.clear()
153+
lcd.setCursor(0,0)
154+
sleep(3)
155+
lcd.backlight(lcd.ON)
156+
lcd.message("Backlight on")

0 commit comments

Comments
 (0)