Guest User

Sure Electronics DE-LD011 Python "driver"

a guest
Feb 26th, 2012
240
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/local/bin/python
  2.  
  3.  
  4. # Sure Electronics' DE-LD011 Driver
  5.  
  6. # This is a crude attempt at a "driver" for the
  7. # Sure Electronics DE-LD011 16x2 Character LCD
  8. # display. Use it was you will, it is Public Domain
  9.  
  10. # Originally written by Chase Phillips, 2/26/2012
  11.  
  12.  
  13. import serial, time
  14.  
  15. CMD = chr(254)
  16. WRITE = chr(71)
  17.  
  18.  
  19. # Backlight hex commands
  20. BACKLIGHT_OFF = [ chr(254), chr(70) ]
  21. BACKLIGHT_ON = [ chr(254), chr(66), chr(00) ]
  22. BACKLIGHT_LOW = [ chr(254), chr(152), chr(37) ]
  23. BACKLIGHT_MED = [ chr(254), chr(152), chr(80) ]
  24. BACKLIGHT_HI = [ chr(254), chr(152), chr(153) ]
  25.  
  26. # Hex sequence to clear the screens
  27. CLEARSCREEN = [
  28.                 # clear the first line
  29.                 chr(254), chr(71), chr(1), chr(1), chr(20), chr(20), chr(20), chr(20), chr(20), chr(20),
  30.                 chr(20), chr(20), chr(20), chr(20), chr(20), chr(20), chr(20), chr(20), chr(20), chr(20),
  31.  
  32.                 # clear the second line
  33.                 chr(254), chr(71), chr(1), chr(2), chr(20), chr(20), chr(20), chr(20), chr(20), chr(20),
  34.                 chr(20), chr(20), chr(20), chr(20), chr(20), chr(20), chr(20), chr(20), chr(20), chr(20)
  35.               ]
  36.  
  37.  
  38. # This is the meat
  39. class Driver:
  40.  
  41.     # Assign the COM port when called
  42.     def __init__(self, com):
  43.         try:
  44.             self.s = serial.Serial(com, 9600,
  45.                                    serial.EIGHTBITS,
  46.                                    serial.PARITY_NONE,
  47.                                    serial.STOPBITS_ONE,
  48.                                    timeout = 5,
  49.                                    rtscts = False)
  50.            
  51.         except serial.SerialException:
  52.             return False
  53.          
  54.  
  55.     # These are simple backlight controls. They don't
  56.     # take any arguments
  57.     def BacklightOn(self):
  58.         for i in BACKLIGHT_ON:
  59.             self.s.write(i)
  60.  
  61.         return True
  62.  
  63.  
  64.     def BacklightOff(self):
  65.         for i in BACKLIGHT_OFF:
  66.             self.s.write(i)
  67.  
  68.         return True
  69.            
  70.  
  71.     def BacklightLevel(self, val):
  72.         if val > 3:
  73.             print "Value must be between 1 and 3"
  74.  
  75.             return False
  76.         elif val ==  1:
  77.             for i in BACKLIGHT_LOW:
  78.                 self.s.write(i)
  79.  
  80.                 return True
  81.  
  82.         elif val == 2:
  83.             for i in BACKLIGHT_MED:
  84.                 self.s.write(i)
  85.  
  86.                 return True
  87.  
  88.         elif val == 3:
  89.             for i in BACKLIGHT_HI:
  90.                 self.s.write(i)
  91.  
  92.                 return True
  93.  
  94.  
  95.     # This will clear the entire screen
  96.     def ClearScreen(self):
  97.         for i in CLEARSCREEN:
  98.             self.s.write(i)
  99.  
  100.  
  101.  
  102.     # This writes the string to the unit. If the
  103.     # string is longer than the 16-character limit
  104.     # of the screen, the text will automagically
  105.     # scroll
  106.     #
  107.     # Usage:
  108.     # Driver.WriteLine("String", 1, 1)
  109.     # This will write "String" to the first row
  110.     # starting at the first column
  111.     def WriteLine(self, data, row, col):
  112.         if len(data) > 16:
  113.             ii = 0
  114.  
  115.             while ii != len(data):
  116.                 lst = [CMD, WRITE, chr(col), chr(row)]
  117.  
  118.                 for c in data[ii:ii+16]:
  119.                     lst.append(chr(int(c.encode('hex'), 16)))
  120.  
  121.                 if len(lst) < 20:
  122.                     while len(lst) < 20:
  123.                         lst.append(chr(20))
  124.  
  125.                 for i in lst:
  126.                     self.s.write(i)
  127.  
  128.                 # Change this if you want the text to scroll faster
  129.                 time.sleep(.3)
  130.                
  131.                 ii = ii + 1
  132.                
  133.         else:
  134.             lst = [CMD, WRITE, chr(col), chr(row)]
  135.            
  136.             for c in data:
  137.                 lst.append(chr(int(c.encode('hex'), 16)))
  138.  
  139.             if len(lst) < 20:
  140.                 while len(lst) < 20:
  141.                     lst.append(chr(20))
  142.  
  143.             for i in lst:
  144.                 self.s.write(i)
RAW Paste Data