Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended print with newline and center char #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions lcd_i2c/lcd_i2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def __init__(self,
self._display_mode: int = 0
self._display_function: int = 0
self._cursor_position: Tuple[int, int] = (0, 0) # (x, y)
self._newline_char="\n"
self._center_char="\c"

@property
def addr(self) -> int:
Expand Down Expand Up @@ -396,15 +398,23 @@ def create_char(self, location: int, charmap: List[int]) -> None:
def print(self, text: str) -> None:
"""
Print text on LCD

:param test: Text to show on the LCD
:type text: str
Note:
- if self._newline_char is passed line will be broken there
- if self._center_char is found, line will be centered
"""
_cursor_x, _cursor_y = self.cursor_position

for char in text:
self._command(value=ord(char), mode=Const.RS)

split_str=text.split(self._newline_char)
for line in split_str:
if line.find('\c')>=0:
newline=line.strip('\c')
newline=newline.center(self._cols)
else:
newline=line
for char in newline:
self._command(value=ord(char), mode=Const.RS)
self.set_cursor(0,1)
self.cursor_position = (_cursor_x + len(text), _cursor_y)

def _command(self, value: int, mode: int = 0) -> None:
Expand Down Expand Up @@ -452,3 +462,4 @@ def _expander_write(self, value: int) -> None:
:type value: int
"""
self._i2c.writeto(self.addr, bytes([value | self._backlightval]))