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'] factory = NativeFactory() with patch('gpiozero.pins.local.SpiDev'): with factory.spi(select_pin=6) as device: assert isinstance(device, LocalPiSoftwareSPI) device.close() assert device.closed with factory.spi(clock_pin=11, mosi_pin=9, miso_pin=10) as device: assert isinstance(device, LocalPiSoftwareSPI) device._bus.close() assert device._bus.closed device.close() assert device.closed with factory.spi(select_pin=6, shared=True) as device: assert isinstance(device, LocalPiSoftwareSPIShared) with patch('gpiozero.pins.local.SpiDev', None): # Clear out the old factory's caches (this is only necessary because # we're being naughty switching out patches) factory.pins.clear() factory._reservations.clear() # Ensure software fallback works when SpiDev isn't present with factory.spi() as device: assert isinstance(device, LocalPiSoftwareSPI)
def register(self): try: self.__logger.debug("Regestriere Raspberry GPIO...") from gpiozero.pins.native import NativeFactory from gpiozero import Device Device.pin_factory = NativeFactory() self.__logger.debug("InputPin = %s", self._config["rpiDoor/openedPin"]) self.input = gpiozero.Button(pin=self._config["rpiDoor/openedPin"]) self.__logger.debug("OuputPin = %s", self._config["rpiDoor/unlockPin"]) self.out = gpiozero.LED( pin=self._config["rpiDoor/unlockPin"]) self.input.when_activated = lambda: self.InputHandler(True ) self.input.when_deactivated = lambda: self.InputHandler(False) schedule.every(15).minutes.do(self.sendUpdate) self.__logger.debug("Regestiere MQTT Topics") unique_id = "sensor.doorOpener-{}.{}".format(self._device_id, self._config["rpiDoor/name"].replace(" ", "_")) self.topic = self._config.get_autodiscovery_topic(conf.autodisc.Component.SWITCH, self._config["rpiDoor/name"], None) payload = self.topic.get_config_payload(self._config["rpiDoor/name"], None, unique_id=unique_id, json_attributes=True, value_template="{{ value_json.sw }}") self.__client.publish(self.topic.config, payload=payload, qos=0, retain=True) self.__client.message_callback_add(self.topic.command, self.on_message) self._registered_callback_topics.append(self.topic.command) self.__client.subscribe(self.topic.command) self.sendUpdate() except Exception as e: self.__logger.exception("Register hat fehler verursacht!") raise e
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'] factory = NativeFactory() with patch('gpiozero.pins.local.SpiDev'): with factory.spi(select_pin=6) as device: assert isinstance(device, LocalPiSoftwareSPI) device.close() assert device.closed with factory.spi(clock_pin=11, mosi_pin=9, miso_pin=10) as device: assert isinstance(device, LocalPiSoftwareSPI) device._bus.close() assert device._bus.closed device.close() assert device.closed with factory.spi(select_pin=6, shared=True) as device: assert isinstance(device, LocalPiSoftwareSPIShared) with patch('gpiozero.pins.local.SpiDev', None): # Clear out the old factory's caches (this is only necessary because # we're being naughty switching out patches) factory.pins.clear() factory._reservations.clear() # Ensure software fallback works when SpiDev isn't present with factory.spi() as device: assert isinstance(device, LocalPiSoftwareSPI)
def setup_devices(): global btn, distance_sensor btn = DigitalInputDevice(FLOW_PIN) btn.when_activated = count_paddle if USE_PIGPIOD: factory = PiGPIOFactory() else: factory = NativeFactory() distance_sensor = DistanceSensor( trigger=TRIGGER_PIN, echo=ECHO_PIN, pin_factory=factory, queue_len=20, partial=True, )
def register(self): self.__logger.debug("Regestriere Raspberry GPIO...") from gpiozero.pins.native import NativeFactory from gpiozero import Device Device.pin_factory = NativeFactory() for p in self._config.get("RaspberryPiGPIO", []): pin = Pin.Pin(p["Pin"], Pin.PinDirection(p["direction"])) if p.get("pulse_width", None) is not None: pin.set_pulse_width(p["pulse_width"]) d = {"n": p["name"], "p": pin} if pin.get_direction() == Pin.PinDirection.OUT: t = self._config.get_autodiscovery_topic( Autodiscovery.Component.SWITCH, p["name"], Autodiscovery.SensorDeviceClasses.GENERIC_SENSOR) else: t = self._config.get_autodiscovery_topic( Autodiscovery.Component.BINARY_SENROR, p["name"], Autodiscovery.BinarySensorDeviceClasses.GENERIC_SENSOR) d["t"] = t if p.get("meassurement_value", None) is not None: d["mv"] = p["meassurement_value"] else: d["mv"] = "" if p.get("isPulse", None) is not None: d["isPulse"] = p["isPulse"] else: d["isPulse"] = False if d.get("init", None) is not None: pin.output(d["init"]) self._pins.append(d) self.__logger.debug( "Pin config gebaut. N: {}, P: {}, D: {}, isPulse: {}".format( d["n"], p["Pin"], pin.get_direction(), d["isPulse"])) for d in self._pins: self.register_pin(d) self.__logger.debug( "Regestriere Schedule Jobs für ¼ Stündliche resend Aufgaben...") schedule.every(15).minutes.do(RaspberryPiGpio.send_updates, self)
def __init__(self, client: mclient.Client, opts: conf.BasicConfig, logger: logging.Logger, device_id: str): self.current_job = rtimer.ResettableTimer(30, self.do_open, userval=None, autorun=False) from gpiozero.pins.native import NativeFactory from gpiozero import Device Device.pin_factory = NativeFactory() self.__client = client self.topic = None self.__logger = logger.getChild("PortaMatic") self._config = opts self._pins = { "isClosed": Pin.Pin(opts["PortaMatic/pins/isClosed"], Pin.PinDirection.IN), "inside": Pin.Pin(opts["PortaMatic/pins/inside"], Pin.PinDirection.IN), "outside": Pin.Pin(opts["PortaMatic/pins/outside"], Pin.PinDirection.IN), "tester": Pin.Pin(opts["PortaMatic/pins/tester"], Pin.PinDirection.IN), "pulseOut": Pin.Pin(opts["PortaMatic/pins/pulseOut"], Pin.PinDirection.OUT) } self._device_id = device_id self._pins["isClosed"].set_detect(self.zu_handler, Pin.PinEventEdge.BOTH) self._pins["inside"].set_detect(self.inside_handler, Pin.PinEventEdge.BOTH) self._pins["outside"].set_detect(self.outside_handler, Pin.PinEventEdge.BOTH) self._pins["taster"].set_detect(self.taster_handler, Pin.PinEventEdge.BOTH) self._schedJob = schedule.every().minute self._schedJob.do(self.check_inputs)
def __init__(self, client: mclient.Client, opts: conf.BasicConfig, logger: logging.Logger, device_id: str): from gpiozero.pins.native import NativeFactory from gpiozero import Device Device.pin_factory = NativeFactory() self._config = conf.PluginConfig(opts, PluginLoader.getConfigKey()) self.__client = client self.__logger = logger.getChild("Fingerprint") self._finger = PyFingerprint( self._config["serial"], 57600, 0xFFFFFFFF, self._config.get("password", 0x00000000) ) if not self._finger.verifyPassword(): self.err_str = "PASSWD" if self._config.get("WAKEUP", 0) is not 0: self.wakupPin = Pin.Pin(pin=self._config.get("WAKEUP", 0), direction=Pin.PinDirection.IN_PULL_LOW) self.wakupPin.set_detect(self.wakeup, Pin.PinEventEdge.BOTH) else: self.wakeup(None, threadsleep=False)
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'] factory = NativeFactory() with patch('gpiozero.pins.local.SpiDev'): with factory.spi() as device: assert isinstance(device, LocalPiHardwareSPI) device.close() assert device.closed with factory.spi(port=0, device=0) as device: assert isinstance(device, LocalPiHardwareSPI) with factory.spi(port=0, device=1) as device: assert isinstance(device, LocalPiHardwareSPI) with factory.spi(clock_pin=11) as device: assert isinstance(device, LocalPiHardwareSPI) with factory.spi(clock_pin=11, mosi_pin=10, select_pin=8) as device: assert isinstance(device, LocalPiHardwareSPI) with factory.spi(clock_pin=11, mosi_pin=10, select_pin=7) as device: assert isinstance(device, LocalPiHardwareSPI) with factory.spi(shared=True) as device: assert isinstance(device, LocalPiHardwareSPIShared) with pytest.raises(ValueError): factory.spi(port=1) with pytest.raises(ValueError): factory.spi(device=2) with pytest.raises(ValueError): factory.spi(port=0, clock_pin=12) with pytest.raises(ValueError): factory.spi(foo='bar')
from time import sleep from gpiozero.pins.native import NativeFactory from gpiozero import DigitalOutputDevice factory = NativeFactory() class Motor: def __init__(self, forward, backward, active_high=True, initial_value=False, pin_factory=None, name=''): self.forward_motor = DigitalOutputDevice(pin=forward, active_high=active_high) self.backward_motor = DigitalOutputDevice(pin=backward, active_high=active_high) self.name = name def forward(self): self.forward_motor.on() self.backward_motor.off() print(self.name, 'forward') def backward(self): self.forward_motor.off() self.backward_motor.on() print(self.name, 'backward')
import json import os import io import time import smbus2 as smbus import rospy import base64 from growbothub_tlc.srv import DeviceReadWrite, DeviceSummary if 'DRY_RUN' in os.environ: from gpiozero.pins.mock import MockFactory Device.pin_factory = MockFactory() else: from picamera import PiCamera from gpiozero.pins.native import NativeFactory Device.pin_factory = NativeFactory() class DeviceManager(): ''' Manages list of devices ''' _devices = {} @staticmethod def add(idx, device): DeviceManager._devices[idx] = device @staticmethod def get(idx): return DeviceManager._devices[idx]
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'] factory = NativeFactory() with patch('gpiozero.pins.local.SpiDev'): with factory.spi() as device: assert isinstance(device, LocalPiHardwareSPI) device.close() assert device.closed with factory.spi(port=0, device=0) as device: assert isinstance(device, LocalPiHardwareSPI) with factory.spi(port=0, device=1) as device: assert isinstance(device, LocalPiHardwareSPI) with factory.spi(clock_pin=11) as device: assert isinstance(device, LocalPiHardwareSPI) with factory.spi(clock_pin=11, mosi_pin=10, select_pin=8) as device: assert isinstance(device, LocalPiHardwareSPI) with factory.spi(clock_pin=11, mosi_pin=10, select_pin=7) as device: assert isinstance(device, LocalPiHardwareSPI) with factory.spi(shared=True) as device: assert isinstance(device, LocalPiHardwareSPIShared) with pytest.raises(ValueError): factory.spi(port=1) with pytest.raises(ValueError): factory.spi(device=2) with pytest.raises(ValueError): factory.spi(port=0, clock_pin=12) with pytest.raises(ValueError): factory.spi(foo='bar')
def native_factory(): return NativeFactory()