|
@@ -1,3 +1,4 @@
|
|
|
+# -*- coding: utf-8 -*-
|
|
|
"""
|
|
|
Author: Tobias Müller
|
|
|
Date: 30.06.2023
|
|
@@ -66,7 +67,7 @@ class LCD:
|
|
|
def __init__(self,
|
|
|
i2c_sda_pin: int|str = 2,
|
|
|
i2c_scl_pin: int|str = 3,
|
|
|
- i2c_frequency: int|str = "FAST_MODE",
|
|
|
+ i2c_frequency: int|str = "STANDARD_MODE",
|
|
|
lcd_backlight_pin: int|str = 28,
|
|
|
lcd_brightness: int = 100,
|
|
|
lcd_contrast: int = 60,
|
|
@@ -129,6 +130,7 @@ class LCD:
|
|
|
|
|
|
@lcd_contrast.setter
|
|
|
def lcd_contrast(self, lcd_contrast: int):
|
|
|
+ print("Hallo")
|
|
|
self.__lcd_contrast = self.__check_lcd_contrast(lcd_contrast)
|
|
|
self.__set_lcd_contrast()
|
|
|
|
|
@@ -138,9 +140,12 @@ class LCD:
|
|
|
|
|
|
@lcd_direction_top.setter
|
|
|
def lcd_direction_top(self, lcd_direction_top: int|bool):
|
|
|
+ buffer = self.read()
|
|
|
self.__lcd_direction_top = self.__check_lcd_direction_top(lcd_direction_top)
|
|
|
+ self.clear()
|
|
|
self.__set_lcd_direction()
|
|
|
self.clear()
|
|
|
+ self.print(buffer)
|
|
|
|
|
|
@property
|
|
|
def lcd_line_mode(self):
|
|
@@ -148,7 +153,12 @@ class LCD:
|
|
|
|
|
|
@lcd_line_mode.setter
|
|
|
def lcd_line_mode(self, lcd_line_mode: int):
|
|
|
- self.__lcd_line_mode = self.__check_lcd_line_mode(lcd_line_mode)
|
|
|
+ buffer = self.__check_lcd_line_mode(lcd_line_mode)
|
|
|
+ if lcd_line_mode:
|
|
|
+ self.clear(row=4)
|
|
|
+ if lcd_line_mode == 3:
|
|
|
+ self.clear(row=3)
|
|
|
+ self.__lcd_line_mode = buffer
|
|
|
self.__set_lcd_line_mode()
|
|
|
|
|
|
@property
|
|
@@ -402,7 +412,7 @@ class LCD:
|
|
|
|
|
|
def __check_keywords(self, keywords: dict[str,int|bool], keys: list[str]):
|
|
|
rows = 0x04 if self.__lcd_line_mode == 0x00 else 0x02 if self.__lcd_line_mode == 0x03 else 0x03
|
|
|
- output = {"rows": rows, "row": 0x01, "column": 0x01, "auto_line_break": True}
|
|
|
+ output = {"rows": rows, "row": 0x01, "column": 0x01, "auto_line_break": True, "separate": False}
|
|
|
for key, val in keywords.items():
|
|
|
if key not in keys:
|
|
|
raise ValueError(f"Keyword \"{key}\" is unknown!")
|
|
@@ -428,6 +438,14 @@ class LCD:
|
|
|
raise ValueError(f"Value \"{val}\" of keyword \"{key}\" is out of range!")
|
|
|
else:
|
|
|
output.update({key: val})
|
|
|
+ elif key == "separate":
|
|
|
+ if not isinstance(val, int) and not isinstance(val, bool):
|
|
|
+ raise ValueError(f"Value \"{val}\" of keyword \"{key}\" is not type" +
|
|
|
+ " \"int\" or \"bool\"!")
|
|
|
+ elif isinstance(val, int) and (val < 0 or val > 1):
|
|
|
+ raise ValueError(f"Value \"{val}\" of keyword \"{key}\" is out of range!")
|
|
|
+ else:
|
|
|
+ output.update({key: val})
|
|
|
return output.copy()
|
|
|
|
|
|
############################## Methods public ##############################
|
|
@@ -499,10 +517,41 @@ class LCD:
|
|
|
break
|
|
|
self.__i2c.writeto(self.__i2c_address, bytes(buffer))
|
|
|
|
|
|
+ def read(self, **keywords):
|
|
|
+ def remove_whitespace(text: str):
|
|
|
+ textlen = len(text)
|
|
|
+ endpos = textlen
|
|
|
+ for char in range(0x00, textlen):
|
|
|
+ if text[textlen - char - 0x01] == " ":
|
|
|
+ endpos -= 0x01
|
|
|
+ else:
|
|
|
+ break
|
|
|
+ return text[:endpos]
|
|
|
+
|
|
|
+ keys = ("row", "column", "separate")
|
|
|
+ keys = self.__check_keywords(keywords, keys)
|
|
|
+ rows = keys["rows"]
|
|
|
+ row = keys["row"]
|
|
|
+ column = keys["column"]
|
|
|
+ separate = keys["separate"]
|
|
|
+ buffer = []
|
|
|
+ for line in range(0x01, rows + 0x01):
|
|
|
+ tmp = (self.__lcd_control_para["CONTROL_BYTE_C0"],
|
|
|
+ self.__lcd_control_para["DISPLAY_ROW_" + str(line)],
|
|
|
+ self.__lcd_control_para["CONTROL_BYTE_RS"])
|
|
|
+ self.__i2c.writeto(self.__i2c_address,bytes(tmp))
|
|
|
+ tmp = self.__i2c.readfrom(self.__i2c_address,11)
|
|
|
+ tmp = list(tmp[1:])
|
|
|
+ tmp = [chr(tmp[i]) for i in range(0, len(tmp))]
|
|
|
+ buffer.append("".join(tmp))
|
|
|
+ sleep_us(10000)
|
|
|
+ if not separate:
|
|
|
+ return remove_whitespace("".join(buffer))
|
|
|
+ else:
|
|
|
+ for pos in range(0, len(buffer)):
|
|
|
+ buffer[pos] = remove_whitespace(buffer[pos])
|
|
|
+ return buffer
|
|
|
+
|
|
|
if __name__ == "__main__":
|
|
|
lcd = LCD()
|
|
|
lcd.print("Hallöle")
|
|
|
- sleep_us(2000000)
|
|
|
- lcd.clear()
|
|
|
- sleep_us(2000000)
|
|
|
- lcd.print("Hallüüüüü")
|