@@ -169,6 +169,11 @@ def __init__(self, rs, en, d4, d5, d6, d7, columns, lines
169
169
self ._message = None
170
170
self ._enable = None
171
171
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
172
177
# pylint: enable-msg=too-many-arguments
173
178
174
179
def home (self ):
@@ -198,6 +203,20 @@ def clear(self):
198
203
self ._write8 (_LCD_CLEARDISPLAY )
199
204
time .sleep (0.003 )
200
205
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
+
201
220
@property
202
221
def cursor (self ):
203
222
"""True if cursor is visible. False to stop displaying the cursor.
@@ -230,7 +249,8 @@ def cursor(self, show):
230
249
self ._write8 (_LCD_DISPLAYCONTROL | self .displaycontrol )
231
250
232
251
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).
234
254
235
255
:param column: column location
236
256
:param row: row location
@@ -243,6 +263,9 @@ def cursor_position(self, column, row):
243
263
column = self .columns - 1
244
264
# Set location
245
265
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
246
269
247
270
@property
248
271
def blink (self ):
@@ -310,6 +333,11 @@ def display(self, enable):
310
333
@property
311
334
def message (self ):
312
335
"""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.
313
341
314
342
The following example displays, "Hello, world!" on the LCD.
315
343
@@ -331,28 +359,45 @@ def message(self):
331
359
@message .setter
332
360
def message (self , message ):
333
361
self ._message = message
334
- line = 0
362
+ # Set line to match self.row from cursor_position()
363
+ line = self .row
335
364
# Track times through iteration, to act on the initial character of the message
336
365
initial_character = 0
337
366
# iterate through each character
338
367
for character in message :
339
368
# If this is the first character in the string:
340
369
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
344
379
self .cursor_position (col , line )
345
380
initial_character += 1
346
381
# If character is \n, go to next line
347
382
if character == '\n ' :
348
383
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
352
395
self .cursor_position (col , line )
353
396
# Write string to display
354
397
else :
355
398
self ._write8 (ord (character ), True )
399
+ # reset column and row to (0,0) after message is displayed
400
+ self .column , self .row = 0 , 0
356
401
357
402
def move_left (self ):
358
403
"""Moves displayed text left one column.
0 commit comments