3232 from time import clock as now
3333else :
3434 from time import perf_counter as now
35-
36- # Duration to rate-limit calls to _send
37- COMPAT_MODE_WAIT_TIME = 0.001
35+ from time import sleep
3836
3937LCDConfig = namedtuple ('LCDConfig' , 'rows cols dotsize' )
4038
@@ -45,7 +43,8 @@ class BaseCharLCD(object):
4543
4644 # Init, setup, teardown
4745
48- def __init__ (self , cols = 20 , rows = 4 , dotsize = 8 , charmap = 'A02' , auto_linebreaks = True , compat_mode = False ):
46+ def __init__ (self , cols = 20 , rows = 4 , dotsize = 8 , charmap = 'A02' , auto_linebreaks = True , compat_mode = False ,
47+ compat_mode_wait_time = 0.001 ):
4948 """
5049 Character LCD controller. Base class only, you should use a subclass.
5150
@@ -63,10 +62,22 @@ def __init__(self, cols=20, rows=4, dotsize=8, charmap='A02', auto_linebreaks=Tr
6362 auto_linebreaks:
6463 Whether or not to automatically insert line breaks.
6564 Default: True.
66-
65+ compat_mode:
66+ Whether to run additional checks to support older LCDs
67+ that may not run at the reference clock (or keep up with it).
68+ Default: False
69+ compat_mode_wait_time: Minimum time to pass between sends.
70+ if zero, turns off compat_mode
71+ Default: ``0.001`` seconds.
6772 """
6873 assert dotsize in [8 , 10 ], 'The ``dotsize`` argument should be either 8 or 10.'
6974
75+ # Configure compatibility mode
76+ self .compat_mode = compat_mode and compat_mode_wait_time > 0
77+ self .compat_mode_wait_time = compat_mode_wait_time
78+ if compat_mode :
79+ self .last_send_event = now ()
80+
7081 # Initialize codec
7182 if charmap == 'A00' :
7283 self .codec = codecs .A00Codec ()
@@ -146,11 +157,6 @@ def __init__(self, cols=20, rows=4, dotsize=8, charmap='A02', auto_linebreaks=Tr
146157 self .command (c .LCD_ENTRYMODESET | self ._text_align_mode | self ._display_shift_mode )
147158 c .usleep (50 )
148159
149- # Configure compatibility mode
150- self .compat_mode = compat_mode
151- if compat_mode :
152- self .last_send_event = now ()
153-
154160 def close (self , clear = False ):
155161 if clear :
156162 self .clear ()
@@ -255,9 +261,10 @@ def _set_cursor_mode(self, value):
255261
256262 def _wait (self ):
257263 """Rate limit the number of send events."""
258- end = self .last_send_event + COMPAT_MODE_WAIT_TIME
259- while now () < end :
260- pass
264+ end = self .last_send_event + self .compat_mode_wait_time
265+ sleep_duration = end - now ()
266+ if sleep_duration > 0 :
267+ sleep (sleep_duration )
261268
262269 # High level commands
263270
0 commit comments