Added source_delay property to SourceMixin which means it'll now appear
everywhere. Default is 0.01 which is just enough to drop CPU usage while
remaining responsive enough for the majority of purposes.
This commit is contained in:
Dave Jones
2016-03-19 16:46:22 +00:00
parent 807bdff2e4
commit 351a323832
2 changed files with 22 additions and 2 deletions

View File

@@ -23,6 +23,7 @@ from .exc import (
GPIODeviceClosed, GPIODeviceClosed,
GPIOBadQueueLen, GPIOBadQueueLen,
GPIOBadSampleWait, GPIOBadSampleWait,
GPIOBadSourceDelay,
) )
# Get a pin implementation to use as the default; we prefer RPi.GPIO's here # Get a pin implementation to use as the default; we prefer RPi.GPIO's here
@@ -164,6 +165,7 @@ class SourceMixin(object):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self._source = None self._source = None
self._source_thread = None self._source_thread = None
self._source_delay = 0.01
super(SourceMixin, self).__init__(*args, **kwargs) super(SourceMixin, self).__init__(*args, **kwargs)
def close(self): def close(self):
@@ -176,13 +178,28 @@ class SourceMixin(object):
def _copy_values(self, source): def _copy_values(self, source):
for v in source: for v in source:
self.value = v self.value = v
if self._source_thread.stopping.wait(0): if self._source_thread.stopping.wait(self._source_delay):
break break
@property
def source_delay(self):
"""
The delay (measured in seconds) in the loop used to read values from
:attr:`source`. Defaults to 0.01 seconds which is generally sufficient
to keep CPU usage to a minimum while providing adequate responsiveness.
"""
return self._source_delay
@source_delay.setter
def source_delay(self, value):
if value < 0:
raise GPIOBadSourceDelay('source_delay must be 0 or greater')
self._source_delay = float(value)
@property @property
def source(self): def source(self):
""" """
The iterable to use as a source of values for `value`. The iterable to use as a source of values for :attr:`value`.
""" """
return self._source return self._source

View File

@@ -31,6 +31,9 @@ class GPIOBadQueueLen(GPIODeviceError, ValueError):
class GPIOBadSampleWait(GPIODeviceError, ValueError): class GPIOBadSampleWait(GPIODeviceError, ValueError):
"Error raised when a negative sampling wait period is specified" "Error raised when a negative sampling wait period is specified"
class GPIOBadSourceDelay(GPIODeviceError, ValueError):
"Error raised when a negative source delay is specified"
class InputDeviceError(GPIODeviceError): class InputDeviceError(GPIODeviceError):
"Base class for errors specific to the InputDevice hierarchy" "Base class for errors specific to the InputDevice hierarchy"