Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/python
- # Sure Electronics' DE-LD011 Driver
- # This is a crude attempt at a "driver" for the
- # Sure Electronics DE-LD011 16x2 Character LCD
- # display. Use it was you will, it is Public Domain
- # Originally written by Chase Phillips, 2/26/2012
- import serial, time
- CMD = chr(254)
- WRITE = chr(71)
- # Backlight hex commands
- BACKLIGHT_OFF = [ chr(254), chr(70) ]
- BACKLIGHT_ON = [ chr(254), chr(66), chr(00) ]
- BACKLIGHT_LOW = [ chr(254), chr(152), chr(37) ]
- BACKLIGHT_MED = [ chr(254), chr(152), chr(80) ]
- BACKLIGHT_HI = [ chr(254), chr(152), chr(153) ]
- # Hex sequence to clear the screens
- CLEARSCREEN = [
- # clear the first line
- chr(254), chr(71), chr(1), chr(1), chr(20), chr(20), chr(20), chr(20), chr(20), chr(20),
- chr(20), chr(20), chr(20), chr(20), chr(20), chr(20), chr(20), chr(20), chr(20), chr(20),
- # clear the second line
- chr(254), chr(71), chr(1), chr(2), chr(20), chr(20), chr(20), chr(20), chr(20), chr(20),
- chr(20), chr(20), chr(20), chr(20), chr(20), chr(20), chr(20), chr(20), chr(20), chr(20)
- ]
- # This is the meat
- class Driver:
- # Assign the COM port when called
- def __init__(self, com):
- try:
- self.s = serial.Serial(com, 9600,
- serial.EIGHTBITS,
- serial.PARITY_NONE,
- serial.STOPBITS_ONE,
- timeout = 5,
- rtscts = False)
- except serial.SerialException:
- return False
- # These are simple backlight controls. They don't
- # take any arguments
- def BacklightOn(self):
- for i in BACKLIGHT_ON:
- self.s.write(i)
- return True
- def BacklightOff(self):
- for i in BACKLIGHT_OFF:
- self.s.write(i)
- return True
- def BacklightLevel(self, val):
- if val > 3:
- print "Value must be between 1 and 3"
- return False
- elif val == 1:
- for i in BACKLIGHT_LOW:
- self.s.write(i)
- return True
- elif val == 2:
- for i in BACKLIGHT_MED:
- self.s.write(i)
- return True
- elif val == 3:
- for i in BACKLIGHT_HI:
- self.s.write(i)
- return True
- # This will clear the entire screen
- def ClearScreen(self):
- for i in CLEARSCREEN:
- self.s.write(i)
- # This writes the string to the unit. If the
- # string is longer than the 16-character limit
- # of the screen, the text will automagically
- # scroll
- #
- # Usage:
- # Driver.WriteLine("String", 1, 1)
- # This will write "String" to the first row
- # starting at the first column
- def WriteLine(self, data, row, col):
- if len(data) > 16:
- ii = 0
- while ii != len(data):
- lst = [CMD, WRITE, chr(col), chr(row)]
- for c in data[ii:ii+16]:
- lst.append(chr(int(c.encode('hex'), 16)))
- if len(lst) < 20:
- while len(lst) < 20:
- lst.append(chr(20))
- for i in lst:
- self.s.write(i)
- # Change this if you want the text to scroll faster
- time.sleep(.3)
- ii = ii + 1
- else:
- lst = [CMD, WRITE, chr(col), chr(row)]
- for c in data:
- lst.append(chr(int(c.encode('hex'), 16)))
- if len(lst) < 20:
- while len(lst) < 20:
- lst.append(chr(20))
- for i in lst:
- self.s.write(i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement