Skip to content

Commit f747f32

Browse files
author
Tomáš Jozífek
committed
Remove busy wait, make sleep amount tune-able, fix initialization bug
1 parent 93e8a43 commit f747f32

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

RPLCD/gpio.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ def __init__(self, numbering_mode=None, pin_rs=None, pin_rw=None, pin_e=None, pi
4646
cols=20, rows=4, dotsize=8,
4747
charmap='A02',
4848
auto_linebreaks=True,
49-
compat_mode=False):
49+
compat_mode=False,
50+
compat_mode_wait_time=0.001):
5051
"""
5152
Character LCD controller.
5253
@@ -94,6 +95,9 @@ def __init__(self, numbering_mode=None, pin_rs=None, pin_rw=None, pin_e=None, pi
9495
:param compat_mode: Whether to run additional checks to support older LCDs
9596
that may not run at the reference clock (or keep up with it).
9697
:type compat_mode: bool
98+
:param compat_mode_wait_time: Minimum time to pass between sends.
99+
if zero, turns off compat_mode Default: ``0.001`` seconds.
100+
:type compat_mode_wait_time: float
97101
98102
"""
99103

@@ -130,7 +134,8 @@ def __init__(self, numbering_mode=None, pin_rs=None, pin_rw=None, pin_e=None, pi
130134
super(CharLCD, self).__init__(cols, rows, dotsize,
131135
charmap=charmap,
132136
auto_linebreaks=auto_linebreaks,
133-
compat_mode=compat_mode)
137+
compat_mode=compat_mode,
138+
compat_mode_wait_time=compat_mode_wait_time)
134139

135140
# Set backlight status
136141
if pin_backlight is not None:

RPLCD/lcd.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@
3232
from time import clock as now
3333
else:
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

3937
LCDConfig = 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

RPLCD/pigpio.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ def __init__(self, pi,
5858
cols=20, rows=4, dotsize=8,
5959
charmap='A02',
6060
auto_linebreaks=True,
61-
compat_mode=False):
61+
compat_mode=False,
62+
compat_mode_wait_time=0.001):
6263
"""
6364
Character LCD controller.
6465
@@ -124,6 +125,9 @@ def __init__(self, pi,
124125
:param auto_linebreaks: Whether or not to automatically insert line
125126
breaks. Default: ``True``.
126127
:type auto_linebreaks: bool
128+
:param compat_mode_wait_time: Minimum time to pass between sends.
129+
if zero, turns off compat_mode Default: ``0.001`` seconds.
130+
:type compat_mode_wait_time: float
127131
128132
"""
129133

@@ -158,7 +162,8 @@ def __init__(self, pi,
158162
super(CharLCD, self).__init__(cols, rows, dotsize,
159163
charmap=charmap,
160164
auto_linebreaks=auto_linebreaks,
161-
compat_mode=compat_mode)
165+
compat_mode=compat_mode,
166+
compat_mode_wait_time=compat_mode_wait_time)
162167

163168
# Set backlight status
164169
if pin_backlight is not None:

0 commit comments

Comments
 (0)