Created
January 12, 2013 06:08
-
-
Save pocketkk/4516377 to your computer and use it in GitHub Desktop.
Ruby Library for Parallax 16 x 2 Serial LCD for Raspberry Pi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# LIBRARY FOR CONTROL PARALLAX 16x2 SERIAL LCD DISPLAY ON A RASBERRY PI | |
# | |
# Wiring Diagram | |
# PI LCD | |
# **************************** | |
# pin 8 (TX) -------> RX | |
# pin 6 (GND) -------> GND | |
# pin 2 (5V) -------> 5V | |
# | |
# | |
# Datasheet available at: | |
# http://www.parallax.com/dl/docs/prod/audiovis/seriallcd-reve.pdf | |
# | |
# | |
# | |
# | |
# | |
# | |
# ********************************************************************* | |
# Example | |
# ********************************************************************* | |
# require '/home/pi/lcd' | |
# | |
# lcd = Display.new('/dev/ttyAMA0', 9600) | |
# lcd.clear #<-----turn on backlight and clear any text | |
# | |
# # lcd.scroll(text, line and direction option, speed) | |
# # text variable accepts any string | |
# # line and direction options 1 - Right to left row 1 | |
# # 2 - Right to left row 2 | |
# # 3 - Left to right row 1 | |
# # 4 - Left to right row 2 | |
# # speed - supplied to delay, smaller number the faster scroll. | |
# # Best results between 0.15 to 0.25 | |
# lcd.scroll("Your text goes here", 1, 0.2) | |
require 'serialport' | |
class Display | |
BLINK = 24.chr | |
OFF = 18.chr | |
ON = 17.chr | |
FRAME_OFF = 21.chr | |
FRAME_ON = 22.chr | |
NEWLINE = 13.chr | |
NOTE = 220.chr | |
PLAY = 212.chr | |
CLEAR = 12.chr | |
CURSOR = 22.chr | |
def initialize (serial_path, baudrate) | |
serial_path, baudrate = serial_path.to_s, baudrate.to_i | |
#OPEN UP SERIAL PORT | |
@display = SerialPort.new serial_path, baudrate | |
#TURN OFF BLINKING CURSOR | |
@display.write(BLINK) | |
#TURN OFF CURSOR | |
@display.write(CURSOR) | |
#TURN ON BACKLIGHT | |
@display.write(ON) | |
end | |
def clear | |
@display.write(CLEAR) | |
end | |
def frame_off | |
@display.write(FRAME_OFF) | |
end | |
def frame_on | |
@display.write(FRAME_ON) | |
end | |
def off | |
self.clear | |
self.scroll("Turning off display...", 1) | |
sleep(2) | |
@display.write(CLEAR) | |
@display.write(OFF) | |
end | |
def print(text) | |
text=text.to_s | |
@display.write(text) | |
end | |
def posCursor(pos) | |
pos=pos.chr | |
@display.write(pos) | |
end | |
def wipe(top_bottom, speed) | |
self.scroll(" ", top_bottom, speed) | |
end | |
def scroll(text, top_bottom, speed) | |
top_RtoL=[143,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128] | |
top_LtoR=[128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143] | |
bottom_RtoL=[163,162,161,160,159,158,157,156,155,154,153,152,151,150,149,148] | |
bottom_LtoR=[148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163] | |
width=15 #WIDTH OF SCREEN STARTING FROM ZERO | |
head = 0 | |
case top_bottom | |
when 1 | |
lcd_array = top_RtoL | |
when 2 | |
lcd_array = bottom_RtoL | |
when 3 | |
lcd_array = top_LtoR | |
when 4 | |
lcd_array = bottom_LtoR | |
end | |
# RIGHT TO LEFT SCROLLING | |
if top_bottom == 1 || top_bottom == 2 | |
text=text.to_s << " " | |
x=0 | |
if text.length<16 | |
text_with_padding=text | |
#ADD PADDING TO TEXT TO FILL SCREEN | |
while x<17-text_with_padding.length | |
text_with_padding << " " | |
end | |
length = text_with_padding.length | |
else | |
text_with_padding=text | |
length=text.length | |
end | |
#SCROLL ALL THE WAY THROUGH THE TEXT | |
while head < length-1 | |
if head >=15 | |
pos=15 | |
else | |
pos=head | |
end | |
self.posCursor(lcd_array[pos]) | |
if head > width | |
start_pos=head-pos | |
else | |
start_pos=0 | |
end | |
if head>=width | |
chars_to_display=width+1 | |
else | |
chars_to_display=head+1 | |
end | |
@display.write(text_with_padding[start_pos,chars_to_display]) | |
sleep(speed) | |
head += 1 | |
end | |
#RETURN TEXT TO THE BEGINNING | |
end | |
#LEFT TO RIGHT SCROLLING | |
if top_bottom == 3 || top_bottom == 4 | |
text=" " << text.to_s << " " | |
length=text.length | |
while head < length | |
self.posCursor(lcd_array[0]) | |
if head>=width | |
chars_to_display=width | |
else | |
chars_to_display=head | |
end | |
@display.write(text[length-head,chars_to_display+1]) | |
sleep(speed) | |
head += 1 | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment