Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- #-*- coding: utf-8 -*-
- import RPi.GPIO as GPIO
- import sys
- from time import sleep
- class textcolors:
- HEADER = '\033[95m'
- OKBLUE = '\033[94m'
- OKGREEN = '\033[92m'
- WARNING = '\033[93m'
- FAIL = '\033[91m'
- END = '\033[0m'
- BOLD = '\033[1m'
- class HD44780:
- def __init__(self, pin_rs=7, pin_e=8, pins_db=[18,23,24,25]):
- self.pin_rs = pin_rs
- self.pin_e = pin_e
- self.pins_db = pins_db
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(self.pin_e, GPIO.OUT)
- GPIO.setup(self.pin_rs, GPIO.OUT)
- for pin in self.pins_db:
- GPIO.setup(pin, GPIO.OUT)
- self.commands = {
- 'cls': 0x01,
- 'home': 0x80,
- 'hide': 0x08,
- 'restore': 0x0C,
- }
- self.clear()
- def clear(self):
- """ Reset LCD """
- self.cmd(0x33)
- self.cmd(0x32)
- self.cmd(0x28)
- self.cmd(0x0C)
- self.cmd(0x06)
- self.cmd(0x01)
- def cmd(self, bits, char_mode=False):
- """ Command to LCD """
- sleep(0.001)
- bits=bin(bits)[2:].zfill(8)
- GPIO.output(self.pin_rs, char_mode)
- for pin in self.pins_db:
- GPIO.output(pin, False)
- for i in range(4):
- if bits[i] == "1":
- GPIO.output(self.pins_db[i], True)
- GPIO.output(self.pin_e, True)
- GPIO.output(self.pin_e, False)
- for pin in self.pins_db:
- GPIO.output(pin, False)
- for i in range(4,8):
- if bits[i] == "1":
- GPIO.output(self.pins_db[i-4], True)
- GPIO.output(self.pin_e, True)
- GPIO.output(self.pin_e, False)
- def msg(self, text):
- """ Send string to LCD """
- for char in text:
- if char == '\n':
- self.cmd(0xC0)
- else:
- self.cmd(ord(char),True)
- def custom(self, bits, position=None):
- """ Save custom character to RAM or show it """
- if position:
- self.cmd(0x40 + (int(position)-1)*8, False)
- for val in bits.replace(",", " ").replace(".", " ").replace(";", " ").split(" "):
- self.cmd(int(val), True)
- self.cmd(0x80)
- print(textcolors.OKBLUE + "Znak został zapisany do pamięci RAM na pozycji:", position, textcolors.END)
- else: self.cmd(int(bits)-1, True)
- def bin(self, bits, char):
- """ Enter binary command """
- char_mode = {"t": True, "f": False}
- self.cmd(int(bits,2), char_mode[char])
- def setpos(self, posy, posx):
- """ Set cursor position """
- newpos = 0x80 + (int(posy)-1)*64 + (int(posx)-1)
- self.cmd(newpos)
- def scroll(self, direction, n=1):
- """ Scroll display (left/right) """
- if direction.lower() == "left":
- for i in range(n):
- self.cmd(0x18)
- elif direction.lower() == "right":
- for i in range(n):
- self.cmd(0x1E)
- else: print(textcolors.WARNING + "Nieprawidłowe argumenty funkcji" + textcolors.END)
- def move(self, direction, n=1):
- """ Move cursor (left/right) """
- if direction.lower() == "left":
- for i in range(n):
- self.cmd(0x10)
- elif direction.lower() == "right":
- for i in range(n):
- self.cmd(0x14)
- else: print(textcolors.WARNING + "Nieprawidłowe argumenty funkcji" + textcolors.END)
- def mode(self, direction, shift):
- """ Set display mode """
- if (direction + shift).lower() == "left on":
- self.cmd(0x05)
- elif (direction + shift).lower() == "left off":
- self.cmd(0x04)
- elif (direction + shift).lower() == "right on":
- self.cmd(0x07)
- elif (direction + shift).lower() == "right off":
- self.cmd(0x06)
- def cursor(self, mode):
- """ Change cursor blink/underline/invisible """
- if mode == "blink": self.cmd(0x0F)
- elif mode == "underline": self.cmd(0x0E)
- elif mode == "hide": self.cmd(0x0C)
- def input(self, text):
- try:
- if len(text.split()) > 1:
- func, *args = text.split()
- if func == "msg": getattr(self, func)(text[4:])
- else: getattr(self, func)(*args)
- else: self.cmd(self.commands[text])
- except Exception as e:
- print(e)
- #print(textcolors.FAIL + textcolors.BOLD + "Błąd: Nie udało się wykonać polecenia" + textcolors.END)
- if __name__ == "__main__":
- lcd = HD44780()
- while True:
- command = input(">>> ")
- lcd.input(command)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement