Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- from RPi import GPIO
- class LCD_Display:
- def __init__(self, pins,isVierBits=False):
- self.isVierBits = isVierBits
- self.E = pins[0]
- self.RS = pins[1]
- self.databits = [pins[2], pins[3], pins[4], pins[5], pins[6],
- pins[7], pins[8], pins[9]] # D0,D1,D2,D3, D4, D5, D6, D7
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(pins, GPIO.OUT)
- GPIO.output(self.E, 0)
- GPIO.output(self.RS, 0)
- for i in range(2, 10):
- GPIO.output(pins[i], 0)
- def __initList(self, databits_list):
- self.databits = []
- for bit in databits_list:
- self.databits.append(bit)
- return self.databits
- def __repr__(self):
- for bit in self.databits:
- print(bit)
- def send_character(self, char):
- GPIO.output(self.RS, 0)
- GPIO.output(self.E, 1) # hoog naar
- self.set_data_bits(char)
- GPIO.output(self.E, 0) # laag
- time.sleep(0.01)
- def set_data_bits(self, byte):
- mask = 0x80
- for i in range(0, 8):
- # byte is de waarde die je doorkrijgt
- if (byte & mask) > 0:
- GPIO.output(self.databits[i], 1)
- # zet GPIO pin i op 1 # code zelf aanpassen
- else:
- GPIO.output(self.databits[i], 0)
- # zet GPIO pin i op 0 # code zelf aanpassen
- mask >>= 1
- def send_instruction(self, byte):
- GPIO.output(self.RS, 0)
- GPIO.output(self.E, 1) # hoog naar
- self.set_data_bits(byte)
- GPIO.output(self.E, 0) # laag
- time.sleep(0.01)
- def function_set(self):
- # 00111000
- self.send_instruction(56)
- def write_message(self, message):
- characters = list(message)
- for char in characters:
- char_ascii = ord(char)
- self.send_character(char_ascii)
- print(f"Char {char} verzonden als {char_ascii}")
- def display_on(self):
- # self.send_instruction([1, 1, 1, 1, 0, 0, 0, 0])
- self.send_instruction(15)
- def clear_LCD(self):
- # self.send_instruction([1, 0, 0, 0, 0, 0, 0, 0])
- self.send_instruction(128)
- def cursor_home(self):
- # self.send_instruction([1, 1, 0, 0, 0, 0, 0, 0])
- self.send_instruction(192)
- def init_LCD(self):
- self.function_set()
- self.display_on()
- self.clear_LCD()
- self.cursor_home()
- def second_row(self):
- pass
- def writeA(self):
- pass
- def short_delay(self):
- time.sleep(0.01)
- def long_delay(self):
- time.sleep(1)
- pins = [21, 20, 16, 12, 25, 24, 23, 26, 19, 13]
- try:
- display = LCD_Display(pins,False)
- display.init_LCD()
- while True:
- message = input("Wat wil je tonen? > ")
- if len(message) > 0:
- display.write_message(message)
- display.long_delay()
- if len(message) > 30:
- display.short_delay()
- else:
- print("** Je moet een bericht ingeven **")
- except KeyboardInterrupt:
- print("Ok, Bye!")
- except Exception as ex:
- print("Error: {0}".format(ex))
- finally:
- GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement