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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user