|
@@ -9,16 +9,28 @@ This file contains a class for a LCD.
|
|
|
############################## Modules ##############################
|
|
|
|
|
|
from machine import Pin, PWM, I2C
|
|
|
-import re
|
|
|
|
|
|
-############################## Code ##############################
|
|
|
+############################## Class ##############################
|
|
|
|
|
|
-class LCD:
|
|
|
+class LCD(object):
|
|
|
"""
|
|
|
Class to control a DOGS104-A LC-Display.
|
|
|
"""
|
|
|
|
|
|
############################## Attributes ##############################
|
|
|
+
|
|
|
+ ROM_A = "ROM A"
|
|
|
+ ROM_B = "ROM B"
|
|
|
+ ROM_C = "ROM C"
|
|
|
+
|
|
|
+ TWO_LINES = "TWO LINES"
|
|
|
+ THREE_LINES_TOP = "THREE LINES TOP"
|
|
|
+ THREE_LINES_MIDDLE = "THREE LINES MIDDLE"
|
|
|
+ THREE_LINES_BOTTOM = "THREE LINES BOTTOM"
|
|
|
+ FOUR_LINES = "FOUR LINES"
|
|
|
+
|
|
|
+ STANDARD_MODE = "STANDARD MODE"
|
|
|
+ FAST_MODE = "FAST MODE"
|
|
|
|
|
|
__lcd_setting_para = {"LCD_PARA_EXT_FUNC": 0x09, # Extended Function (4 line mode) # type: ignore
|
|
|
"LCD_PARA_BIAS_SET": 0x12, # Bias Setting (BS1 = 1) # type: ignore
|
|
@@ -49,44 +61,47 @@ class LCD:
|
|
|
|
|
|
__lcd_rom_para = {"LCD_PARA_ROM_SEL": 0x72}
|
|
|
|
|
|
- __lcd_rom = {"ROM A": 0, "ROM B": 1, "ROM C": 2,
|
|
|
- "A": 0, "B": 1, "C": 2,
|
|
|
+ __lcd_roms = {ROM_A: 0, ROM_B: 1, ROM_C: 2,
|
|
|
+ ROM_A[-1]: 0, ROM_B[-1]: 1, ROM_C[-1]: 2,
|
|
|
1: 0, 2: 1, 3: 2}
|
|
|
+
|
|
|
+ __lcd_line_modes = {TWO_LINES: 3, FOUR_LINES: 0, THREE_LINES_TOP: 4,
|
|
|
+ THREE_LINES_MIDDLE: 2, THREE_LINES_BOTTOM: 1}
|
|
|
|
|
|
- __i2c_ports = {"I2C0": {"SDA_PINS": [0, 4 ,8 ,12 ,16 ,20],
|
|
|
- "SCL_PINS": [1, 5, 9, 13, 17, 21]},
|
|
|
- "I2C1": {"SDA_PINS": [2, 6, 10, 14, 18, 26],
|
|
|
- "SCL_PINS": [3, 7, 11, 15, 19, 27]}}
|
|
|
+ __i2c_ports = {"I2C0": {"SDA_PINS": [Pin(0), Pin(4) ,Pin(8) ,Pin(12) ,Pin(16) ,Pin(20)],
|
|
|
+ "SCL_PINS": [Pin(1), Pin(5), Pin(9), Pin(13), Pin(17), Pin(21)]},
|
|
|
+ "I2C1": {"SDA_PINS": [Pin(2), Pin(6), Pin(10), Pin(14), Pin(18), Pin(26)],
|
|
|
+ "SCL_PINS": [Pin(3), Pin(7), Pin(11), Pin(15), Pin(19), Pin(27)]}}
|
|
|
|
|
|
- __i2c_frequencies = {"STANDARD_MODE": 100000,
|
|
|
- "FAST_MODE": 400000}
|
|
|
+ __i2c_frequencies = {STANDARD_MODE: 100000,
|
|
|
+ FAST_MODE: 400000}
|
|
|
|
|
|
__i2c_address = 0x3c
|
|
|
|
|
|
############################## Constructor ##############################
|
|
|
|
|
|
def __init__(self,
|
|
|
- i2c_sda_pin: int|str = 2,
|
|
|
- i2c_scl_pin: int|str = 3,
|
|
|
- i2c_frequency: int|str = "STANDARD_MODE",
|
|
|
- lcd_backlight_pin: int|str = 28,
|
|
|
+ i2c_sda_pin: Pin = Pin(2),
|
|
|
+ i2c_scl_pin: Pin = Pin(3),
|
|
|
+ i2c_frequency: int|str = "STANDARD MODE",
|
|
|
+ lcd_backlight_pin: Pin = Pin(28),
|
|
|
lcd_brightness: int = 100,
|
|
|
lcd_contrast: int = 60,
|
|
|
- lcd_ascii_rom: int|str = "ROM B",
|
|
|
- lcd_line_mode: int = 0,
|
|
|
+ lcd_rom: int|str = "ROM B",
|
|
|
+ lcd_line_mode: str = "FOUR LINES",
|
|
|
lcd_direction_top: int|bool = False):
|
|
|
"""
|
|
|
Initialize LCD. Alls arguments can be changed later as a property.
|
|
|
|
|
|
Args:
|
|
|
- i2c_sda_pin (int | str, optional): Set SDA-Pin for I2C-Bus. Defaults to 2.
|
|
|
- i2c_scl_pin (int | str, optional): Set SCL-Pin for I2C-Bus. Defaults to 3.
|
|
|
- i2c_frequency (int | str, optional): Set I2C frequency. Defaults to "STANDARD_MODE".
|
|
|
- lcd_backlight_pin (int | str, optional): Set LCD backlight pin. Defaults to 28.
|
|
|
+ i2c_sda_pin (Pin, optional): Set SDA-Pin for I2C-Bus. Defaults to Pin(2).
|
|
|
+ i2c_scl_pin (Pin, optional): Set SCL-Pin for I2C-Bus. Defaults to Pin(3).
|
|
|
+ i2c_frequency (int | str, optional): Set I2C frequency. Defaults to "STANDARD MODE".
|
|
|
+ lcd_backlight_pin (Pin, optional): Set LCD backlight pin. Defaults to Pin(28).
|
|
|
lcd_brightness (int, optional): Set LCD brightness in percent. Defaults to 100.
|
|
|
lcd_contrast (int, optional): Set LCD contrast in percent. Defaults to 60.
|
|
|
- lcd_ascii_rom (int | str, optional): Set ASCII-ROM. Defaults to "ROM B".
|
|
|
- lcd_line_mode (int, optional): Set LCD line mode for 2-, 3- or 4-lines. Defaults to 0.
|
|
|
+ lcd_rom (int | str, optional): Set ASCII-ROM. Defaults to "ROM B".
|
|
|
+ lcd_line_mode (str, optional): Set LCD line mode for 2-, 3- or 4-lines. Defaults to "FOUR LINES".
|
|
|
lcd_direction_top (int | bool, optional): Set LCD direction to top. Defaults to False.
|
|
|
"""
|
|
|
|
|
@@ -97,7 +112,7 @@ class LCD:
|
|
|
self.__lcd_backlight_pin = self.__check_lcd_backlight_pin(lcd_backlight_pin)
|
|
|
self.__lcd_brightness = self.__check_lcd_brightness(lcd_brightness)
|
|
|
self.__lcd_contrast = self.__check_lcd_contrast(lcd_contrast)
|
|
|
- self.__lcd_ascii_rom = self.__check_lcd_ascii_rom(lcd_ascii_rom)
|
|
|
+ self.__lcd_rom = self.__check_lcd_rom(lcd_rom)
|
|
|
self.__lcd_line_mode = self.__check_lcd_line_mode(lcd_line_mode)
|
|
|
self.__lcd_direction_top = self.__check_lcd_direction_top(lcd_direction_top)
|
|
|
|
|
@@ -110,7 +125,7 @@ class LCD:
|
|
|
@property
|
|
|
def __lcd_control_para(self):
|
|
|
lcd_control_para = self.__lcd_control_para_priv.copy()
|
|
|
- if self.__lcd_line_mode:
|
|
|
+ if self.__lcd_line_modes[self.__lcd_line_mode]:
|
|
|
lcd_control_para["FUNCTION_SET"] = 0x3C
|
|
|
if self.__lcd_direction_top:
|
|
|
lcd_control_para["DISPLAY_ROW_1"] = 0x8A
|
|
@@ -127,17 +142,34 @@ class LCD:
|
|
|
|
|
|
@lcd_brightness.setter
|
|
|
def lcd_brightness(self, lcd_brightness: int):
|
|
|
+ """
|
|
|
+ lcd_brightness: Set LCD brightness in percent.
|
|
|
+
|
|
|
+ Args:
|
|
|
+ lcd_brightness (int): Value from 0-100.
|
|
|
+ """
|
|
|
+
|
|
|
self.__lcd_brightness = self.__check_lcd_brightness(lcd_brightness)
|
|
|
self.__set_lcd_brightness()
|
|
|
|
|
|
@property
|
|
|
- def lcd_ascii_rom(self):
|
|
|
- return self.__lcd_ascii_rom
|
|
|
+ def lcd_rom(self):
|
|
|
+ return self.__lcd_rom
|
|
|
+
|
|
|
+ @lcd_rom.setter
|
|
|
+ def lcd_rom(self, lcd_rom: int|str):
|
|
|
+ """
|
|
|
+ lcd_rom: Set ROM for ASCII-Table. Check available ROMs at:
|
|
|
+ https://www.lcd-module.com/fileadmin/eng/pdf/doma/dogs104e.pdf
|
|
|
+
|
|
|
+ Args:
|
|
|
+ lcd_rom (int | str): 1 or "A" or "ROM A" for ROM A,
|
|
|
+ 2 or "B" or "ROM B" for ROM B,
|
|
|
+ 3 or "C" or "ROM C" for ROM C.
|
|
|
+ """
|
|
|
|
|
|
- @lcd_ascii_rom.setter
|
|
|
- def lcd_ascii_rom(self, lcd_ascii_rom: int|str):
|
|
|
- self.__lcd_ascii_rom = self.__check_lcd_ascii_rom(lcd_ascii_rom)
|
|
|
- self.__set_lcd_ascii_rom()
|
|
|
+ self.__lcd_rom = self.__check_lcd_rom(lcd_rom)
|
|
|
+ self.__set_lcd_rom()
|
|
|
|
|
|
@property
|
|
|
def lcd_contrast(self):
|
|
@@ -145,7 +177,13 @@ class LCD:
|
|
|
|
|
|
@lcd_contrast.setter
|
|
|
def lcd_contrast(self, lcd_contrast: int):
|
|
|
- print("Hallo")
|
|
|
+ """
|
|
|
+ lcd_contrast: Set LCD contrast in percent.
|
|
|
+
|
|
|
+ Args:
|
|
|
+ lcd_contrast (int): Value from 0-100.
|
|
|
+ """
|
|
|
+
|
|
|
self.__lcd_contrast = self.__check_lcd_contrast(lcd_contrast)
|
|
|
self.__set_lcd_contrast()
|
|
|
|
|
@@ -155,6 +193,14 @@ class LCD:
|
|
|
|
|
|
@lcd_direction_top.setter
|
|
|
def lcd_direction_top(self, lcd_direction_top: int|bool):
|
|
|
+ """
|
|
|
+ lcd_direction_top: Set LCD direction to top.
|
|
|
+
|
|
|
+ Args:
|
|
|
+ lcd_direction_top (int | bool): 0 or False for LCD direction to bottom,
|
|
|
+ 1 or True for LCD direction to top.
|
|
|
+ """
|
|
|
+
|
|
|
buffer = self.read()
|
|
|
self.__lcd_direction_top = self.__check_lcd_direction_top(lcd_direction_top)
|
|
|
self.clear()
|
|
@@ -167,13 +213,26 @@ class LCD:
|
|
|
return self.__lcd_line_mode
|
|
|
|
|
|
@lcd_line_mode.setter
|
|
|
- def lcd_line_mode(self, lcd_line_mode: int):
|
|
|
- buffer = self.__check_lcd_line_mode(lcd_line_mode)
|
|
|
- if lcd_line_mode and self.__lcd_line_mode == 0:
|
|
|
+ def lcd_line_mode(self, lcd_line_mode: str):
|
|
|
+ """
|
|
|
+ lcd_line_mode: Set LCD line mode to 2-, 3- or 4-lines. For 3-lines are three additional modes available.
|
|
|
+
|
|
|
+ Args:
|
|
|
+ lcd_line_mode (str): "TWO LINES" to set the display in two line mode,
|
|
|
+ "THREE LINES TOP" to set the display in three line mode with one bigger line on top position,
|
|
|
+ "THREE LINES MIDDLE" to set the display in three line mode with one bigger line on middle position,
|
|
|
+ "THREE LINES BOTTOM" to set the display in three line mode with one bigger line on bottom position,
|
|
|
+ "FOUR LINES" to set the display in four line mode.
|
|
|
+ """
|
|
|
+
|
|
|
+ self.__check_lcd_line_mode(lcd_line_mode)
|
|
|
+ if (self.__lcd_line_modes[lcd_line_mode] and
|
|
|
+ self.__lcd_line_modes[self.__lcd_line_mode] == 0):
|
|
|
self.clear(row=4)
|
|
|
- if lcd_line_mode == 3 and self.__lcd_line_mode != 3:
|
|
|
+ if (self.__lcd_line_modes[lcd_line_mode] == 3 and
|
|
|
+ self.__lcd_line_modes[self.__lcd_line_mode] != 3):
|
|
|
self.clear(row=3)
|
|
|
- self.__lcd_line_mode = buffer
|
|
|
+ self.__lcd_line_mode = lcd_line_mode
|
|
|
self.__set_lcd_line_mode()
|
|
|
|
|
|
@property
|
|
@@ -181,7 +240,13 @@ class LCD:
|
|
|
return self.__lcd_backlight_pin
|
|
|
|
|
|
@lcd_backlight_pin.setter
|
|
|
- def lcd_backlight_pin(self, lcd_backlight_pin: int|str):
|
|
|
+ def lcd_backlight_pin(self, lcd_backlight_pin: Pin):
|
|
|
+ """
|
|
|
+ lcd_backlight_pin: Set LCD backlight pin.
|
|
|
+
|
|
|
+ Args:
|
|
|
+ lcd_backlight_pin (Pin): Value of class Pin.
|
|
|
+ """
|
|
|
self.__check_lcd_backlight_pin(lcd_backlight_pin)
|
|
|
self.__deinit_backlight_pwm()
|
|
|
self.lcd_backlight_pin = lcd_backlight_pin
|
|
@@ -193,6 +258,13 @@ class LCD:
|
|
|
|
|
|
@i2c_frequency.setter
|
|
|
def i2c_frequency(self, i2c_frequency: int|str):
|
|
|
+ """
|
|
|
+ i2c_frequency: Set I2C frequency.
|
|
|
+
|
|
|
+ Args:
|
|
|
+ i2c_frequency (int | str): "STANDARD MODE" or 100000 for a clock frequency of 100 kHz,
|
|
|
+ "FAST MODE" or 400000 for a clock frequency of 400 kHz.
|
|
|
+ """
|
|
|
self.__check_i2c_frequency(i2c_frequency)
|
|
|
self.__deinit_i2c()
|
|
|
self.__i2c_frequency = i2c_frequency
|
|
@@ -203,7 +275,13 @@ class LCD:
|
|
|
return self.__i2c_sda_pin
|
|
|
|
|
|
@i2c_sda_pin.setter
|
|
|
- def i2c_sda_pin(self, i2c_sda_pin: int|str):
|
|
|
+ def i2c_sda_pin(self, i2c_sda_pin: Pin):
|
|
|
+ """
|
|
|
+ i2c_sda_pin: Set SDA-Pin for I2C-Bus.
|
|
|
+
|
|
|
+ Args:
|
|
|
+ i2c_sda_pin (Pin): Value of class Pin.
|
|
|
+ """
|
|
|
self.change_i2c_port(i2c_sda_pin, self.__i2c_scl_pin)
|
|
|
|
|
|
@property
|
|
@@ -211,15 +289,21 @@ class LCD:
|
|
|
return self.__i2c_scl_pin
|
|
|
|
|
|
@i2c_scl_pin.setter
|
|
|
- def i2c_scl_pin(self, i2c_scl_pin: int|str):
|
|
|
+ def i2c_scl_pin(self, i2c_scl_pin: Pin):
|
|
|
+ """
|
|
|
+ i2c_scl_pin: Set SCL-Pin for I2C-Bus.
|
|
|
+
|
|
|
+ Args:
|
|
|
+ i2c_scl_pin (Pin): Value of class Pin.
|
|
|
+ """
|
|
|
self.change_i2c_port(self.__i2c_sda_pin, i2c_scl_pin)
|
|
|
|
|
|
############################## Methods private ##############################
|
|
|
|
|
|
@staticmethod
|
|
|
- def __reset_pin(pin: int|str):
|
|
|
- Pin(pin, Pin.IN, pull=None)
|
|
|
-
|
|
|
+ def __reset_pin(pin: Pin):
|
|
|
+ pin.init(mode=Pin.IN)
|
|
|
+
|
|
|
@staticmethod
|
|
|
def __check_lcd_brightness(lcd_brightness: int):
|
|
|
if not isinstance(lcd_brightness, int):
|
|
@@ -230,22 +314,17 @@ class LCD:
|
|
|
return lcd_brightness
|
|
|
|
|
|
@staticmethod
|
|
|
- def __check_lcd_backlight_pin(lcd_backlight_pin: int|str):
|
|
|
- if not isinstance(lcd_backlight_pin, int) and not isinstance(lcd_backlight_pin, str):
|
|
|
- raise ValueError(f"Value \"{lcd_backlight_pin}\" of \"lcd_backlight_pin\" is not type" +
|
|
|
- " \"int\" or \"str\"!")
|
|
|
- elif isinstance(lcd_backlight_pin, str) and not re.match(r"^GP\d\d?$",lcd_backlight_pin):
|
|
|
- raise ValueError(f"String value \"{lcd_backlight_pin}\" of \"lcd_backlight_pin\" has" +
|
|
|
- " wrong format!")
|
|
|
+ def __check_lcd_backlight_pin(lcd_backlight_pin: Pin):
|
|
|
+ if not isinstance(lcd_backlight_pin, Pin):
|
|
|
+ raise ValueError(f"Value \"{lcd_backlight_pin}\" of \"lcd_backlight_pin\" is not type \"Pin\"!")
|
|
|
else:
|
|
|
return lcd_backlight_pin
|
|
|
|
|
|
- @staticmethod
|
|
|
- def __check_lcd_line_mode(lcd_line_mode: int):
|
|
|
- if not isinstance(lcd_line_mode, int):
|
|
|
- raise ValueError(f"Value \"{lcd_line_mode}\" of \"lcd_line_mode\" is not type \"int\"!")
|
|
|
- elif lcd_line_mode < 0 or lcd_line_mode > 4:
|
|
|
- raise ValueError(f"Value \"{lcd_line_mode}\" of \"lcd_line_mode\" is out of range!")
|
|
|
+ def __check_lcd_line_mode(self, lcd_line_mode: str):
|
|
|
+ if not isinstance(lcd_line_mode, str):
|
|
|
+ raise ValueError(f"Value \"{lcd_line_mode}\" of \"lcd_line_mode\" is not type \"str\"!")
|
|
|
+ elif lcd_line_mode.upper() not in self.__lcd_line_modes:
|
|
|
+ raise ValueError(f"Value \"{lcd_line_mode}\" of \"lcd_line_mode\" is not supported!")
|
|
|
else:
|
|
|
return lcd_line_mode
|
|
|
|
|
@@ -268,65 +347,36 @@ class LCD:
|
|
|
else:
|
|
|
return lcd_contrast
|
|
|
|
|
|
- def __check_lcd_ascii_rom(self, lcd_ascii_rom: int|str):
|
|
|
- if not isinstance(lcd_ascii_rom, int) and not isinstance(lcd_ascii_rom, str):
|
|
|
- raise ValueError(f"Value \"{lcd_ascii_rom}\" of \"lcd_ascii_rom\" is not type" +
|
|
|
+ def __check_lcd_rom(self, lcd_rom: int|str):
|
|
|
+ if not isinstance(lcd_rom, int) and not isinstance(lcd_rom, str):
|
|
|
+ raise ValueError(f"Value \"{lcd_rom}\" of \"lcd_rom\" is not type" +
|
|
|
" \"int\" or \"str\"!")
|
|
|
- elif((isinstance(lcd_ascii_rom, int) and lcd_ascii_rom not in self.__lcd_rom.keys()) or
|
|
|
- (isinstance(lcd_ascii_rom, str) and lcd_ascii_rom.upper() not in self.__lcd_rom.keys())):
|
|
|
- raise ValueError(f"Value \"{lcd_ascii_rom}\" of \"lcd_ascii_rom\" is not supported!")
|
|
|
+ elif((isinstance(lcd_rom, int) and lcd_rom not in self.__lcd_roms.keys()) or
|
|
|
+ (isinstance(lcd_rom, str) and lcd_rom.upper() not in self.__lcd_roms.keys())):
|
|
|
+ raise ValueError(f"Value \"{lcd_rom}\" of \"lcd_rom\" is not supported!")
|
|
|
else:
|
|
|
- return lcd_ascii_rom
|
|
|
-
|
|
|
- def __check_i2c_ports(self, i2c_sda_pin: int|str, i2c_scl_pin: int|str):
|
|
|
- if not isinstance(i2c_sda_pin, int) and not isinstance(i2c_sda_pin, str):
|
|
|
- raise ValueError(f"Value \"{i2c_sda_pin}\" of \"i2c_sda_pin\" is not type \"int\"" +
|
|
|
- " or \"str\"!")
|
|
|
- elif not isinstance(i2c_scl_pin, int) and not isinstance(i2c_scl_pin, str):
|
|
|
- raise ValueError(f"Value \"{i2c_scl_pin}\" of \"i2c_scl_pin\" is not type \"int\"" +
|
|
|
- " or \"str\"!")
|
|
|
- elif isinstance(i2c_sda_pin, str) and not re.match(r"^GP\d\d?$",i2c_sda_pin):
|
|
|
- raise ValueError(f"String value \"{i2c_sda_pin}\" of \"i2c_sda_pin\" has wrong format!")
|
|
|
- elif isinstance(i2c_scl_pin, str) and not re.match(r"^GP\d\d?$",i2c_scl_pin):
|
|
|
- raise ValueError(f"String value \"{i2c_scl_pin}\" of \"i2c_scl_pin\" has wrong format!")
|
|
|
- elif ((isinstance(i2c_sda_pin, int) and
|
|
|
- not i2c_sda_pin in self.__i2c_ports["I2C0"]["SDA_PINS"] and
|
|
|
- not i2c_sda_pin in self.__i2c_ports["I2C1"]["SDA_PINS"]) or
|
|
|
- (isinstance(i2c_sda_pin, str) and
|
|
|
- not int(i2c_sda_pin[2:]) in self.__i2c_ports["I2C0"]["SDA_PINS"] and
|
|
|
- not int(i2c_sda_pin[2:]) in self.__i2c_ports["I2C1"]["SDA_PINS"])):
|
|
|
+ return lcd_rom
|
|
|
+
|
|
|
+ def __check_i2c_ports(self, i2c_sda_pin: Pin, i2c_scl_pin: Pin):
|
|
|
+ if not isinstance(i2c_sda_pin, Pin):
|
|
|
+ raise ValueError(f"Value \"{i2c_sda_pin}\" of \"i2c_sda_pin\" is not type \"Pin\"!")
|
|
|
+ elif not isinstance(i2c_scl_pin, Pin):
|
|
|
+ raise ValueError(f"Value \"{i2c_scl_pin}\" of \"i2c_scl_pin\" is not type \"Pin\"!")
|
|
|
+ elif (not i2c_sda_pin in self.__i2c_ports["I2C0"]["SDA_PINS"] and
|
|
|
+ not i2c_sda_pin in self.__i2c_ports["I2C1"]["SDA_PINS"]):
|
|
|
raise ValueError(f"Value \"{i2c_sda_pin}\" of \"i2c_sda_pin\" is not an I2C SDA-Pin!")
|
|
|
- elif ((isinstance(i2c_scl_pin, int) and
|
|
|
- not i2c_scl_pin in self.__i2c_ports["I2C0"]["SCL_PINS"] and
|
|
|
- not i2c_scl_pin in self.__i2c_ports["I2C1"]["SCL_PINS"]) or
|
|
|
- (isinstance(i2c_scl_pin, str) and
|
|
|
- not int(i2c_scl_pin[2:]) in self.__i2c_ports["I2C0"]["SCL_PINS"] and
|
|
|
- not int(i2c_scl_pin[2:]) in self.__i2c_ports["I2C1"]["SCL_PINS"])):
|
|
|
+ elif (not i2c_scl_pin in self.__i2c_ports["I2C0"]["SCL_PINS"] and
|
|
|
+ not i2c_scl_pin in self.__i2c_ports["I2C1"]["SCL_PINS"]):
|
|
|
raise ValueError(f"Value \"{i2c_scl_pin}\" of \"i2c_scl_pin\" is not an I2C SCL-Pin!")
|
|
|
- elif ((((isinstance(i2c_sda_pin, int) and
|
|
|
- i2c_sda_pin in self.__i2c_ports["I2C0"]["SDA_PINS"]) or
|
|
|
- (isinstance(i2c_sda_pin, str) and
|
|
|
- int(i2c_sda_pin[2:]) in self.__i2c_ports["I2C0"]["SDA_PINS"])) and not
|
|
|
- ((isinstance(i2c_scl_pin, int) and
|
|
|
- i2c_scl_pin in self.__i2c_ports["I2C0"]["SCL_PINS"]) or
|
|
|
- (isinstance(i2c_scl_pin, str) and
|
|
|
- int(i2c_scl_pin[2:]) in self.__i2c_ports["I2C0"]["SCL_PINS"]))) or
|
|
|
- (((isinstance(i2c_sda_pin, int) and
|
|
|
- i2c_sda_pin in self.__i2c_ports["I2C1"]["SDA_PINS"]) or
|
|
|
- (isinstance(i2c_sda_pin, str) and
|
|
|
- int(i2c_sda_pin[2:]) in self.__i2c_ports["I2C1"]["SDA_PINS"])) and not
|
|
|
- ((isinstance(i2c_scl_pin, int) and
|
|
|
- i2c_scl_pin in self.__i2c_ports["I2C1"]["SCL_PINS"]) or
|
|
|
- (isinstance(i2c_scl_pin, str) and
|
|
|
- int(i2c_scl_pin[2:]) in self.__i2c_ports["I2C1"]["SCL_PINS"])))):
|
|
|
+ elif ((i2c_sda_pin in self.__i2c_ports["I2C0"]["SDA_PINS"] and not
|
|
|
+ i2c_scl_pin in self.__i2c_ports["I2C0"]["SCL_PINS"]) or
|
|
|
+ (i2c_sda_pin in self.__i2c_ports["I2C1"]["SDA_PINS"] and not
|
|
|
+ i2c_scl_pin in self.__i2c_ports["I2C1"]["SCL_PINS"])):
|
|
|
raise ValueError(f"Value \"{i2c_scl_pin}\" of \"i2c_scl_pin\" and " +
|
|
|
f"Value \"{i2c_sda_pin}\" of \"i2c_sda_pin\" " +
|
|
|
"are not part of the same I2C Bus!")
|
|
|
else:
|
|
|
- if ((isinstance(i2c_sda_pin, int) and
|
|
|
- i2c_sda_pin in self.__i2c_ports["I2C0"]["SDA_PINS"]) or
|
|
|
- (isinstance(i2c_sda_pin, str) and
|
|
|
- int(i2c_sda_pin[2:]) in self.__i2c_ports["I2C0"]["SDA_PINS"])):
|
|
|
+ if i2c_sda_pin in self.__i2c_ports["I2C0"]["SDA_PINS"]:
|
|
|
return 0, i2c_sda_pin, i2c_scl_pin
|
|
|
else:
|
|
|
return 1, i2c_sda_pin, i2c_scl_pin
|
|
@@ -344,8 +394,8 @@ class LCD:
|
|
|
|
|
|
def __init_i2c(self):
|
|
|
self.__i2c = I2C(self.__i2c_bus,
|
|
|
- sda=Pin(self.__i2c_sda_pin),
|
|
|
- scl=Pin(self.__i2c_scl_pin),
|
|
|
+ sda=self.__i2c_sda_pin,
|
|
|
+ scl=self.__i2c_scl_pin,
|
|
|
freq=(self.__i2c_frequency if isinstance(self.__i2c_frequency, int) else
|
|
|
self.__i2c_frequencies[self.__i2c_frequency]))
|
|
|
buffer = (self.__lcd_control_para["CONTROL_BYTE"],
|
|
@@ -359,7 +409,7 @@ class LCD:
|
|
|
self.__lcd_setting_para["LCD_PARA_DIS_CON"])
|
|
|
self.__i2c.writeto(self.__i2c_address, bytes(buffer))
|
|
|
self.__set_lcd_line_mode()
|
|
|
- self.__set_lcd_ascii_rom()
|
|
|
+ self.__set_lcd_rom()
|
|
|
self.__set_lcd_contrast()
|
|
|
self.__set_lcd_direction()
|
|
|
self.clear()
|
|
@@ -369,7 +419,7 @@ class LCD:
|
|
|
self.__reset_pin(self.__i2c_sda_pin)
|
|
|
|
|
|
def __init_backlight_pwm(self):
|
|
|
- self.__backlight_pwm = PWM(Pin(self.__lcd_backlight_pin))
|
|
|
+ self.__backlight_pwm = PWM(self.__lcd_backlight_pin)
|
|
|
self.__backlight_pwm.freq(1000)
|
|
|
self.__set_lcd_brightness()
|
|
|
|
|
@@ -380,17 +430,17 @@ class LCD:
|
|
|
def __set_lcd_brightness(self):
|
|
|
self.__backlight_pwm.duty_u16(round(65635 * (self.__lcd_brightness / 100)))
|
|
|
|
|
|
- def __set_lcd_ascii_rom(self):
|
|
|
- if isinstance(self.__lcd_ascii_rom, str):
|
|
|
- lcd_ascii_rom = self.__lcd_ascii_rom.upper()
|
|
|
+ def __set_lcd_rom(self):
|
|
|
+ if isinstance(self.__lcd_rom, str):
|
|
|
+ lcd_rom = self.__lcd_rom.upper()
|
|
|
else:
|
|
|
- lcd_ascii_rom = self.__lcd_ascii_rom
|
|
|
+ lcd_rom = self.__lcd_rom
|
|
|
buffer = (self.__lcd_control_para["CONTROL_BYTE_C0"],
|
|
|
self.__lcd_control_para["FUNCTION_SET_RE"],
|
|
|
self.__lcd_control_para["CONTROL_BYTE_C0"],
|
|
|
self.__lcd_rom_para["LCD_PARA_ROM_SEL"],
|
|
|
self.__lcd_control_para["CONTROL_BYTE_C0_RS"],
|
|
|
- self.__lcd_rom[lcd_ascii_rom] << 0x02,
|
|
|
+ self.__lcd_roms[lcd_rom] << 0x02,
|
|
|
self.__lcd_control_para["CONTROL_BYTE"],
|
|
|
self.__lcd_control_para["FUNCTION_SET"])
|
|
|
self.__i2c.writeto(self.__i2c_address, bytes(buffer))
|
|
@@ -417,16 +467,18 @@ class LCD:
|
|
|
def __set_lcd_line_mode(self):
|
|
|
buffer = [self.__lcd_control_para["CONTROL_BYTE"],
|
|
|
self.__lcd_control_para["FUNCTION_SET_RE"]]
|
|
|
- if self.__lcd_line_mode:
|
|
|
+ lcd_line_mode = self.__lcd_line_modes[self.__lcd_line_mode]
|
|
|
+ if lcd_line_mode:
|
|
|
buffer.append(self.__lcd_line_mode_para["LCD_PARA_LINES"] |
|
|
|
- ((self.__lcd_line_mode - 1) << 0x02))
|
|
|
+ ((lcd_line_mode - 1) << 0x02))
|
|
|
else:
|
|
|
buffer.append(self.__lcd_line_mode_para["LCD_PARA_LINES"])
|
|
|
buffer.append(self.__lcd_control_para["FUNCTION_SET"])
|
|
|
self.__i2c.writeto(self.__i2c_address, bytes(buffer))
|
|
|
|
|
|
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
|
|
|
+ lcd_line_mode = self.__lcd_line_modes[self.__lcd_line_mode]
|
|
|
+ rows = 0x04 if lcd_line_mode == 0x00 else 0x02 if lcd_line_mode == 0x03 else 0x03
|
|
|
output = {"rows": rows, "row": 0x01, "column": 0x01, "auto_line_break": True, "separate": False}
|
|
|
for key, val in keywords.items():
|
|
|
if key not in keys:
|
|
@@ -465,7 +517,7 @@ class LCD:
|
|
|
|
|
|
############################## Methods public ##############################
|
|
|
|
|
|
- def change_i2c_port(self, i2c_sda_pin: int|str, i2c_scl_pin: int|str):
|
|
|
+ def change_i2c_port(self, i2c_sda_pin: Pin, i2c_scl_pin: Pin):
|
|
|
"""
|
|
|
Used to change I2C port.
|
|
|
|
|
@@ -540,7 +592,6 @@ class LCD:
|
|
|
Keywords:
|
|
|
row (int): Row to start printing. Defaults to 1.
|
|
|
column (int): Column to start printing. Defaults to 1.
|
|
|
- auto_line_break (int | bool): Breaks the line automatically. Defaults to True.
|
|
|
"""
|
|
|
|
|
|
buffer = []
|
|
@@ -571,8 +622,8 @@ class LCD:
|
|
|
separate (int | bool): Separate each line into a list. Defaults to False.
|
|
|
|
|
|
Returns:
|
|
|
- list: (separate=True) Returns each line into a list of separate strings.
|
|
|
- str: (separate=False) Returns all lines as one string.
|
|
|
+ list: (separate=True) Return each line into a list of separated strings.
|
|
|
+ str: (separate=False) Return all lines as one string.
|
|
|
"""
|
|
|
|
|
|
def remove_whitespace(text: str):
|
|
@@ -613,6 +664,6 @@ class LCD:
|
|
|
return buffer
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
- lcd = LCD(lcd_line_mode=0x03)
|
|
|
+ lcd = LCD(lcd_line_mode=LCD.TWO_LINES)
|
|
|
lcd.print("Hochschule Anhalt")
|
|
|
lcd.lcd_brightness = 50
|