Skip to content
Next Next commit
Update character_lcd.py
Update so `self.message` respects the `cursor_position()` settings. If `'\n'` is in a message string, the new line will be use the same column setting as the first line.
  • Loading branch information
profbrady authored Apr 6, 2019
commit c7d02b565472af548f6bec733a563cc9df8e57b9
16 changes: 13 additions & 3 deletions adafruit_character_lcd/character_lcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ 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
Copy link
Collaborator

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, done

self.row = 0
self.column = 0
# pylint: enable-msg=too-many-arguments

def home(self):
Expand Down Expand Up @@ -243,6 +247,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):
Expand Down Expand Up @@ -331,7 +338,8 @@ 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
Expand All @@ -340,15 +348,17 @@ def message(self, message):
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 self.column instead of 0
col = self.column if self.displaymode & _LCD_ENTRYLEFT > 0 else self.columns - 1
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 at self.column instead of 0
col = self.column if self.displaymode & _LCD_ENTRYLEFT > 0 else self.columns - 1
self.cursor_position(col, line)
# Write string to display
else:
Expand Down