My Pi Description

My Experiences With the Raspberry Pi -- Tracking My Learning -- My Pi Projects

Wednesday, March 27, 2013

16 X 2 LCD Input Text and Display

Here is a more practical program that asks you to input text and then displays that text on the LCD display.  The challenge here was to parse the input text so that words were not broken up.  Click on the video to see it work:
Here is python code for this program:
#!/usr/bin/python
# For two line dispalys using the Hitachi HD4478U Disolay Driver Chip
# Gets input from user and sends it to the display
# The inputed text is parsed into segments no longer than 16 characters that
# end in complete words.
# Will breakdown on words that are over 16 characters in length.
# First segment goes to line 1, the next to line 2, the next to line 1, etc.
# MJL -- www.thepiandi.blogspot.com 0-- 3/23/2013
from LCD_2X16 import *
def getinput(): # inputs text and parses it into a list where each element is no more than 16 characters
text = raw_input('Input your text: ')
text = text + ' '
listtext = []
stop = False
while stop == False:
spacepos = -1
index_pos = 0
while spacepos < 16 and stop == False:
spacepos = text.find(' ', spacepos + 1)
if spacepos <= 16 and spacepos != -1:
index_pos = spacepos
if spacepos == - 1:
stop = True
if stop:
if text != '':
listtext = listtext + [text[:index_pos]]
else:
listtext = listtext + [text[:index_pos]]
text = text[index_pos + 1:]
return listtext
def wait4switch():
lcd_byte(LCD_LINE_1, LCD_CMD)
lcd_string("Push My Button")
lcd_byte(LCD_LINE_2, LCD_CMD)
lcd_string(" Please")
# wait for switch to be pressed
input_switch = GPIO.input(SwitchInput)
while input_switch:
input_switch = GPIO.input(SwitchInput)
# where it all happens
outstrng = getinput() # Get dispaly string and parse
wait4switch()
TopLine = True
for lineChars in outstrng:
if TopLine == True:
lcd_byte(0x01, LCD_CMD) # Clear Display
lcd_byte(LCD_LINE_1, LCD_CMD)
lcd_string(lineChars)
TopLine = False
else:
lcd_byte(LCD_LINE_2, LCD_CMD)
lcd_string(lineChars)
TopLine = True
time.sleep(3)
if TopLine == False:
time.sleep(3)
lcd_byte(0x01, LCD_CMD) # Clear Display
GPIO.cleanup()
I apologize that the name of the program in the video (LCD_Module_1,py) does not match the name in the Gist file (LCD_Disploy2Lines.py). Somewhere along the way, after I made the video, I changed the file name to make the name more meaningful, at least to me.
The program does not take into account individual words of more that 16 characters in length.
Notice line 16 of the code. That makes LCD_2X16.py (discussed in the last post) part of this program.

1 comment:

  1. This is most incredibly awesome, thank you very much!

    ReplyDelete