27
27
from . import codecs
28
28
from . import common as c
29
29
from .compat import range
30
+ import sys
31
+ if sys .version_info .major < 3 :
32
+ from time import clock as now
33
+ else :
34
+ from time import perf_counter as now
30
35
36
+ # Duration to rate-limit calls to _send
37
+ COMPAT_MODE_WAIT_TIME = 0.001
31
38
32
39
LCDConfig = namedtuple ('LCDConfig' , 'rows cols dotsize' )
33
40
@@ -38,7 +45,7 @@ class BaseCharLCD(object):
38
45
39
46
# Init, setup, teardown
40
47
41
- def __init__ (self , cols = 20 , rows = 4 , dotsize = 8 , charmap = 'A02' , auto_linebreaks = True ):
48
+ def __init__ (self , cols = 20 , rows = 4 , dotsize = 8 , charmap = 'A02' , auto_linebreaks = True , compat_mode = False ):
42
49
"""
43
50
Character LCD controller. Base class only, you should use a subclass.
44
51
@@ -117,6 +124,8 @@ def __init__(self, cols=20, rows=4, dotsize=8, charmap='A02', auto_linebreaks=Tr
117
124
else :
118
125
raise ValueError ('Invalid data bus mode: {}' .format (self .data_bus_mode ))
119
126
127
+
128
+
120
129
# Write configuration to display
121
130
self .command (c .LCD_FUNCTIONSET | displayfunction )
122
131
c .usleep (50 )
@@ -137,6 +146,11 @@ def __init__(self, cols=20, rows=4, dotsize=8, charmap='A02', auto_linebreaks=Tr
137
146
self .command (c .LCD_ENTRYMODESET | self ._text_align_mode | self ._display_shift_mode )
138
147
c .usleep (50 )
139
148
149
+ # Configure compatibility mode
150
+ self .compat_mode = compat_mode
151
+ if compat_mode :
152
+ self .last_send_event = now ()
153
+
140
154
def close (self , clear = False ):
141
155
if clear :
142
156
self .clear ()
@@ -239,6 +253,12 @@ def _set_cursor_mode(self, value):
239
253
cursor_mode = property (_get_cursor_mode , _set_cursor_mode ,
240
254
doc = 'How the cursor should behave (``hide``, ``line`` or ``blink``).' )
241
255
256
+ def _wait (self ):
257
+ """Rate limit the number of send events."""
258
+ end = self .last_send_event + COMPAT_MODE_WAIT_TIME
259
+ while now () < end :
260
+ pass
261
+
242
262
# High level commands
243
263
244
264
def write_string (self , value ):
@@ -446,3 +466,4 @@ def lf(self): # type: () -> None
446
466
def crlf (self ): # type: () -> None
447
467
"""Write a line feed and a carriage return (``\\ r\\ n``) character to the LCD."""
448
468
self .write_string ('\r \n ' )
469
+
0 commit comments