Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions RPLCD/gpio.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@
else:
from time import perf_counter as now

# Duration to rate-limit calls to _send
COMPAT_MODE_WAIT_TIME = 0.001

PinConfig = namedtuple('PinConfig', 'rs rw e d0 d1 d2 d3 d4 d5 d6 d7 backlight mode')


Expand Down Expand Up @@ -99,10 +96,6 @@ def __init__(self, numbering_mode=None, pin_rs=None, pin_rw=None, pin_e=None, pi
:type compat_mode: bool

"""
# Configure compatibility mode
self.compat_mode = compat_mode
if compat_mode:
self.last_send_event = now()

# Set attributes
if numbering_mode == GPIO.BCM or numbering_mode == GPIO.BOARD:
Expand Down Expand Up @@ -136,7 +129,8 @@ def __init__(self, numbering_mode=None, pin_rs=None, pin_rw=None, pin_e=None, pi
# Call superclass
super(CharLCD, self).__init__(cols, rows, dotsize,
charmap=charmap,
auto_linebreaks=auto_linebreaks)
auto_linebreaks=auto_linebreaks,
compat_mode=compat_mode)

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

def _wait(self):
"""Rate limit the number of send events."""
end = self.last_send_event + COMPAT_MODE_WAIT_TIME
while now() < end:
pass

23 changes: 22 additions & 1 deletion RPLCD/lcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@
from . import codecs
from . import common as c
from .compat import range
import sys
if sys.version_info.major < 3:
from time import clock as now
else:
from time import perf_counter as now

# Duration to rate-limit calls to _send
COMPAT_MODE_WAIT_TIME = 0.001

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

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

# Init, setup, teardown

def __init__(self, cols=20, rows=4, dotsize=8, charmap='A02', auto_linebreaks=True):
def __init__(self, cols=20, rows=4, dotsize=8, charmap='A02', auto_linebreaks=True, compat_mode=False):
"""
Character LCD controller. Base class only, you should use a subclass.

Expand Down Expand Up @@ -117,6 +124,8 @@ def __init__(self, cols=20, rows=4, dotsize=8, charmap='A02', auto_linebreaks=Tr
else:
raise ValueError('Invalid data bus mode: {}'.format(self.data_bus_mode))



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

# Configure compatibility mode
self.compat_mode = compat_mode
if compat_mode:
self.last_send_event = now()

def close(self, clear=False):
if clear:
self.clear()
Expand Down Expand Up @@ -239,6 +253,12 @@ def _set_cursor_mode(self, value):
cursor_mode = property(_get_cursor_mode, _set_cursor_mode,
doc='How the cursor should behave (``hide``, ``line`` or ``blink``).')

def _wait(self):
"""Rate limit the number of send events."""
end = self.last_send_event + COMPAT_MODE_WAIT_TIME
while now() < end:
pass

# High level commands

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

22 changes: 19 additions & 3 deletions RPLCD/pigpio.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
from .lcd import BaseCharLCD
from .compat import range

import sys
if sys.version_info.major < 3:
from time import clock as now
else:
from time import perf_counter as now

# https://diarmuid.ie/blog/pwm-exponential-led-fading-on-arduino-or-other-platforms/
# p 101 .. maximum value of the PWM cycle
Expand All @@ -52,7 +57,8 @@ def __init__(self, pi,
contrast_pwm=None, contrast=0.5,
cols=20, rows=4, dotsize=8,
charmap='A02',
auto_linebreaks=True):
auto_linebreaks=True,
compat_mode=False):
"""
Character LCD controller.

Expand Down Expand Up @@ -151,14 +157,16 @@ def __init__(self, pi,
# Call superclass
super(CharLCD, self).__init__(cols, rows, dotsize,
charmap=charmap,
auto_linebreaks=auto_linebreaks)
auto_linebreaks=auto_linebreaks,
compat_mode=compat_mode)

# Set backlight status
if pin_backlight is not None:
self.backlight_enabled = backlight_enabled

# Set contrast
self.contrast = contrast
if pin_contrast is not None:
self.contrast = contrast

def _init_connection(self):
# Setup GPIO
Expand Down Expand Up @@ -299,6 +307,10 @@ def _send(self, value, mode):
"""Send the specified value to the display with automatic 4bit / 8bit
selection. The rs_mode is either ``RS_DATA`` or ``RS_INSTRUCTION``."""

# Wait, if compatibility mode is enabled
if self.compat_mode:
self._wait()

# Assemble the parameters sent to the pigpio script
params = [mode]
params.extend([(value >> i) & 0x01 for i in range(8)])
Expand All @@ -315,6 +327,10 @@ def _send(self, value, mode):
# Switch on pigpio's exceptions
pigpio.exceptions = True

# Record the time for the tail-end of the last send event
if self.compat_mode:
self.last_send_event = now()

def _send_data(self, value):
"""Send data to the display. """
self._send(value, c.RS_DATA)
Expand Down