__init__.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. """
  2. Author: Tobias Müller
  3. Date: 13.09.2023
  4. Version: 1.0
  5. This file contains a class for a LCD.
  6. """
  7. ############################## Modules ##############################
  8. from machine import Pin, PWM, I2C
  9. import re
  10. ############################## Code ##############################
  11. class LCD:
  12. """
  13. Class to control a DOGS104-A LC-Display.
  14. """
  15. ############################## Attributes ##############################
  16. __lcd_setting_para = {"LCD_PARA_EXT_FUNC": 0x09, # Extended Function (4 line mode) # type: ignore
  17. "LCD_PARA_BIAS_SET": 0x12, # Bias Setting (BS1 = 1) # type: ignore
  18. "LCD_PARA_INT_OSC": 0x1B, # Internal OSC (BS0 = 1, Bias = 1/6)
  19. "LCD_PARA_FOL_CON": 0x6E, # Follower Control (Devider on, Set Value)
  20. "LCD_PARA_DIS_CON": 0x0C} # Display on/off Control (Cursor off, Blink off)
  21. __lcd_contrast_para = {"LCD_PARA_POW_CON": 0x54, # Power Control (Booster on) and Contrast (C5, C4)
  22. "LCD_PARA_CONTRAST": 0x70} # Contrast (C3 - C0)
  23. __lcd_line_mode_para = {"LCD_PARA_LINES": 0x12} # Line Mode
  24. __lcd_direction_para = {"LCD_PARA_ENTRY_MODE": 0x04} # Display direction / orientation
  25. __lcd_control_para_priv = {"CONTROL_BYTE": 0x00, # Control Byte
  26. "CONTROL_BYTE_C0": 0x80, # Control Byte (C0 = 1)
  27. "CONTROL_BYTE_RS": 0x40, # Control Byte (RS = 1)
  28. "CONTROL_BYTE_C0_RS": 0xC0, # Control Byte (C0 = 1, RS = 1)
  29. "FUNCTION_SET": 0x38, # Function Set
  30. "FUNCTION_SET_RE": 0x3A, # Function Set (RE = 1)
  31. "FUNCTION_SET_IS": 0x39, # Function Set (IS = 1)
  32. "CLEAR_DISPLAY": 0x01, # Clear Display
  33. "DISPLAY_ROW_1": 0x80, # Display Row 1
  34. "DISPLAY_ROW_2": 0xA0, # Display Row 2
  35. "DISPLAY_ROW_3": 0xC0, # Display Row 3
  36. "DISPLAY_ROW_4": 0xE0, # Display Row 4
  37. "DISPLAY_COLUMN": 0x0A} # Columns per Display Row
  38. __lcd_rom_para = {"LCD_PARA_ROM_SEL": 0x72}
  39. __lcd_rom = {"ROM A": 0, "ROM B": 1, "ROM C": 2,
  40. "A": 0, "B": 1, "C": 2,
  41. 1: 0, 2: 1, 3: 2}
  42. __i2c_ports = {"I2C0": {"SDA_PINS": [0, 4 ,8 ,12 ,16 ,20],
  43. "SCL_PINS": [1, 5, 9, 13, 17, 21]},
  44. "I2C1": {"SDA_PINS": [2, 6, 10, 14, 18, 26],
  45. "SCL_PINS": [3, 7, 11, 15, 19, 27]}}
  46. __i2c_frequencies = {"STANDARD_MODE": 100000,
  47. "FAST_MODE": 400000}
  48. __i2c_address = 0x3c
  49. ############################## Constructor ##############################
  50. def __init__(self,
  51. i2c_sda_pin: int|str = 2,
  52. i2c_scl_pin: int|str = 3,
  53. i2c_frequency: int|str = "STANDARD_MODE",
  54. lcd_backlight_pin: int|str = 28,
  55. lcd_brightness: int = 100,
  56. lcd_contrast: int = 60,
  57. lcd_ascii_rom: int|str = "ROM B",
  58. lcd_line_mode: int = 0,
  59. lcd_direction_top: int|bool = False):
  60. """
  61. Initialize LCD. Alls arguments can be changed later as a property.
  62. Args:
  63. i2c_sda_pin (int | str, optional): Set SDA-Pin for I2C-Bus. Defaults to 2.
  64. i2c_scl_pin (int | str, optional): Set SCL-Pin for I2C-Bus. Defaults to 3.
  65. i2c_frequency (int | str, optional): Set I2C frequency. Defaults to "STANDARD_MODE".
  66. lcd_backlight_pin (int | str, optional): Set LCD backlight pin. Defaults to 28.
  67. lcd_brightness (int, optional): Set LCD brightness in percent. Defaults to 100.
  68. lcd_contrast (int, optional): Set LCD contrast in percent. Defaults to 60.
  69. lcd_ascii_rom (int | str, optional): Set ASCII-ROM. Defaults to "ROM B".
  70. lcd_line_mode (int, optional): Set LCD line mode for 2-, 3- or 4-lines. Defaults to 0.
  71. lcd_direction_top (int | bool, optional): Set LCD direction to top. Defaults to False.
  72. """
  73. # Check and store input values
  74. self.__i2c_bus, self.__i2c_sda_pin, self.__i2c_scl_pin = self.__check_i2c_ports(i2c_sda_pin,
  75. i2c_scl_pin)
  76. self.__i2c_frequency = self.__check_i2c_frequency(i2c_frequency)
  77. self.__lcd_backlight_pin = self.__check_lcd_backlight_pin(lcd_backlight_pin)
  78. self.__lcd_brightness = self.__check_lcd_brightness(lcd_brightness)
  79. self.__lcd_contrast = self.__check_lcd_contrast(lcd_contrast)
  80. self.__lcd_ascii_rom = self.__check_lcd_ascii_rom(lcd_ascii_rom)
  81. self.__lcd_line_mode = self.__check_lcd_line_mode(lcd_line_mode)
  82. self.__lcd_direction_top = self.__check_lcd_direction_top(lcd_direction_top)
  83. # init i2c and backlight
  84. self.__init_i2c()
  85. self.__init_backlight_pwm()
  86. ############################## Properties private ##############################
  87. @property
  88. def __lcd_control_para(self):
  89. lcd_control_para = self.__lcd_control_para_priv.copy()
  90. if self.__lcd_line_mode:
  91. lcd_control_para["FUNCTION_SET"] = 0x3C
  92. if self.__lcd_direction_top:
  93. lcd_control_para["DISPLAY_ROW_1"] = 0x8A
  94. lcd_control_para["DISPLAY_ROW_2"] = 0xAA
  95. lcd_control_para["DISPLAY_ROW_3"] = 0xCA
  96. lcd_control_para["DISPLAY_ROW_4"] = 0xEA
  97. return lcd_control_para
  98. ############################## Properties ##############################
  99. @property
  100. def lcd_brightness(self):
  101. return self.__lcd_brightness
  102. @lcd_brightness.setter
  103. def lcd_brightness(self, lcd_brightness: int):
  104. self.__lcd_brightness = self.__check_lcd_brightness(lcd_brightness)
  105. self.__set_lcd_brightness()
  106. @property
  107. def lcd_ascii_rom(self):
  108. return self.__lcd_ascii_rom
  109. @lcd_ascii_rom.setter
  110. def lcd_ascii_rom(self, lcd_ascii_rom: int|str):
  111. self.__lcd_ascii_rom = self.__check_lcd_ascii_rom(lcd_ascii_rom)
  112. self.__set_lcd_ascii_rom()
  113. @property
  114. def lcd_contrast(self):
  115. return self.__lcd_contrast
  116. @lcd_contrast.setter
  117. def lcd_contrast(self, lcd_contrast: int):
  118. print("Hallo")
  119. self.__lcd_contrast = self.__check_lcd_contrast(lcd_contrast)
  120. self.__set_lcd_contrast()
  121. @property
  122. def lcd_direction_top(self):
  123. return self.__lcd_direction_top
  124. @lcd_direction_top.setter
  125. def lcd_direction_top(self, lcd_direction_top: int|bool):
  126. buffer = self.read()
  127. self.__lcd_direction_top = self.__check_lcd_direction_top(lcd_direction_top)
  128. self.clear()
  129. self.__set_lcd_direction()
  130. self.clear()
  131. self.print(str(buffer))
  132. @property
  133. def lcd_line_mode(self):
  134. return self.__lcd_line_mode
  135. @lcd_line_mode.setter
  136. def lcd_line_mode(self, lcd_line_mode: int):
  137. buffer = self.__check_lcd_line_mode(lcd_line_mode)
  138. if lcd_line_mode and self.__lcd_line_mode == 0:
  139. self.clear(row=4)
  140. if lcd_line_mode == 3 and self.__lcd_line_mode != 3:
  141. self.clear(row=3)
  142. self.__lcd_line_mode = buffer
  143. self.__set_lcd_line_mode()
  144. @property
  145. def lcd_backlight_pin(self):
  146. return self.__lcd_backlight_pin
  147. @lcd_backlight_pin.setter
  148. def lcd_backlight_pin(self, lcd_backlight_pin: int|str):
  149. self.__check_lcd_backlight_pin(lcd_backlight_pin)
  150. self.__deinit_backlight_pwm()
  151. self.lcd_backlight_pin = lcd_backlight_pin
  152. self.__init_backlight_pwm()
  153. @property
  154. def i2c_frequency(self):
  155. return self.__i2c_frequency
  156. @i2c_frequency.setter
  157. def i2c_frequency(self, i2c_frequency: int|str):
  158. self.__check_i2c_frequency(i2c_frequency)
  159. self.__deinit_i2c()
  160. self.__i2c_frequency = i2c_frequency
  161. self.__init_i2c()
  162. @property
  163. def i2c_sda_pin(self):
  164. return self.__i2c_sda_pin
  165. @i2c_sda_pin.setter
  166. def i2c_sda_pin(self, i2c_sda_pin: int|str):
  167. self.change_i2c_port(i2c_sda_pin, self.__i2c_scl_pin)
  168. @property
  169. def i2c_scl_pin(self):
  170. return self.__i2c_scl_pin
  171. @i2c_scl_pin.setter
  172. def i2c_scl_pin(self, i2c_scl_pin: int|str):
  173. self.change_i2c_port(self.__i2c_sda_pin, i2c_scl_pin)
  174. ############################## Methods private ##############################
  175. @staticmethod
  176. def __reset_pin(pin: int|str):
  177. Pin(pin, Pin.IN, pull=None)
  178. @staticmethod
  179. def __check_lcd_brightness(lcd_brightness: int):
  180. if not isinstance(lcd_brightness, int):
  181. raise ValueError(f"Value \"{lcd_brightness}\" of \"lcd_brightness\" is not type \"int\"!")
  182. elif lcd_brightness < 0 or lcd_brightness > 100:
  183. raise ValueError(f"Value \"{lcd_brightness}\" of \"lcd_brightness\" is out of range!")
  184. else:
  185. return lcd_brightness
  186. @staticmethod
  187. def __check_lcd_backlight_pin(lcd_backlight_pin: int|str):
  188. if not isinstance(lcd_backlight_pin, int) and not isinstance(lcd_backlight_pin, str):
  189. raise ValueError(f"Value \"{lcd_backlight_pin}\" of \"lcd_backlight_pin\" is not type" +
  190. " \"int\" or \"str\"!")
  191. elif isinstance(lcd_backlight_pin, str) and not re.match(r"^GP\d\d?$",lcd_backlight_pin):
  192. raise ValueError(f"String value \"{lcd_backlight_pin}\" of \"lcd_backlight_pin\" has" +
  193. " wrong format!")
  194. else:
  195. return lcd_backlight_pin
  196. @staticmethod
  197. def __check_lcd_line_mode(lcd_line_mode: int):
  198. if not isinstance(lcd_line_mode, int):
  199. raise ValueError(f"Value \"{lcd_line_mode}\" of \"lcd_line_mode\" is not type \"int\"!")
  200. elif lcd_line_mode < 0 or lcd_line_mode > 4:
  201. raise ValueError(f"Value \"{lcd_line_mode}\" of \"lcd_line_mode\" is out of range!")
  202. else:
  203. return lcd_line_mode
  204. @staticmethod
  205. def __check_lcd_direction_top(lcd_direction_top: int|bool):
  206. if not isinstance(lcd_direction_top, int) and not isinstance(lcd_direction_top, bool):
  207. raise ValueError(f"Value \"{lcd_direction_top}\" of \"lcd_direction_top\" is not type" +
  208. " \"int\" or \"bool\"!")
  209. elif isinstance(lcd_direction_top, int) and (lcd_direction_top < 0 or lcd_direction_top > 1):
  210. raise ValueError(f"Value \"{lcd_direction_top}\" of \"lcd_direction_top\" is out of range!")
  211. else:
  212. return lcd_direction_top
  213. @staticmethod
  214. def __check_lcd_contrast(lcd_contrast: int):
  215. if not isinstance(lcd_contrast, int):
  216. raise ValueError(f"Value \"{lcd_contrast}\" of \"lcd_contrast\" is not type \"int\"!")
  217. elif lcd_contrast < 0 or lcd_contrast > 100:
  218. raise ValueError(f"Value \"{lcd_contrast}\" of \"lcd_contrast\" is out of range!")
  219. else:
  220. return lcd_contrast
  221. def __check_lcd_ascii_rom(self, lcd_ascii_rom: int|str):
  222. if not isinstance(lcd_ascii_rom, int) and not isinstance(lcd_ascii_rom, str):
  223. raise ValueError(f"Value \"{lcd_ascii_rom}\" of \"lcd_ascii_rom\" is not type" +
  224. " \"int\" or \"str\"!")
  225. elif((isinstance(lcd_ascii_rom, int) and lcd_ascii_rom not in self.__lcd_rom.keys()) or
  226. (isinstance(lcd_ascii_rom, str) and lcd_ascii_rom.upper() not in self.__lcd_rom.keys())):
  227. raise ValueError(f"Value \"{lcd_ascii_rom}\" of \"lcd_ascii_rom\" is not supported!")
  228. else:
  229. return lcd_ascii_rom
  230. def __check_i2c_ports(self, i2c_sda_pin: int|str, i2c_scl_pin: int|str):
  231. if not isinstance(i2c_sda_pin, int) and not isinstance(i2c_sda_pin, str):
  232. raise ValueError(f"Value \"{i2c_sda_pin}\" of \"i2c_sda_pin\" is not type \"int\"" +
  233. " or \"str\"!")
  234. elif not isinstance(i2c_scl_pin, int) and not isinstance(i2c_scl_pin, str):
  235. raise ValueError(f"Value \"{i2c_scl_pin}\" of \"i2c_scl_pin\" is not type \"int\"" +
  236. " or \"str\"!")
  237. elif isinstance(i2c_sda_pin, str) and not re.match(r"^GP\d\d?$",i2c_sda_pin):
  238. raise ValueError(f"String value \"{i2c_sda_pin}\" of \"i2c_sda_pin\" has wrong format!")
  239. elif isinstance(i2c_scl_pin, str) and not re.match(r"^GP\d\d?$",i2c_scl_pin):
  240. raise ValueError(f"String value \"{i2c_scl_pin}\" of \"i2c_scl_pin\" has wrong format!")
  241. elif ((isinstance(i2c_sda_pin, int) and
  242. not i2c_sda_pin in self.__i2c_ports["I2C0"]["SDA_PINS"] and
  243. not i2c_sda_pin in self.__i2c_ports["I2C1"]["SDA_PINS"]) or
  244. (isinstance(i2c_sda_pin, str) and
  245. not int(i2c_sda_pin[2:]) in self.__i2c_ports["I2C0"]["SDA_PINS"] and
  246. not int(i2c_sda_pin[2:]) in self.__i2c_ports["I2C1"]["SDA_PINS"])):
  247. raise ValueError(f"Value \"{i2c_sda_pin}\" of \"i2c_sda_pin\" is not an I2C SDA-Pin!")
  248. elif ((isinstance(i2c_scl_pin, int) and
  249. not i2c_scl_pin in self.__i2c_ports["I2C0"]["SCL_PINS"] and
  250. not i2c_scl_pin in self.__i2c_ports["I2C1"]["SCL_PINS"]) or
  251. (isinstance(i2c_scl_pin, str) and
  252. not int(i2c_scl_pin[2:]) in self.__i2c_ports["I2C0"]["SCL_PINS"] and
  253. not int(i2c_scl_pin[2:]) in self.__i2c_ports["I2C1"]["SCL_PINS"])):
  254. raise ValueError(f"Value \"{i2c_scl_pin}\" of \"i2c_scl_pin\" is not an I2C SCL-Pin!")
  255. elif ((((isinstance(i2c_sda_pin, int) and
  256. i2c_sda_pin in self.__i2c_ports["I2C0"]["SDA_PINS"]) or
  257. (isinstance(i2c_sda_pin, str) and
  258. int(i2c_sda_pin[2:]) in self.__i2c_ports["I2C0"]["SDA_PINS"])) and not
  259. ((isinstance(i2c_scl_pin, int) and
  260. i2c_scl_pin in self.__i2c_ports["I2C0"]["SCL_PINS"]) or
  261. (isinstance(i2c_scl_pin, str) and
  262. int(i2c_scl_pin[2:]) in self.__i2c_ports["I2C0"]["SCL_PINS"]))) or
  263. (((isinstance(i2c_sda_pin, int) and
  264. i2c_sda_pin in self.__i2c_ports["I2C1"]["SDA_PINS"]) or
  265. (isinstance(i2c_sda_pin, str) and
  266. int(i2c_sda_pin[2:]) in self.__i2c_ports["I2C1"]["SDA_PINS"])) and not
  267. ((isinstance(i2c_scl_pin, int) and
  268. i2c_scl_pin in self.__i2c_ports["I2C1"]["SCL_PINS"]) or
  269. (isinstance(i2c_scl_pin, str) and
  270. int(i2c_scl_pin[2:]) in self.__i2c_ports["I2C1"]["SCL_PINS"])))):
  271. raise ValueError(f"Value \"{i2c_scl_pin}\" of \"i2c_scl_pin\" and " +
  272. f"Value \"{i2c_sda_pin}\" of \"i2c_sda_pin\" " +
  273. "are not part of the same I2C Bus!")
  274. else:
  275. if ((isinstance(i2c_sda_pin, int) and
  276. i2c_sda_pin in self.__i2c_ports["I2C0"]["SDA_PINS"]) or
  277. (isinstance(i2c_sda_pin, str) and
  278. int(i2c_sda_pin[2:]) in self.__i2c_ports["I2C0"]["SDA_PINS"])):
  279. return 0, i2c_sda_pin, i2c_scl_pin
  280. else:
  281. return 1, i2c_sda_pin, i2c_scl_pin
  282. def __check_i2c_frequency(self, i2c_frequency: int|str):
  283. if not isinstance(i2c_frequency, int) and not isinstance(i2c_frequency, str):
  284. raise ValueError(f"Value \"{i2c_frequency}\" of \"i2c_frequency\" is not type \"int\"" +
  285. " or \"str\"!")
  286. elif (isinstance(i2c_frequency, int) and not i2c_frequency in self.__i2c_frequencies.values() or
  287. isinstance(i2c_frequency, str) and not i2c_frequency in self.__i2c_frequencies.keys()):
  288. raise ValueError(f"Value \"{i2c_frequency}\" of \"i2c_frequency\" is not supported!")
  289. else:
  290. return i2c_frequency
  291. def __init_i2c(self):
  292. self.__i2c = I2C(self.__i2c_bus,
  293. sda=Pin(self.__i2c_sda_pin),
  294. scl=Pin(self.__i2c_scl_pin),
  295. freq=(self.__i2c_frequency if isinstance(self.__i2c_frequency, int) else
  296. self.__i2c_frequencies[self.__i2c_frequency]))
  297. buffer = (self.__lcd_control_para["CONTROL_BYTE"],
  298. self.__lcd_control_para["FUNCTION_SET_RE"],
  299. self.__lcd_setting_para["LCD_PARA_EXT_FUNC"],
  300. self.__lcd_setting_para["LCD_PARA_BIAS_SET"],
  301. self.__lcd_control_para["FUNCTION_SET_IS"],
  302. self.__lcd_setting_para["LCD_PARA_INT_OSC"],
  303. self.__lcd_setting_para["LCD_PARA_FOL_CON"],
  304. self.__lcd_control_para["FUNCTION_SET"],
  305. self.__lcd_setting_para["LCD_PARA_DIS_CON"])
  306. self.__i2c.writeto(self.__i2c_address, bytes(buffer))
  307. self.__set_lcd_line_mode()
  308. self.__set_lcd_ascii_rom()
  309. self.__set_lcd_contrast()
  310. self.__set_lcd_direction()
  311. self.clear()
  312. def __deinit_i2c(self):
  313. self.__reset_pin(self.__i2c_scl_pin)
  314. self.__reset_pin(self.__i2c_sda_pin)
  315. def __init_backlight_pwm(self):
  316. self.__backlight_pwm = PWM(Pin(self.__lcd_backlight_pin))
  317. self.__backlight_pwm.freq(1000)
  318. self.__set_lcd_brightness()
  319. def __deinit_backlight_pwm(self):
  320. self.__backlight_pwm.deinit()
  321. self.__reset_pin(self.__lcd_backlight_pin)
  322. def __set_lcd_brightness(self):
  323. self.__backlight_pwm.duty_u16(round(65635 * (self.__lcd_brightness / 100)))
  324. def __set_lcd_ascii_rom(self):
  325. if isinstance(self.__lcd_ascii_rom, str):
  326. lcd_ascii_rom = self.__lcd_ascii_rom.upper()
  327. else:
  328. lcd_ascii_rom = self.__lcd_ascii_rom
  329. buffer = (self.__lcd_control_para["CONTROL_BYTE_C0"],
  330. self.__lcd_control_para["FUNCTION_SET_RE"],
  331. self.__lcd_control_para["CONTROL_BYTE_C0"],
  332. self.__lcd_rom_para["LCD_PARA_ROM_SEL"],
  333. self.__lcd_control_para["CONTROL_BYTE_C0_RS"],
  334. self.__lcd_rom[lcd_ascii_rom] << 0x02,
  335. self.__lcd_control_para["CONTROL_BYTE"],
  336. self.__lcd_control_para["FUNCTION_SET"])
  337. self.__i2c.writeto(self.__i2c_address, bytes(buffer))
  338. def __set_lcd_contrast(self):
  339. contrast = round(63 * (self.__lcd_contrast / 100))
  340. buffer = (self.__lcd_control_para["CONTROL_BYTE"],
  341. self.__lcd_control_para["FUNCTION_SET_IS"],
  342. self.__lcd_contrast_para["LCD_PARA_POW_CON"] | (contrast >> 0x04),
  343. self.__lcd_contrast_para["LCD_PARA_CONTRAST"] | (contrast & 0x0F),
  344. self.__lcd_control_para["FUNCTION_SET"])
  345. self.__i2c.writeto(self.__i2c_address, bytes(buffer))
  346. def __set_lcd_direction(self):
  347. buffer = [self.__lcd_control_para["CONTROL_BYTE"],
  348. self.__lcd_control_para["FUNCTION_SET_RE"]]
  349. if self.__lcd_direction_top:
  350. buffer.append(self.__lcd_direction_para["LCD_PARA_ENTRY_MODE"] | 0x01)
  351. else:
  352. buffer.append(self.__lcd_direction_para["LCD_PARA_ENTRY_MODE"] | 0x02)
  353. buffer.append(self.__lcd_control_para["FUNCTION_SET"])
  354. self.__i2c.writeto(self.__i2c_address, bytes(buffer))
  355. def __set_lcd_line_mode(self):
  356. buffer = [self.__lcd_control_para["CONTROL_BYTE"],
  357. self.__lcd_control_para["FUNCTION_SET_RE"]]
  358. if self.__lcd_line_mode:
  359. buffer.append(self.__lcd_line_mode_para["LCD_PARA_LINES"] |
  360. ((self.__lcd_line_mode - 1) << 0x02))
  361. else:
  362. buffer.append(self.__lcd_line_mode_para["LCD_PARA_LINES"])
  363. buffer.append(self.__lcd_control_para["FUNCTION_SET"])
  364. self.__i2c.writeto(self.__i2c_address, bytes(buffer))
  365. def __check_keywords(self, keywords: dict[str,int|bool], keys: list[str]):
  366. rows = 0x04 if self.__lcd_line_mode == 0x00 else 0x02 if self.__lcd_line_mode == 0x03 else 0x03
  367. output = {"rows": rows, "row": 0x01, "column": 0x01, "auto_line_break": True, "separate": False}
  368. for key, val in keywords.items():
  369. if key not in keys:
  370. raise KeyError(f"Keyword \"{key}\" is unknown!")
  371. elif key == "row":
  372. if not isinstance(val, int):
  373. raise ValueError(f"Value \"{val}\" of keyword \"{key}\" is not type \"int\"!")
  374. elif val > rows or val < 1:
  375. raise ValueError(f"Value \"{val}\" of keyword \"{key}\" is out of range!")
  376. else:
  377. output.update({key: val})
  378. elif key == "column":
  379. if not isinstance(val, int):
  380. raise ValueError(f"Value \"{val}\" of keyword \"{key}\" is not type \"int\"!")
  381. elif val > self.__lcd_control_para["DISPLAY_COLUMN"] or val < 0x01:
  382. raise ValueError(f"Value \"{val}\" of keyword \"{key}\" is out of range!")
  383. else:
  384. output.update({key: val})
  385. elif key == "auto_line_break":
  386. if not isinstance(val, int) and not isinstance(val, bool):
  387. raise ValueError(f"Value \"{val}\" of keyword \"{key}\" is not type" +
  388. " \"int\" or \"bool\"!")
  389. elif isinstance(val, int) and (val < 0 or val > 1):
  390. raise ValueError(f"Value \"{val}\" of keyword \"{key}\" is out of range!")
  391. else:
  392. output.update({key: val})
  393. elif key == "separate":
  394. if not isinstance(val, int) and not isinstance(val, bool):
  395. raise ValueError(f"Value \"{val}\" of keyword \"{key}\" is not type" +
  396. " \"int\" or \"bool\"!")
  397. elif isinstance(val, int) and (val < 0 or val > 1):
  398. raise ValueError(f"Value \"{val}\" of keyword \"{key}\" is out of range!")
  399. else:
  400. output.update({key: val})
  401. return output.copy()
  402. ############################## Methods public ##############################
  403. def change_i2c_port(self, i2c_sda_pin: int|str, i2c_scl_pin: int|str):
  404. """
  405. Used to change I2C port.
  406. Args:
  407. i2c_sda_pin (int | str): SDA-Pin of I2C Port
  408. i2c_scl_pin (int | str): SCL-Pin of I2C Port
  409. """
  410. self.__check_i2c_ports(i2c_sda_pin, i2c_scl_pin)
  411. self.__deinit_i2c()
  412. (self.__i2c_bus,
  413. self.__i2c_sda_pin,
  414. self.__i2c_scl_pin) = self.__check_i2c_ports(i2c_sda_pin, i2c_scl_pin)
  415. self.__init_i2c()
  416. def print(self, text: str, **keywords):
  417. """
  418. Used to print text on the LCD.
  419. Args:
  420. text (str): Text, which is printed on the LCD.
  421. Keywords:
  422. row (int): Row to start printing. Defaults to 1.
  423. column (int): Column to start printing. Defaults to 1.
  424. auto_line_break (int | bool): Breaks the line automatically. Defaults to True.
  425. """
  426. def check_text(text: str):
  427. if not isinstance(text, str):
  428. raise ValueError(f"Value \"{text}\" of \"text\" is not type \"str\"!")
  429. elif len(text) == 0:
  430. raise ValueError(f"Value \"{text}\" of \"text\" is out of range!")
  431. keys = ["row", "column", "auto_line_break"]
  432. keys = self.__check_keywords(keywords, keys)
  433. rows = keys["rows"]
  434. row = keys["row"]
  435. column = keys["column"]
  436. auto_line_break = keys["auto_line_break"]
  437. check_text(text)
  438. linefeed = ("\n","\r")
  439. buffer = []
  440. for line in range(row,rows + 0x01):
  441. if len(text) == 0x00 or (not auto_line_break and line > row):
  442. break
  443. buffer.append(self.__lcd_control_para["CONTROL_BYTE_C0"])
  444. buffer.append(self.__lcd_control_para["DISPLAY_ROW_" + str(line)] + (column - 0x01))
  445. for char in range(0x00, (self.__lcd_control_para["DISPLAY_COLUMN"] - (column - 0x01)) if
  446. len(text) > (self.__lcd_control_para["DISPLAY_COLUMN"] - (column - 0x01)) else
  447. len(text)):
  448. if text[char] in linefeed:
  449. text = text[char + 0x01:]
  450. for _ in range(char, (self.__lcd_control_para["DISPLAY_COLUMN"] - (column - 0x01)) if
  451. len(text) > (self.__lcd_control_para["DISPLAY_COLUMN"] - (column - 0x01)) else
  452. len(text)):
  453. buffer.append(self.__lcd_control_para["CONTROL_BYTE_C0_RS"])
  454. buffer.append(ord(" "))
  455. break
  456. buffer.append(self.__lcd_control_para["CONTROL_BYTE_C0_RS"])
  457. buffer.append(ord(text[char]))
  458. else:
  459. text = text[self.__lcd_control_para["DISPLAY_COLUMN"] - (column - 0x01):]
  460. column = 0x01
  461. self.__i2c.writeto(self.__i2c_address, bytes(buffer))
  462. def clear(self, **keywords):
  463. """
  464. Used to clear the LCD. If the keywords row or column are not used,
  465. the method will clear the whole display.
  466. Keywords:
  467. row (int): Row to start printing. Defaults to 1.
  468. column (int): Column to start printing. Defaults to 1.
  469. auto_line_break (int | bool): Breaks the line automatically. Defaults to True.
  470. """
  471. buffer = []
  472. if len(keywords) == 0:
  473. buffer.append(self.__lcd_control_para["CONTROL_BYTE"])
  474. buffer.append(self.__lcd_control_para["CLEAR_DISPLAY"])
  475. else:
  476. keys = ["row", "column"]
  477. keys = self.__check_keywords(keywords, keys)
  478. row = keys["row"]
  479. column = keys["column"]
  480. buffer.append(self.__lcd_control_para["CONTROL_BYTE_C0"])
  481. buffer.append(self.__lcd_control_para["DISPLAY_ROW_" + str(row)] + (column - 0x01))
  482. for _ in range(0x00, self.__lcd_control_para["DISPLAY_COLUMN"]):
  483. buffer.append(self.__lcd_control_para["CONTROL_BYTE_C0_RS"])
  484. buffer.append(ord(" "))
  485. if "column" in keywords.keys():
  486. break
  487. self.__i2c.writeto(self.__i2c_address, bytes(buffer))
  488. def read(self, **keywords):
  489. """
  490. Used to read from the LCD.
  491. Keywords:
  492. row (int): Row to start reading from. Defaults to 1.
  493. column (int): Column to start reading from. Defaults to 1.
  494. separate (int | bool): Separate each line into a list. Defaults to False.
  495. Returns:
  496. list: (separate=True) Returns each line into a list of separate strings.
  497. str: (separate=False) Returns all lines as one string.
  498. """
  499. def remove_whitespace(text: str):
  500. textlen = len(text)
  501. endpos = textlen
  502. for char in range(0x00, textlen):
  503. if text[textlen - char - 0x01] == " ":
  504. endpos -= 0x01
  505. else:
  506. break
  507. return text[:endpos]
  508. keys = ["row", "column", "separate"]
  509. keys = self.__check_keywords(keywords, keys)
  510. rows = keys["rows"]
  511. row = keys["row"]
  512. column = keys["column"]
  513. separate = keys["separate"]
  514. buffer = []
  515. for line in range(0x01, rows + 0x01):
  516. tmp = (self.__lcd_control_para["CONTROL_BYTE_C0"],
  517. self.__lcd_control_para["DISPLAY_ROW_" + str(line)],
  518. self.__lcd_control_para["CONTROL_BYTE_RS"])
  519. self.__i2c.writeto(self.__i2c_address,bytes(tmp))
  520. tmp = self.__i2c.readfrom(self.__i2c_address,11)
  521. tmp = list(tmp[1:])
  522. tmp = [chr(tmp[i]) for i in range(0, len(tmp))]
  523. if ("row" in keywords.keys() or "column" in keywords.keys()) and line != row:
  524. tmp = [""]
  525. if "column" in keywords.keys() and line == row:
  526. tmp = tmp[column - 0x01]
  527. buffer.append("".join(tmp))
  528. if not separate:
  529. return remove_whitespace("".join(buffer))
  530. else:
  531. for pos in range(0, len(buffer)):
  532. buffer[pos] = remove_whitespace(buffer[pos])
  533. return buffer
  534. if __name__ == "__main__":
  535. lcd = LCD(lcd_line_mode=0x03)
  536. lcd.print("Hochschule Anhalt")
  537. lcd.lcd_brightness = 50