Skip to content

Instantly share code, notes, and snippets.

@omsai
Created January 23, 2012 08:00
Show Gist options
  • Save omsai/1661538 to your computer and use it in GitHub Desktop.
Save omsai/1661538 to your computer and use it in GitHub Desktop.
Sparkfun LCD control
# -*- 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