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=[25, 24, 23, 18]):
- 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[::-1][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[::-1][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 " ".join(text):
- if char == '\n':
- self.cmd(0xC0)
- else:
- self.cmd(ord(char),True)
- def custom(self, args):
- """ Save custom character to RAM or show it """
- if len(args) > 1:
- self.cmd(0x40 + int(args[1])*8, False)
- for val in args[0].replace(",", " ").replace(".", " ").replace(";", " ").split(" "):
- self.cmd(int(val), True)
- self.cmd(0x80)
- print textcolors.OKBLUE + "Znak został zapisany do pamięci RAM na pozycji:", args[1], textcolors.END
- else: self.cmd(int(args[0]), True)
- def bin(self, args):
- """ Enter binary command """
- char_mode = {"t": True, "f": False}
- self.cmd(int(args[0],2), char_mode[args[1]])
- def setpos(self, position):
- """ Set cursor position """
- newpos = 0x80 + (int(position[0])-1)*64 + (int(position[1])-1)
- self.cmd(newpos)
- def scroll(self, args):
- """ Scroll display (left/right) """
- try: n = int(args[1])
- except: n = 1
- if args[0].lower() == "left":
- for i in range(n):
- self.cmd(0x18)
- elif args[0].lower() == "right":
- for i in range(n):
- self.cmd(0x1E)
- else: print textcolors.WARNING + "Nieprawidłowe argumenty funkcji" + textcolors.END
- def move(self, args):
- """ Move cursor (left/right) """
- try: n = int(args[1])
- except: n = 1
- if args[0].lower() == "left":
- for i in range(n):
- self.cmd(0x10)
- elif args[0].lower() == "right":
- for i in range(n):
- self.cmd(0x14)
- else: print textcolors.WARNING + "Nieprawidłowe argumenty funkcji" + textcolors.END
- def mode(self, args):
- """ Set display mode """
- if " ".join(args) == "left on":
- self.cmd(0x05)
- elif " ".join(args) == "left off":
- self.cmd(0x04)
- elif " ".join(args) == "right on":
- self.cmd(0x07)
- elif " ".join(args) == "right off":
- self.cmd(0x06)
- def cursor(self, mode):
- """ Change cursor blink/underline/invisible """
- if mode[0] == "blink": self.cmd(0x0F)
- elif mode[0] == "underline": self.cmd(0x0E)
- elif mode[0] == "invisible": self.cmd(0x0C)
- def input(self, text):
- try:
- if len(text.split()) > 1:
- func, args = text.split()[0], text.split()[1:]
- getattr(self, func)(args)
- else: self.cmd(self.commands[text])
- except:
- print textcolors.FAIL + textcolors.BOLD + "Błąd: Nie udało się wykonać polecenia" + textcolors.END
- if __name__ == "__main__":
- lcd = HD44780()
- while True:
- command = raw_input(">>> ")
- lcd.input(command)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement