Skip to content

Commit d0fc756

Browse files
authored
Merge pull request #37 from profbrady/profbrady-patch-1
Update character_lcd.py
2 parents fe5da67 + f9d662e commit d0fc756

File tree

2 files changed

+53
-18
lines changed

2 files changed

+53
-18
lines changed

README.rst

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,6 @@ Contributing
113113
Contributions are welcome! Please read our `Code of Conduct
114114
<https://github.com/adafruit/Adafruit_CircuitPython_CharLCD/blob/master/CODE_OF_CONDUCT.md>`_ before contributing to help this project stay welcoming.
115115

116-
Installation
117-
============
118-
119-
This library is **NOT** built into CircuitPython to make it easy to update. To
120-
install it either follow the directions below or :ref:`install the library bundle <bundle_installation>`.
121-
122-
To install:
123-
124-
#. Download and unzip the `latest release zip <https://github.com/adafruit/Adafruit_CircuitPython_CharLCD/releases>`_.
125-
#. Copy the unzipped ``adafruit_character_lcd`` to the ``lib`` directory on the ``CIRCUITPY`` or ``MICROPYTHON`` drive.
126116

127117
Building locally
128118
================

adafruit_character_lcd/character_lcd.py

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ def __init__(self, rs, en, d4, d5, d6, d7, columns, lines
169169
self._message = None
170170
self._enable = None
171171
self._direction = None
172+
# track row and column used in cursor_position
173+
# initialize to 0,0
174+
self.row = 0
175+
self.column = 0
176+
self._column_align = False
172177
# pylint: enable-msg=too-many-arguments
173178

174179
def home(self):
@@ -198,6 +203,20 @@ def clear(self):
198203
self._write8(_LCD_CLEARDISPLAY)
199204
time.sleep(0.003)
200205

206+
@property
207+
def column_align(self):
208+
"""If True, message text after '\\n' starts directly below start of first
209+
character in message. If False, text after '\\n' starts at column zero.
210+
"""
211+
return self._column_align
212+
213+
@column_align.setter
214+
def column_align(self, enable):
215+
if isinstance(enable, bool):
216+
self._column_align = enable
217+
else:
218+
raise ValueError('The column_align value must be either True or False')
219+
201220
@property
202221
def cursor(self):
203222
"""True if cursor is visible. False to stop displaying the cursor.
@@ -230,7 +249,8 @@ def cursor(self, show):
230249
self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol)
231250

232251
def cursor_position(self, column, row):
233-
"""Move the cursor to position ``column``, ``row``
252+
"""Move the cursor to position ``column``, ``row`` for the next
253+
message only. Displaying a message resets the cursor position to (0, 0).
234254
235255
:param column: column location
236256
:param row: row location
@@ -243,6 +263,9 @@ def cursor_position(self, column, row):
243263
column = self.columns - 1
244264
# Set location
245265
self._write8(_LCD_SETDDRAMADDR | (column + _LCD_ROW_OFFSETS[row]))
266+
# Update self.row and self.column to match setter
267+
self.row = row
268+
self.column = column
246269

247270
@property
248271
def blink(self):
@@ -310,6 +333,11 @@ def display(self, enable):
310333
@property
311334
def message(self):
312335
"""Display a string of text on the character LCD.
336+
Start position is (0,0) if cursor_position is not set.
337+
If cursor_position is set, message starts at the set
338+
position from the left for left to right text and from
339+
the right for right to left text. Resets cursor column
340+
and row to (0,0) after displaying the message.
313341
314342
The following example displays, "Hello, world!" on the LCD.
315343
@@ -331,28 +359,45 @@ def message(self):
331359
@message.setter
332360
def message(self, message):
333361
self._message = message
334-
line = 0
362+
# Set line to match self.row from cursor_position()
363+
line = self.row
335364
# Track times through iteration, to act on the initial character of the message
336365
initial_character = 0
337366
# iterate through each character
338367
for character in message:
339368
# If this is the first character in the string:
340369
if initial_character == 0:
341-
# Start at (1, 1) unless direction is set right to left, in which case start
342-
# on the opposite side of the display.
343-
col = 0 if self.displaymode & _LCD_ENTRYLEFT > 0 else self.columns - 1
370+
# Start at (0, 0) unless direction is set right to left, in which case start
371+
# on the opposite side of the display if cursor_position not set or (0,0)
372+
# If cursor_position is set then starts at the specified location for
373+
# LEFT_TO_RIGHT. If RIGHT_TO_LEFT cursor_position is determined from right.
374+
# allows for cursor_position to work in RIGHT_TO_LEFT mode
375+
if self.displaymode & _LCD_ENTRYLEFT > 0:
376+
col = self.column
377+
else:
378+
col = self.columns - 1 - self.column
344379
self.cursor_position(col, line)
345380
initial_character += 1
346381
# If character is \n, go to next line
347382
if character == '\n':
348383
line += 1
349-
# Start the second line at (1, 1) unless direction is set right to left in which
350-
# case start on the opposite side of the display.
351-
col = 0 if self.displaymode & _LCD_ENTRYLEFT > 0 else self.columns - 1
384+
# Start the second line at (0, 1) unless direction is set right to left in
385+
# which case start on the opposite side of the display if cursor_position
386+
# is (0,0) or not set. Start second line at same column as first line when
387+
# cursor_position is set
388+
if self.displaymode & _LCD_ENTRYLEFT > 0:
389+
col = self.column * self._column_align
390+
else:
391+
if self._column_align:
392+
col = self.column
393+
else:
394+
col = self.columns - 1
352395
self.cursor_position(col, line)
353396
# Write string to display
354397
else:
355398
self._write8(ord(character), True)
399+
# reset column and row to (0,0) after message is displayed
400+
self.column, self.row = 0, 0
356401

357402
def move_left(self):
358403
"""Moves displayed text left one column.

0 commit comments

Comments
 (0)