# This Program is Free Software
# (c) Patrik Nilsson 2005 (blambi@telia.com)
# Licensed under the GNU GPL v2

import serial
from time import sleep

# init
ser = serial.Serial( # as read from the manual
	port='/dev/ttyS0',
	baudrate=19200,
	bytesize=serial.EIGHTBITS,
	parity=serial.PARITY_NONE,
	stopbits=serial.STOPBITS_ONE,
)
clear = chr(0) + chr(160) + chr(0) + chr(167)

# a needed delay (was needed ;) )
def lcddelay():
	sleep(0.08)

# clear screen
def lcdclear(fade=0):
	#ser.write(clear)
	#lcddelay()
	#ser.write(clear2)
	# replacement hack (don't know howto return else)
	for y in range(0,2):
		for x in range(0,20):
			lcdwrite(' ',x,y)
			if fade != 0: # just for pritty output :)
				lcddelay()

def lcdwrite(char,x,y):
	"""write a char to the display! (x = 0 - 19, y = 0 - 1)"""
	ser.write(chr(0))
	ser.write(chr(161+y))
	ser.write(chr(x))
	ser.write(chr(167))
	ser.write(char)

def lcdstrwrite(string,x,y):
	"""Write's a string instead of a chr"""	
	for xn in range(0,len(string)):
		curchr = string[xn]
		lcdwrite(curchr,x+xn,y)
	
	
lcdclear(0)
lcdstrwrite("Free Software for",1,0)
lcdstrwrite("The L.I.S LCD",3,1)
ser.close()

