mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
Fix #459 - properly support remote SPI with pigpio
Sorry! Dave's messing around with the pin implementations again. Hopefully the last time. The pin_factory is now really a factory object which can be asked to produce individual pins or pin-based interfaces like SPI (which can be supported properly via pigpio).
This commit is contained in:
10
tests/conftest.py
Normal file
10
tests/conftest.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from __future__ import (
|
||||
unicode_literals,
|
||||
print_function,
|
||||
absolute_import,
|
||||
division,
|
||||
)
|
||||
str = type('')
|
||||
|
||||
import os
|
||||
os.environ['GPIOZERO_PIN_FACTORY'] = 'mock'
|
||||
@@ -11,26 +11,39 @@ import sys
|
||||
import pytest
|
||||
from time import sleep
|
||||
|
||||
from gpiozero.pins.mock import MockPin, MockPWMPin
|
||||
from gpiozero import *
|
||||
from gpiozero.pins.mock import MockPWMPin, MockPin
|
||||
|
||||
|
||||
def setup_function(function):
|
||||
import gpiozero.devices
|
||||
# dirty, but it does the job
|
||||
if function.__name__ in ('test_robot', 'test_ryanteck_robot', 'test_camjam_kit_robot', 'test_led_borg', 'test_snow_pi_initial_value_pwm'):
|
||||
gpiozero.devices.pin_factory = MockPWMPin
|
||||
else:
|
||||
gpiozero.devices.pin_factory = MockPin
|
||||
Device._pin_factory.pin_class = MockPWMPin if function.__name__ in (
|
||||
'test_robot',
|
||||
'test_ryanteck_robot',
|
||||
'test_camjam_kit_robot',
|
||||
'test_led_borg',
|
||||
'test_led_board_pwm_value',
|
||||
'test_led_board_pwm_bad_value',
|
||||
'test_snow_pi_initial_value_pwm',
|
||||
'test_led_board_pwm_initial_value',
|
||||
'test_led_board_pwm_bad_initial_value',
|
||||
'test_led_board_fade_background',
|
||||
'test_led_bar_graph_pwm_value',
|
||||
'test_led_bar_graph_pwm_initial_value',
|
||||
) else MockPin
|
||||
|
||||
def teardown_function(function):
|
||||
MockPin.clear_pins()
|
||||
Device._pin_factory.reset()
|
||||
|
||||
def teardown_module(module):
|
||||
# make sure we reset the default
|
||||
Device._pin_factory.pwm = False
|
||||
|
||||
|
||||
def test_composite_output_on_off():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(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))
|
||||
@@ -38,9 +51,9 @@ def test_composite_output_on_off():
|
||||
assert not any((pin1.state, pin2.state, pin3.state))
|
||||
|
||||
def test_composite_output_toggle():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(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))
|
||||
@@ -51,9 +64,9 @@ def test_composite_output_toggle():
|
||||
assert not pin3.state
|
||||
|
||||
def test_composite_output_value():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(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()
|
||||
@@ -64,9 +77,9 @@ def test_composite_output_value():
|
||||
assert device[2].is_active
|
||||
|
||||
def test_led_board_on_off():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(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)
|
||||
@@ -121,9 +134,9 @@ def test_led_board_on_off():
|
||||
assert pin3.state
|
||||
|
||||
def test_led_board_active_low():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(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
|
||||
@@ -145,9 +158,9 @@ def test_led_board_active_low():
|
||||
assert not pin3.state
|
||||
|
||||
def test_led_board_value():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(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)
|
||||
@@ -156,9 +169,9 @@ def test_led_board_value():
|
||||
assert board.value == (1, 0, 1)
|
||||
|
||||
def test_led_board_pwm_value():
|
||||
pin1 = MockPWMPin(2)
|
||||
pin2 = MockPWMPin(3)
|
||||
pin3 = MockPWMPin(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)
|
||||
@@ -167,9 +180,9 @@ def test_led_board_pwm_value():
|
||||
assert board.value == (0.5, 0, 0.75)
|
||||
|
||||
def test_led_board_pwm_bad_value():
|
||||
pin1 = MockPWMPin(2)
|
||||
pin2 = MockPWMPin(3)
|
||||
pin3 = MockPWMPin(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)
|
||||
@@ -177,18 +190,18 @@ def test_led_board_pwm_bad_value():
|
||||
board.value = (0, 2, 0)
|
||||
|
||||
def test_led_board_initial_value():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(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 = MockPWMPin(2)
|
||||
pin2 = MockPWMPin(3)
|
||||
pin3 = MockPWMPin(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:
|
||||
@@ -197,18 +210,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 = MockPWMPin(2)
|
||||
pin2 = MockPWMPin(3)
|
||||
pin3 = MockPWMPin(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 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(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))
|
||||
@@ -218,9 +231,9 @@ def test_led_board_nested():
|
||||
assert pin3.state
|
||||
|
||||
def test_led_board_bad_blink():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(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)
|
||||
@@ -232,9 +245,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 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(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:
|
||||
board.blink(0.1, 0.1, n=2)
|
||||
board._blink_thread.join() # naughty, but ensures no arbitrary waits in the test
|
||||
@@ -252,9 +265,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 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(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:
|
||||
board.blink(0.1, 0.1, n=2, background=False)
|
||||
test = [
|
||||
@@ -271,9 +284,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 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(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:
|
||||
board.blink(0.1, 0.1, n=2)
|
||||
# make sure the blink thread's started
|
||||
@@ -296,9 +309,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 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(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:
|
||||
board[1].blink(0.1, 0.1, n=2)
|
||||
board.blink(0.1, 0.1, n=2) # immediately take over blinking
|
||||
@@ -318,9 +331,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 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(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:
|
||||
board.blink(0.1, 0.1, n=2)
|
||||
# make sure the blink thread's started
|
||||
@@ -340,9 +353,9 @@ def test_led_board_blink_control_all():
|
||||
pin3.assert_states_and_times(test)
|
||||
|
||||
def test_led_board_blink_interrupt_on():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(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:
|
||||
board.blink(1, 0.1)
|
||||
sleep(0.2)
|
||||
@@ -352,9 +365,9 @@ def test_led_board_blink_interrupt_on():
|
||||
pin3.assert_states([False, True, False])
|
||||
|
||||
def test_led_board_blink_interrupt_off():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(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:
|
||||
board.blink(0.1, 1)
|
||||
sleep(0.2)
|
||||
@@ -366,9 +379,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 = MockPWMPin(2)
|
||||
pin2 = MockPWMPin(3)
|
||||
pin3 = MockPWMPin(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, pwm=True), pwm=True) as board:
|
||||
board.blink(0, 0, 0.2, 0.2, n=2)
|
||||
board._blink_thread.join()
|
||||
@@ -400,9 +413,9 @@ def test_led_board_fade_background():
|
||||
pin3.assert_states_and_times(test)
|
||||
|
||||
def test_led_bar_graph_value():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(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)
|
||||
@@ -433,9 +446,9 @@ def test_led_bar_graph_value():
|
||||
assert graph.value == -2/3
|
||||
|
||||
def test_led_bar_graph_active_low():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(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
|
||||
@@ -455,9 +468,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 = MockPWMPin(2)
|
||||
pin2 = MockPWMPin(3)
|
||||
pin3 = MockPWMPin(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)
|
||||
@@ -482,9 +495,9 @@ def test_led_bar_graph_pwm_value():
|
||||
assert graph.value == -1/2
|
||||
|
||||
def test_led_bar_graph_bad_value():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(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
|
||||
@@ -492,9 +505,9 @@ def test_led_bar_graph_bad_value():
|
||||
graph.value = 2
|
||||
|
||||
def test_led_bar_graph_bad_init():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(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):
|
||||
@@ -503,9 +516,9 @@ def test_led_bar_graph_bad_init():
|
||||
LEDBarGraph(pin1, pin2, pin3, initial_value=2)
|
||||
|
||||
def test_led_bar_graph_initial_value():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(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)
|
||||
@@ -514,9 +527,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 = MockPWMPin(2)
|
||||
pin2 = MockPWMPin(3)
|
||||
pin3 = MockPWMPin(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)
|
||||
@@ -525,17 +538,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 = [MockPWMPin(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 = [MockPin(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 = [MockPin(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]
|
||||
@@ -543,9 +556,9 @@ def test_pi_liter_graph():
|
||||
assert board.value == 5/8
|
||||
|
||||
def test_traffic_lights():
|
||||
red_pin = MockPin(2)
|
||||
amber_pin = MockPin(3)
|
||||
green_pin = MockPin(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
|
||||
@@ -574,15 +587,15 @@ def test_traffic_lights():
|
||||
def test_traffic_lights_bad_init():
|
||||
with pytest.raises(ValueError):
|
||||
TrafficLights()
|
||||
red_pin = MockPin(2)
|
||||
amber_pin = MockPin(3)
|
||||
green_pin = MockPin(4)
|
||||
yellow_pin = MockPin(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 = [MockPin(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
|
||||
|
||||
@@ -591,27 +604,27 @@ def test_pi_stop():
|
||||
PiStop()
|
||||
with pytest.raises(ValueError):
|
||||
PiStop('E')
|
||||
pins_a = [MockPin(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 = [MockPin(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 = [MockPin(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 = [MockPin(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 = [MockPin(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 = [MockPin(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 = [MockPin(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
|
||||
|
||||
@@ -626,17 +639,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 = [MockPWMPin(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 = MockPin(2)
|
||||
amber_pin = MockPin(3)
|
||||
green_pin = MockPin(4)
|
||||
buzzer_pin = MockPin(5)
|
||||
button_pin = MockPin(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),
|
||||
@@ -651,17 +664,17 @@ def test_traffic_lights_buzzer():
|
||||
assert board.button.is_active
|
||||
|
||||
def test_fish_dish():
|
||||
pins = [MockPin(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 = [MockPin(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 = [MockPWMPin(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] +
|
||||
@@ -696,12 +709,12 @@ def test_robot():
|
||||
assert robot.value == (0, -0.5)
|
||||
|
||||
def test_ryanteck_robot():
|
||||
pins = [MockPWMPin(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 = [MockPWMPin(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
|
||||
|
||||
@@ -714,7 +727,7 @@ def test_energenie_bad_init():
|
||||
Energenie(5)
|
||||
|
||||
def test_energenie():
|
||||
pins = [MockPin(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) == '<gpiozero.Energenie object on socket 1>'
|
||||
|
||||
@@ -6,86 +6,87 @@ from __future__ import (
|
||||
)
|
||||
str = type('')
|
||||
|
||||
import warnings
|
||||
|
||||
import pytest
|
||||
|
||||
from gpiozero.pins.mock import MockPin
|
||||
from gpiozero import *
|
||||
|
||||
|
||||
def teardown_function(function):
|
||||
MockPin.clear_pins()
|
||||
Device._pin_factory.reset()
|
||||
|
||||
|
||||
# TODO add more devices tests!
|
||||
|
||||
def test_device_no_pin():
|
||||
def test_device_bad_pin():
|
||||
with pytest.raises(GPIOPinMissing):
|
||||
device = GPIODevice()
|
||||
with pytest.raises(PinInvalidPin):
|
||||
device = GPIODevice(60)
|
||||
|
||||
def test_device_non_physical():
|
||||
with warnings.catch_warnings(record=True) as w:
|
||||
device = GPIODevice(37)
|
||||
assert len(w) == 1
|
||||
assert w[0].category == PinNonPhysical
|
||||
|
||||
def test_device_init():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with GPIODevice(pin) as device:
|
||||
assert not device.closed
|
||||
assert device.pin == pin
|
||||
|
||||
def test_device_init_twice_same_pin():
|
||||
pin = MockPin(2)
|
||||
with GPIODevice(pin) as device:
|
||||
with GPIODevice(2) as device:
|
||||
with pytest.raises(GPIOPinInUse):
|
||||
device2 = GPIODevice(pin)
|
||||
GPIODevice(2)
|
||||
|
||||
def test_device_init_twice_different_pin():
|
||||
pin = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
with GPIODevice(pin) as device:
|
||||
with GPIODevice(pin2) as device2:
|
||||
with GPIODevice(2) as device:
|
||||
with GPIODevice(3) as device2:
|
||||
pass
|
||||
|
||||
def test_device_close():
|
||||
pin = MockPin(2)
|
||||
device = GPIODevice(pin)
|
||||
device = GPIODevice(2)
|
||||
device.close()
|
||||
assert device.closed
|
||||
assert device.pin is None
|
||||
|
||||
def test_device_reopen_same_pin():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
device = GPIODevice(pin)
|
||||
device.close()
|
||||
device2 = GPIODevice(pin)
|
||||
assert not device2.closed
|
||||
assert device2.pin == pin
|
||||
assert device2.pin is pin
|
||||
assert device.closed
|
||||
assert device.pin is None
|
||||
device2.close()
|
||||
|
||||
def test_device_repr():
|
||||
pin = MockPin(2)
|
||||
with GPIODevice(pin) as device:
|
||||
assert repr(device) == '<gpiozero.GPIODevice object on pin %s, is_active=False>' % pin
|
||||
with GPIODevice(2) as device:
|
||||
assert repr(device) == '<gpiozero.GPIODevice object on pin %s, is_active=False>' % device.pin
|
||||
|
||||
def test_device_repr_after_close():
|
||||
pin = MockPin(2)
|
||||
device = GPIODevice(pin)
|
||||
device = GPIODevice(2)
|
||||
device.close()
|
||||
assert repr(device) == '<gpiozero.GPIODevice object closed>'
|
||||
|
||||
def test_device_unknown_attr():
|
||||
pin = MockPin(2)
|
||||
with GPIODevice(pin) as device:
|
||||
with GPIODevice(2) as device:
|
||||
with pytest.raises(AttributeError):
|
||||
device.foo = 1
|
||||
|
||||
def test_device_context_manager():
|
||||
pin = MockPin(2)
|
||||
with GPIODevice(pin) as device:
|
||||
with GPIODevice(2) as device:
|
||||
assert not device.closed
|
||||
assert device.closed
|
||||
|
||||
def test_composite_device_sequence():
|
||||
with CompositeDevice(
|
||||
InputDevice(MockPin(2)),
|
||||
InputDevice(MockPin(3))
|
||||
InputDevice(2),
|
||||
InputDevice(3)
|
||||
) as device:
|
||||
assert len(device) == 2
|
||||
assert device[0].pin.number == 2
|
||||
@@ -94,8 +95,8 @@ def test_composite_device_sequence():
|
||||
|
||||
def test_composite_device_values():
|
||||
with CompositeDevice(
|
||||
InputDevice(MockPin(2)),
|
||||
InputDevice(MockPin(3))
|
||||
InputDevice(2),
|
||||
InputDevice(3)
|
||||
) as device:
|
||||
assert device.value == (0, 0)
|
||||
assert not device.is_active
|
||||
@@ -105,8 +106,8 @@ def test_composite_device_values():
|
||||
|
||||
def test_composite_device_named():
|
||||
with CompositeDevice(
|
||||
foo=InputDevice(MockPin(2)),
|
||||
bar=InputDevice(MockPin(3)),
|
||||
foo=InputDevice(2),
|
||||
bar=InputDevice(3),
|
||||
_order=('foo', 'bar')
|
||||
) as device:
|
||||
assert device.namedtuple._fields == ('foo', 'bar')
|
||||
@@ -121,13 +122,13 @@ def test_composite_device_bad_init():
|
||||
with pytest.raises(ValueError):
|
||||
CompositeDevice(2)
|
||||
with pytest.raises(ValueError):
|
||||
CompositeDevice(MockPin(2))
|
||||
CompositeDevice(Device._pin_factory.pin(2))
|
||||
|
||||
def test_composite_device_read_only():
|
||||
device = CompositeDevice(
|
||||
foo=InputDevice(MockPin(2)),
|
||||
bar=InputDevice(MockPin(3))
|
||||
)
|
||||
with pytest.raises(AttributeError):
|
||||
device.foo = 1
|
||||
with CompositeDevice(
|
||||
foo=InputDevice(2),
|
||||
bar=InputDevice(3)
|
||||
) as device:
|
||||
with pytest.raises(AttributeError):
|
||||
device.foo = 1
|
||||
|
||||
|
||||
@@ -12,57 +12,52 @@ import pytest
|
||||
from threading import Event
|
||||
from functools import partial
|
||||
|
||||
from gpiozero.pins.mock import (
|
||||
MockPin,
|
||||
MockPulledUpPin,
|
||||
MockChargingPin,
|
||||
MockTriggerPin,
|
||||
)
|
||||
from gpiozero.pins.mock import MockPulledUpPin, MockChargingPin, MockTriggerPin
|
||||
from gpiozero import *
|
||||
|
||||
|
||||
def teardown_function(function):
|
||||
MockPin.clear_pins()
|
||||
Device._pin_factory.reset()
|
||||
|
||||
|
||||
def test_input_initial_values():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with InputDevice(pin, pull_up=True) as device:
|
||||
assert pin.function == 'input'
|
||||
assert pin.pull == 'up'
|
||||
assert device.pull_up
|
||||
device.close()
|
||||
device = InputDevice(pin, pull_up=False)
|
||||
with InputDevice(pin, pull_up=False) as device:
|
||||
assert pin.pull == 'down'
|
||||
assert not device.pull_up
|
||||
|
||||
def test_input_is_active_low():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with InputDevice(pin, pull_up=True) as device:
|
||||
pin.drive_high()
|
||||
assert not device.is_active
|
||||
assert repr(device) == '<gpiozero.InputDevice object on pin MOCK2, pull_up=True, is_active=False>'
|
||||
assert repr(device) == '<gpiozero.InputDevice object on pin GPIO2, pull_up=True, is_active=False>'
|
||||
pin.drive_low()
|
||||
assert device.is_active
|
||||
assert repr(device) == '<gpiozero.InputDevice object on pin MOCK2, pull_up=True, is_active=True>'
|
||||
assert repr(device) == '<gpiozero.InputDevice object on pin GPIO2, pull_up=True, is_active=True>'
|
||||
|
||||
def test_input_is_active_high():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with InputDevice(pin, pull_up=False) as device:
|
||||
pin.drive_high()
|
||||
assert device.is_active
|
||||
assert repr(device) == '<gpiozero.InputDevice object on pin MOCK2, pull_up=False, is_active=True>'
|
||||
assert repr(device) == '<gpiozero.InputDevice object on pin GPIO2, pull_up=False, is_active=True>'
|
||||
pin.drive_low()
|
||||
assert not device.is_active
|
||||
assert repr(device) == '<gpiozero.InputDevice object on pin MOCK2, pull_up=False, is_active=False>'
|
||||
assert repr(device) == '<gpiozero.InputDevice object on pin GPIO2, pull_up=False, is_active=False>'
|
||||
|
||||
def test_input_pulled_up():
|
||||
pin = MockPulledUpPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPulledUpPin)
|
||||
with pytest.raises(PinFixedPull):
|
||||
InputDevice(pin, pull_up=False)
|
||||
|
||||
def test_input_event_activated():
|
||||
event = Event()
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with DigitalInputDevice(pin) as device:
|
||||
device.when_activated = lambda: event.set()
|
||||
assert not event.is_set()
|
||||
@@ -71,7 +66,7 @@ def test_input_event_activated():
|
||||
|
||||
def test_input_event_deactivated():
|
||||
event = Event()
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with DigitalInputDevice(pin) as device:
|
||||
device.when_deactivated = lambda: event.set()
|
||||
assert not event.is_set()
|
||||
@@ -82,7 +77,7 @@ def test_input_event_deactivated():
|
||||
|
||||
def test_input_partial_callback():
|
||||
event = Event()
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
def foo(a, b):
|
||||
event.set()
|
||||
return a + b
|
||||
@@ -95,22 +90,22 @@ def test_input_partial_callback():
|
||||
assert event.is_set()
|
||||
|
||||
def test_input_wait_active():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
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 = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with DigitalInputDevice(pin) as device:
|
||||
assert device.wait_for_inactive(1)
|
||||
assert not device.wait_for_active(0)
|
||||
|
||||
def test_input_smoothed_attrib():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with SmoothedInputDevice(pin, threshold=0.5, queue_len=5, partial=False) as device:
|
||||
assert repr(device) == '<gpiozero.SmoothedInputDevice object on pin=MOCK2, pull_up=False>'
|
||||
assert repr(device) == '<gpiozero.SmoothedInputDevice object on pin GPIO2, pull_up=False>'
|
||||
assert device.threshold == 0.5
|
||||
assert device.queue_len == 5
|
||||
assert not device.partial
|
||||
@@ -120,7 +115,7 @@ def test_input_smoothed_attrib():
|
||||
device.threshold = 1
|
||||
|
||||
def test_input_smoothed_values():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with SmoothedInputDevice(pin) as device:
|
||||
device._queue.start()
|
||||
assert not device.is_active
|
||||
@@ -130,7 +125,7 @@ def test_input_smoothed_values():
|
||||
assert device.wait_for_inactive(1)
|
||||
|
||||
def test_input_button():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with Button(pin) as button:
|
||||
assert pin.pull == 'up'
|
||||
assert not button.is_pressed
|
||||
@@ -142,7 +137,7 @@ def test_input_button():
|
||||
assert button.wait_for_release(1)
|
||||
|
||||
def test_input_line_sensor():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with LineSensor(pin) as sensor:
|
||||
pin.drive_low() # logic is inverted for line sensor
|
||||
assert sensor.wait_for_line(1)
|
||||
@@ -152,7 +147,7 @@ def test_input_line_sensor():
|
||||
assert not sensor.line_detected
|
||||
|
||||
def test_input_motion_sensor():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with MotionSensor(pin) as sensor:
|
||||
pin.drive_high()
|
||||
assert sensor.wait_for_motion(1)
|
||||
@@ -164,7 +159,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 = MockChargingPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockChargingPin)
|
||||
with LightSensor(pin) as sensor:
|
||||
pin.charge_time = 0.1
|
||||
assert sensor.wait_for_dark(1)
|
||||
@@ -174,8 +169,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 = MockPin(2)
|
||||
trig_pin = MockTriggerPin(3)
|
||||
echo_pin = Device._pin_factory.pin(2)
|
||||
trig_pin = Device._pin_factory.pin(3, pin_class=MockTriggerPin)
|
||||
trig_pin.echo_pin = echo_pin
|
||||
trig_pin.echo_time = 0.02
|
||||
with pytest.raises(ValueError):
|
||||
|
||||
@@ -11,25 +11,24 @@ from threading import Event
|
||||
|
||||
import pytest
|
||||
|
||||
from gpiozero.pins.mock import MockPin, MockPWMPin
|
||||
from gpiozero.pins.mock import MockPWMPin, MockPin
|
||||
from gpiozero import *
|
||||
|
||||
|
||||
def teardown_function(function):
|
||||
MockPin.clear_pins()
|
||||
Device._pin_factory.reset()
|
||||
|
||||
|
||||
# Some rough tests to make sure our MockPin is up to snuff. This is just
|
||||
# enough to get reasonable coverage but it's by no means comprehensive...
|
||||
|
||||
def test_mock_pin_init():
|
||||
with pytest.raises(TypeError):
|
||||
MockPin()
|
||||
with pytest.raises(ValueError):
|
||||
MockPin(60)
|
||||
assert MockPin(2).number == 2
|
||||
Device._pin_factory.pin(60)
|
||||
assert Device._pin_factory.pin(2).number == 2
|
||||
|
||||
def test_mock_pin_defaults():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
assert pin.bounce == None
|
||||
assert pin.edges == 'both'
|
||||
assert pin.frequency == None
|
||||
@@ -39,30 +38,23 @@ def test_mock_pin_defaults():
|
||||
assert pin.when_changed == None
|
||||
|
||||
def test_mock_pin_open_close():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
pin.close()
|
||||
|
||||
def test_mock_pin_init_twice_same_pin():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(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 = MockPin(2)
|
||||
pin2 = MockPin(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_init():
|
||||
with pytest.raises(TypeError):
|
||||
MockPWMPin()
|
||||
with pytest.raises(ValueError):
|
||||
MockPWMPin(60)
|
||||
assert MockPWMPin(2).number == 2
|
||||
|
||||
def test_mock_pwm_pin_defaults():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
assert pin.bounce == None
|
||||
assert pin.edges == 'both'
|
||||
assert pin.frequency == None
|
||||
@@ -72,38 +64,38 @@ def test_mock_pwm_pin_defaults():
|
||||
assert pin.when_changed == None
|
||||
|
||||
def test_mock_pwm_pin_open_close():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
pin.close()
|
||||
|
||||
def test_mock_pwm_pin_init_twice_same_pin():
|
||||
pin1 = MockPWMPin(2)
|
||||
pin2 = MockPWMPin(pin1.number)
|
||||
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 = MockPWMPin(2)
|
||||
pin2 = MockPWMPin(pin1.number+1)
|
||||
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 = MockPin(2)
|
||||
pin2 = MockPWMPin(pin1.number+1)
|
||||
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):
|
||||
pin3 = MockPWMPin(pin1.number)
|
||||
Device._pin_factory.pin(pin1.number, pin_class=MockPWMPin)
|
||||
with pytest.raises(ValueError):
|
||||
pin4 = MockPin(pin2.number)
|
||||
Device._pin_factory.pin(pin2.number, pin_class=MockPin)
|
||||
|
||||
def test_mock_pin_frequency_unsupported():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
pin.frequency = None
|
||||
with pytest.raises(PinPWMUnsupported):
|
||||
pin.frequency = 100
|
||||
|
||||
def test_mock_pin_frequency_supported():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
pin.function = 'output'
|
||||
assert pin.frequency is None
|
||||
pin.frequency = 100
|
||||
@@ -112,7 +104,7 @@ def test_mock_pin_frequency_supported():
|
||||
assert not pin.state
|
||||
|
||||
def test_mock_pin_pull():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
pin.function = 'input'
|
||||
assert pin.pull == 'floating'
|
||||
pin.pull = 'up'
|
||||
@@ -121,7 +113,7 @@ def test_mock_pin_pull():
|
||||
assert not pin.state
|
||||
|
||||
def test_mock_pin_state():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with pytest.raises(PinSetInput):
|
||||
pin.state = 1
|
||||
pin.function = 'output'
|
||||
@@ -134,7 +126,7 @@ def test_mock_pin_state():
|
||||
assert pin.state == 1
|
||||
|
||||
def test_mock_pwm_pin_state():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with pytest.raises(PinSetInput):
|
||||
pin.state = 1
|
||||
pin.function = 'output'
|
||||
@@ -147,7 +139,7 @@ def test_mock_pwm_pin_state():
|
||||
assert pin.state == 0.5
|
||||
|
||||
def test_mock_pin_edges():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
assert pin.when_changed is None
|
||||
fired = Event()
|
||||
pin.function = 'input'
|
||||
|
||||
@@ -16,15 +16,16 @@ except ImportError:
|
||||
|
||||
import pytest
|
||||
|
||||
from gpiozero.pins.mock import MockPin, MockPWMPin
|
||||
from gpiozero.pins.mock import MockPWMPin
|
||||
from gpiozero import *
|
||||
|
||||
|
||||
def teardown_function(function):
|
||||
MockPin.clear_pins()
|
||||
Device._pin_factory.reset()
|
||||
|
||||
|
||||
def test_output_initial_values():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with OutputDevice(pin, initial_value=False) as device:
|
||||
assert pin.function == 'output'
|
||||
assert not pin.state
|
||||
@@ -35,7 +36,7 @@ def test_output_initial_values():
|
||||
assert state == pin.state
|
||||
|
||||
def test_output_write_active_high():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with OutputDevice(pin) as device:
|
||||
device.on()
|
||||
assert pin.state
|
||||
@@ -43,7 +44,7 @@ def test_output_write_active_high():
|
||||
assert not pin.state
|
||||
|
||||
def test_output_write_active_low():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with OutputDevice(pin, active_high=False) as device:
|
||||
device.on()
|
||||
assert not pin.state
|
||||
@@ -51,7 +52,7 @@ def test_output_write_active_low():
|
||||
assert pin.state
|
||||
|
||||
def test_output_write_closed():
|
||||
with OutputDevice(MockPin(2)) as device:
|
||||
with OutputDevice(Device._pin_factory.pin(2)) as device:
|
||||
device.close()
|
||||
assert device.closed
|
||||
device.close()
|
||||
@@ -60,14 +61,14 @@ def test_output_write_closed():
|
||||
device.on()
|
||||
|
||||
def test_output_write_silly():
|
||||
pin = MockPin(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 = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with OutputDevice(pin) as device:
|
||||
assert not device.value
|
||||
assert not pin.state
|
||||
@@ -79,7 +80,7 @@ def test_output_value():
|
||||
assert not pin.state
|
||||
|
||||
def test_output_digital_toggle():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with DigitalOutputDevice(pin) as device:
|
||||
assert not device.value
|
||||
assert not pin.state
|
||||
@@ -93,7 +94,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 = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with DigitalOutputDevice(pin) as device:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2)
|
||||
@@ -111,7 +112,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 = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with DigitalOutputDevice(pin) as device:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2, background=False)
|
||||
@@ -125,7 +126,7 @@ def test_output_blink_foreground():
|
||||
])
|
||||
|
||||
def test_output_blink_interrupt_on():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with DigitalOutputDevice(pin) as device:
|
||||
device.blink(1, 0.1)
|
||||
sleep(0.2)
|
||||
@@ -133,7 +134,7 @@ def test_output_blink_interrupt_on():
|
||||
pin.assert_states([False, True, False])
|
||||
|
||||
def test_output_blink_interrupt_off():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with DigitalOutputDevice(pin) as device:
|
||||
device.blink(0.1, 1)
|
||||
sleep(0.2)
|
||||
@@ -142,14 +143,14 @@ def test_output_blink_interrupt_off():
|
||||
|
||||
def test_output_pwm_bad_initial_value():
|
||||
with pytest.raises(ValueError):
|
||||
PWMOutputDevice(MockPin(2), initial_value=2)
|
||||
PWMOutputDevice(Device._pin_factory.pin(2), initial_value=2)
|
||||
|
||||
def test_output_pwm_not_supported():
|
||||
with pytest.raises(AttributeError):
|
||||
PWMOutputDevice(MockPin(2))
|
||||
PWMOutputDevice(Device._pin_factory.pin(2))
|
||||
|
||||
def test_output_pwm_states():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
device.value = 0.1
|
||||
device.value = 0.2
|
||||
@@ -157,7 +158,7 @@ def test_output_pwm_states():
|
||||
pin.assert_states([0.0, 0.1, 0.2, 0.0])
|
||||
|
||||
def test_output_pwm_read():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin, frequency=100) as device:
|
||||
assert device.frequency == 100
|
||||
device.value = 0.1
|
||||
@@ -170,14 +171,14 @@ def test_output_pwm_read():
|
||||
assert device.frequency is None
|
||||
|
||||
def test_output_pwm_write():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
device.on()
|
||||
device.off()
|
||||
pin.assert_states([False, True, False])
|
||||
|
||||
def test_output_pwm_toggle():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
device.toggle()
|
||||
device.value = 0.5
|
||||
@@ -187,7 +188,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 = MockPWMPin(2)
|
||||
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)
|
||||
@@ -196,17 +197,18 @@ def test_output_pwm_active_high_read():
|
||||
assert device.value
|
||||
|
||||
def test_output_pwm_bad_value():
|
||||
with pytest.raises(ValueError):
|
||||
PWMOutputDevice(MockPWMPin(2)).value = 2
|
||||
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():
|
||||
device = PWMOutputDevice(MockPWMPin(2))
|
||||
device.close()
|
||||
with pytest.raises(GPIODeviceClosed):
|
||||
device.on()
|
||||
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 = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
pin.function = 'input'
|
||||
with pytest.raises(AttributeError):
|
||||
@@ -215,7 +217,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 = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2)
|
||||
@@ -233,7 +235,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 = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2, background=False)
|
||||
@@ -249,7 +251,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 = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
start = time()
|
||||
device.blink(0, 0, 0.2, 0.2, n=2)
|
||||
@@ -283,7 +285,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 = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
start = time()
|
||||
device.blink(0, 0, 0.2, 0.2, n=2, background=False)
|
||||
@@ -315,7 +317,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 = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
start = time()
|
||||
device.pulse(0.2, 0.2, n=2)
|
||||
@@ -349,7 +351,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 = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
start = time()
|
||||
device.pulse(0.2, 0.2, n=2, background=False)
|
||||
@@ -379,7 +381,7 @@ def test_output_pwm_pulse_foreground():
|
||||
])
|
||||
|
||||
def test_output_pwm_blink_interrupt():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
device.blink(1, 0.1)
|
||||
sleep(0.2)
|
||||
@@ -391,7 +393,7 @@ def test_rgbled_missing_pins():
|
||||
RGBLED()
|
||||
|
||||
def test_rgbled_initial_value():
|
||||
r, g, b = (MockPWMPin(i) 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
|
||||
@@ -401,24 +403,24 @@ def test_rgbled_initial_value():
|
||||
assert isclose(b.state, 0.0)
|
||||
|
||||
def test_rgbled_initial_value_nonpwm():
|
||||
r, g, b = (MockPin(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 = (MockPWMPin(i) 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 = (MockPin(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 = (MockPWMPin(i) 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)
|
||||
@@ -436,7 +438,7 @@ def test_rgbled_value():
|
||||
assert device.value == (0.5, 0.5, 0.5)
|
||||
|
||||
def test_rgbled_value_nonpwm():
|
||||
r, g, b = (MockPin(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)
|
||||
@@ -451,7 +453,7 @@ def test_rgbled_value_nonpwm():
|
||||
assert device.value == (0, 0, 0)
|
||||
|
||||
def test_rgbled_bad_value():
|
||||
r, g, b = (MockPWMPin(i) 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)
|
||||
@@ -460,7 +462,7 @@ def test_rgbled_bad_value():
|
||||
device.value = (0, -1, 0)
|
||||
|
||||
def test_rgbled_bad_value_nonpwm():
|
||||
r, g, b = (MockPin(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)
|
||||
@@ -478,7 +480,7 @@ def test_rgbled_bad_value_nonpwm():
|
||||
device.value = (0, 0, 0.5)
|
||||
|
||||
def test_rgbled_toggle():
|
||||
r, g, b = (MockPWMPin(i) 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)
|
||||
@@ -490,7 +492,7 @@ def test_rgbled_toggle():
|
||||
assert device.value == (0, 0, 0)
|
||||
|
||||
def test_rgbled_toggle_nonpwm():
|
||||
r, g, b = (MockPin(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)
|
||||
@@ -501,10 +503,18 @@ def test_rgbled_toggle_nonpwm():
|
||||
assert not device.is_active
|
||||
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))
|
||||
with RGBLED(r, g, b, pwm=False) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.blink(fade_in_time=1)
|
||||
with pytest.raises(ValueError):
|
||||
device.blink(fade_out_time=1)
|
||||
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_rgbled_blink_background():
|
||||
r, g, b = (MockPWMPin(i) 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:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2)
|
||||
@@ -525,7 +535,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 = (MockPin(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:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2)
|
||||
@@ -546,7 +556,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 = (MockPWMPin(i) 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:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2, background=False)
|
||||
@@ -565,7 +575,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 = (MockPin(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:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2, background=False)
|
||||
@@ -584,7 +594,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 = (MockPWMPin(i) 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:
|
||||
start = time()
|
||||
device.blink(0, 0, 0.2, 0.2, n=2)
|
||||
@@ -619,7 +629,7 @@ def test_rgbled_fade_background():
|
||||
b.assert_states_and_times(expected)
|
||||
|
||||
def test_rgbled_fade_background_nonpwm():
|
||||
r, g, b = (MockPin(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)
|
||||
@@ -627,7 +637,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 = (MockPWMPin(i) 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:
|
||||
start = time()
|
||||
device.blink(0, 0, 0.2, 0.2, n=2, background=False)
|
||||
@@ -660,7 +670,7 @@ def test_rgbled_fade_foreground():
|
||||
b.assert_states_and_times(expected)
|
||||
|
||||
def test_rgbled_fade_foreground_nonpwm():
|
||||
r, g, b = (MockPin(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)
|
||||
@@ -668,7 +678,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 = (MockPWMPin(i) 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:
|
||||
start = time()
|
||||
device.pulse(0.2, 0.2, n=2)
|
||||
@@ -703,7 +713,7 @@ def test_rgbled_pulse_background():
|
||||
b.assert_states_and_times(expected)
|
||||
|
||||
def test_rgbled_pulse_background_nonpwm():
|
||||
r, g, b = (MockPin(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)
|
||||
@@ -711,7 +721,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 = (MockPWMPin(i) 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:
|
||||
start = time()
|
||||
device.pulse(0.2, 0.2, n=2, background=False)
|
||||
@@ -744,13 +754,13 @@ def test_rgbled_pulse_foreground():
|
||||
b.assert_states_and_times(expected)
|
||||
|
||||
def test_rgbled_pulse_foreground_nonpwm():
|
||||
r, g, b = (MockPin(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 = (MockPWMPin(i) 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:
|
||||
device.blink(1, 0.1)
|
||||
sleep(0.2)
|
||||
@@ -760,7 +770,7 @@ def test_rgbled_blink_interrupt():
|
||||
b.assert_states([0, 1, 0])
|
||||
|
||||
def test_rgbled_blink_interrupt_nonpwm():
|
||||
r, g, b = (MockPin(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:
|
||||
device.blink(1, 0.1)
|
||||
sleep(0.2)
|
||||
@@ -770,7 +780,7 @@ def test_rgbled_blink_interrupt_nonpwm():
|
||||
b.assert_states([0, 1, 0])
|
||||
|
||||
def test_rgbled_close():
|
||||
r, g, b = (MockPWMPin(i) 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()
|
||||
@@ -779,7 +789,7 @@ def test_rgbled_close():
|
||||
assert device.closed
|
||||
|
||||
def test_rgbled_close_nonpwm():
|
||||
r, g, b = (MockPin(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()
|
||||
@@ -792,8 +802,8 @@ def test_motor_missing_pins():
|
||||
Motor()
|
||||
|
||||
def test_motor_pins():
|
||||
f = MockPWMPin(1)
|
||||
b = MockPWMPin(2)
|
||||
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)
|
||||
@@ -801,8 +811,8 @@ def test_motor_pins():
|
||||
assert isinstance(device.backward_device, PWMOutputDevice)
|
||||
|
||||
def test_motor_pins_nonpwm():
|
||||
f = MockPin(1)
|
||||
b = MockPin(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)
|
||||
@@ -810,8 +820,8 @@ def test_motor_pins_nonpwm():
|
||||
assert isinstance(device.backward_device, DigitalOutputDevice)
|
||||
|
||||
def test_motor_close():
|
||||
f = MockPWMPin(1)
|
||||
b = MockPWMPin(2)
|
||||
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
|
||||
@@ -821,8 +831,8 @@ def test_motor_close():
|
||||
assert device.closed
|
||||
|
||||
def test_motor_close_nonpwm():
|
||||
f = MockPin(1)
|
||||
b = MockPin(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
|
||||
@@ -830,8 +840,8 @@ def test_motor_close_nonpwm():
|
||||
assert device.backward_device.pin is None
|
||||
|
||||
def test_motor_value():
|
||||
f = MockPWMPin(1)
|
||||
b = MockPWMPin(2)
|
||||
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
|
||||
@@ -855,8 +865,8 @@ def test_motor_value():
|
||||
assert b.state == 0 and f.state == 0
|
||||
|
||||
def test_motor_value_nonpwm():
|
||||
f = MockPin(1)
|
||||
b = MockPin(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
|
||||
@@ -872,17 +882,21 @@ def test_motor_value_nonpwm():
|
||||
assert b.state == 0 and f.state == 0
|
||||
|
||||
def test_motor_bad_value():
|
||||
f = MockPWMPin(1)
|
||||
b = MockPWMPin(2)
|
||||
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
|
||||
with pytest.raises(ValueError):
|
||||
device.value = 2
|
||||
with pytest.raises(ValueError):
|
||||
device.forward(2)
|
||||
with pytest.raises(ValueError):
|
||||
device.backward(2)
|
||||
|
||||
def test_motor_bad_value_nonpwm():
|
||||
f = MockPin(1)
|
||||
b = MockPin(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
|
||||
@@ -894,8 +908,8 @@ def test_motor_bad_value_nonpwm():
|
||||
device.value = -0.5
|
||||
|
||||
def test_motor_reverse():
|
||||
f = MockPWMPin(1)
|
||||
b = MockPWMPin(2)
|
||||
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
|
||||
@@ -911,8 +925,8 @@ def test_motor_reverse():
|
||||
assert b.state == 0 and f.state == 0.5
|
||||
|
||||
def test_motor_reverse_nonpwm():
|
||||
f = MockPin(1)
|
||||
b = MockPin(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
|
||||
@@ -922,13 +936,13 @@ def test_motor_reverse_nonpwm():
|
||||
assert b.state == 1 and f.state == 0
|
||||
|
||||
def test_servo_pins():
|
||||
p = MockPWMPin(1)
|
||||
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 = MockPWMPin(1)
|
||||
p = Device._pin_factory.pin(1, pin_class=MockPWMPin)
|
||||
with pytest.raises(ValueError):
|
||||
Servo(p, initial_value=2)
|
||||
with pytest.raises(ValueError):
|
||||
@@ -937,12 +951,12 @@ def test_servo_bad_value():
|
||||
Servo(p, max_pulse_width=30/1000)
|
||||
|
||||
def test_servo_pins_nonpwm():
|
||||
p = MockPin(2)
|
||||
p = Device._pin_factory.pin(2)
|
||||
with pytest.raises(PinPWMUnsupported):
|
||||
Servo(p)
|
||||
|
||||
def test_servo_close():
|
||||
p = MockPWMPin(2)
|
||||
p = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with Servo(p) as device:
|
||||
device.close()
|
||||
assert device.closed
|
||||
@@ -951,7 +965,7 @@ def test_servo_close():
|
||||
assert device.closed
|
||||
|
||||
def test_servo_pulse_width():
|
||||
p = MockPWMPin(2)
|
||||
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)
|
||||
@@ -965,7 +979,7 @@ def test_servo_pulse_width():
|
||||
assert device.pulse_width is None
|
||||
|
||||
def test_servo_values():
|
||||
p = MockPWMPin(1)
|
||||
p = Device._pin_factory.pin(1, pin_class=MockPWMPin)
|
||||
with Servo(p) as device:
|
||||
device.min()
|
||||
assert device.is_active
|
||||
@@ -992,13 +1006,13 @@ def test_servo_values():
|
||||
assert device.value is None
|
||||
|
||||
def test_angular_servo_range():
|
||||
p = MockPWMPin(1)
|
||||
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 = MockPWMPin(1)
|
||||
p = Device._pin_factory.pin(1, pin_class=MockPWMPin)
|
||||
with AngularServo(p) as device:
|
||||
device.angle = 0
|
||||
assert device.angle == 0
|
||||
|
||||
@@ -11,44 +11,44 @@ import re
|
||||
import pytest
|
||||
from mock import patch, MagicMock
|
||||
|
||||
import gpiozero.devices
|
||||
import gpiozero.pins.data
|
||||
import gpiozero.pins.native
|
||||
from gpiozero.pins.data import pi_info, Style, HeaderInfo, PinInfo
|
||||
from gpiozero import PinMultiplePins, PinNoPins, PinUnknownPi
|
||||
import gpiozero.pins.local
|
||||
from gpiozero.pins.local import LocalPiFactory
|
||||
from gpiozero.pins.data import Style, HeaderInfo, PinInfo
|
||||
from gpiozero import *
|
||||
|
||||
|
||||
def test_pi_revision():
|
||||
save_factory = gpiozero.devices.pin_factory
|
||||
try:
|
||||
# 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()):
|
||||
# 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);
|
||||
# NativePin is used as we're guaranteed to be able to import it
|
||||
gpiozero.devices.pin_factory = gpiozero.pins.native.NativePin
|
||||
# LocalPiFactory is used as we can definitely instantiate it (strictly
|
||||
# speaking it's abstract but we're only interested in the pi_info
|
||||
# stuff)
|
||||
with patch('io.open') as m:
|
||||
m.return_value.__enter__.return_value = ['lots of irrelevant', 'lines', 'followed by', 'Revision: 0002', 'Serial: xxxxxxxxxxx']
|
||||
assert pi_info().revision == '0002'
|
||||
# LocalPin caches the revision (because realistically it isn't going to
|
||||
# change at runtime); we need to wipe it here though
|
||||
gpiozero.pins.native.NativePin._PI_REVISION = None
|
||||
# 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
|
||||
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)
|
||||
gpiozero.pins.native.NativePin._PI_REVISION = None
|
||||
Device._pin_factory._info = None
|
||||
m.return_value.__enter__.return_value = ['Revision: 1000003']
|
||||
assert pi_info().revision == '0003'
|
||||
gpiozero.pins.native.NativePin._PI_REVISION = 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']
|
||||
gpiozero.pins.native.NativePin._PI_REVISION = None
|
||||
Device._pin_factory._info = None
|
||||
pi_info()
|
||||
with pytest.raises(PinUnknownPi):
|
||||
pi_info('0fff')
|
||||
finally:
|
||||
gpiozero.devices.pin_factory = save_factory
|
||||
|
||||
def test_pi_info():
|
||||
r = pi_info('900011')
|
||||
@@ -73,14 +73,14 @@ def test_pi_info_other_types():
|
||||
|
||||
def test_physical_pins():
|
||||
# Assert physical pins for some well-known Pi's; a21041 is a Pi2B
|
||||
assert pi_info('a21041').physical_pins('3V3') == {('P1', 1), ('P1', 17)}
|
||||
assert pi_info('a21041').physical_pins('GPIO2') == {('P1', 3)}
|
||||
assert pi_info('a21041').physical_pins('3V3') == {('J8', 1), ('J8', 17)}
|
||||
assert pi_info('a21041').physical_pins('GPIO2') == {('J8', 3)}
|
||||
assert pi_info('a21041').physical_pins('GPIO47') == set()
|
||||
|
||||
def test_physical_pin():
|
||||
with pytest.raises(PinMultiplePins):
|
||||
assert pi_info('a21041').physical_pin('GND')
|
||||
assert pi_info('a21041').physical_pin('GPIO3') == ('P1', 5)
|
||||
assert pi_info('a21041').physical_pin('GPIO3') == ('J8', 5)
|
||||
with pytest.raises(PinNoPins):
|
||||
assert pi_info('a21041').physical_pin('GPIO47')
|
||||
|
||||
@@ -114,6 +114,18 @@ def test_pprint_content():
|
||||
pi_info('0014').headers['SODIMM'].pprint(color=False)
|
||||
assert len(''.join(stdout.output).splitlines()) == 100
|
||||
|
||||
def test_format_content():
|
||||
with patch('sys.stdout') as stdout:
|
||||
stdout.output = []
|
||||
stdout.write = lambda buf: stdout.output.append(buf)
|
||||
pi_info('900092').pprint(color=False)
|
||||
s = ''.join(stdout.output)
|
||||
assert '{0:mono}\n'.format(pi_info('900092')) == s
|
||||
stdout.output = []
|
||||
pi_info('900092').pprint(color=True)
|
||||
s = ''.join(stdout.output)
|
||||
assert '{0:color full}\n'.format(pi_info('900092')) == s
|
||||
|
||||
def test_pprint_headers():
|
||||
assert len(pi_info('0002').headers) == 1
|
||||
assert len(pi_info('000e').headers) == 2
|
||||
@@ -133,7 +145,8 @@ def test_pprint_headers():
|
||||
stdout.output = []
|
||||
pi_info('900092').pprint()
|
||||
s = ''.join(stdout.output)
|
||||
assert 'P1:\n' in s
|
||||
assert 'J8:\n' in s
|
||||
assert 'P1:\n' not in s
|
||||
assert 'P5:\n' not in s
|
||||
|
||||
def test_pprint_color():
|
||||
@@ -194,11 +207,12 @@ def test_pprint_missing_pin():
|
||||
assert ('(%d)' % i)
|
||||
|
||||
def test_pprint_rows_cols():
|
||||
assert '{0:row1}'.format(pi_info('900092').headers['P1']) == '1o'
|
||||
assert '{0:row2}'.format(pi_info('900092').headers['P1']) == 'oo'
|
||||
assert '{0:row1}'.format(pi_info('900092').headers['J8']) == '1o'
|
||||
assert '{0:row2}'.format(pi_info('900092').headers['J8']) == 'oo'
|
||||
assert '{0:col1}'.format(pi_info('0002').headers['P1']) == '1oooooooooooo'
|
||||
assert '{0:col2}'.format(pi_info('0002').headers['P1']) == 'ooooooooooooo'
|
||||
with pytest.raises(ValueError):
|
||||
'{0:row16}'.format(pi_info('0002').headers['P1'])
|
||||
with pytest.raises(ValueError):
|
||||
'{0:col3}'.format(pi_info('0002').headers['P1'])
|
||||
|
||||
|
||||
@@ -4,93 +4,113 @@ from __future__ import (
|
||||
print_function,
|
||||
division,
|
||||
)
|
||||
nstr = str
|
||||
str = type('')
|
||||
|
||||
|
||||
import sys
|
||||
import mock
|
||||
import pytest
|
||||
from array import array
|
||||
from mock import patch
|
||||
from collections import namedtuple
|
||||
|
||||
from gpiozero.pins.native import NativeFactory
|
||||
from gpiozero.pins.local import (
|
||||
LocalPiHardwareSPI,
|
||||
LocalPiSoftwareSPI,
|
||||
LocalPiHardwareSPIShared,
|
||||
LocalPiSoftwareSPIShared,
|
||||
)
|
||||
from gpiozero.pins.mock import MockSPIDevice
|
||||
from gpiozero import *
|
||||
from gpiozero.pins.mock import MockPin, MockSPIDevice
|
||||
from gpiozero.spi import *
|
||||
|
||||
|
||||
def setup_function(function):
|
||||
import gpiozero.devices
|
||||
gpiozero.devices.pin_factory = MockPin
|
||||
|
||||
def teardown_function(function):
|
||||
MockPin.clear_pins()
|
||||
Device._pin_factory.reset()
|
||||
|
||||
|
||||
def test_spi_hardware_params():
|
||||
with mock.patch('gpiozero.spi.SpiDev') as spidev:
|
||||
with SPI() as device:
|
||||
assert isinstance(device, SPIHardwareInterface)
|
||||
with SPI(port=0, device=0) as device:
|
||||
assert isinstance(device, SPIHardwareInterface)
|
||||
with SPI(port=0, device=1) as device:
|
||||
assert isinstance(device, SPIHardwareInterface)
|
||||
with SPI(clock_pin=11) as device:
|
||||
assert isinstance(device, SPIHardwareInterface)
|
||||
with SPI(clock_pin=11, mosi_pin=10, select_pin=8) as device:
|
||||
assert isinstance(device, SPIHardwareInterface)
|
||||
with SPI(clock_pin=11, mosi_pin=10, select_pin=7) as device:
|
||||
assert isinstance(device, SPIHardwareInterface)
|
||||
with SPI(shared=True) as device:
|
||||
assert isinstance(device, SharedSPIHardwareInterface)
|
||||
with pytest.raises(ValueError):
|
||||
SPI(port=1)
|
||||
with pytest.raises(ValueError):
|
||||
SPI(device=2)
|
||||
with pytest.raises(ValueError):
|
||||
SPI(port=0, clock_pin=12)
|
||||
with pytest.raises(ValueError):
|
||||
SPI(foo='bar')
|
||||
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()), \
|
||||
patch('gpiozero.pins.local.SpiDev'):
|
||||
with Device._pin_factory.spi() as device:
|
||||
assert isinstance(device, LocalPiHardwareSPI)
|
||||
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:
|
||||
assert isinstance(device, LocalPiHardwareSPI)
|
||||
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:
|
||||
assert isinstance(device, LocalPiHardwareSPI)
|
||||
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:
|
||||
assert isinstance(device, LocalPiHardwareSPIShared)
|
||||
with pytest.raises(ValueError):
|
||||
Device._pin_factory.spi(port=1)
|
||||
with pytest.raises(ValueError):
|
||||
Device._pin_factory.spi(device=2)
|
||||
with pytest.raises(ValueError):
|
||||
Device._pin_factory.spi(port=0, clock_pin=12)
|
||||
with pytest.raises(ValueError):
|
||||
Device._pin_factory.spi(foo='bar')
|
||||
|
||||
def test_spi_software_params():
|
||||
with mock.patch('gpiozero.spi.SpiDev') as spidev:
|
||||
with SPI(select_pin=6) as device:
|
||||
assert isinstance(device, SPISoftwareInterface)
|
||||
with SPI(clock_pin=11, mosi_pin=9, miso_pin=10) as device:
|
||||
assert isinstance(device, SPISoftwareInterface)
|
||||
with SPI(select_pin=6, shared=True) as device:
|
||||
assert isinstance(device, SharedSPISoftwareInterface)
|
||||
# Ensure software fallback works when SpiDev isn't present
|
||||
with SPI() as device:
|
||||
assert isinstance(device, SPISoftwareInterface)
|
||||
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()), \
|
||||
patch('gpiozero.pins.local.SpiDev'):
|
||||
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:
|
||||
assert isinstance(device, LocalPiSoftwareSPI)
|
||||
with Device._pin_factory.spi(select_pin=6, shared=True) as device:
|
||||
assert isinstance(device, LocalPiSoftwareSPIShared)
|
||||
with patch('gpiozero.devices.Device._pin_factory', NativeFactory()):
|
||||
# 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()
|
||||
# Ensure software fallback works when SpiDev isn't present
|
||||
with Device._pin_factory.spi() as device:
|
||||
assert isinstance(device, LocalPiSoftwareSPI)
|
||||
|
||||
def test_spi_hardware_conflict():
|
||||
with mock.patch('gpiozero.spi.SpiDev') as spidev:
|
||||
with patch('gpiozero.pins.local.SpiDev') as spidev:
|
||||
with LED(11) as led:
|
||||
with pytest.raises(GPIOPinInUse):
|
||||
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 pytest.raises(GPIOPinInUse):
|
||||
LED(11)
|
||||
|
||||
def test_spi_hardware_read():
|
||||
with mock.patch('gpiozero.spi.SpiDev') as spidev:
|
||||
with patch('gpiozero.pins.local.SpiDev') as spidev:
|
||||
spidev.return_value.xfer2.side_effect = lambda data: list(range(10))[:len(data)]
|
||||
with 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 mock.patch('gpiozero.spi.SpiDev') as spidev:
|
||||
with patch('gpiozero.pins.local.SpiDev') as spidev:
|
||||
spidev.return_value.xfer2.side_effect = lambda data: list(range(10))[:len(data)]
|
||||
with 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
|
||||
assert spidev.return_value.xfer2.called_with(list(range(6)))
|
||||
|
||||
def test_spi_hardware_modes():
|
||||
with mock.patch('gpiozero.spi.SpiDev') as spidev:
|
||||
with patch('gpiozero.pins.local.SpiDev') as spidev:
|
||||
spidev.return_value.mode = 0
|
||||
spidev.return_value.lsbfirst = False
|
||||
spidev.return_value.cshigh = True
|
||||
spidev.return_value.bits_per_word = 8
|
||||
with 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
|
||||
@@ -116,7 +136,7 @@ def test_spi_software_read():
|
||||
super(SPISlave, self).on_start()
|
||||
for i in range(10):
|
||||
self.tx_word(i)
|
||||
with SPISlave(11, 10, 9, 8) as slave, SPI() as master:
|
||||
with SPISlave(11, 10, 9, 8) as slave, 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
|
||||
@@ -125,7 +145,7 @@ def test_spi_software_read():
|
||||
assert master.read(6) == [0, 1, 2, 3, 4, 5]
|
||||
|
||||
def test_spi_software_write():
|
||||
with MockSPIDevice(11, 10, 9, 8) as test_device, SPI() as master:
|
||||
with MockSPIDevice(11, 10, 9, 8) as test_device, Device._pin_factory.spi() as master:
|
||||
master.write([0])
|
||||
assert test_device.rx_word() == 0
|
||||
master.write([2, 0])
|
||||
@@ -134,7 +154,7 @@ def test_spi_software_write():
|
||||
assert test_device.rx_word() == 257
|
||||
|
||||
def test_spi_software_clock_mode():
|
||||
with SPI() as master:
|
||||
with Device._pin_factory.spi() as master:
|
||||
assert master.clock_mode == 0
|
||||
assert not master.clock_polarity
|
||||
assert not master.clock_phase
|
||||
@@ -151,7 +171,7 @@ def test_spi_software_clock_mode():
|
||||
master.clock_mode = 5
|
||||
|
||||
def test_spi_software_attr():
|
||||
with SPI() as master:
|
||||
with Device._pin_factory.spi() as master:
|
||||
assert not master.lsb_first
|
||||
assert not master.select_high
|
||||
assert master.bits_per_word == 8
|
||||
|
||||
@@ -15,16 +15,12 @@ try:
|
||||
except ImportError:
|
||||
from gpiozero.compat import isclose
|
||||
|
||||
from gpiozero import *
|
||||
from gpiozero.pins.mock import MockSPIDevice, MockPin
|
||||
from gpiozero import *
|
||||
|
||||
|
||||
def setup_function(function):
|
||||
import gpiozero.devices
|
||||
gpiozero.devices.pin_factory = MockPin
|
||||
|
||||
def teardown_function(function):
|
||||
MockPin.clear_pins()
|
||||
Device._pin_factory.reset()
|
||||
|
||||
def clamp(v, min_value, max_value):
|
||||
return min(max_value, max(min_value, v))
|
||||
|
||||
Reference in New Issue
Block a user