Skip to content

Commit 93e8a43

Browse files
maksleventalTomáš Jozífek
authored and
Tomáš Jozífek
committed
-move compat mode to base
-implement for pigpio
1 parent f622063 commit 93e8a43

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

RPLCD/gpio.py

+3-13
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@
3636
else:
3737
from time import perf_counter as now
3838

39-
# Duration to rate-limit calls to _send
40-
COMPAT_MODE_WAIT_TIME = 0.001
41-
4239
PinConfig = namedtuple('PinConfig', 'rs rw e d0 d1 d2 d3 d4 d5 d6 d7 backlight mode')
4340

4441

@@ -99,10 +96,6 @@ def __init__(self, numbering_mode=None, pin_rs=None, pin_rw=None, pin_e=None, pi
9996
:type compat_mode: bool
10097
10198
"""
102-
# Configure compatibility mode
103-
self.compat_mode = compat_mode
104-
if compat_mode:
105-
self.last_send_event = now()
10699

107100
# Set attributes
108101
if numbering_mode == GPIO.BCM or numbering_mode == GPIO.BOARD:
@@ -136,7 +129,8 @@ def __init__(self, numbering_mode=None, pin_rs=None, pin_rw=None, pin_e=None, pi
136129
# Call superclass
137130
super(CharLCD, self).__init__(cols, rows, dotsize,
138131
charmap=charmap,
139-
auto_linebreaks=auto_linebreaks)
132+
auto_linebreaks=auto_linebreaks,
133+
compat_mode=compat_mode)
140134

141135
# Set backlight status
142136
if pin_backlight is not None:
@@ -243,8 +237,4 @@ def _pulse_enable(self):
243237
GPIO.output(self.pins.e, 0)
244238
c.usleep(100) # commands need > 37us to settle
245239

246-
def _wait(self):
247-
"""Rate limit the number of send events."""
248-
end = self.last_send_event + COMPAT_MODE_WAIT_TIME
249-
while now() < end:
250-
pass
240+

RPLCD/lcd.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@
2727
from . import codecs
2828
from . import common as c
2929
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
3035

36+
# Duration to rate-limit calls to _send
37+
COMPAT_MODE_WAIT_TIME = 0.001
3138

3239
LCDConfig = namedtuple('LCDConfig', 'rows cols dotsize')
3340

@@ -38,7 +45,7 @@ class BaseCharLCD(object):
3845

3946
# Init, setup, teardown
4047

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):
4249
"""
4350
Character LCD controller. Base class only, you should use a subclass.
4451
@@ -117,6 +124,8 @@ def __init__(self, cols=20, rows=4, dotsize=8, charmap='A02', auto_linebreaks=Tr
117124
else:
118125
raise ValueError('Invalid data bus mode: {}'.format(self.data_bus_mode))
119126

127+
128+
120129
# Write configuration to display
121130
self.command(c.LCD_FUNCTIONSET | displayfunction)
122131
c.usleep(50)
@@ -137,6 +146,11 @@ def __init__(self, cols=20, rows=4, dotsize=8, charmap='A02', auto_linebreaks=Tr
137146
self.command(c.LCD_ENTRYMODESET | self._text_align_mode | self._display_shift_mode)
138147
c.usleep(50)
139148

149+
# Configure compatibility mode
150+
self.compat_mode = compat_mode
151+
if compat_mode:
152+
self.last_send_event = now()
153+
140154
def close(self, clear=False):
141155
if clear:
142156
self.clear()
@@ -239,6 +253,12 @@ def _set_cursor_mode(self, value):
239253
cursor_mode = property(_get_cursor_mode, _set_cursor_mode,
240254
doc='How the cursor should behave (``hide``, ``line`` or ``blink``).')
241255

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+
242262
# High level commands
243263

244264
def write_string(self, value):
@@ -446,3 +466,4 @@ def lf(self): # type: () -> None
446466
def crlf(self): # type: () -> None
447467
"""Write a line feed and a carriage return (``\\r\\n``) character to the LCD."""
448468
self.write_string('\r\n')
469+

RPLCD/pigpio.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
from .lcd import BaseCharLCD
3232
from .compat import range
3333

34+
import sys
35+
if sys.version_info.major < 3:
36+
from time import clock as now
37+
else:
38+
from time import perf_counter as now
3439

3540
# https://diarmuid.ie/blog/pwm-exponential-led-fading-on-arduino-or-other-platforms/
3641
# p 101 .. maximum value of the PWM cycle
@@ -52,7 +57,8 @@ def __init__(self, pi,
5257
contrast_pwm=None, contrast=0.5,
5358
cols=20, rows=4, dotsize=8,
5459
charmap='A02',
55-
auto_linebreaks=True):
60+
auto_linebreaks=True,
61+
compat_mode=False):
5662
"""
5763
Character LCD controller.
5864
@@ -151,7 +157,8 @@ def __init__(self, pi,
151157
# Call superclass
152158
super(CharLCD, self).__init__(cols, rows, dotsize,
153159
charmap=charmap,
154-
auto_linebreaks=auto_linebreaks)
160+
auto_linebreaks=auto_linebreaks,
161+
compat_mode=compat_mode)
155162

156163
# Set backlight status
157164
if pin_backlight is not None:
@@ -300,6 +307,10 @@ def _send(self, value, mode):
300307
"""Send the specified value to the display with automatic 4bit / 8bit
301308
selection. The rs_mode is either ``RS_DATA`` or ``RS_INSTRUCTION``."""
302309

310+
# Wait, if compatibility mode is enabled
311+
if self.compat_mode:
312+
self._wait()
313+
303314
# Assemble the parameters sent to the pigpio script
304315
params = [mode]
305316
params.extend([(value >> i) & 0x01 for i in range(8)])
@@ -316,6 +327,10 @@ def _send(self, value, mode):
316327
# Switch on pigpio's exceptions
317328
pigpio.exceptions = True
318329

330+
# Record the time for the tail-end of the last send event
331+
if self.compat_mode:
332+
self.last_send_event = now()
333+
319334
def _send_data(self, value):
320335
"""Send data to the display. """
321336
self._send(value, c.RS_DATA)

0 commit comments

Comments
 (0)