Fix #279 once and for all (ha! ;)

This implements the proposal discussed in the re-opened #279 to add a
pin_factory argument at the device level and remove the ability to
specify a pin instance to device constructors (they now only accept a
pin specification).

Note: there's still a couple of bits to tidy up (tests on "real" Pis,
and pin_factory.release_all needs refinement) but the test suite is now
at least capable of passing on a PC.
This commit is contained in:
Dave Jones
2017-07-04 00:26:41 +01:00
parent 448feaf68f
commit c820636fcb
14 changed files with 396 additions and 403 deletions

View File

@@ -7,8 +7,9 @@ from __future__ import (
str = type('')
import io
from threading import RLock
from threading import RLock, Lock
from types import MethodType
from collections import defaultdict
try:
from weakref import ref, WeakMethod
except ImportError:
@@ -48,6 +49,7 @@ class PiFactory(Factory):
forms the base of :class:`~gpiozero.pins.local.LocalPiFactory`.
"""
def __init__(self):
super(PiFactory, self).__init__()
self._info = None
self.pins = {}
self.pin_class = None
@@ -72,10 +74,6 @@ class PiFactory(Factory):
self.pins[n] = pin
return pin
def pin_address(self, spec):
n = self._to_gpio(spec)
return self.address + ('GPIO%d' % n,)
def _to_gpio(self, spec):
"""
Converts the pin *spec* to a GPIO port number.
@@ -240,23 +238,23 @@ class PiPin(Pin):
self._when_changed = None
self._number = number
try:
factory.pi_info.physical_pin(self.address[-1])
factory.pi_info.physical_pin('GPIO%d' % self.number)
except PinNoPins:
warnings.warn(
PinNonPhysical(
'no physical pins exist for %s' % self.address[-1]))
'no physical pins exist for GPIO%d' % self.number))
@property
def number(self):
return self._number
def __repr__(self):
return 'GPIO%d' % self._number
@property
def factory(self):
return self._factory
def _get_address(self):
return self.factory.address + ('GPIO%d' % self.number,)
def _call_when_changed(self):
"""
Called to fire the :attr:`when_changed` event handler; override this