How to use the gpiozero.devices.Device function in gpiozero

To help you get started, we’ve selected a few gpiozero examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github gpiozero / gpiozero / gpiozero / pins / spi.py View on Github external
print_function,
    absolute_import,
    division,
    )
str = type('')


import operator
from threading import RLock

from ..devices import Device, SharedMixin
from ..input_devices import InputDevice
from ..output_devices import OutputDevice


class SPISoftwareBus(SharedMixin, Device):
    def __init__(self, clock_pin, mosi_pin, miso_pin):
        self.lock = None
        self.clock = None
        self.mosi = None
        self.miso = None
        super(SPISoftwareBus, self).__init__()
        self.lock = RLock()
        try:
            self.clock = OutputDevice(clock_pin, active_high=True)
            if mosi_pin is not None:
                self.mosi = OutputDevice(mosi_pin)
            if miso_pin is not None:
                self.miso = InputDevice(miso_pin)
        except:
            self.close()
            raise
github gpiozero / gpiozero / gpiozero / pins / pigpio.py View on Github external
self._factory.connection.spi_close(self._handle)
        self._spi_flags = (self._spi_flags & ~0x3f0000) | ((value & 0x3f) << 16)
        self._handle = self._factory.connection.spi_open(
            self._device, self._baud, self._spi_flags)

    def transfer(self, data):
        self._check_open()
        count, data = self._factory.connection.spi_xfer(self._handle, data)
        if count < 0:
            raise IOError('SPI transfer error %d' % count)
        # Convert returned bytearray to list of ints. XXX Not sure how non-byte
        # sized words (aux intf only) are returned ... padded to 16/32-bits?
        return [int(b) for b in data]


class PiGPIOSoftwareSPI(SPI, Device):
    """
    Software SPI implementation for the `pigpio`_ library. Uses the ``bb_spi_*``
    functions from the pigpio API.

    .. _pigpio: http://abyz.co.uk/rpi/pigpio/
    """
    def __init__(self, factory, clock_pin, mosi_pin, miso_pin, select_pin):
        self._closed = True
        self._select_pin = select_pin
        self._clock_pin = clock_pin
        self._mosi_pin = mosi_pin
        self._miso_pin = miso_pin
        self._factory = factory
        super(PiGPIOSoftwareSPI, self).__init__()
        self._factory.reserve_pins(
            self,
github gpiozero / gpiozero / gpiozero / spi.py View on Github external
import warnings
import operator
from threading import RLock

try:
    from spidev import SpiDev
except ImportError:
    SpiDev = None

from .devices import Device, SharedMixin, _PINS, _PINS_LOCK
from .input_devices import InputDevice
from .output_devices import OutputDevice
from .exc import SPIBadArgs, SPISoftwareFallback, GPIOPinInUse, DeviceClosed


class SPIHardwareInterface(Device):
    def __init__(self, port, device):
        self._device = None
        super(SPIHardwareInterface, self).__init__()
        # XXX How can we detect conflicts with existing GPIO instances? This
        # isn't ideal ... in fact, it's downright crap and doesn't guard
        # against conflicts created *after* this instance, but it's all I can
        # come up with right now ...
        conflicts = (11, 10, 9, (8, 7)[device])
        with _PINS_LOCK:
            for pin in _PINS:
                if pin.number in conflicts:
                    raise GPIOPinInUse(
                        'pin %r is already in use by another gpiozero object' % pin
                    )
        self._device_num = device
        self._device = SpiDev()
github gpiozero / gpiozero / gpiozero / spi_devices.py View on Github external
)
str = type('')


from math import log, ceil
from operator import or_
try:
    from functools import reduce
except ImportError:
    pass # py2's reduce is built-in

from .exc import DeviceClosed, SPIBadChannel, InputDeviceError
from .devices import Device


class SPIDevice(Device):
    """
    Extends :class:`Device`. Represents a device that communicates via the SPI
    protocol.

    See :ref:`spi_args` for information on the keyword arguments that can be
    specified with the constructor.
    """
    def __init__(self, **spi_args):
        self._spi = None
        super(SPIDevice, self).__init__(
            pin_factory=spi_args.pop('pin_factory', None)
        )
        self._spi = self.pin_factory.spi(**spi_args)

    def close(self):
        if getattr(self, '_spi', None):
github gpiozero / gpiozero / gpiozero / pins / data.py View on Github external
def pi_info(revision=None):
    """
    Returns a :class:`PiBoardInfo` instance containing information about a
    *revision* of the Raspberry Pi.

    :param str revision:
        The revision of the Pi to return information about. If this is omitted
        or :data:`None` (the default), then the library will attempt to determine
        the model of Pi it is running on and return information about that.
    """
    if revision is None:
        if Device.pin_factory is None:
            Device.pin_factory = Device._default_pin_factory()
        result = Device.pin_factory.pi_info
        if result is None:
            raise PinUnknownPi('The default pin_factory is not attached to a Pi')
        else:
            return result
    else:
        if isinstance(revision, bytes):
            revision = revision.decode('ascii')
        if isinstance(revision, str):
            revision = int(revision, base=16)
        else:
            # be nice to people passing an int (or something numeric anyway)
            revision = int(revision)
        return PiBoardInfo.from_revision(revision)
github gpiozero / gpiozero / gpiozero / spi.py View on Github external
def _get_bits_per_word(self):
        return self._device.bits_per_word

    def _set_bits_per_word(self, value):
        self._device.bits_per_word = value

    clock_polarity = property(_get_clock_polarity, _set_clock_polarity)
    clock_phase = property(_get_clock_phase, _set_clock_phase)
    clock_mode = property(_get_clock_mode, _set_clock_mode)
    lsb_first = property(_get_lsb_first, _set_lsb_first)
    select_high = property(_get_select_high, _set_select_high)
    bits_per_word = property(_get_bits_per_word, _set_bits_per_word)


class SPISoftwareBus(SharedMixin, Device):
    def __init__(self, clock_pin, mosi_pin, miso_pin):
        self.lock = None
        self.clock = None
        self.mosi = None
        self.miso = None
        super(SPISoftwareBus, self).__init__()
        self.lock = RLock()
        try:
            self.clock = OutputDevice(clock_pin, active_high=True)
            if mosi_pin is not None:
                self.mosi = OutputDevice(mosi_pin)
            if miso_pin is not None:
                self.miso = InputDevice(miso_pin)
        except:
            self.close()
            raise
github gpiozero / gpiozero / gpiozero / output_devices.py View on Github external
:param int frequency:
        The frequency (in Hz) of pulses emitted to drive the LED. Defaults
        to 100Hz.

    :type pin_factory: Factory or None
    :param pin_factory:
        See :doc:`api_pins` for more information (this is an advanced feature
        which most users can ignore).
    """
    pass

PWMLED.is_lit = PWMLED.is_active


class RGBLED(SourceMixin, Device):
    """
    Extends :class:`Device` and represents a full color LED component (composed
    of red, green, and blue LEDs).

    Connect the common cathode (longest leg) to a ground pin; connect each of
    the other legs (representing the red, green, and blue anodes) to any GPIO
    pins.  You should use three limiting resistors (one per anode).

    The following code will make the LED yellow::

        from gpiozero import RGBLED

        led = RGBLED(2, 3, 4)
        led.color = (1, 1, 0)

    The `colorzero`_ library is also supported::