Advertisement
KsaneK

RaspberryPi LCD display

Dec 6th, 2014
1,180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.91 KB | None | 0 0
  1. #!/usr/bin/python
  2. #-*- coding: utf-8 -*-
  3.  
  4. import RPi.GPIO as GPIO
  5. import sys
  6. from time import sleep
  7.  
  8. class textcolors:
  9.     HEADER = '\033[95m'
  10.     OKBLUE = '\033[94m'
  11.     OKGREEN = '\033[92m'
  12.     WARNING = '\033[93m'
  13.     FAIL = '\033[91m'
  14.     END = '\033[0m'
  15.     BOLD = "\033[1m"
  16.  
  17. class HD44780:
  18.  
  19.     def __init__(self, pin_rs=7, pin_e=8, pins_db=[25, 24, 23, 18]):
  20.        
  21.         self.pin_rs = pin_rs
  22.         self.pin_e = pin_e
  23.         self.pins_db = pins_db
  24.  
  25.         GPIO.setmode(GPIO.BCM)
  26.         GPIO.setwarnings(False)
  27.         GPIO.setup(self.pin_e, GPIO.OUT)
  28.         GPIO.setup(self.pin_rs, GPIO.OUT)
  29.         for pin in self.pins_db:
  30.             GPIO.setup(pin, GPIO.OUT)
  31.  
  32.         self.commands = {
  33.             'cls': 0x01,
  34.             'home': 0x80,
  35.             'hide': 0x08,
  36.             'restore': 0x0C,
  37.         }
  38.  
  39.         self.clear()
  40.  
  41.     def clear(self):
  42.         """ Reset LCD """
  43.         self.cmd(0x33)
  44.         self.cmd(0x32)
  45.         self.cmd(0x28)
  46.         self.cmd(0x0C)
  47.         self.cmd(0x06)
  48.         self.cmd(0x01)
  49.        
  50.     def cmd(self, bits, char_mode=False):
  51.         """ Command to LCD """
  52.  
  53.         sleep(0.001)
  54.         bits=bin(bits)[2:].zfill(8)
  55.         GPIO.output(self.pin_rs, char_mode)
  56.  
  57.         for pin in self.pins_db:
  58.             GPIO.output(pin, False)
  59.  
  60.         for i in range(4):
  61.             if bits[i] == "1":
  62.                 GPIO.output(self.pins_db[::-1][i], True)
  63.  
  64.         GPIO.output(self.pin_e, True)
  65.         GPIO.output(self.pin_e, False)
  66.  
  67.         for pin in self.pins_db:
  68.             GPIO.output(pin, False)
  69.  
  70.         for i in range(4,8):
  71.             if bits[i] == "1":
  72.                 GPIO.output(self.pins_db[::-1][i-4], True)
  73.  
  74.         GPIO.output(self.pin_e, True)
  75.         GPIO.output(self.pin_e, False)
  76.        
  77.     def msg(self, text):
  78.         """ Send string to LCD """
  79.         for char in " ".join(text):
  80.             if char == '\n':
  81.                 self.cmd(0xC0)
  82.             else:
  83.                 self.cmd(ord(char),True)
  84.  
  85.     def custom(self, args):
  86.         """ Save custom character to RAM or show it """
  87.         if len(args) > 1:
  88.             self.cmd(0x40 + int(args[1])*8, False)
  89.             for val in args[0].replace(",", " ").replace(".", " ").replace(";", " ").split(" "):
  90.                 self.cmd(int(val), True)
  91.             self.cmd(0x80)
  92.             print textcolors.OKBLUE + "Znak został zapisany do pamięci RAM na pozycji:", args[1], textcolors.END
  93.         else: self.cmd(int(args[0]), True)
  94.  
  95.     def bin(self, args):
  96.         """ Enter binary command """
  97.         char_mode = {"t": True, "f": False}
  98.         self.cmd(int(args[0],2), char_mode[args[1]])
  99.  
  100.     def setpos(self, position):
  101.         """ Set cursor position """
  102.         newpos = 0x80 + (int(position[0])-1)*64 + (int(position[1])-1)
  103.         self.cmd(newpos)
  104.  
  105.     def scroll(self, args):
  106.         """ Scroll display (left/right) """
  107.         try: n = int(args[1])
  108.         except: n = 1
  109.         if args[0].lower() == "left":
  110.             for i in range(n):
  111.                 self.cmd(0x18)
  112.         elif args[0].lower() == "right":
  113.             for i in range(n):
  114.                 self.cmd(0x1E)
  115.         else: print textcolors.WARNING + "Nieprawidłowe argumenty funkcji" + textcolors.END
  116.  
  117.     def move(self, args):
  118.         """ Move cursor (left/right) """
  119.         try: n = int(args[1])
  120.         except: n = 1
  121.         if args[0].lower() == "left":
  122.             for i in range(n):
  123.                 self.cmd(0x10)
  124.         elif args[0].lower() == "right":
  125.             for i in range(n):
  126.                 self.cmd(0x14)
  127.         else: print textcolors.WARNING + "Nieprawidłowe argumenty funkcji" + textcolors.END
  128.  
  129.     def mode(self, args):
  130.         """ Set display mode """
  131.         if " ".join(args) == "left on":
  132.             self.cmd(0x05)
  133.         elif " ".join(args) == "left off":
  134.             self.cmd(0x04)
  135.         elif " ".join(args) == "right on":
  136.             self.cmd(0x07)
  137.         elif " ".join(args) == "right off":
  138.             self.cmd(0x06)
  139.  
  140.     def cursor(self, mode):
  141.         """ Change cursor blink/underline/invisible """
  142.         if mode[0] == "blink": self.cmd(0x0F)
  143.         elif mode[0] == "underline": self.cmd(0x0E)
  144.         elif mode[0] == "invisible": self.cmd(0x0C)
  145.  
  146.     def input(self, text):
  147.         try:
  148.             if len(text.split()) > 1:
  149.                 func, args = text.split()[0], text.split()[1:]
  150.                 getattr(self, func)(args)
  151.             else: self.cmd(self.commands[text])
  152.         except:
  153.             print textcolors.FAIL + textcolors.BOLD + "Błąd: Nie udało się wykonać polecenia" + textcolors.END
  154.  
  155. if __name__ == "__main__":
  156.     lcd = HD44780()
  157.     while True:
  158.         command = raw_input(">>> ")
  159.         lcd.input(command)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement