Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.34 KB | None | 0 0
  1. import time
  2. from RPi import GPIO
  3.  
  4.  
  5. class LCD_Display:
  6.  
  7.     def __init__(self, pins, isVierBits=False):
  8.         self.isVierBits = isVierBits
  9.         self.E = pins[0]
  10.         self.RS = pins[1]
  11.  
  12.         self.databits = [pins[9], pins[8], pins[7], pins[6], pins[5],
  13.                          pins[4], pins[3], pins[2]]  # D0,D1,D2,D3, D4, D5, D6, D7
  14.  
  15.         self.tekens = 0
  16.  
  17.         GPIO.setmode(GPIO.BCM)
  18.         GPIO.setup(pins, GPIO.OUT)
  19.  
  20.         GPIO.output(self.E, 0)
  21.         GPIO.output(self.RS, 0)
  22.         for i in range(2, 10):
  23.             GPIO.output(pins[i], 0)
  24.  
  25.     def __initList(self, databits_list):
  26.         self.databits = []
  27.         for bit in databits_list:
  28.             self.databits.append(bit)
  29.  
  30.         return self.databits
  31.  
  32.     def __repr__(self):
  33.         for bit in self.databits:
  34.             print(bit)
  35.  
  36.     def send_character(self, char):
  37.         GPIO.output(self.RS, 1)  # Teken versturen DUS 1
  38.         GPIO.output(self.E, 1)  # hoog
  39.  
  40.         self.set_data_bits(char)
  41.  
  42.         GPIO.output(self.E, 0)  # laag, nu verstuurd hij de bytes
  43.  
  44.         time.sleep(0.01)
  45.  
  46.     def send_instruction(self, byte):
  47.         GPIO.output(self.RS, 0)  # Instructie versturen DUS 0
  48.         GPIO.output(self.E, 1)  # hoog
  49.  
  50.         self.set_data_bits(byte)
  51.  
  52.         GPIO.output(self.E, 0)  # laag, nu verstuurd hij de bytes
  53.  
  54.         time.sleep(0.01)
  55.  
  56.     def set_data_bits(self, byte):
  57.         mask = 0x80
  58.         for i in range(0, 8):
  59.             if (byte & mask) > 0:
  60.                 GPIO.output(self.databits[i], 1)
  61.             else:
  62.                 GPIO.output(self.databits[i], 0)
  63.             mask >>= 1
  64.  
  65.     def function_set(self):
  66.         # 00111000
  67.         self.send_instruction(56)
  68.  
  69.     def write_message(self, message):
  70.         if(message == "CLEAR"):
  71.             print("** Wis alles op het LCD **")
  72.             display.init_LCD()
  73.  
  74.         elif(len(message) > 0):
  75.  
  76.             characters = list(message)
  77.             for char in characters:
  78.                 char_ascii = ord(char)
  79.                 self.tekens += 1
  80.                 if self.tekens > 16 and self.tekens < 18:
  81.                     display.second_row()
  82.                 self.send_character(char_ascii)
  83.                 print(f"Char {char} verzonden als {char_ascii}")
  84.                 if self.tekens > 32:
  85.                     display.scroll("LEFT")
  86.                     display.long_delay()
  87.                 if self.tekens > 56:
  88.                     self.tekens = 0
  89.                     display.init_LCD()
  90.         else:
  91.             print("** Bericht moet langer zijn dan 0 tekens **")
  92.  
  93.     def display_on(self):
  94.         # self.convert_bin_dec([1, 1, 1, 1, 0, 0, 0, 0])
  95.         self.send_instruction(15)
  96.  
  97.     def clear_LCD(self):
  98.         # self.convert_bin_dec([1, 0, 0, 0, 0, 0, 0, 0])
  99.         # self.send_instruction(128)
  100.         self.send_instruction(1)
  101.  
  102.     def cursor_home(self):
  103.         # self.convert_bin_dec([0, 0, 0, 0, 0, 0, 0, 1])
  104.         self.send_instruction(128)
  105.  
  106.     def init_LCD(self):
  107.         self.function_set()
  108.         self.display_on()
  109.         self.clear_LCD()
  110.  
  111.     def second_row(self):
  112.         self.send_instruction(192)
  113.  
  114.     def scroll(self, direction):
  115.         if(direction.upper() == "LEFT"):
  116.             self.send_instruction(24)  # LEFT
  117.         elif(direction.upper() == "RIGHT"):
  118.             self.send_instruction(28)  # RIGHT
  119.         else:
  120.             raise ValueError("Wrong direction given")
  121.  
  122.     def writeA(self):
  123.         pass
  124.  
  125.     def short_delay(self):
  126.         time.sleep(0.01)
  127.  
  128.     def long_delay(self):
  129.         time.sleep(0.5)
  130.  
  131.     def convert_bin_dec(self, bin_list):
  132.         bin_string = ""
  133.         for bit in bin_list:
  134.             bin_string += str(bit)
  135.  
  136.         return int(bin_string, 2)
  137.  
  138. # pins = [20, 21, 16, 12, 25, 24, 23, 19, 26, 13] # Geert
  139. pins = [20, 21, 16, 12, 25, 24, 23, 26, 19, 13]
  140. #E, RS, D0, D1 , D2, D3, D4, D5, D6, D7
  141.  
  142. try:
  143.     display = LCD_Display(pins, False)
  144.     display.init_LCD()
  145.     print(display.convert_bin_dec([0, 0, 0, 1, 1, 0, 0, 0]))
  146.     display.write_message(f"Dit is een test")
  147.     while True:
  148.  
  149.         message = input("Wat wil je tonen? > ")
  150.  
  151.         display.write_message(message)
  152.         display.short_delay()
  153.  
  154.  
  155. except KeyboardInterrupt:
  156.     print("Ok, Bye!")
  157. except Exception as ex:
  158.     print("Error: {0}".format(ex))
  159. finally:
  160.     GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement