Advertisement
KsaneK

lcd.py

Dec 13th, 2014
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.95 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=[18,23,24,25]):
  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[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[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 text:
  80.             if char == '\n':
  81.                 self.cmd(0xC0)
  82.             else:
  83.                 self.cmd(ord(char),True)
  84.  
  85.     def custom(self, bits, position=None):
  86.         """ Save custom character to RAM or show it """
  87.         if position:
  88.             self.cmd(0x40 + (int(position)-1)*8, False)
  89.             for val in bits.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:", position, textcolors.END)
  93.         else: self.cmd(int(bits)-1, True)
  94.  
  95.     def bin(self, bits, char):
  96.         """ Enter binary command """
  97.         char_mode = {"t": True, "f": False}
  98.         self.cmd(int(bits,2), char_mode[char])
  99.  
  100.     def setpos(self, posy, posx):
  101.         """ Set cursor position """
  102.         newpos = 0x80 + (int(posy)-1)*64 + (int(posx)-1)
  103.         self.cmd(newpos)
  104.  
  105.     def scroll(self, direction, n=1):
  106.         """ Scroll display (left/right) """
  107.         if direction.lower() == "left":
  108.             for i in range(n):
  109.                 self.cmd(0x18)
  110.         elif direction.lower() == "right":
  111.             for i in range(n):
  112.                 self.cmd(0x1E)
  113.         else: print(textcolors.WARNING + "Nieprawidłowe argumenty funkcji" + textcolors.END)
  114.  
  115.     def move(self, direction, n=1):
  116.         """ Move cursor (left/right) """
  117.         if direction.lower() == "left":
  118.             for i in range(n):
  119.                 self.cmd(0x10)
  120.         elif direction.lower() == "right":
  121.             for i in range(n):
  122.                 self.cmd(0x14)
  123.         else: print(textcolors.WARNING + "Nieprawidłowe argumenty funkcji" + textcolors.END)
  124.  
  125.     def mode(self, direction, shift):
  126.         """ Set display mode """
  127.         if (direction + shift).lower() == "left on":
  128.             self.cmd(0x05)
  129.         elif (direction + shift).lower() == "left off":
  130.             self.cmd(0x04)
  131.         elif (direction + shift).lower() == "right on":
  132.             self.cmd(0x07)
  133.         elif (direction + shift).lower() == "right off":
  134.             self.cmd(0x06)
  135.  
  136.     def cursor(self, mode):
  137.         """ Change cursor blink/underline/invisible """
  138.         if mode == "blink": self.cmd(0x0F)
  139.         elif mode == "underline": self.cmd(0x0E)
  140.         elif mode == "hide": self.cmd(0x0C)
  141.  
  142.     def input(self, text):
  143.         try:
  144.             if len(text.split()) > 1:
  145.                 func, *args = text.split()
  146.                 if func == "msg": getattr(self, func)(text[4:])
  147.                 else: getattr(self, func)(*args)
  148.             else: self.cmd(self.commands[text])
  149.         except Exception as e:
  150.             print(e)
  151.             #print(textcolors.FAIL + textcolors.BOLD + "Błąd: Nie udało się wykonać polecenia" + textcolors.END)
  152.  
  153. if __name__ == "__main__":
  154.     lcd = HD44780()
  155.     while True:
  156.         command = input(">>> ")
  157.         lcd.input(command)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement