Created
January 23, 2012 08:00
-
-
Save omsai/1661538 to your computer and use it in GitHub Desktop.
Sparkfun LCD control
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
''' | |
Serial LCD control | |
Control Sparkfun LCD-10097 with DEV-09716 | |
''' | |
from serial import Serial | |
from time import sleep | |
# For binary commands | |
from struct import pack | |
class SerialLCD: | |
'''Serial LCD communication''' | |
def __init__(self, port='COM2', baud=9600, debug=False): | |
try: | |
self.ser = \ | |
Serial( | |
port = port, | |
baudrate = baud, | |
bytesize = 8, | |
parity='N', stopbits=1, timeout=1 | |
) | |
if debug and self.ser.isOpen(): | |
print 'Successfully opened', self.port | |
elif debug: | |
print 'Failed to open', self.port | |
return | |
except: | |
self.ser.close() # since an exception leaves the COM port open | |
raise | |
def special_command(self, command): | |
format = 'BB' | |
special_command_byte = 0xFE | |
byte = pack(format, special_command_byte, command) | |
self.write(byte) | |
def write(self, command, wait=0, debug=False): | |
if debug: | |
print 'Writing', command | |
self.ser.write(command) | |
if wait is not 0: | |
sleep(wait) | |
def clear(self): | |
self.special_command(0x01) | |
if __name__ == '__main__': | |
s = SerialLCD() | |
while 1: | |
s.clear() | |
s.write('Hello', 1) | |
s.clear() | |
s.write('World', 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment