7070#-----------------------------------------------------------------------------
7171from __future__ import print_function
7272import struct
73+ import time
7374
7475import qwiic_i2c
7576
@@ -201,6 +202,7 @@ def is_connected(self):
201202 return qwiic_i2c .isDeviceConnected (self .address )
202203
203204 connected = property (is_connected )
205+
204206 # ----------------------------------
205207 # begin()
206208 #
@@ -213,8 +215,15 @@ def begin(self):
213215 :rtype: bool
214216
215217 """
216- # Basically return True if we are connected...
217- return self .is_connected ()
218+ # set default settings, as defined in constructor
219+ result0 = self .specialCommand (LCD_DISPLAYCONTROL | self ._displayControl )
220+ time .sleep (1 )
221+ result1 = self .specialCommand (LCD_ENTRYMODESET | self ._displayMode )
222+ time .sleep (1 )
223+ result2 = self .clearScreen ()
224+ time .sleep (1 )
225+
226+ return (bool (result0 ) & bool (result1 ) & bool (result2 ))
218227
219228 # ----------------------------------
220229 # print()
@@ -247,7 +256,9 @@ def clearScreen(self):
247256 :rtype: bool
248257
249258 """
250- return self .command (CLEAR_COMMAND )
259+ result = self .command (CLEAR_COMMAND )
260+ time .sleep (0.01 )
261+ return result
251262
252263 # ----------------------------------
253264 # setCursor()
@@ -303,7 +314,9 @@ def setContrast(self, contrast):
303314 block = [CONTRAST_COMMAND , contrast ]
304315
305316 # send the complete bytes (address, settings command , contrast command, contrast value)
306- return self ._i2c .writeBlock (self .address , SETTING_COMMAND , block )
317+ result = self ._i2c .writeBlock (self .address , SETTING_COMMAND , block )
318+ time .sleep (0.01 )
319+ return result
307320
308321 # ----------------------------------
309322 # setBacklight()
@@ -352,7 +365,9 @@ def setBacklight(self, r, g, b):
352365 block [9 ] = (LCD_DISPLAYCONTROL | self ._displayControl )
353366
354367 # send the complete bytes (address, settings command , contrast command, contrast value)
355- return self ._i2c .writeBlock (self .address , SETTING_COMMAND , block )
368+ result = self ._i2c .writeBlock (self .address , SETTING_COMMAND , block )
369+ time .sleep (0.05 )
370+ return result
356371
357372 # ----------------------------------
358373 # specialCommand()
@@ -373,7 +388,9 @@ def specialCommand(self, command, count = 1):
373388 """
374389 for i in range (0 , count ):
375390 # send the complete bytes (special command + command)
376- return self ._i2c .writeByte (self .address , SPECIAL_COMMAND , command )
391+ result = self ._i2c .writeByte (self .address , SPECIAL_COMMAND , command )
392+ time .sleep (0.05 )
393+ return result
377394
378395 # ----------------------------------
379396 # command()
@@ -391,7 +408,9 @@ def command(self, command):
391408 :rtype: bool
392409
393410 """
394- return self ._i2c .writeByte (self .address , SETTING_COMMAND , command )
411+ result = self ._i2c .writeByte (self .address , SETTING_COMMAND , command )
412+ time .sleep (0.01 )
413+ return result
395414
396415 # ----------------------------------
397416 # moveCursorLeft()
@@ -515,4 +534,65 @@ def scrollDisplayRight(self, count = 1):
515534 :rtype: bool
516535
517536 """
518- return self .specialCommand (LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT , count )
537+ return self .specialCommand (LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT , count )
538+
539+ # ----------------------------------
540+ # autoscroll()
541+ #
542+ # Turn autoscrolling on. This will right-justify text from the cursor.
543+ def autoscroll (self ):
544+ """
545+ Turn autoscrolling on. This will right-justify text from the cursor.
546+
547+ :return: Returns true if the I2C write was successful, otherwise False.
548+ :rtype: bool
549+
550+ """
551+ self ._displayControl |= LCD_ENTRYSHIFTINCREMENT
552+ return self .specialCommand (LCD_ENTRYMODESET | self ._displayControl )
553+
554+ # ----------------------------------
555+ # noAutoscroll()
556+ #
557+ # Turn autoscrolling off.
558+ def noAutoscroll (self ):
559+ """
560+ Turn autoscrolling off.
561+
562+ :return: Returns true if the I2C write was successful, otherwise False.
563+ :rtype: bool
564+
565+ """
566+ self ._displayControl &= ~ LCD_ENTRYSHIFTINCREMENT
567+ return self .specialCommand (LCD_ENTRYMODESET | self ._displayControl )
568+
569+ # ----------------------------------
570+ # leftToRight()
571+ #
572+ # Set the text to flow from left to right. This is the direction
573+ # that is common to most Western languages.
574+ def leftToRight (self ):
575+ """
576+ Set the text to flow from left to right.
577+
578+ :return: Returns true if the I2C write was successful, otherwise False.
579+ :rtype: bool
580+
581+ """
582+ self ._displayControl |= LCD_ENTRYLEFT
583+ return self .specialCommand (LCD_ENTRYMODESET | self ._displayControl )
584+
585+ # ----------------------------------
586+ # rightToLeft()
587+ #
588+ # Set the text to flow from right to left.
589+ def rightToLeft (self ):
590+ """
591+ Set the text to flow from right to left
592+
593+ :return: Returns true if the I2C write was successful, otherwise False.
594+ :rtype: bool
595+
596+ """
597+ self ._displayControl &= ~ LCD_ENTRYLEFT
598+ return self .specialCommand (LCD_ENTRYMODESET | self ._displayControl )
0 commit comments