Advertisement
Skew

HD44780

Jul 2nd, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.62 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import RPi.GPIO as GPIO
  4. from time import sleep, strftime, localtime
  5.  
  6. class HD44780:
  7.     def __init__(self):
  8.         # Deklaracja zmiennych. Numeracja pinow wedlug BCM
  9.         self.LCD_RS = 7
  10.         self.LCD_E  = 8
  11.         self.LCD_D4 = 25
  12.         self.LCD_D5 = 24
  13.         self.LCD_D6 = 23
  14.         self.LCD_D7 = 2
  15.  
  16.         # Inicjalizacja pinow GPIO
  17.         GPIO.setwarnings(False)
  18.         GPIO.setmode(GPIO.BCM)
  19.         GPIO.setup(self.LCD_E, GPIO.OUT)  # E
  20.         GPIO.setup(self.LCD_RS, GPIO.OUT) # RS
  21.         GPIO.setup(self.LCD_D4, GPIO.OUT) # DB4
  22.         GPIO.setup(self.LCD_D5, GPIO.OUT) # DB5
  23.         GPIO.setup(self.LCD_D6, GPIO.OUT) # DB6
  24.         GPIO.setup(self.LCD_D7, GPIO.OUT) # DB7
  25.  
  26.         # Zresetowanie pinow
  27.         GPIO.output(self.LCD_E, False)
  28.         GPIO.output(self.LCD_RS, False)
  29.         GPIO.output(self.LCD_D4, False)
  30.         GPIO.output(self.LCD_D5, False)
  31.         GPIO.output(self.LCD_D6, False)
  32.         GPIO.output(self.LCD_D7, False)
  33.  
  34.     def init(self):
  35.         # Przygotowanie ekranu
  36.         self._write(0x33)
  37.         self._write(0x32)
  38.         self._write(0x28) # Przesylanie danych w 4 bitach, 2 linie, 5x7 pikseli
  39.         self._write(0x0C) # Nie wyswietlaj kursora
  40.         self._write(0x06) # Tryb wpisywania (Normalny, od lewej do prawej)
  41.         self._write(0x01) # Wyczyszczenie znakow
  42.  
  43.         # Pozycje wlasnych znakow
  44.         self.LCD_TAB_CHARS = [0x40,0x48,0x50,0x58,0x60,0x68,0x70,0x78]
  45.  
  46.         self.customChar(0,[0x00, 0x00, 0x04, 0x0E, 0x1F, 0x00, 0x00, 0x00]) # ARROW UP
  47.         self.customChar(1,[0x00, 0x00, 0x00, 0x1F, 0x0E, 0x04, 0x00, 0x00]) # ARROW DOWN
  48.         self.customChar(2,[0x1F, 0x1F, 0x1B, 0x15, 0x0E, 0x1F, 0x1F, 0x1F]) # ARROW UP NEG
  49.         self.customChar(3,[0x1F, 0x1F, 0x1F, 0x0E, 0x15, 0x1B, 0x1F, 0x1F]) # ARROW DOWN NEG
  50.     def _write(self, bits, char_mode=False):
  51.         bits=bin(bits)[2:].zfill(8)
  52.  
  53.         # Tryb wpisywania komend/znakow
  54.         GPIO.output(self.LCD_E, False)
  55.         GPIO.output(self.LCD_RS, char_mode)
  56.  
  57.         # Wpisanie pierwszych 4 bitow
  58.         GPIO.output(self.LCD_D4, int(bits[3]))
  59.         GPIO.output(self.LCD_D5, int(bits[2]))
  60.         GPIO.output(self.LCD_D6, int(bits[1]))
  61.         GPIO.output(self.LCD_D7, int(bits[0]))
  62.         # Odebranie ich przez LCD
  63.         GPIO.output(self.LCD_E, True)
  64.         sleep(0.00000045)
  65.         GPIO.output(self.LCD_E, False)
  66.  
  67.         # Odczekanie wymaganego czasu
  68.         if char_mode:
  69.             sleep(0.0002)
  70.         else:
  71.             sleep(0.005)
  72.  
  73.         # Wpisanie kolejnych 4 bitow
  74.         GPIO.output(self.LCD_D4, int(bits[7]))
  75.         GPIO.output(self.LCD_D5, int(bits[6]))
  76.         GPIO.output(self.LCD_D6, int(bits[5]))
  77.         GPIO.output(self.LCD_D7, int(bits[4]))
  78.         # Odebranie ich przez LCD
  79.         GPIO.output(self.LCD_E, True)
  80.         sleep(0.00000045)
  81.         GPIO.output(self.LCD_E, False)
  82.  
  83.         # Odczekanie wymaganego czasu
  84.         if char_mode:
  85.             sleep(0.0002)
  86.         else:
  87.             sleep(0.005)
  88.     def customChar(self,pos,char):
  89.         self._write(self.LCD_TAB_CHARS[pos])
  90.         for i in char:
  91.             self._write(i,True)
  92.     def writeChar(self, char):
  93.         self._write(ord(char),True)
  94.     def writeMsg(self, text, time=0):
  95.         self.home()
  96.         for char in text:
  97.             if char == '\n': # NEW LINE
  98.                 self._write(0xC0)
  99.             elif char == '\a': # ARROW UP
  100.                 sleep(time)
  101.                 self.writeChar(chr(0))
  102.             elif char == '\b': # ARROW DOWN
  103.                 sleep(time)
  104.                 self.writeChar(chr(1))
  105.             elif char == '\f': # ARROW UP NEG
  106.                 sleep(time)
  107.                 self.writeChar(chr(2))
  108.             elif char == '\t': # ARROW DOWN NEG
  109.                 sleep(time)
  110.                 self.writeChar(chr(3))
  111.             else:
  112.                 sleep(time)
  113.                 self.writeChar(char)
  114.     def setChar(self,line,column,newchar):
  115.         self._write(0x02) # Powrot kursora na poczatek
  116.         for index in range(line-1):
  117.             self._write(0xC0)
  118.         for index in range(column-1):
  119.             self._write(0x14)
  120.         self.writeChar(newchar)
  121.  
  122.     def test(self):
  123.         self.writeMsg("Hello! :)",0.25)
  124.         sleep(2)
  125.         for index in range(5):
  126.             self.writeMsg("Local time:\n"+strftime("%H:%M:%S", localtime()))
  127.             sleep(1)
  128.         self.writeMsg("Today's date:\n"+strftime("%Y-%m-%d", localtime()))
  129.         sleep(2)
  130.         cpu_temp = open( "/sys/class/thermal/thermal_zone0/temp" ).read()
  131.         self.clear()
  132.         self.writeMsg("CPU Temp:\n"+str(float(cpu_temp)/1000)+"C")
  133.        
  134.  
  135.     def clear(self):        # Clear display
  136.         self._write(0x01)
  137.     def scrollRight(self):  # Move all characters right
  138.         self._write(0x1E)
  139.     def scrollLeft(self):   # Move all characters left
  140.         self._write(0x18)
  141.     def blank(self):        # Hide all characters
  142.         self._write(0x08)
  143.     def restore(self):      # Show all characters
  144.         self._write(0x0C)
  145.  
  146.     def home(self):         # Move cursor to left, top corner
  147.         self._write(0x02)
  148.     def cursorRight(self):  # Move cursor one place right
  149.         self._write(0x02)
  150.     def cursorLeft(self):   # Move cursor one place left
  151.         self._write(0x02)
  152.     def cursorDown(self):   # Move cursor one line down
  153.         self._write(0x02)
  154.  
  155.     def cleanup(self):      # Clear LCD and restore GPIO to default
  156.         self.clear()
  157.         GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement