From a99e0746c3e9ce80cd6bb310da10b7b67c5f30ce Mon Sep 17 00:00:00 2001 From: Dave Jones Date: Thu, 22 Jun 2017 22:45:00 +0100 Subject: [PATCH] Fix #279 Permit replacement of pin_factory without closing old factory. However, continue closing devices associated with extant pin factory at script termination. --- docs/api_pins.rst | 17 ++- gpiozero/devices.py | 49 ++++---- gpiozero/exc.py | 3 - gpiozero/pins/__init__.py | 3 + gpiozero/pins/data.py | 2 +- gpiozero/pins/mock.py | 8 +- gpiozero/spi_devices.py | 2 +- tests/test_boards.py | 234 +++++++++++++++++++------------------- tests/test_devices.py | 8 +- tests/test_inputs.py | 36 +++--- tests/test_mock_pin.py | 56 ++++----- tests/test_outputs.py | 168 +++++++++++++-------------- tests/test_pins_data.py | 12 +- tests/test_real_pins.py | 8 +- tests/test_spi.py | 58 +++++----- tests/test_spi_devices.py | 2 +- 16 files changed, 328 insertions(+), 338 deletions(-) diff --git a/docs/api_pins.rst b/docs/api_pins.rst index b052010..6a34a72 100644 --- a/docs/api_pins.rst +++ b/docs/api_pins.rst @@ -13,7 +13,7 @@ extender chips. This is the purpose of the pins portion of the library. When you construct a device, you pass in a pin specification. However, what the library actually expects is a :class:`Pin` implementation. If it finds anything -else, it uses the existing ``Device._pin_factory`` to construct a :class:`Pin` +else, it uses the existing ``Device.pin_factory`` to construct a :class:`Pin` implementation based on the specification. Changing the pin factory @@ -29,7 +29,7 @@ The default pin factory can be replaced by specifying a value for the [GCC 4.9.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import gpiozero - >>> gpiozero.Device._pin_factory + >>> gpiozero.Device.pin_factory To set the ``GPIOZERO_PIN_FACTORY`` for the rest of your session you can @@ -43,7 +43,7 @@ export this value: [GCC 4.9.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import gpiozero - >>> gpiozero.Device._pin_factory + >>> gpiozero.Device.pin_factory >>> quit() pi@raspberrypi $ python @@ -51,7 +51,7 @@ export this value: [GCC 4.9.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import gpiozero - >>> gpiozero.Device._pin_factory + >>> gpiozero.Device.pin_factory If you add the ``export`` command to your :file:`~/.bashrc` file, you'll set @@ -73,15 +73,12 @@ they are tried by default. | native | :class:`gpiozero.pins.native.NativeFactory` | :class:`gpiozero.pins.native.NativePin` | +---------+-----------------------------------------------+-------------------------------------------+ -If you need to change the default pin factory from within a script, use the -``Device._set_pin_factory`` class method, passing in the instance of the new -factory to use. This is only supported at script startup (replacing the factory -closes all existing pin instances which can have interesting consequences for -any devices using them):: +If you need to change the default pin factory from within a script, set +``Device.pin_factory`` to the new factory instance to use:: from gpiozero.pins.native import NativeFactory from gpiozero import * - Device._set_pin_factory(NativeFactory()) + Device.pin_factory = NativeFactory() from gpiozero import LED diff --git a/gpiozero/devices.py b/gpiozero/devices.py index 4b282d0..e993b0d 100644 --- a/gpiozero/devices.py +++ b/gpiozero/devices.py @@ -34,7 +34,6 @@ from .exc import ( GPIOPinInUse, GPIODeviceClosed, PinFactoryFallback, - PinReservationsExist, ) from .compat import frozendict @@ -194,33 +193,13 @@ class Device(ValuesMixin, GPIOBase): services applicable to all devices (specifically the :attr:`is_active` property, the :attr:`value` property, and the :meth:`close` method). """ - _pin_factory = None # instance of a Factory sub-class + pin_factory = None # instance of a Factory sub-class _reservations = defaultdict(list) # maps pin addresses to lists of devices _res_lock = Lock() def __repr__(self): return "" % (self.__class__.__name__) - @classmethod - def _set_pin_factory(cls, new_factory): - reserved_devices = { - dev - for ref_list in cls._reservations.values() - for ref in ref_list - for dev in (ref(),) - if dev is not None - } - if new_factory is None: - for dev in reserved_devices: - dev.close() - elif reserved_devices: - raise PinReservationsExist( - "can't change factory while devices still hold pin " - "reservations (%r)" % dev) - if cls._pin_factory is not None: - cls._pin_factory.close() - cls._pin_factory = new_factory - def _reserve_pins(self, *pins_or_addresses): """ Called to indicate that the device reserves the right to use the @@ -439,8 +418,8 @@ class GPIODevice(Device): self._reserve_pins(pin) else: # Check you can reserve *before* constructing the pin - self._reserve_pins(Device._pin_factory.pin_address(pin)) - pin = Device._pin_factory.pin(pin) + self._reserve_pins(Device.pin_factory.pin_address(pin)) + pin = Device.pin_factory.pin(pin) self._pin = pin self._active_state = True self._inactive_state = False @@ -524,11 +503,27 @@ def _default_pin_factory(name=os.getenv('GPIOZERO_PIN_FACTORY', None)): return factory.load()() raise BadPinFactory('Unable to find pin factory "%s"' % name) -Device._set_pin_factory(_default_pin_factory()) + +def _devices_shutdown(): + if Device.pin_factory: + with Device._res_lock: + reserved_devices = { + dev + for ref_list in Device._reservations.values() + for ref in ref_list + for dev in (ref(),) + if dev is not None + } + for dev in reserved_devices: + dev.close() + Device.pin_factory.close() + Device.pin_factory = None + def _shutdown(): _threads_shutdown() - Device._set_pin_factory(None) + _devices_shutdown() + +Device.pin_factory = _default_pin_factory() atexit.register(_shutdown) - diff --git a/gpiozero/exc.py b/gpiozero/exc.py index 1a3182d..416f1a6 100644 --- a/gpiozero/exc.py +++ b/gpiozero/exc.py @@ -145,9 +145,6 @@ class PinNoPins(PinError, RuntimeError): class PinInvalidPin(PinError, ValueError): "Error raised when an invalid pin specification is provided" -class PinReservationsExist(PinError, RuntimeError): - "Error raised when pin factory is changed while reservations still exist" - class GPIOZeroWarning(Warning): "Base class for all warnings in GPIO Zero" diff --git a/gpiozero/pins/__init__.py b/gpiozero/pins/__init__.py index df30f61..60d325c 100644 --- a/gpiozero/pins/__init__.py +++ b/gpiozero/pins/__init__.py @@ -562,8 +562,11 @@ class SPI(object): | mode | polarity (CPOL) | phase (CPHA) | +======+=================+==============+ | 0 | False | False | + +------+-----------------+--------------+ | 1 | False | True | + +------+-----------------+--------------+ | 2 | True | False | + +------+-----------------+--------------+ | 3 | True | True | +------+-----------------+--------------+ diff --git a/gpiozero/pins/data.py b/gpiozero/pins/data.py index 4dbbda3..54fa7fa 100644 --- a/gpiozero/pins/data.py +++ b/gpiozero/pins/data.py @@ -1140,7 +1140,7 @@ def pi_info(revision=None): # The reason this import is located here is to avoid a circular # dependency; devices->pins.local->pins.data->devices from ..devices import Device - result = Device._pin_factory.pi_info + result = Device.pin_factory.pi_info if result is None: raise PinUnknownPi('The default pin_factory is not attached to a Pi') else: diff --git a/gpiozero/pins/mock.py b/gpiozero/pins/mock.py index 7af2658..4d974c3 100644 --- a/gpiozero/pins/mock.py +++ b/gpiozero/pins/mock.py @@ -312,10 +312,10 @@ class MockSPIDevice(object): self, clock_pin, mosi_pin=None, miso_pin=None, select_pin=None, clock_polarity=False, clock_phase=False, lsb_first=False, bits_per_word=8, select_high=False): - self.clock_pin = Device._pin_factory.pin(clock_pin, pin_class=MockSPIClockPin) - self.mosi_pin = None if mosi_pin is None else Device._pin_factory.pin(mosi_pin) - self.miso_pin = None if miso_pin is None else Device._pin_factory.pin(miso_pin) - self.select_pin = None if select_pin is None else Device._pin_factory.pin(select_pin, pin_class=MockSPISelectPin) + self.clock_pin = Device.pin_factory.pin(clock_pin, pin_class=MockSPIClockPin) + self.mosi_pin = None if mosi_pin is None else Device.pin_factory.pin(mosi_pin) + self.miso_pin = None if miso_pin is None else Device.pin_factory.pin(miso_pin) + self.select_pin = None if select_pin is None else Device.pin_factory.pin(select_pin, pin_class=MockSPISelectPin) self.clock_polarity = clock_polarity self.clock_phase = clock_phase self.lsb_first = lsb_first diff --git a/gpiozero/spi_devices.py b/gpiozero/spi_devices.py index 0a30de1..621828a 100644 --- a/gpiozero/spi_devices.py +++ b/gpiozero/spi_devices.py @@ -29,7 +29,7 @@ class SPIDevice(Device): def __init__(self, **spi_args): self._spi = None super(SPIDevice, self).__init__() - self._spi = self._pin_factory.spi(**spi_args) + self._spi = self.pin_factory.spi(**spi_args) def close(self): if self._spi: diff --git a/tests/test_boards.py b/tests/test_boards.py index 36e3277..1ba4879 100644 --- a/tests/test_boards.py +++ b/tests/test_boards.py @@ -17,7 +17,7 @@ from gpiozero.pins.mock import MockPWMPin, MockPin def setup_function(function): # dirty, but it does the job - Device._pin_factory.pin_class = MockPWMPin if function.__name__ in ( + Device.pin_factory.pin_class = MockPWMPin if function.__name__ in ( 'test_robot', 'test_ryanteck_robot', 'test_camjam_kit_robot', @@ -33,18 +33,18 @@ def setup_function(function): ) else MockPin def teardown_function(function): - Device._pin_factory.reset() + Device.pin_factory.reset() Device._reservations.clear() def teardown_module(module): # make sure we reset the default - Device._pin_factory.pwm = False + Device.pin_factory.pwm = False def test_composite_output_on_off(): - pin1 = Device._pin_factory.pin(2) - pin2 = Device._pin_factory.pin(3) - pin3 = Device._pin_factory.pin(4) + pin1 = Device.pin_factory.pin(2) + pin2 = Device.pin_factory.pin(3) + pin3 = Device.pin_factory.pin(4) with CompositeOutputDevice(OutputDevice(pin1), OutputDevice(pin2), foo=OutputDevice(pin3)) as device: device.on() assert all((pin1.state, pin2.state, pin3.state)) @@ -52,9 +52,9 @@ def test_composite_output_on_off(): assert not any((pin1.state, pin2.state, pin3.state)) def test_composite_output_toggle(): - pin1 = Device._pin_factory.pin(2) - pin2 = Device._pin_factory.pin(3) - pin3 = Device._pin_factory.pin(4) + pin1 = Device.pin_factory.pin(2) + pin2 = Device.pin_factory.pin(3) + pin3 = Device.pin_factory.pin(4) with CompositeOutputDevice(OutputDevice(pin1), OutputDevice(pin2), foo=OutputDevice(pin3)) as device: device.toggle() assert all((pin1.state, pin2.state, pin3.state)) @@ -65,9 +65,9 @@ def test_composite_output_toggle(): assert not pin3.state def test_composite_output_value(): - pin1 = Device._pin_factory.pin(2) - pin2 = Device._pin_factory.pin(3) - pin3 = Device._pin_factory.pin(4) + pin1 = Device.pin_factory.pin(2) + pin2 = Device.pin_factory.pin(3) + pin3 = Device.pin_factory.pin(4) with CompositeOutputDevice(OutputDevice(pin1), OutputDevice(pin2), foo=OutputDevice(pin3)) as device: assert device.value == (0, 0, 0) device.toggle() @@ -78,9 +78,9 @@ def test_composite_output_value(): assert device[2].is_active def test_led_board_on_off(): - pin1 = Device._pin_factory.pin(2) - pin2 = Device._pin_factory.pin(3) - pin3 = Device._pin_factory.pin(4) + pin1 = Device.pin_factory.pin(2) + pin2 = Device.pin_factory.pin(3) + pin3 = Device.pin_factory.pin(4) with LEDBoard(pin1, pin2, foo=pin3) as board: assert isinstance(board[0], LED) assert isinstance(board[1], LED) @@ -135,9 +135,9 @@ def test_led_board_on_off(): assert pin3.state def test_led_board_active_low(): - pin1 = Device._pin_factory.pin(2) - pin2 = Device._pin_factory.pin(3) - pin3 = Device._pin_factory.pin(4) + pin1 = Device.pin_factory.pin(2) + pin2 = Device.pin_factory.pin(3) + pin3 = Device.pin_factory.pin(4) with LEDBoard(pin1, pin2, foo=pin3, active_high=False) as board: assert not board.active_high assert not board[0].active_high @@ -159,9 +159,9 @@ def test_led_board_active_low(): assert not pin3.state def test_led_board_value(): - pin1 = Device._pin_factory.pin(2) - pin2 = Device._pin_factory.pin(3) - pin3 = Device._pin_factory.pin(4) + pin1 = Device.pin_factory.pin(2) + pin2 = Device.pin_factory.pin(3) + pin3 = Device.pin_factory.pin(4) with LEDBoard(pin1, pin2, foo=pin3) as board: assert board.value == (0, 0, 0) board.value = (0, 1, 0) @@ -170,9 +170,9 @@ def test_led_board_value(): assert board.value == (1, 0, 1) def test_led_board_pwm_value(): - pin1 = Device._pin_factory.pin(2) - pin2 = Device._pin_factory.pin(3) - pin3 = Device._pin_factory.pin(4) + pin1 = Device.pin_factory.pin(2) + pin2 = Device.pin_factory.pin(3) + pin3 = Device.pin_factory.pin(4) with LEDBoard(pin1, pin2, foo=pin3, pwm=True) as board: assert board.value == (0, 0, 0) board.value = (0, 1, 0) @@ -181,9 +181,9 @@ def test_led_board_pwm_value(): assert board.value == (0.5, 0, 0.75) def test_led_board_pwm_bad_value(): - pin1 = Device._pin_factory.pin(2) - pin2 = Device._pin_factory.pin(3) - pin3 = Device._pin_factory.pin(4) + pin1 = Device.pin_factory.pin(2) + pin2 = Device.pin_factory.pin(3) + pin3 = Device.pin_factory.pin(4) with LEDBoard(pin1, pin2, foo=pin3, pwm=True) as board: with pytest.raises(ValueError): board.value = (-1, 0, 0) @@ -191,18 +191,18 @@ def test_led_board_pwm_bad_value(): board.value = (0, 2, 0) def test_led_board_initial_value(): - pin1 = Device._pin_factory.pin(2) - pin2 = Device._pin_factory.pin(3) - pin3 = Device._pin_factory.pin(4) + pin1 = Device.pin_factory.pin(2) + pin2 = Device.pin_factory.pin(3) + pin3 = Device.pin_factory.pin(4) with LEDBoard(pin1, pin2, foo=pin3, initial_value=0) as board: assert board.value == (0, 0, 0) with LEDBoard(pin1, pin2, foo=pin3, initial_value=1) as board: assert board.value == (1, 1, 1) def test_led_board_pwm_initial_value(): - pin1 = Device._pin_factory.pin(2) - pin2 = Device._pin_factory.pin(3) - pin3 = Device._pin_factory.pin(4) + pin1 = Device.pin_factory.pin(2) + pin2 = Device.pin_factory.pin(3) + pin3 = Device.pin_factory.pin(4) with LEDBoard(pin1, pin2, foo=pin3, pwm=True, initial_value=0) as board: assert board.value == (0, 0, 0) with LEDBoard(pin1, pin2, foo=pin3, pwm=True, initial_value=1) as board: @@ -211,18 +211,18 @@ def test_led_board_pwm_initial_value(): assert board.value == (0.5, 0.5, 0.5) def test_led_board_pwm_bad_initial_value(): - pin1 = Device._pin_factory.pin(2) - pin2 = Device._pin_factory.pin(3) - pin3 = Device._pin_factory.pin(4) + pin1 = Device.pin_factory.pin(2) + pin2 = Device.pin_factory.pin(3) + pin3 = Device.pin_factory.pin(4) with pytest.raises(ValueError): LEDBoard(pin1, pin2, foo=pin3, pwm=True, initial_value=-1) with pytest.raises(ValueError): LEDBoard(pin1, pin2, foo=pin3, pwm=True, initial_value=2) def test_led_board_nested(): - pin1 = Device._pin_factory.pin(2) - pin2 = Device._pin_factory.pin(3) - pin3 = Device._pin_factory.pin(4) + pin1 = Device.pin_factory.pin(2) + pin2 = Device.pin_factory.pin(3) + pin3 = Device.pin_factory.pin(4) with LEDBoard(pin1, LEDBoard(pin2, pin3)) as board: assert list(led.pin for led in board.leds) == [pin1, pin2, pin3] assert board.value == (0, (0, 0)) @@ -232,9 +232,9 @@ def test_led_board_nested(): assert pin3.state def test_led_board_bad_blink(): - pin1 = Device._pin_factory.pin(2) - pin2 = Device._pin_factory.pin(3) - pin3 = Device._pin_factory.pin(4) + pin1 = Device.pin_factory.pin(2) + pin2 = Device.pin_factory.pin(3) + pin3 = Device.pin_factory.pin(4) with LEDBoard(pin1, LEDBoard(pin2, pin3)) as board: with pytest.raises(ValueError): board.blink(fade_in_time=1, fade_out_time=1) @@ -246,9 +246,9 @@ def test_led_board_bad_blink(): @pytest.mark.skipif(hasattr(sys, 'pypy_version_info'), reason='timing is too random on pypy') def test_led_board_blink_background(): - pin1 = Device._pin_factory.pin(4) - pin2 = Device._pin_factory.pin(5) - pin3 = Device._pin_factory.pin(6) + pin1 = Device.pin_factory.pin(4) + pin2 = Device.pin_factory.pin(5) + pin3 = Device.pin_factory.pin(6) with LEDBoard(pin1, LEDBoard(pin2, pin3)) as board: # Instantiation takes a long enough time that it throws off our timing # here! @@ -271,9 +271,9 @@ def test_led_board_blink_background(): @pytest.mark.skipif(hasattr(sys, 'pypy_version_info'), reason='timing is too random on pypy') def test_led_board_blink_foreground(): - pin1 = Device._pin_factory.pin(4) - pin2 = Device._pin_factory.pin(5) - pin3 = Device._pin_factory.pin(6) + pin1 = Device.pin_factory.pin(4) + pin2 = Device.pin_factory.pin(5) + pin3 = Device.pin_factory.pin(6) with LEDBoard(pin1, LEDBoard(pin2, pin3)) as board: pin1.clear_states() pin2.clear_states() @@ -293,9 +293,9 @@ def test_led_board_blink_foreground(): @pytest.mark.skipif(hasattr(sys, 'pypy_version_info'), reason='timing is too random on pypy') def test_led_board_blink_control(): - pin1 = Device._pin_factory.pin(4) - pin2 = Device._pin_factory.pin(5) - pin3 = Device._pin_factory.pin(6) + pin1 = Device.pin_factory.pin(4) + pin2 = Device.pin_factory.pin(5) + pin3 = Device.pin_factory.pin(6) with LEDBoard(pin1, LEDBoard(pin2, pin3)) as board: pin1.clear_states() pin2.clear_states() @@ -321,9 +321,9 @@ def test_led_board_blink_control(): @pytest.mark.skipif(hasattr(sys, 'pypy_version_info'), reason='timing is too random on pypy') def test_led_board_blink_take_over(): - pin1 = Device._pin_factory.pin(4) - pin2 = Device._pin_factory.pin(5) - pin3 = Device._pin_factory.pin(6) + pin1 = Device.pin_factory.pin(4) + pin2 = Device.pin_factory.pin(5) + pin3 = Device.pin_factory.pin(6) with LEDBoard(pin1, LEDBoard(pin2, pin3)) as board: pin1.clear_states() pin2.clear_states() @@ -346,9 +346,9 @@ def test_led_board_blink_take_over(): @pytest.mark.skipif(hasattr(sys, 'pypy_version_info'), reason='timing is too random on pypy') def test_led_board_blink_control_all(): - pin1 = Device._pin_factory.pin(4) - pin2 = Device._pin_factory.pin(5) - pin3 = Device._pin_factory.pin(6) + pin1 = Device.pin_factory.pin(4) + pin2 = Device.pin_factory.pin(5) + pin3 = Device.pin_factory.pin(6) with LEDBoard(pin1, LEDBoard(pin2, pin3)) as board: pin1.clear_states() pin2.clear_states() @@ -371,9 +371,9 @@ def test_led_board_blink_control_all(): pin3.assert_states_and_times(test) def test_led_board_blink_interrupt_on(): - pin1 = Device._pin_factory.pin(4) - pin2 = Device._pin_factory.pin(5) - pin3 = Device._pin_factory.pin(6) + pin1 = Device.pin_factory.pin(4) + pin2 = Device.pin_factory.pin(5) + pin3 = Device.pin_factory.pin(6) with LEDBoard(pin1, LEDBoard(pin2, pin3)) as board: board.blink(1, 0.1) sleep(0.2) @@ -383,9 +383,9 @@ def test_led_board_blink_interrupt_on(): pin3.assert_states([False, True, False]) def test_led_board_blink_interrupt_off(): - pin1 = Device._pin_factory.pin(4) - pin2 = Device._pin_factory.pin(5) - pin3 = Device._pin_factory.pin(6) + pin1 = Device.pin_factory.pin(4) + pin2 = Device.pin_factory.pin(5) + pin3 = Device.pin_factory.pin(6) with LEDBoard(pin1, LEDBoard(pin2, pin3)) as board: pin1.clear_states() pin2.clear_states() @@ -400,9 +400,9 @@ def test_led_board_blink_interrupt_off(): @pytest.mark.skipif(hasattr(sys, 'pypy_version_info'), reason='timing is too random on pypy') def test_led_board_fade_background(): - pin1 = Device._pin_factory.pin(4) - pin2 = Device._pin_factory.pin(5) - pin3 = Device._pin_factory.pin(6) + pin1 = Device.pin_factory.pin(4) + pin2 = Device.pin_factory.pin(5) + pin3 = Device.pin_factory.pin(6) with LEDBoard(pin1, LEDBoard(pin2, pin3, pwm=True), pwm=True) as board: pin1.clear_states() pin2.clear_states() @@ -437,9 +437,9 @@ def test_led_board_fade_background(): pin3.assert_states_and_times(test) def test_led_bar_graph_value(): - pin1 = Device._pin_factory.pin(2) - pin2 = Device._pin_factory.pin(3) - pin3 = Device._pin_factory.pin(4) + pin1 = Device.pin_factory.pin(2) + pin2 = Device.pin_factory.pin(3) + pin3 = Device.pin_factory.pin(4) with LEDBarGraph(pin1, pin2, pin3) as graph: assert isinstance(graph[0], LED) assert isinstance(graph[1], LED) @@ -470,9 +470,9 @@ def test_led_bar_graph_value(): assert graph.value == -2/3 def test_led_bar_graph_active_low(): - pin1 = Device._pin_factory.pin(2) - pin2 = Device._pin_factory.pin(3) - pin3 = Device._pin_factory.pin(4) + pin1 = Device.pin_factory.pin(2) + pin2 = Device.pin_factory.pin(3) + pin3 = Device.pin_factory.pin(4) with LEDBarGraph(pin1, pin2, pin3, active_high=False) as graph: assert not graph.active_high assert not graph[0].active_high @@ -492,9 +492,9 @@ def test_led_bar_graph_active_low(): assert not pin3.state and pin1.state and pin2.state def test_led_bar_graph_pwm_value(): - pin1 = Device._pin_factory.pin(2) - pin2 = Device._pin_factory.pin(3) - pin3 = Device._pin_factory.pin(4) + pin1 = Device.pin_factory.pin(2) + pin2 = Device.pin_factory.pin(3) + pin3 = Device.pin_factory.pin(4) with LEDBarGraph(pin1, pin2, pin3, pwm=True) as graph: assert isinstance(graph[0], PWMLED) assert isinstance(graph[1], PWMLED) @@ -519,9 +519,9 @@ def test_led_bar_graph_pwm_value(): assert graph.value == -1/2 def test_led_bar_graph_bad_value(): - pin1 = Device._pin_factory.pin(2) - pin2 = Device._pin_factory.pin(3) - pin3 = Device._pin_factory.pin(4) + pin1 = Device.pin_factory.pin(2) + pin2 = Device.pin_factory.pin(3) + pin3 = Device.pin_factory.pin(4) with LEDBarGraph(pin1, pin2, pin3) as graph: with pytest.raises(ValueError): graph.value = -2 @@ -529,9 +529,9 @@ def test_led_bar_graph_bad_value(): graph.value = 2 def test_led_bar_graph_bad_init(): - pin1 = Device._pin_factory.pin(2) - pin2 = Device._pin_factory.pin(3) - pin3 = Device._pin_factory.pin(4) + pin1 = Device.pin_factory.pin(2) + pin2 = Device.pin_factory.pin(3) + pin3 = Device.pin_factory.pin(4) with pytest.raises(TypeError): LEDBarGraph(pin1, pin2, foo=pin3) with pytest.raises(ValueError): @@ -540,9 +540,9 @@ def test_led_bar_graph_bad_init(): LEDBarGraph(pin1, pin2, pin3, initial_value=2) def test_led_bar_graph_initial_value(): - pin1 = Device._pin_factory.pin(2) - pin2 = Device._pin_factory.pin(3) - pin3 = Device._pin_factory.pin(4) + pin1 = Device.pin_factory.pin(2) + pin2 = Device.pin_factory.pin(3) + pin3 = Device.pin_factory.pin(4) with LEDBarGraph(pin1, pin2, pin3, initial_value=1/3) as graph: assert graph.value == 1/3 assert pin1.state and not (pin2.state or pin3.state) @@ -551,9 +551,9 @@ def test_led_bar_graph_initial_value(): assert pin3.state and not (pin1.state or pin2.state) def test_led_bar_graph_pwm_initial_value(): - pin1 = Device._pin_factory.pin(2) - pin2 = Device._pin_factory.pin(3) - pin3 = Device._pin_factory.pin(4) + pin1 = Device.pin_factory.pin(2) + pin2 = Device.pin_factory.pin(3) + pin3 = Device.pin_factory.pin(4) with LEDBarGraph(pin1, pin2, pin3, pwm=True, initial_value=0.5) as graph: assert graph.value == 0.5 assert (pin1.state, pin2.state, pin3.state) == (1, 0.5, 0) @@ -562,17 +562,17 @@ def test_led_bar_graph_pwm_initial_value(): assert (pin1.state, pin2.state, pin3.state) == (0, 0.5, 1) def test_led_borg(): - pins = [Device._pin_factory.pin(n) for n in (17, 27, 22)] + pins = [Device.pin_factory.pin(n) for n in (17, 27, 22)] with LedBorg() as board: assert [device.pin for device in board._leds] == pins def test_pi_liter(): - pins = [Device._pin_factory.pin(n) for n in (4, 17, 27, 18, 22, 23, 24, 25)] + pins = [Device.pin_factory.pin(n) for n in (4, 17, 27, 18, 22, 23, 24, 25)] with PiLiter() as board: assert [device.pin for device in board] == pins def test_pi_liter_graph(): - pins = [Device._pin_factory.pin(n) for n in (4, 17, 27, 18, 22, 23, 24, 25)] + pins = [Device.pin_factory.pin(n) for n in (4, 17, 27, 18, 22, 23, 24, 25)] with PiLiterBarGraph() as board: board.value = 0.5 assert [pin.state for pin in pins] == [1, 1, 1, 1, 0, 0, 0, 0] @@ -580,9 +580,9 @@ def test_pi_liter_graph(): assert board.value == 5/8 def test_traffic_lights(): - red_pin = Device._pin_factory.pin(2) - amber_pin = Device._pin_factory.pin(3) - green_pin = Device._pin_factory.pin(4) + red_pin = Device.pin_factory.pin(2) + amber_pin = Device.pin_factory.pin(3) + green_pin = Device.pin_factory.pin(4) with TrafficLights(red_pin, amber_pin, green_pin) as board: board.red.on() assert board.red.value @@ -611,15 +611,15 @@ def test_traffic_lights(): def test_traffic_lights_bad_init(): with pytest.raises(ValueError): TrafficLights() - red_pin = Device._pin_factory.pin(2) - amber_pin = Device._pin_factory.pin(3) - green_pin = Device._pin_factory.pin(4) - yellow_pin = Device._pin_factory.pin(5) + red_pin = Device.pin_factory.pin(2) + amber_pin = Device.pin_factory.pin(3) + green_pin = Device.pin_factory.pin(4) + yellow_pin = Device.pin_factory.pin(5) with pytest.raises(ValueError): TrafficLights(red=red_pin, amber=amber_pin, yellow=yellow_pin, green=green_pin) def test_pi_traffic(): - pins = [Device._pin_factory.pin(n) for n in (9, 10, 11)] + pins = [Device.pin_factory.pin(n) for n in (9, 10, 11)] with PiTraffic() as board: assert [device.pin for device in board] == pins @@ -628,27 +628,27 @@ def test_pi_stop(): PiStop() with pytest.raises(ValueError): PiStop('E') - pins_a = [Device._pin_factory.pin(n) for n in (7, 8, 25)] + pins_a = [Device.pin_factory.pin(n) for n in (7, 8, 25)] with PiStop('A') as board: assert [device.pin for device in board] == pins_a - pins_aplus = [Device._pin_factory.pin(n) for n in (21, 20, 16)] + pins_aplus = [Device.pin_factory.pin(n) for n in (21, 20, 16)] with PiStop('A+') as board: assert [device.pin for device in board] == pins_aplus - pins_b = [Device._pin_factory.pin(n) for n in (10, 9, 11)] + pins_b = [Device.pin_factory.pin(n) for n in (10, 9, 11)] with PiStop('B') as board: assert [device.pin for device in board] == pins_b - pins_bplus = [Device._pin_factory.pin(n) for n in (13, 19, 26)] + pins_bplus = [Device.pin_factory.pin(n) for n in (13, 19, 26)] with PiStop('B+') as board: assert [device.pin for device in board] == pins_bplus - pins_c = [Device._pin_factory.pin(n) for n in (18, 15, 14)] + pins_c = [Device.pin_factory.pin(n) for n in (18, 15, 14)] with PiStop('C') as board: assert [device.pin for device in board] == pins_c - pins_d = [Device._pin_factory.pin(n) for n in (2, 3, 4)] + pins_d = [Device.pin_factory.pin(n) for n in (2, 3, 4)] with PiStop('D') as board: assert [device.pin for device in board] == pins_d def test_snow_pi(): - pins = [Device._pin_factory.pin(n) for n in (23, 24, 25, 17, 18, 22, 7, 8, 9)] + pins = [Device.pin_factory.pin(n) for n in (23, 24, 25, 17, 18, 22, 7, 8, 9)] with SnowPi() as board: assert [device.pin for device in board.leds] == pins @@ -663,17 +663,17 @@ def test_snow_pi_initial_value(): assert all(device.pin.state == True for device in board.leds) def test_snow_pi_initial_value_pwm(): - pins = [Device._pin_factory.pin(n) for n in (23, 24, 25, 17, 18, 22, 7, 8, 9)] + pins = [Device.pin_factory.pin(n) for n in (23, 24, 25, 17, 18, 22, 7, 8, 9)] with SnowPi(pwm=True, initial_value=0.5) as board: assert [device.pin for device in board.leds] == pins assert all(device.pin.state == 0.5 for device in board.leds) def test_traffic_lights_buzzer(): - red_pin = Device._pin_factory.pin(2) - amber_pin = Device._pin_factory.pin(3) - green_pin = Device._pin_factory.pin(4) - buzzer_pin = Device._pin_factory.pin(5) - button_pin = Device._pin_factory.pin(6) + red_pin = Device.pin_factory.pin(2) + amber_pin = Device.pin_factory.pin(3) + green_pin = Device.pin_factory.pin(4) + buzzer_pin = Device.pin_factory.pin(5) + button_pin = Device.pin_factory.pin(6) with TrafficLightsBuzzer( TrafficLights(red_pin, amber_pin, green_pin), Buzzer(buzzer_pin), @@ -688,17 +688,17 @@ def test_traffic_lights_buzzer(): assert board.button.is_active def test_fish_dish(): - pins = [Device._pin_factory.pin(n) for n in (9, 22, 4, 8, 7)] + pins = [Device.pin_factory.pin(n) for n in (9, 22, 4, 8, 7)] with FishDish() as board: assert [led.pin for led in board.lights] + [board.buzzer.pin, board.button.pin] == pins def test_traffic_hat(): - pins = [Device._pin_factory.pin(n) for n in (24, 23, 22, 5, 25)] + pins = [Device.pin_factory.pin(n) for n in (24, 23, 22, 5, 25)] with TrafficHat() as board: assert [led.pin for led in board.lights] + [board.buzzer.pin, board.button.pin] == pins def test_robot(): - pins = [Device._pin_factory.pin(n) for n in (2, 3, 4, 5)] + pins = [Device.pin_factory.pin(n) for n in (2, 3, 4, 5)] with Robot((2, 3), (4, 5)) as robot: assert ( [device.pin for device in robot.left_motor] + @@ -733,12 +733,12 @@ def test_robot(): assert robot.value == (0, -0.5) def test_ryanteck_robot(): - pins = [Device._pin_factory.pin(n) for n in (17, 18, 22, 23)] + pins = [Device.pin_factory.pin(n) for n in (17, 18, 22, 23)] with RyanteckRobot() as board: assert [device.pin for motor in board for device in motor] == pins def test_camjam_kit_robot(): - pins = [Device._pin_factory.pin(n) for n in (9, 10, 7, 8)] + pins = [Device.pin_factory.pin(n) for n in (9, 10, 7, 8)] with CamJamKitRobot() as board: assert [device.pin for motor in board for device in motor] == pins @@ -751,7 +751,7 @@ def test_energenie_bad_init(): Energenie(5) def test_energenie(): - pins = [Device._pin_factory.pin(n) for n in (17, 22, 23, 27, 24, 25)] + pins = [Device.pin_factory.pin(n) for n in (17, 22, 23, 27, 24, 25)] with Energenie(1, initial_value=True) as device1, \ Energenie(2, initial_value=False) as device2: assert repr(device1) == '' diff --git a/tests/test_devices.py b/tests/test_devices.py index cd28f4a..de5fcd0 100644 --- a/tests/test_devices.py +++ b/tests/test_devices.py @@ -14,7 +14,7 @@ from gpiozero import * def teardown_function(function): - Device._pin_factory.reset() + Device.pin_factory.reset() # TODO add more devices tests! @@ -32,7 +32,7 @@ def test_device_non_physical(): assert w[0].category == PinNonPhysical def test_device_init(): - pin = Device._pin_factory.pin(2) + pin = Device.pin_factory.pin(2) with GPIODevice(pin) as device: assert not device.closed assert device.pin == pin @@ -55,7 +55,7 @@ def test_device_close(): assert device.pin is None def test_device_reopen_same_pin(): - pin = Device._pin_factory.pin(2) + pin = Device.pin_factory.pin(2) with GPIODevice(pin) as device: pass with GPIODevice(pin) as device2: @@ -122,7 +122,7 @@ def test_composite_device_bad_init(): with pytest.raises(ValueError): CompositeDevice(2) with pytest.raises(ValueError): - CompositeDevice(Device._pin_factory.pin(2)) + CompositeDevice(Device.pin_factory.pin(2)) def test_composite_device_read_only(): with CompositeDevice( diff --git a/tests/test_inputs.py b/tests/test_inputs.py index d2bf447..d05940c 100644 --- a/tests/test_inputs.py +++ b/tests/test_inputs.py @@ -17,12 +17,12 @@ from gpiozero import * def teardown_function(function): - Device._pin_factory.reset() + Device.pin_factory.reset() Device._reservations.clear() def test_input_initial_values(): - pin = Device._pin_factory.pin(4) + pin = Device.pin_factory.pin(4) with InputDevice(pin, pull_up=True) as device: assert pin.function == 'input' assert pin.pull == 'up' @@ -32,7 +32,7 @@ def test_input_initial_values(): assert not device.pull_up def test_input_is_active_low(): - pin = Device._pin_factory.pin(2) + pin = Device.pin_factory.pin(2) with InputDevice(pin, pull_up=True) as device: pin.drive_high() assert not device.is_active @@ -42,7 +42,7 @@ def test_input_is_active_low(): assert repr(device) == '' def test_input_is_active_high(): - pin = Device._pin_factory.pin(4) + pin = Device.pin_factory.pin(4) with InputDevice(pin, pull_up=False) as device: pin.drive_high() assert device.is_active @@ -52,13 +52,13 @@ def test_input_is_active_high(): assert repr(device) == '' def test_input_pulled_up(): - pin = Device._pin_factory.pin(2) + pin = Device.pin_factory.pin(2) with pytest.raises(PinFixedPull): InputDevice(pin, pull_up=False) def test_input_event_activated(): event = Event() - pin = Device._pin_factory.pin(4) + pin = Device.pin_factory.pin(4) with DigitalInputDevice(pin) as device: device.when_activated = lambda: event.set() assert not event.is_set() @@ -67,7 +67,7 @@ def test_input_event_activated(): def test_input_event_deactivated(): event = Event() - pin = Device._pin_factory.pin(4) + pin = Device.pin_factory.pin(4) with DigitalInputDevice(pin) as device: device.when_deactivated = lambda: event.set() assert not event.is_set() @@ -78,7 +78,7 @@ def test_input_event_deactivated(): def test_input_partial_callback(): event = Event() - pin = Device._pin_factory.pin(4) + pin = Device.pin_factory.pin(4) def foo(a, b): event.set() return a + b @@ -91,20 +91,20 @@ def test_input_partial_callback(): assert event.is_set() def test_input_wait_active(): - pin = Device._pin_factory.pin(4) + pin = Device.pin_factory.pin(4) with DigitalInputDevice(pin) as device: pin.drive_high() assert device.wait_for_active(1) assert not device.wait_for_inactive(0) def test_input_wait_inactive(): - pin = Device._pin_factory.pin(4) + pin = Device.pin_factory.pin(4) with DigitalInputDevice(pin) as device: assert device.wait_for_inactive(1) assert not device.wait_for_active(0) def test_input_smoothed_attrib(): - pin = Device._pin_factory.pin(4) + pin = Device.pin_factory.pin(4) with SmoothedInputDevice(pin, threshold=0.5, queue_len=5, partial=False) as device: assert repr(device) == '' assert device.threshold == 0.5 @@ -116,7 +116,7 @@ def test_input_smoothed_attrib(): device.threshold = 1 def test_input_smoothed_values(): - pin = Device._pin_factory.pin(4) + pin = Device.pin_factory.pin(4) with SmoothedInputDevice(pin) as device: device._queue.start() assert not device.is_active @@ -126,7 +126,7 @@ def test_input_smoothed_values(): assert device.wait_for_inactive(1) def test_input_button(): - pin = Device._pin_factory.pin(2) + pin = Device.pin_factory.pin(2) with Button(pin) as button: assert pin.pull == 'up' assert not button.is_pressed @@ -138,7 +138,7 @@ def test_input_button(): assert button.wait_for_release(1) def test_input_line_sensor(): - pin = Device._pin_factory.pin(4) + pin = Device.pin_factory.pin(4) with LineSensor(pin) as sensor: pin.drive_low() # logic is inverted for line sensor assert sensor.wait_for_line(1) @@ -148,7 +148,7 @@ def test_input_line_sensor(): assert not sensor.line_detected def test_input_motion_sensor(): - pin = Device._pin_factory.pin(4) + pin = Device.pin_factory.pin(4) with MotionSensor(pin) as sensor: pin.drive_high() assert sensor.wait_for_motion(1) @@ -160,7 +160,7 @@ def test_input_motion_sensor(): @pytest.mark.skipif(hasattr(sys, 'pypy_version_info'), reason='timing is too random on pypy') def test_input_light_sensor(): - pin = Device._pin_factory.pin(4, pin_class=MockChargingPin) + pin = Device.pin_factory.pin(4, pin_class=MockChargingPin) with LightSensor(pin) as sensor: pin.charge_time = 0.1 assert sensor.wait_for_dark(1) @@ -170,8 +170,8 @@ def test_input_light_sensor(): @pytest.mark.skipif(hasattr(sys, 'pypy_version_info'), reason='timing is too random on pypy') def test_input_distance_sensor(): - echo_pin = Device._pin_factory.pin(4) - trig_pin = Device._pin_factory.pin(5, pin_class=MockTriggerPin, echo_pin=echo_pin, echo_time=0.02) + echo_pin = Device.pin_factory.pin(4) + trig_pin = Device.pin_factory.pin(5, pin_class=MockTriggerPin, echo_pin=echo_pin, echo_time=0.02) with pytest.raises(ValueError): DistanceSensor(echo_pin, trig_pin, max_distance=-1) # normal queue len is large (because the sensor is *really* jittery) but diff --git a/tests/test_mock_pin.py b/tests/test_mock_pin.py index 880f859..e4c3124 100644 --- a/tests/test_mock_pin.py +++ b/tests/test_mock_pin.py @@ -16,7 +16,7 @@ from gpiozero import * def teardown_function(function): - Device._pin_factory.reset() + Device.pin_factory.reset() # Some rough tests to make sure our MockPin is up to snuff. This is just @@ -24,11 +24,11 @@ def teardown_function(function): def test_mock_pin_init(): with pytest.raises(ValueError): - Device._pin_factory.pin(60) - assert Device._pin_factory.pin(2).number == 2 + Device.pin_factory.pin(60) + assert Device.pin_factory.pin(2).number == 2 def test_mock_pin_defaults(): - pin = Device._pin_factory.pin(4) + pin = Device.pin_factory.pin(4) assert pin.bounce == None assert pin.edges == 'both' assert pin.frequency == None @@ -37,27 +37,27 @@ def test_mock_pin_defaults(): assert pin.state == 0 assert pin.when_changed == None pin.close() - pin = Device._pin_factory.pin(2) + pin = Device.pin_factory.pin(2) assert pin.pull == 'up' def test_mock_pin_open_close(): - pin = Device._pin_factory.pin(2) + pin = Device.pin_factory.pin(2) pin.close() def test_mock_pin_init_twice_same_pin(): - pin1 = Device._pin_factory.pin(2) - pin2 = Device._pin_factory.pin(pin1.number) + pin1 = Device.pin_factory.pin(2) + pin2 = Device.pin_factory.pin(pin1.number) assert pin1 is pin2 def test_mock_pin_init_twice_different_pin(): - pin1 = Device._pin_factory.pin(2) - pin2 = Device._pin_factory.pin(pin1.number+1) + pin1 = Device.pin_factory.pin(2) + pin2 = Device.pin_factory.pin(pin1.number+1) assert pin1 != pin2 assert pin1.number == 2 assert pin2.number == pin1.number+1 def test_mock_pwm_pin_defaults(): - pin = Device._pin_factory.pin(4, pin_class=MockPWMPin) + pin = Device.pin_factory.pin(4, pin_class=MockPWMPin) assert pin.bounce == None assert pin.edges == 'both' assert pin.frequency == None @@ -66,42 +66,42 @@ def test_mock_pwm_pin_defaults(): assert pin.state == 0 assert pin.when_changed == None pin.close() - pin = Device._pin_factory.pin(2, pin_class=MockPWMPin) + pin = Device.pin_factory.pin(2, pin_class=MockPWMPin) assert pin.pull == 'up' def test_mock_pwm_pin_open_close(): - pin = Device._pin_factory.pin(2, pin_class=MockPWMPin) + pin = Device.pin_factory.pin(2, pin_class=MockPWMPin) pin.close() def test_mock_pwm_pin_init_twice_same_pin(): - pin1 = Device._pin_factory.pin(2, pin_class=MockPWMPin) - pin2 = Device._pin_factory.pin(pin1.number, pin_class=MockPWMPin) + pin1 = Device.pin_factory.pin(2, pin_class=MockPWMPin) + pin2 = Device.pin_factory.pin(pin1.number, pin_class=MockPWMPin) assert pin1 is pin2 def test_mock_pwm_pin_init_twice_different_pin(): - pin1 = Device._pin_factory.pin(2, pin_class=MockPWMPin) - pin2 = Device._pin_factory.pin(pin1.number + 1, pin_class=MockPWMPin) + pin1 = Device.pin_factory.pin(2, pin_class=MockPWMPin) + pin2 = Device.pin_factory.pin(pin1.number + 1, pin_class=MockPWMPin) assert pin1 != pin2 assert pin1.number == 2 assert pin2.number == pin1.number+1 def test_mock_pin_init_twice_different_modes(): - pin1 = Device._pin_factory.pin(2, pin_class=MockPin) - pin2 = Device._pin_factory.pin(pin1.number + 1, pin_class=MockPWMPin) + pin1 = Device.pin_factory.pin(2, pin_class=MockPin) + pin2 = Device.pin_factory.pin(pin1.number + 1, pin_class=MockPWMPin) assert pin1 != pin2 with pytest.raises(ValueError): - Device._pin_factory.pin(pin1.number, pin_class=MockPWMPin) + Device.pin_factory.pin(pin1.number, pin_class=MockPWMPin) with pytest.raises(ValueError): - Device._pin_factory.pin(pin2.number, pin_class=MockPin) + Device.pin_factory.pin(pin2.number, pin_class=MockPin) def test_mock_pin_frequency_unsupported(): - pin = Device._pin_factory.pin(2) + pin = Device.pin_factory.pin(2) pin.frequency = None with pytest.raises(PinPWMUnsupported): pin.frequency = 100 def test_mock_pin_frequency_supported(): - pin = Device._pin_factory.pin(2, pin_class=MockPWMPin) + pin = Device.pin_factory.pin(2, pin_class=MockPWMPin) pin.function = 'output' assert pin.frequency is None pin.frequency = 100 @@ -110,7 +110,7 @@ def test_mock_pin_frequency_supported(): assert not pin.state def test_mock_pin_pull(): - pin = Device._pin_factory.pin(4) + pin = Device.pin_factory.pin(4) pin.function = 'input' assert pin.pull == 'floating' pin.pull = 'up' @@ -118,14 +118,14 @@ def test_mock_pin_pull(): pin.pull = 'down' assert not pin.state pin.close() - pin = Device._pin_factory.pin(2) + pin = Device.pin_factory.pin(2) pin.function = 'input' assert pin.pull == 'up' with pytest.raises(PinFixedPull): pin.pull = 'floating' def test_mock_pin_state(): - pin = Device._pin_factory.pin(2) + pin = Device.pin_factory.pin(2) with pytest.raises(PinSetInput): pin.state = 1 pin.function = 'output' @@ -137,7 +137,7 @@ def test_mock_pin_state(): assert pin.state == 1 def test_mock_pwm_pin_state(): - pin = Device._pin_factory.pin(2, pin_class=MockPWMPin) + pin = Device.pin_factory.pin(2, pin_class=MockPWMPin) with pytest.raises(PinSetInput): pin.state = 1 pin.function = 'output' @@ -149,7 +149,7 @@ def test_mock_pwm_pin_state(): assert pin.state == 0.5 def test_mock_pin_edges(): - pin = Device._pin_factory.pin(2) + pin = Device.pin_factory.pin(2) assert pin.when_changed is None fired = Event() pin.function = 'input' diff --git a/tests/test_outputs.py b/tests/test_outputs.py index 6908270..b50dda5 100644 --- a/tests/test_outputs.py +++ b/tests/test_outputs.py @@ -21,12 +21,12 @@ from gpiozero import * def teardown_function(function): - Device._pin_factory.reset() + Device.pin_factory.reset() Device._reservations.clear() def test_output_initial_values(): - pin = Device._pin_factory.pin(2) + pin = Device.pin_factory.pin(2) with OutputDevice(pin, initial_value=False) as device: assert pin.function == 'output' assert not pin.state @@ -37,7 +37,7 @@ def test_output_initial_values(): assert state == pin.state def test_output_write_active_high(): - pin = Device._pin_factory.pin(2) + pin = Device.pin_factory.pin(2) with OutputDevice(pin) as device: device.on() assert pin.state @@ -45,7 +45,7 @@ def test_output_write_active_high(): assert not pin.state def test_output_write_active_low(): - pin = Device._pin_factory.pin(2) + pin = Device.pin_factory.pin(2) with OutputDevice(pin, active_high=False) as device: device.on() assert not pin.state @@ -53,7 +53,7 @@ def test_output_write_active_low(): assert pin.state def test_output_write_closed(): - with OutputDevice(Device._pin_factory.pin(2)) as device: + with OutputDevice(Device.pin_factory.pin(2)) as device: device.close() assert device.closed device.close() @@ -62,14 +62,14 @@ def test_output_write_closed(): device.on() def test_output_write_silly(): - pin = Device._pin_factory.pin(2) + pin = Device.pin_factory.pin(2) with OutputDevice(pin) as device: pin.function = 'input' with pytest.raises(AttributeError): device.on() def test_output_value(): - pin = Device._pin_factory.pin(2) + pin = Device.pin_factory.pin(2) with OutputDevice(pin) as device: assert not device.value assert not pin.state @@ -81,7 +81,7 @@ def test_output_value(): assert not pin.state def test_output_digital_toggle(): - pin = Device._pin_factory.pin(2) + pin = Device.pin_factory.pin(2) with DigitalOutputDevice(pin) as device: assert not device.value assert not pin.state @@ -95,7 +95,7 @@ def test_output_digital_toggle(): @pytest.mark.skipif(hasattr(sys, 'pypy_version_info'), reason='timing is too random on pypy') def test_output_blink_background(): - pin = Device._pin_factory.pin(4) + pin = Device.pin_factory.pin(4) with DigitalOutputDevice(pin) as device: start = time() device.blink(0.1, 0.1, n=2) @@ -113,7 +113,7 @@ def test_output_blink_background(): @pytest.mark.skipif(hasattr(sys, 'pypy_version_info'), reason='timing is too random on pypy') def test_output_blink_foreground(): - pin = Device._pin_factory.pin(4) + pin = Device.pin_factory.pin(4) with DigitalOutputDevice(pin) as device: start = time() device.blink(0.1, 0.1, n=2, background=False) @@ -127,7 +127,7 @@ def test_output_blink_foreground(): ]) def test_output_blink_interrupt_on(): - pin = Device._pin_factory.pin(4) + pin = Device.pin_factory.pin(4) with DigitalOutputDevice(pin) as device: device.blink(1, 0.1) sleep(0.2) @@ -135,7 +135,7 @@ def test_output_blink_interrupt_on(): pin.assert_states([False, True, False]) def test_output_blink_interrupt_off(): - pin = Device._pin_factory.pin(4) + pin = Device.pin_factory.pin(4) with DigitalOutputDevice(pin) as device: device.blink(0.1, 1) sleep(0.2) @@ -144,14 +144,14 @@ def test_output_blink_interrupt_off(): def test_output_pwm_bad_initial_value(): with pytest.raises(ValueError): - PWMOutputDevice(Device._pin_factory.pin(2), initial_value=2) + PWMOutputDevice(Device.pin_factory.pin(2), initial_value=2) def test_output_pwm_not_supported(): with pytest.raises(AttributeError): - PWMOutputDevice(Device._pin_factory.pin(2)) + PWMOutputDevice(Device.pin_factory.pin(2)) def test_output_pwm_states(): - pin = Device._pin_factory.pin(4, pin_class=MockPWMPin) + pin = Device.pin_factory.pin(4, pin_class=MockPWMPin) with PWMOutputDevice(pin) as device: device.value = 0.1 device.value = 0.2 @@ -159,7 +159,7 @@ def test_output_pwm_states(): pin.assert_states([0.0, 0.1, 0.2, 0.0]) def test_output_pwm_read(): - pin = Device._pin_factory.pin(2, pin_class=MockPWMPin) + pin = Device.pin_factory.pin(2, pin_class=MockPWMPin) with PWMOutputDevice(pin, frequency=100) as device: assert device.frequency == 100 device.value = 0.1 @@ -172,14 +172,14 @@ def test_output_pwm_read(): assert device.frequency is None def test_output_pwm_write(): - pin = Device._pin_factory.pin(4, pin_class=MockPWMPin) + pin = Device.pin_factory.pin(4, pin_class=MockPWMPin) with PWMOutputDevice(pin) as device: device.on() device.off() pin.assert_states([False, True, False]) def test_output_pwm_toggle(): - pin = Device._pin_factory.pin(4, pin_class=MockPWMPin) + pin = Device.pin_factory.pin(4, pin_class=MockPWMPin) with PWMOutputDevice(pin) as device: device.toggle() device.value = 0.5 @@ -189,7 +189,7 @@ def test_output_pwm_toggle(): pin.assert_states([False, True, 0.5, 0.1, 0.9, False]) def test_output_pwm_active_high_read(): - pin = Device._pin_factory.pin(2, pin_class=MockPWMPin) + pin = Device.pin_factory.pin(2, pin_class=MockPWMPin) with PWMOutputDevice(pin, active_high=False) as device: device.value = 0.1 assert isclose(device.value, 0.1) @@ -198,18 +198,18 @@ def test_output_pwm_active_high_read(): assert device.value def test_output_pwm_bad_value(): - with PWMOutputDevice(Device._pin_factory.pin(2, pin_class=MockPWMPin)) as device: + with PWMOutputDevice(Device.pin_factory.pin(2, pin_class=MockPWMPin)) as device: with pytest.raises(ValueError): device.value = 2 def test_output_pwm_write_closed(): - with PWMOutputDevice(Device._pin_factory.pin(2, pin_class=MockPWMPin)) as device: + with PWMOutputDevice(Device.pin_factory.pin(2, pin_class=MockPWMPin)) as device: device.close() with pytest.raises(GPIODeviceClosed): device.on() def test_output_pwm_write_silly(): - pin = Device._pin_factory.pin(2, pin_class=MockPWMPin) + pin = Device.pin_factory.pin(2, pin_class=MockPWMPin) with PWMOutputDevice(pin) as device: pin.function = 'input' with pytest.raises(AttributeError): @@ -218,7 +218,7 @@ def test_output_pwm_write_silly(): @pytest.mark.skipif(hasattr(sys, 'pypy_version_info'), reason='timing is too random on pypy') def test_output_pwm_blink_background(): - pin = Device._pin_factory.pin(4, pin_class=MockPWMPin) + pin = Device.pin_factory.pin(4, pin_class=MockPWMPin) with PWMOutputDevice(pin) as device: start = time() device.blink(0.1, 0.1, n=2) @@ -236,7 +236,7 @@ def test_output_pwm_blink_background(): @pytest.mark.skipif(hasattr(sys, 'pypy_version_info'), reason='timing is too random on pypy') def test_output_pwm_blink_foreground(): - pin = Device._pin_factory.pin(4, pin_class=MockPWMPin) + pin = Device.pin_factory.pin(4, pin_class=MockPWMPin) with PWMOutputDevice(pin) as device: start = time() device.blink(0.1, 0.1, n=2, background=False) @@ -252,7 +252,7 @@ def test_output_pwm_blink_foreground(): @pytest.mark.skipif(hasattr(sys, 'pypy_version_info'), reason='timing is too random on pypy') def test_output_pwm_fade_background(): - pin = Device._pin_factory.pin(4, pin_class=MockPWMPin) + pin = Device.pin_factory.pin(4, pin_class=MockPWMPin) with PWMOutputDevice(pin) as device: start = time() device.blink(0, 0, 0.2, 0.2, n=2) @@ -286,7 +286,7 @@ def test_output_pwm_fade_background(): @pytest.mark.skipif(hasattr(sys, 'pypy_version_info'), reason='timing is too random on pypy') def test_output_pwm_fade_foreground(): - pin = Device._pin_factory.pin(4, pin_class=MockPWMPin) + pin = Device.pin_factory.pin(4, pin_class=MockPWMPin) with PWMOutputDevice(pin) as device: start = time() device.blink(0, 0, 0.2, 0.2, n=2, background=False) @@ -318,7 +318,7 @@ def test_output_pwm_fade_foreground(): @pytest.mark.skipif(hasattr(sys, 'pypy_version_info'), reason='timing is too random on pypy') def test_output_pwm_pulse_background(): - pin = Device._pin_factory.pin(4, pin_class=MockPWMPin) + pin = Device.pin_factory.pin(4, pin_class=MockPWMPin) with PWMOutputDevice(pin) as device: start = time() device.pulse(0.2, 0.2, n=2) @@ -352,7 +352,7 @@ def test_output_pwm_pulse_background(): @pytest.mark.skipif(hasattr(sys, 'pypy_version_info'), reason='timing is too random on pypy') def test_output_pwm_pulse_foreground(): - pin = Device._pin_factory.pin(4, pin_class=MockPWMPin) + pin = Device.pin_factory.pin(4, pin_class=MockPWMPin) with PWMOutputDevice(pin) as device: start = time() device.pulse(0.2, 0.2, n=2, background=False) @@ -382,7 +382,7 @@ def test_output_pwm_pulse_foreground(): ]) def test_output_pwm_blink_interrupt(): - pin = Device._pin_factory.pin(4, pin_class=MockPWMPin) + pin = Device.pin_factory.pin(4, pin_class=MockPWMPin) with PWMOutputDevice(pin) as device: device.blink(1, 0.1) sleep(0.2) @@ -394,7 +394,7 @@ def test_rgbled_missing_pins(): RGBLED() def test_rgbled_initial_value(): - r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3)) + r, g, b = (Device.pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3)) with RGBLED(r, g, b, initial_value=(0.1, 0.2, 0)) as device: assert r.frequency assert g.frequency @@ -404,24 +404,24 @@ def test_rgbled_initial_value(): assert isclose(b.state, 0.0) def test_rgbled_initial_value_nonpwm(): - r, g, b = (Device._pin_factory.pin(i) for i in (1, 2, 3)) + r, g, b = (Device.pin_factory.pin(i) for i in (1, 2, 3)) with RGBLED(r, g, b, pwm=False, initial_value=(0, 1, 1)) as device: assert r.state == 0 assert g.state == 1 assert b.state == 1 def test_rgbled_initial_bad_value(): - r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3)) + r, g, b = (Device.pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3)) with pytest.raises(ValueError): RGBLED(r, g, b, initial_value=(0.1, 0.2, 1.2)) def test_rgbled_initial_bad_value_nonpwm(): - r, g, b = (Device._pin_factory.pin(i) for i in (1, 2, 3)) + r, g, b = (Device.pin_factory.pin(i) for i in (1, 2, 3)) with pytest.raises(ValueError): RGBLED(r, g, b, pwm=False, initial_value=(0.1, 0.2, 0)) def test_rgbled_value(): - r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3)) + r, g, b = (Device.pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3)) with RGBLED(r, g, b) as device: assert isinstance(device._leds[0], PWMLED) assert isinstance(device._leds[1], PWMLED) @@ -439,7 +439,7 @@ def test_rgbled_value(): assert device.value == (0.5, 0.5, 0.5) def test_rgbled_value_nonpwm(): - r, g, b = (Device._pin_factory.pin(i) for i in (1, 2, 3)) + r, g, b = (Device.pin_factory.pin(i) for i in (1, 2, 3)) with RGBLED(r, g, b, pwm=False) as device: assert isinstance(device._leds[0], LED) assert isinstance(device._leds[1], LED) @@ -454,7 +454,7 @@ def test_rgbled_value_nonpwm(): assert device.value == (0, 0, 0) def test_rgbled_bad_value(): - r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3)) + r, g, b = (Device.pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3)) with RGBLED(r, g, b) as device: with pytest.raises(ValueError): device.value = (2, 0, 0) @@ -463,7 +463,7 @@ def test_rgbled_bad_value(): device.value = (0, -1, 0) def test_rgbled_bad_value_nonpwm(): - r, g, b = (Device._pin_factory.pin(i) for i in (1, 2, 3)) + r, g, b = (Device.pin_factory.pin(i) for i in (1, 2, 3)) with RGBLED(r, g, b, pwm=False) as device: with pytest.raises(ValueError): device.value = (2, 0, 0) @@ -481,7 +481,7 @@ def test_rgbled_bad_value_nonpwm(): device.value = (0, 0, 0.5) def test_rgbled_toggle(): - r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3)) + r, g, b = (Device.pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3)) with RGBLED(r, g, b) as device: assert not device.is_active assert device.value == (0, 0, 0) @@ -493,7 +493,7 @@ def test_rgbled_toggle(): assert device.value == (0, 0, 0) def test_rgbled_toggle_nonpwm(): - r, g, b = (Device._pin_factory.pin(i) for i in (1, 2, 3)) + r, g, b = (Device.pin_factory.pin(i) for i in (1, 2, 3)) with RGBLED(r, g, b, pwm=False) as device: assert not device.is_active assert device.value == (0, 0, 0) @@ -505,7 +505,7 @@ def test_rgbled_toggle_nonpwm(): assert device.value == (0, 0, 0) def test_rgbled_blink_nonpwm(): - r, g, b = (Device._pin_factory.pin(i) for i in (1, 2, 3)) + r, g, b = (Device.pin_factory.pin(i) for i in (1, 2, 3)) with RGBLED(r, g, b, pwm=False) as device: with pytest.raises(ValueError): device.blink(fade_in_time=1) @@ -515,7 +515,7 @@ def test_rgbled_blink_nonpwm(): @pytest.mark.skipif(hasattr(sys, 'pypy_version_info'), reason='timing is too random on pypy') def test_rgbled_blink_background(): - r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) for i in (4, 5, 6)) + r, g, b = (Device.pin_factory.pin(i, pin_class=MockPWMPin) for i in (4, 5, 6)) with RGBLED(r, g, b) as device: start = time() device.blink(0.1, 0.1, n=2) @@ -536,7 +536,7 @@ def test_rgbled_blink_background(): @pytest.mark.skipif(hasattr(sys, 'pypy_version_info'), reason='timing is too random on pypy') def test_rgbled_blink_background_nonpwm(): - r, g, b = (Device._pin_factory.pin(i) for i in (4, 5, 6)) + r, g, b = (Device.pin_factory.pin(i) for i in (4, 5, 6)) with RGBLED(r, g, b, pwm=False) as device: start = time() device.blink(0.1, 0.1, n=2) @@ -557,7 +557,7 @@ def test_rgbled_blink_background_nonpwm(): @pytest.mark.skipif(hasattr(sys, 'pypy_version_info'), reason='timing is too random on pypy') def test_rgbled_blink_foreground(): - r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) for i in (4, 5, 6)) + r, g, b = (Device.pin_factory.pin(i, pin_class=MockPWMPin) for i in (4, 5, 6)) with RGBLED(r, g, b) as device: start = time() device.blink(0.1, 0.1, n=2, background=False) @@ -576,7 +576,7 @@ def test_rgbled_blink_foreground(): @pytest.mark.skipif(hasattr(sys, 'pypy_version_info'), reason='timing is too random on pypy') def test_rgbled_blink_foreground_nonpwm(): - r, g, b = (Device._pin_factory.pin(i) for i in (4, 5, 6)) + r, g, b = (Device.pin_factory.pin(i) for i in (4, 5, 6)) with RGBLED(r, g, b, pwm=False) as device: start = time() device.blink(0.1, 0.1, n=2, background=False) @@ -595,7 +595,7 @@ def test_rgbled_blink_foreground_nonpwm(): @pytest.mark.skipif(hasattr(sys, 'pypy_version_info'), reason='timing is too random on pypy') def test_rgbled_fade_background(): - r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) for i in (4, 5, 6)) + r, g, b = (Device.pin_factory.pin(i, pin_class=MockPWMPin) for i in (4, 5, 6)) with RGBLED(r, g, b) as device: start = time() device.blink(0, 0, 0.2, 0.2, n=2) @@ -630,7 +630,7 @@ def test_rgbled_fade_background(): b.assert_states_and_times(expected) def test_rgbled_fade_background_nonpwm(): - r, g, b = (Device._pin_factory.pin(i) for i in (1, 2, 3)) + r, g, b = (Device.pin_factory.pin(i) for i in (1, 2, 3)) with RGBLED(r, g, b, pwm=False) as device: with pytest.raises(ValueError): device.blink(0, 0, 0.2, 0.2, n=2) @@ -638,7 +638,7 @@ def test_rgbled_fade_background_nonpwm(): @pytest.mark.skipif(hasattr(sys, 'pypy_version_info'), reason='timing is too random on pypy') def test_rgbled_fade_foreground(): - r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) for i in (4, 5, 6)) + r, g, b = (Device.pin_factory.pin(i, pin_class=MockPWMPin) for i in (4, 5, 6)) with RGBLED(r, g, b) as device: start = time() device.blink(0, 0, 0.2, 0.2, n=2, background=False) @@ -671,7 +671,7 @@ def test_rgbled_fade_foreground(): b.assert_states_and_times(expected) def test_rgbled_fade_foreground_nonpwm(): - r, g, b = (Device._pin_factory.pin(i) for i in (1, 2, 3)) + r, g, b = (Device.pin_factory.pin(i) for i in (1, 2, 3)) with RGBLED(r, g, b, pwm=False) as device: with pytest.raises(ValueError): device.blink(0, 0, 0.2, 0.2, n=2, background=False) @@ -679,7 +679,7 @@ def test_rgbled_fade_foreground_nonpwm(): @pytest.mark.skipif(hasattr(sys, 'pypy_version_info'), reason='timing is too random on pypy') def test_rgbled_pulse_background(): - r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) for i in (4, 5, 6)) + r, g, b = (Device.pin_factory.pin(i, pin_class=MockPWMPin) for i in (4, 5, 6)) with RGBLED(r, g, b) as device: start = time() device.pulse(0.2, 0.2, n=2) @@ -714,7 +714,7 @@ def test_rgbled_pulse_background(): b.assert_states_and_times(expected) def test_rgbled_pulse_background_nonpwm(): - r, g, b = (Device._pin_factory.pin(i) for i in (1, 2, 3)) + r, g, b = (Device.pin_factory.pin(i) for i in (1, 2, 3)) with RGBLED(r, g, b, pwm=False) as device: with pytest.raises(ValueError): device.pulse(0.2, 0.2, n=2) @@ -722,7 +722,7 @@ def test_rgbled_pulse_background_nonpwm(): @pytest.mark.skipif(hasattr(sys, 'pypy_version_info'), reason='timing is too random on pypy') def test_rgbled_pulse_foreground(): - r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) for i in (4, 5, 6)) + r, g, b = (Device.pin_factory.pin(i, pin_class=MockPWMPin) for i in (4, 5, 6)) with RGBLED(r, g, b) as device: start = time() device.pulse(0.2, 0.2, n=2, background=False) @@ -755,13 +755,13 @@ def test_rgbled_pulse_foreground(): b.assert_states_and_times(expected) def test_rgbled_pulse_foreground_nonpwm(): - r, g, b = (Device._pin_factory.pin(i) for i in (1, 2, 3)) + r, g, b = (Device.pin_factory.pin(i) for i in (1, 2, 3)) with RGBLED(r, g, b, pwm=False) as device: with pytest.raises(ValueError): device.pulse(0.2, 0.2, n=2, background=False) def test_rgbled_blink_interrupt(): - r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) for i in (4, 5, 6)) + r, g, b = (Device.pin_factory.pin(i, pin_class=MockPWMPin) for i in (4, 5, 6)) with RGBLED(r, g, b) as device: device.blink(1, 0.1) sleep(0.2) @@ -771,7 +771,7 @@ def test_rgbled_blink_interrupt(): b.assert_states([0, 1, 0]) def test_rgbled_blink_interrupt_nonpwm(): - r, g, b = (Device._pin_factory.pin(i) for i in (4, 5, 6)) + r, g, b = (Device.pin_factory.pin(i) for i in (4, 5, 6)) with RGBLED(r, g, b, pwm=False) as device: device.blink(1, 0.1) sleep(0.2) @@ -781,7 +781,7 @@ def test_rgbled_blink_interrupt_nonpwm(): b.assert_states([0, 1, 0]) def test_rgbled_close(): - r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3)) + r, g, b = (Device.pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3)) with RGBLED(r, g, b) as device: assert not device.closed device.close() @@ -790,7 +790,7 @@ def test_rgbled_close(): assert device.closed def test_rgbled_close_nonpwm(): - r, g, b = (Device._pin_factory.pin(i) for i in (1, 2, 3)) + r, g, b = (Device.pin_factory.pin(i) for i in (1, 2, 3)) with RGBLED(r, g, b, pwm=False) as device: assert not device.closed device.close() @@ -803,8 +803,8 @@ def test_motor_missing_pins(): Motor() def test_motor_pins(): - f = Device._pin_factory.pin(1, pin_class=MockPWMPin) - b = Device._pin_factory.pin(2, pin_class=MockPWMPin) + f = Device.pin_factory.pin(1, pin_class=MockPWMPin) + b = Device.pin_factory.pin(2, pin_class=MockPWMPin) with Motor(f, b) as device: assert device.forward_device.pin is f assert isinstance(device.forward_device, PWMOutputDevice) @@ -812,8 +812,8 @@ def test_motor_pins(): assert isinstance(device.backward_device, PWMOutputDevice) def test_motor_pins_nonpwm(): - f = Device._pin_factory.pin(1) - b = Device._pin_factory.pin(2) + f = Device.pin_factory.pin(1) + b = Device.pin_factory.pin(2) with Motor(f, b, pwm=False) as device: assert device.forward_device.pin is f assert isinstance(device.forward_device, DigitalOutputDevice) @@ -821,8 +821,8 @@ def test_motor_pins_nonpwm(): assert isinstance(device.backward_device, DigitalOutputDevice) def test_motor_close(): - f = Device._pin_factory.pin(1, pin_class=MockPWMPin) - b = Device._pin_factory.pin(2, pin_class=MockPWMPin) + f = Device.pin_factory.pin(1, pin_class=MockPWMPin) + b = Device.pin_factory.pin(2, pin_class=MockPWMPin) with Motor(f, b) as device: device.close() assert device.closed @@ -832,8 +832,8 @@ def test_motor_close(): assert device.closed def test_motor_close_nonpwm(): - f = Device._pin_factory.pin(1) - b = Device._pin_factory.pin(2) + f = Device.pin_factory.pin(1) + b = Device.pin_factory.pin(2) with Motor(f, b, pwm=False) as device: device.close() assert device.closed @@ -841,8 +841,8 @@ def test_motor_close_nonpwm(): assert device.backward_device.pin is None def test_motor_value(): - f = Device._pin_factory.pin(1, pin_class=MockPWMPin) - b = Device._pin_factory.pin(2, pin_class=MockPWMPin) + f = Device.pin_factory.pin(1, pin_class=MockPWMPin) + b = Device.pin_factory.pin(2, pin_class=MockPWMPin) with Motor(f, b) as device: device.value = -1 assert device.is_active @@ -866,8 +866,8 @@ def test_motor_value(): assert b.state == 0 and f.state == 0 def test_motor_value_nonpwm(): - f = Device._pin_factory.pin(1) - b = Device._pin_factory.pin(2) + f = Device.pin_factory.pin(1) + b = Device.pin_factory.pin(2) with Motor(f, b, pwm=False) as device: device.value = -1 assert device.is_active @@ -883,8 +883,8 @@ def test_motor_value_nonpwm(): assert b.state == 0 and f.state == 0 def test_motor_bad_value(): - f = Device._pin_factory.pin(1, pin_class=MockPWMPin) - b = Device._pin_factory.pin(2, pin_class=MockPWMPin) + f = Device.pin_factory.pin(1, pin_class=MockPWMPin) + b = Device.pin_factory.pin(2, pin_class=MockPWMPin) with Motor(f, b) as device: with pytest.raises(ValueError): device.value = -2 @@ -896,8 +896,8 @@ def test_motor_bad_value(): device.backward(2) def test_motor_bad_value_nonpwm(): - f = Device._pin_factory.pin(1) - b = Device._pin_factory.pin(2) + f = Device.pin_factory.pin(1) + b = Device.pin_factory.pin(2) with Motor(f, b, pwm=False) as device: with pytest.raises(ValueError): device.value = -2 @@ -909,8 +909,8 @@ def test_motor_bad_value_nonpwm(): device.value = -0.5 def test_motor_reverse(): - f = Device._pin_factory.pin(1, pin_class=MockPWMPin) - b = Device._pin_factory.pin(2, pin_class=MockPWMPin) + f = Device.pin_factory.pin(1, pin_class=MockPWMPin) + b = Device.pin_factory.pin(2, pin_class=MockPWMPin) with Motor(f, b) as device: device.forward() assert device.value == 1 @@ -926,8 +926,8 @@ def test_motor_reverse(): assert b.state == 0 and f.state == 0.5 def test_motor_reverse_nonpwm(): - f = Device._pin_factory.pin(1) - b = Device._pin_factory.pin(2) + f = Device.pin_factory.pin(1) + b = Device.pin_factory.pin(2) with Motor(f, b, pwm=False) as device: device.forward() assert device.value == 1 @@ -937,13 +937,13 @@ def test_motor_reverse_nonpwm(): assert b.state == 1 and f.state == 0 def test_servo_pins(): - p = Device._pin_factory.pin(1, pin_class=MockPWMPin) + p = Device.pin_factory.pin(1, pin_class=MockPWMPin) with Servo(p) as device: assert device.pwm_device.pin is p assert isinstance(device.pwm_device, PWMOutputDevice) def test_servo_bad_value(): - p = Device._pin_factory.pin(1, pin_class=MockPWMPin) + p = Device.pin_factory.pin(1, pin_class=MockPWMPin) with pytest.raises(ValueError): Servo(p, initial_value=2) with pytest.raises(ValueError): @@ -952,12 +952,12 @@ def test_servo_bad_value(): Servo(p, max_pulse_width=30/1000) def test_servo_pins_nonpwm(): - p = Device._pin_factory.pin(2) + p = Device.pin_factory.pin(2) with pytest.raises(PinPWMUnsupported): Servo(p) def test_servo_close(): - p = Device._pin_factory.pin(2, pin_class=MockPWMPin) + p = Device.pin_factory.pin(2, pin_class=MockPWMPin) with Servo(p) as device: device.close() assert device.closed @@ -966,7 +966,7 @@ def test_servo_close(): assert device.closed def test_servo_pulse_width(): - p = Device._pin_factory.pin(2, pin_class=MockPWMPin) + p = Device.pin_factory.pin(2, pin_class=MockPWMPin) with Servo(p, min_pulse_width=5/10000, max_pulse_width=25/10000) as device: assert isclose(device.min_pulse_width, 5/10000) assert isclose(device.max_pulse_width, 25/10000) @@ -980,7 +980,7 @@ def test_servo_pulse_width(): assert device.pulse_width is None def test_servo_values(): - p = Device._pin_factory.pin(1, pin_class=MockPWMPin) + p = Device.pin_factory.pin(1, pin_class=MockPWMPin) with Servo(p) as device: device.min() assert device.is_active @@ -1007,13 +1007,13 @@ def test_servo_values(): assert device.value is None def test_angular_servo_range(): - p = Device._pin_factory.pin(1, pin_class=MockPWMPin) + p = Device.pin_factory.pin(1, pin_class=MockPWMPin) with AngularServo(p, initial_angle=15, min_angle=0, max_angle=90) as device: assert device.min_angle == 0 assert device.max_angle == 90 def test_angular_servo_angles(): - p = Device._pin_factory.pin(1, pin_class=MockPWMPin) + p = Device.pin_factory.pin(1, pin_class=MockPWMPin) with AngularServo(p) as device: device.angle = 0 assert device.angle == 0 diff --git a/tests/test_pins_data.py b/tests/test_pins_data.py index 2a9af16..109257b 100644 --- a/tests/test_pins_data.py +++ b/tests/test_pins_data.py @@ -19,9 +19,7 @@ from gpiozero import * def test_pi_revision(): - # We're not using _set_pin_factory here because we don't want to implicitly - # close the old instance, just replace it while we test stuff - with patch('gpiozero.devices.Device._pin_factory', LocalPiFactory()): + with patch('gpiozero.devices.Device.pin_factory', LocalPiFactory()): # Can't use MockPin for this as we want something that'll actually try # and read /proc/cpuinfo (MockPin simply parrots the 2B's data); # LocalPiFactory is used as we can definitely instantiate it (strictly @@ -32,20 +30,20 @@ def test_pi_revision(): assert pi_info().revision == '0002' # LocalPiFactory caches the revision (because realistically it # isn't going to change at runtime); we need to wipe it here though - Device._pin_factory._info = None + Device.pin_factory._info = None m.return_value.__enter__.return_value = ['Revision: a21042'] assert pi_info().revision == 'a21042' # Check over-volting result (some argument over whether this is 7 or # 8 character result; make sure both work) - Device._pin_factory._info = None + Device.pin_factory._info = None m.return_value.__enter__.return_value = ['Revision: 1000003'] assert pi_info().revision == '0003' - Device._pin_factory._info = None + Device.pin_factory._info = None m.return_value.__enter__.return_value = ['Revision: 100003'] assert pi_info().revision == '0003' with pytest.raises(PinUnknownPi): m.return_value.__enter__.return_value = ['nothing', 'relevant', 'at all'] - Device._pin_factory._info = None + Device.pin_factory._info = None pi_info() with pytest.raises(PinUnknownPi): pi_info('0fff') diff --git a/tests/test_real_pins.py b/tests/test_real_pins.py index bccf045..8b9b7da 100644 --- a/tests/test_real_pins.py +++ b/tests/test_real_pins.py @@ -52,9 +52,9 @@ def pin_factory(request): except Exception as e: pytest.skip("skipped factory %s: %s" % (request.param, str(e))) else: - Device._set_pin_factory(factory) + Device.pin_factory = factory def fin(): - Device._set_pin_factory(MockFactory()) + Device.pin_factory = MockFactory() request.addfinalizer(fin) return factory @@ -147,7 +147,7 @@ def test_bad_duty_cycle(pins): # NOTE: There's some race in RPi.GPIO that causes a segfault if we # don't pause before starting PWM; only seems to happen when stopping # and restarting PWM very rapidly (i.e. between test cases). - if Device._pin_factory.__class__.__name__ == 'RPiGPIOFactory': + if Device.pin_factory.__class__.__name__ == 'RPiGPIOFactory': sleep(0.1) test_pin.frequency = 100 except PinPWMUnsupported: @@ -164,7 +164,7 @@ def test_duty_cycles(pins): test_pin.function = 'output' try: # NOTE: see above - if Device._pin_factory.__class__.__name__ == 'RPiGPIOFactory': + if Device.pin_factory.__class__.__name__ == 'RPiGPIOFactory': sleep(0.1) test_pin.frequency = 100 except PinPWMUnsupported: diff --git a/tests/test_spi.py b/tests/test_spi.py index 7b514bf..6ae6967 100644 --- a/tests/test_spi.py +++ b/tests/test_spi.py @@ -26,80 +26,80 @@ from gpiozero import * def teardown_function(function): - Device._pin_factory.reset() + Device.pin_factory.reset() def test_spi_hardware_params(): with patch('os.open'), patch('mmap.mmap') as mmap_mmap, patch('io.open') as io_open: mmap_mmap.return_value = array(nstr('B'), (0,) * 4096) io_open.return_value.__enter__.return_value = ['Revision: a21042'] - with patch('gpiozero.devices.Device._pin_factory', NativeFactory()), \ + with patch('gpiozero.devices.Device.pin_factory', NativeFactory()), \ patch('gpiozero.pins.local.SpiDev'): - with Device._pin_factory.spi() as device: + with Device.pin_factory.spi() as device: assert isinstance(device, LocalPiHardwareSPI) - with Device._pin_factory.spi(port=0, device=0) as device: + with Device.pin_factory.spi(port=0, device=0) as device: assert isinstance(device, LocalPiHardwareSPI) - with Device._pin_factory.spi(port=0, device=1) as device: + with Device.pin_factory.spi(port=0, device=1) as device: assert isinstance(device, LocalPiHardwareSPI) - with Device._pin_factory.spi(clock_pin=11) as device: + with Device.pin_factory.spi(clock_pin=11) as device: assert isinstance(device, LocalPiHardwareSPI) - with Device._pin_factory.spi(clock_pin=11, mosi_pin=10, select_pin=8) as device: + with Device.pin_factory.spi(clock_pin=11, mosi_pin=10, select_pin=8) as device: assert isinstance(device, LocalPiHardwareSPI) - with Device._pin_factory.spi(clock_pin=11, mosi_pin=10, select_pin=7) as device: + with Device.pin_factory.spi(clock_pin=11, mosi_pin=10, select_pin=7) as device: assert isinstance(device, LocalPiHardwareSPI) - with Device._pin_factory.spi(shared=True) as device: + with Device.pin_factory.spi(shared=True) as device: assert isinstance(device, LocalPiHardwareSPIShared) with pytest.raises(ValueError): - Device._pin_factory.spi(port=1) + Device.pin_factory.spi(port=1) with pytest.raises(ValueError): - Device._pin_factory.spi(device=2) + Device.pin_factory.spi(device=2) with pytest.raises(ValueError): - Device._pin_factory.spi(port=0, clock_pin=12) + Device.pin_factory.spi(port=0, clock_pin=12) with pytest.raises(ValueError): - Device._pin_factory.spi(foo='bar') + Device.pin_factory.spi(foo='bar') def test_spi_software_params(): with patch('os.open'), patch('mmap.mmap') as mmap_mmap, patch('io.open') as io_open: mmap_mmap.return_value = array(nstr('B'), (0,) * 4096) io_open.return_value.__enter__.return_value = ['Revision: a21042'] - with patch('gpiozero.devices.Device._pin_factory', NativeFactory()), \ + with patch('gpiozero.devices.Device.pin_factory', NativeFactory()), \ patch('gpiozero.pins.local.SpiDev'): - with Device._pin_factory.spi(select_pin=6) as device: + with Device.pin_factory.spi(select_pin=6) as device: assert isinstance(device, LocalPiSoftwareSPI) - with Device._pin_factory.spi(clock_pin=11, mosi_pin=9, miso_pin=10) as device: + with Device.pin_factory.spi(clock_pin=11, mosi_pin=9, miso_pin=10) as device: assert isinstance(device, LocalPiSoftwareSPI) - with Device._pin_factory.spi(select_pin=6, shared=True) as device: + with Device.pin_factory.spi(select_pin=6, shared=True) as device: assert isinstance(device, LocalPiSoftwareSPIShared) - with patch('gpiozero.devices.Device._pin_factory', NativeFactory()), \ + with patch('gpiozero.devices.Device.pin_factory', NativeFactory()), \ patch('gpiozero.pins.local.SpiDev', None): # Clear out the old factory's pins cache (this is only necessary # because we're being very naughty switching out pin factories) - Device._pin_factory.pins.clear() + Device.pin_factory.pins.clear() # Ensure software fallback works when SpiDev isn't present - with Device._pin_factory.spi() as device: + with Device.pin_factory.spi() as device: assert isinstance(device, LocalPiSoftwareSPI) def test_spi_hardware_conflict(): with patch('gpiozero.pins.local.SpiDev') as spidev: with LED(11) as led: with pytest.raises(GPIOPinInUse): - Device._pin_factory.spi(port=0, device=0) + Device.pin_factory.spi(port=0, device=0) with patch('gpiozero.pins.local.SpiDev') as spidev: - with Device._pin_factory.spi(port=0, device=0) as spi: + with Device.pin_factory.spi(port=0, device=0) as spi: with pytest.raises(GPIOPinInUse): LED(11) def test_spi_hardware_read(): with patch('gpiozero.pins.local.SpiDev') as spidev: spidev.return_value.xfer2.side_effect = lambda data: list(range(10))[:len(data)] - with Device._pin_factory.spi() as device: + with Device.pin_factory.spi() as device: assert device.read(3) == [0, 1, 2] assert device.read(6) == list(range(6)) def test_spi_hardware_write(): with patch('gpiozero.pins.local.SpiDev') as spidev: spidev.return_value.xfer2.side_effect = lambda data: list(range(10))[:len(data)] - with Device._pin_factory.spi() as device: + with Device.pin_factory.spi() as device: assert device.write([0, 1, 2]) == 3 assert spidev.return_value.xfer2.called_with([0, 1, 2]) assert device.write(list(range(6))) == 6 @@ -111,7 +111,7 @@ def test_spi_hardware_modes(): spidev.return_value.lsbfirst = False spidev.return_value.cshigh = True spidev.return_value.bits_per_word = 8 - with Device._pin_factory.spi() as device: + with Device.pin_factory.spi() as device: assert device.clock_mode == 0 assert not device.clock_polarity assert not device.clock_phase @@ -139,7 +139,7 @@ def test_spi_software_read(): self.tx_word(i) with patch('gpiozero.pins.local.SpiDev', None), \ SPISlave(11, 10, 9, 8) as slave, \ - Device._pin_factory.spi() as master: + Device.pin_factory.spi() as master: assert master.read(3) == [0, 1, 2] assert master.read(6) == [0, 1, 2, 3, 4, 5] slave.clock_phase = True @@ -150,7 +150,7 @@ def test_spi_software_read(): def test_spi_software_write(): with patch('gpiozero.pins.local.SpiDev', None), \ MockSPIDevice(11, 10, 9, 8) as test_device, \ - Device._pin_factory.spi() as master: + Device.pin_factory.spi() as master: master.write([0]) assert test_device.rx_word() == 0 master.write([2, 0]) @@ -160,7 +160,7 @@ def test_spi_software_write(): def test_spi_software_clock_mode(): with patch('gpiozero.pins.local.SpiDev', None), \ - Device._pin_factory.spi() as master: + Device.pin_factory.spi() as master: assert master.clock_mode == 0 assert not master.clock_polarity assert not master.clock_phase @@ -178,7 +178,7 @@ def test_spi_software_clock_mode(): def test_spi_software_attr(): with patch('gpiozero.pins.local.SpiDev', None), \ - Device._pin_factory.spi() as master: + Device.pin_factory.spi() as master: assert not master.lsb_first assert not master.select_high assert master.bits_per_word == 8 diff --git a/tests/test_spi_devices.py b/tests/test_spi_devices.py index 4f45171..e6ae140 100644 --- a/tests/test_spi_devices.py +++ b/tests/test_spi_devices.py @@ -21,7 +21,7 @@ from gpiozero import * def teardown_function(function): - Device._pin_factory.reset() + Device.pin_factory.reset() def clamp(v, min_value, max_value): return min(max_value, max(min_value, v))