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[9], pins[8], pins[7], pins[6], pins[5],
- pins[4], pins[3], pins[2]] # D0,D1,D2,D3, D4, D5, D6, D7
- self.tekens = 0
- 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, 1) # Teken versturen DUS 1
- GPIO.output(self.E, 1) # hoog
- self.set_data_bits(char)
- GPIO.output(self.E, 0) # laag, nu verstuurd hij de bytes
- time.sleep(0.01)
- def send_instruction(self, byte):
- GPIO.output(self.RS, 0) # Instructie versturen DUS 0
- GPIO.output(self.E, 1) # hoog
- self.set_data_bits(byte)
- GPIO.output(self.E, 0) # laag, nu verstuurd hij de bytes
- time.sleep(0.01)
- def set_data_bits(self, byte):
- mask = 0x80
- for i in range(0, 8):
- if (byte & mask) > 0:
- GPIO.output(self.databits[i], 1)
- else:
- GPIO.output(self.databits[i], 0)
- mask >>= 1
- def function_set(self):
- # 00111000
- self.send_instruction(56)
- def write_message(self, message):
- if(message == "CLEAR"):
- print("** Wis alles op het LCD **")
- display.init_LCD()
- elif(len(message) > 0):
- characters = list(message)
- for char in characters:
- char_ascii = ord(char)
- self.tekens += 1
- if self.tekens > 16 and self.tekens < 18:
- display.second_row()
- self.send_character(char_ascii)
- print(f"Char {char} verzonden als {char_ascii}")
- if self.tekens > 32:
- display.scroll("LEFT")
- display.long_delay()
- if self.tekens > 56:
- self.tekens = 0
- display.init_LCD()
- else:
- print("** Bericht moet langer zijn dan 0 tekens **")
- def display_on(self):
- # self.convert_bin_dec([1, 1, 1, 1, 0, 0, 0, 0])
- self.send_instruction(15)
- def clear_LCD(self):
- # self.convert_bin_dec([1, 0, 0, 0, 0, 0, 0, 0])
- # self.send_instruction(128)
- self.send_instruction(1)
- def cursor_home(self):
- # self.convert_bin_dec([0, 0, 0, 0, 0, 0, 0, 1])
- self.send_instruction(128)
- def init_LCD(self):
- self.function_set()
- self.display_on()
- self.clear_LCD()
- def second_row(self):
- self.send_instruction(192)
- def scroll(self, direction):
- if(direction.upper() == "LEFT"):
- self.send_instruction(24) # LEFT
- elif(direction.upper() == "RIGHT"):
- self.send_instruction(28) # RIGHT
- else:
- raise ValueError("Wrong direction given")
- def writeA(self):
- pass
- def short_delay(self):
- time.sleep(0.01)
- def long_delay(self):
- time.sleep(0.5)
- def convert_bin_dec(self, bin_list):
- bin_string = ""
- for bit in bin_list:
- bin_string += str(bit)
- return int(bin_string, 2)
- # pins = [20, 21, 16, 12, 25, 24, 23, 19, 26, 13] # Geert
- pins = [20, 21, 16, 12, 25, 24, 23, 26, 19, 13]
- #E, RS, D0, D1 , D2, D3, D4, D5, D6, D7
- try:
- display = LCD_Display(pins, False)
- display.init_LCD()
- print(display.convert_bin_dec([0, 0, 0, 1, 1, 0, 0, 0]))
- display.write_message(f"Dit is een test")
- while True:
- message = input("Wat wil je tonen? > ")
- display.write_message(message)
- display.short_delay()
- 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