-
Notifications
You must be signed in to change notification settings - Fork 51
Update character_lcd.py #37
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
Changes from 11 commits
c7d02b5
d6338ab
a6eb4ef
2516129
2c1a7a4
5bc8ec5
7ef2cc6
3d4340c
4ffe6de
eb8612e
ff1df52
f9d662e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,6 +169,11 @@ def __init__(self, rs, en, d4, d5, d6, d7, columns, lines | |
self._message = None | ||
self._enable = None | ||
self._direction = None | ||
# track row and column used in cursor_position | ||
# itialize to 0,0 | ||
self.row = 0 | ||
self.column = 0 | ||
self._column_align = False | ||
# pylint: enable-msg=too-many-arguments | ||
|
||
def home(self): | ||
|
@@ -198,6 +203,20 @@ def clear(self): | |
self._write8(_LCD_CLEARDISPLAY) | ||
time.sleep(0.003) | ||
|
||
@property | ||
def column_align(self): | ||
"""If True, message text after '\n' starts directly below start of first | ||
character in message. If False, text after '\n' starts at column zero. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Been looking into this since you submitted changes. It looks like it's failing in travis because the \n's need to be escaped. Instead of '\n' use '\\n' (two slashes. I had to use three for formatting the comment) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
""" | ||
return self._column_align | ||
|
||
@column_align.setter | ||
def column_align(self, enable): | ||
if isinstance(enable, bool): | ||
self._column_align = enable | ||
else: | ||
raise ValueError('The column_align value must be either True or False') | ||
|
||
@property | ||
def cursor(self): | ||
"""True if cursor is visible. False to stop displaying the cursor. | ||
|
@@ -230,7 +249,8 @@ def cursor(self, show): | |
self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol) | ||
|
||
def cursor_position(self, column, row): | ||
"""Move the cursor to position ``column``, ``row`` | ||
"""Move the cursor to position ``column``, ``row`` for the next | ||
message only. Displaying a message resets the cursor position to (0, 0). | ||
|
||
:param column: column location | ||
:param row: row location | ||
|
@@ -243,6 +263,9 @@ def cursor_position(self, column, row): | |
column = self.columns - 1 | ||
# Set location | ||
self._write8(_LCD_SETDDRAMADDR | (column + _LCD_ROW_OFFSETS[row])) | ||
# Update self.row and self.column to match setter | ||
self.row = row | ||
self.column = column | ||
|
||
@property | ||
def blink(self): | ||
|
@@ -310,6 +333,11 @@ def display(self, enable): | |
@property | ||
def message(self): | ||
"""Display a string of text on the character LCD. | ||
Start position is (0,0) if cursor_position is not set. | ||
If cursor_position is set, message starts at the set | ||
position from the left for left to right text and from | ||
the right for right to left text. Resets cursor column | ||
and row to (0,0) after displaying the message. | ||
|
||
The following example displays, "Hello, world!" on the LCD. | ||
|
||
|
@@ -331,28 +359,45 @@ def message(self): | |
@message.setter | ||
def message(self, message): | ||
self._message = message | ||
line = 0 | ||
# Set line to match self.row from cursor_position() | ||
line = self.row | ||
# Track times through iteration, to act on the initial character of the message | ||
initial_character = 0 | ||
# iterate through each character | ||
for character in message: | ||
# If this is the first character in the string: | ||
if initial_character == 0: | ||
# Start at (1, 1) unless direction is set right to left, in which case start | ||
# on the opposite side of the display. | ||
col = 0 if self.displaymode & _LCD_ENTRYLEFT > 0 else self.columns - 1 | ||
# Start at (0, 0) unless direction is set right to left, in which case start | ||
# on the opposite side of the display if cursor_position not set or (0,0) | ||
# If cursor_position is set then starts at the specified location for | ||
# LEFT_TO_RIGHT. If RIGHT_TO_LEFT cursor_position is determined from right. | ||
# allows for cursor_position to work in RIGHT_TO_LEFT mode | ||
if self.displaymode & _LCD_ENTRYLEFT > 0: | ||
col = self.column | ||
else: | ||
col = self.columns - 1 - self.column | ||
self.cursor_position(col, line) | ||
initial_character += 1 | ||
# If character is \n, go to next line | ||
if character == '\n': | ||
line += 1 | ||
# Start the second line at (1, 1) unless direction is set right to left in which | ||
# case start on the opposite side of the display. | ||
col = 0 if self.displaymode & _LCD_ENTRYLEFT > 0 else self.columns - 1 | ||
# Start the second line at (0, 1) unless direction is set right to left in | ||
# which case start on the opposite side of the display if cursor_position | ||
# is (0,0) or not set. Start second line at same column as first line when | ||
# cursor_position is set | ||
if self.displaymode & _LCD_ENTRYLEFT > 0: | ||
col = self.column * self._column_align | ||
else: | ||
if self._column_align: | ||
col = self.column | ||
else: | ||
col = self.columns - 1 | ||
self.cursor_position(col, line) | ||
# Write string to display | ||
else: | ||
self._write8(ord(character), True) | ||
# reset column and row to (0,0) after message is displayed | ||
self.column, self.row = 0, 0 | ||
|
||
def move_left(self): | ||
"""Moves displayed text left one column. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I figured you mean initialize here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, done