Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import RPi.GPIO as GPIO
- from time import sleep, strftime, localtime
- class HD44780:
- def __init__(self):
- # Deklaracja zmiennych. Numeracja pinow wedlug BCM
- self.LCD_RS = 7
- self.LCD_E = 8
- self.LCD_D4 = 25
- self.LCD_D5 = 24
- self.LCD_D6 = 23
- self.LCD_D7 = 2
- # Inicjalizacja pinow GPIO
- GPIO.setwarnings(False)
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(self.LCD_E, GPIO.OUT) # E
- GPIO.setup(self.LCD_RS, GPIO.OUT) # RS
- GPIO.setup(self.LCD_D4, GPIO.OUT) # DB4
- GPIO.setup(self.LCD_D5, GPIO.OUT) # DB5
- GPIO.setup(self.LCD_D6, GPIO.OUT) # DB6
- GPIO.setup(self.LCD_D7, GPIO.OUT) # DB7
- # Zresetowanie pinow
- GPIO.output(self.LCD_E, False)
- GPIO.output(self.LCD_RS, False)
- GPIO.output(self.LCD_D4, False)
- GPIO.output(self.LCD_D5, False)
- GPIO.output(self.LCD_D6, False)
- GPIO.output(self.LCD_D7, False)
- def init(self):
- # Przygotowanie ekranu
- self._write(0x33)
- self._write(0x32)
- self._write(0x28) # Przesylanie danych w 4 bitach, 2 linie, 5x7 pikseli
- self._write(0x0C) # Nie wyswietlaj kursora
- self._write(0x06) # Tryb wpisywania (Normalny, od lewej do prawej)
- self._write(0x01) # Wyczyszczenie znakow
- # Pozycje wlasnych znakow
- self.LCD_TAB_CHARS = [0x40,0x48,0x50,0x58,0x60,0x68,0x70,0x78]
- self.customChar(0,[0x00, 0x00, 0x04, 0x0E, 0x1F, 0x00, 0x00, 0x00]) # ARROW UP
- self.customChar(1,[0x00, 0x00, 0x00, 0x1F, 0x0E, 0x04, 0x00, 0x00]) # ARROW DOWN
- self.customChar(2,[0x1F, 0x1F, 0x1B, 0x15, 0x0E, 0x1F, 0x1F, 0x1F]) # ARROW UP NEG
- self.customChar(3,[0x1F, 0x1F, 0x1F, 0x0E, 0x15, 0x1B, 0x1F, 0x1F]) # ARROW DOWN NEG
- def _write(self, bits, char_mode=False):
- bits=bin(bits)[2:].zfill(8)
- # Tryb wpisywania komend/znakow
- GPIO.output(self.LCD_E, False)
- GPIO.output(self.LCD_RS, char_mode)
- # Wpisanie pierwszych 4 bitow
- GPIO.output(self.LCD_D4, int(bits[3]))
- GPIO.output(self.LCD_D5, int(bits[2]))
- GPIO.output(self.LCD_D6, int(bits[1]))
- GPIO.output(self.LCD_D7, int(bits[0]))
- # Odebranie ich przez LCD
- GPIO.output(self.LCD_E, True)
- sleep(0.00000045)
- GPIO.output(self.LCD_E, False)
- # Odczekanie wymaganego czasu
- if char_mode:
- sleep(0.0002)
- else:
- sleep(0.005)
- # Wpisanie kolejnych 4 bitow
- GPIO.output(self.LCD_D4, int(bits[7]))
- GPIO.output(self.LCD_D5, int(bits[6]))
- GPIO.output(self.LCD_D6, int(bits[5]))
- GPIO.output(self.LCD_D7, int(bits[4]))
- # Odebranie ich przez LCD
- GPIO.output(self.LCD_E, True)
- sleep(0.00000045)
- GPIO.output(self.LCD_E, False)
- # Odczekanie wymaganego czasu
- if char_mode:
- sleep(0.0002)
- else:
- sleep(0.005)
- def customChar(self,pos,char):
- self._write(self.LCD_TAB_CHARS[pos])
- for i in char:
- self._write(i,True)
- def writeChar(self, char):
- self._write(ord(char),True)
- def writeMsg(self, text, time=0):
- self.home()
- for char in text:
- if char == '\n': # NEW LINE
- self._write(0xC0)
- elif char == '\a': # ARROW UP
- sleep(time)
- self.writeChar(chr(0))
- elif char == '\b': # ARROW DOWN
- sleep(time)
- self.writeChar(chr(1))
- elif char == '\f': # ARROW UP NEG
- sleep(time)
- self.writeChar(chr(2))
- elif char == '\t': # ARROW DOWN NEG
- sleep(time)
- self.writeChar(chr(3))
- else:
- sleep(time)
- self.writeChar(char)
- def setChar(self,line,column,newchar):
- self._write(0x02) # Powrot kursora na poczatek
- for index in range(line-1):
- self._write(0xC0)
- for index in range(column-1):
- self._write(0x14)
- self.writeChar(newchar)
- def test(self):
- self.writeMsg("Hello! :)",0.25)
- sleep(2)
- for index in range(5):
- self.writeMsg("Local time:\n"+strftime("%H:%M:%S", localtime()))
- sleep(1)
- self.writeMsg("Today's date:\n"+strftime("%Y-%m-%d", localtime()))
- sleep(2)
- cpu_temp = open( "/sys/class/thermal/thermal_zone0/temp" ).read()
- self.clear()
- self.writeMsg("CPU Temp:\n"+str(float(cpu_temp)/1000)+"C")
- def clear(self): # Clear display
- self._write(0x01)
- def scrollRight(self): # Move all characters right
- self._write(0x1E)
- def scrollLeft(self): # Move all characters left
- self._write(0x18)
- def blank(self): # Hide all characters
- self._write(0x08)
- def restore(self): # Show all characters
- self._write(0x0C)
- def home(self): # Move cursor to left, top corner
- self._write(0x02)
- def cursorRight(self): # Move cursor one place right
- self._write(0x02)
- def cursorLeft(self): # Move cursor one place left
- self._write(0x02)
- def cursorDown(self): # Move cursor one line down
- self._write(0x02)
- def cleanup(self): # Clear LCD and restore GPIO to default
- self.clear()
- GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement