Skip to content

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

Merged
merged 12 commits into from
Apr 12, 2019
Merged
10 changes: 0 additions & 10 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,6 @@ Contributing
Contributions are welcome! Please read our `Code of Conduct
<https://github.com/adafruit/Adafruit_CircuitPython_CharLCD/blob/master/CODE_OF_CONDUCT.md>`_ before contributing to help this project stay welcoming.

Installation
============

This library is **NOT** built into CircuitPython to make it easy to update. To
install it either follow the directions below or :ref:`install the library bundle <bundle_installation>`.

To install:

#. Download and unzip the `latest release zip <https://github.com/adafruit/Adafruit_CircuitPython_CharLCD/releases>`_.
#. Copy the unzipped ``adafruit_character_lcd`` to the ``lib`` directory on the ``CIRCUITPY`` or ``MICROPYTHON`` drive.

Building locally
================
Expand Down
62 changes: 52 additions & 10 deletions adafruit_character_lcd/character_lcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
self._column_align = False
# pylint: enable-msg=too-many-arguments

def home(self):
Expand Down Expand Up @@ -197,6 +202,17 @@ 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.
Copy link
Collaborator

@makermelissa makermelissa Apr 12, 2019

Choose a reason for hiding this comment

The 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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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):
self._column_align = enable
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's add some type checking here, so something like:

        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):
Expand Down Expand Up @@ -230,8 +246,9 @@ 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
"""
Expand All @@ -243,6 +260,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 @@ -310,6 +330,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.

Expand All @@ -331,29 +356,46 @@ 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.

Expand Down
3 changes: 2 additions & 1 deletion examples/charlcd_mono_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
lcd.message = "Hello\nCircuitPython"
# Wait 5s
time.sleep(5)
lcd.clear()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's remove the lcd.clear() just to keep the examples consistent.

# Return text direction to left to right
lcd.text_direction = lcd.LEFT_TO_RIGHT
# Display cursor
Expand All @@ -53,7 +54,7 @@
scroll_msg = '<-- Scroll'
lcd.message = scroll_msg
# Scroll message to the left
for i in range(len(scroll_msg)):
for i in range(len(scroll_msg)+5):
time.sleep(0.5)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like there are still some changes to the simpletest, so just remove the +5

lcd.move_left()
lcd.clear()
Expand Down