How to use the gpiozero.mixins.SourceMixin 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 / output_devices.py View on Github external
sequence += [
                (1 - (i * (1 / fps) / fade_out_time), 1 / fps)
                for i in range(int(fps * fade_out_time))
                ]
        sequence.append((0, off_time))
        sequence = (
                cycle(sequence) if n is None else
                chain.from_iterable(repeat(sequence, n))
                )
        for value, delay in sequence:
            self._write(value)
            if self._blink_thread.stopping.wait(delay):
                break


class TonalBuzzer(SourceMixin, CompositeDevice):
    """
    Extends :class:`CompositeDevice` and represents a tonal buzzer.

    :type pin: int or str
    :param pin:
        The GPIO pin which the buzzer is connected to. See :ref:`pin-numbering`
        for valid pin numbers. If this is :data:`None` a :exc:`GPIODeviceError`
        will be raised.

    :param float initial_value:
        If :data:`None` (the default), the buzzer will be off initially. Values
        between -1 and 1 can be specified as an initial value for the buzzer.

    :type mid_tone: int or str
    :param mid_tone:
        The tone which is represented the device's middle value (0). The
github gpiozero / gpiozero / gpiozero / boards.py View on Github external
def transmit(self, socket, enable):
        with self._lock:
            try:
                code = (8 * bool(enable)) + (8 - socket)
                for bit in self[:4]:
                    bit.value = (code & 1)
                    code >>= 1
                sleep(0.1)
                self.enable.on()
                sleep(0.25)
            finally:
                self.enable.off()


class Energenie(SourceMixin, Device):
    """
    Extends :class:`Device` to represent an `Energenie socket`_ controller.

    This class is constructed with a socket number and an optional initial
    state (defaults to :data:`False`, meaning off). Instances of this class can
    be used to switch peripherals on and off. For example::

        from gpiozero import Energenie

        lamp = Energenie(1)
        lamp.on()

    :param int socket:
        Which socket this instance should control. This is an integer number
        between 1 and 4.
github WayneKeenan / picraftzero / picraftzero / zero.py View on Github external
try:
            self._state = self._motor.set_speed(value)
        except AttributeError:
            self._check_open()
            raise

    @property
    def value(self):
        return super(PiCraftMotor, self).value

    @value.setter
    def value(self, value):
        self._write(value)


class PiCraftServo(SourceMixin, Device):
    def __init__(self, _id=0):
        super(PiCraftServo, self).__init__()
        self._lock = Lock()
        self._id = _id
        Servo = get_servo_provider()
        self._servo = Servo(_id)

    def _write(self, value):
        try:
            self._servo.set_angle(value)
        except AttributeError:
            # self._check_open()
            raise

    @property
    def value(self):
github WayneKeenan / picraftzero / picraftzero / zero.py View on Github external
representing stopped.
        """
        return super(PanTilt, self).value

    @value.setter
    def value(self, value):
        if self.invert_pan_axis:
            value[0] = 180 - value[0]
        if self.invert_tilt_axis:
            value[1] = 180 - value[1]

        self.pan_servo.value, self.tilt_servo.value = value

# Debugging Helper

class SourcePrinter(SourceMixin, Device):
    def __init__(self, name=""):
        self._name = name
        super(SourcePrinter, self).__init__()

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, name):
        if name:
            self._name = name

    @property
    def value(self):
        return super(SourcePrinter, self).value
github gpiozero / gpiozero / gpiozero / output_devices.py View on Github external
"""
        Reverse the current direction of the motor. If the motor is currently
        idle this does nothing. Otherwise, the motor's direction will be
        reversed at the current speed.
        """
        self.value = -self.value

    def stop(self):
        """
        Stop the motor.
        """
        self.forward_device.off()
        self.backward_device.off()


class PhaseEnableMotor(SourceMixin, CompositeDevice):
    """
    Extends :class:`CompositeDevice` and represents a generic motor connected
    to a Phase/Enable motor driver circuit; the phase of the driver controls
    whether the motor turns forwards or backwards, while enable controls the
    speed with PWM.

    The following code will make the motor turn "forwards"::

        from gpiozero import PhaseEnableMotor
        motor = PhaseEnableMotor(12, 5)
        motor.forward()

    :type phase: int or str
    :param phase:
        The GPIO pin that the phase (direction) input of the motor driver chip
        is connected to. See :ref:`pin-numbering` for valid pin numbers. If
github gpiozero / gpiozero / gpiozero / mixins.py View on Github external
def __init__(self, *args, **kwargs):
        self._source = None
        self._source_thread = None
        self._source_delay = 0.01
        super(SourceMixin, self).__init__(*args, **kwargs)
github WayneKeenan / picraftzero / picraftzero / zero.py View on Github external
def value(self):
        """
        Represents the motion of the robot as a tuple of (left_motor_speed,
        right_motor_speed) with ``(-1, -1)`` representing full speed backwards,
        ``(1, 1)`` representing full speed forwards, and ``(0, 0)``
        representing stopped.
        """
        return super(Wheelbase, self).value

    @value.setter
    def value(self, value):
        logger.debug("Wheelbase.value={}".format(value))
        self.left_motor.value, self.right_motor.value = value


class PanTilt(SourceMixin, CompositeDevice):
    def __init__(self, pan=None, tilt=None):
        super(PanTilt, self).__init__(
            pan_servo=PiCraftServo(pan),
            tilt_servo=PiCraftServo(tilt),
            _order=('pan_servo', 'tilt_servo'))
        config = get_config()
        config_section = 'pantilt_0'
        self.invert_pan_axis = config.getboolean(config_section, 'invert_pan_axis', fallback=False)
        self.invert_tilt_axis = config.getboolean(config_section, 'invert_tilt_axis', fallback=False)


    @property
    def value(self):
        """
        Represents the motion of the robot as a tuple of (left_motor_speed,
        right_motor_speed) with ``(-1, -1)`` representing full speed backwards,
github WayneKeenan / picraftzero / picraftzero / zero.py View on Github external
raise

    @property
    def value(self):
        return super(PiCraftMotor, self).value

    @value.setter
    def value(self, value):
        self._write(value)


# ----------
# Composite devices


class Wheelbase(SourceMixin, CompositeDevice):
    def __init__(self, left=None, right=None):
        super(Wheelbase, self).__init__(
            left_motor=PiCraftMotor(left),
            right_motor=PiCraftMotor(right),
            _order=('left_motor', 'right_motor'))

    @property
    def value(self):
        """
        Represents the motion of the robot as a tuple of (left_motor_speed,
        right_motor_speed) with ``(-1, -1)`` representing full speed backwards,
        ``(1, 1)`` representing full speed forwards, and ``(0, 0)``
        representing stopped.
        """
        return super(Wheelbase, self).value
github WayneKeenan / picraftzero / picraftzero / zero.py View on Github external
    @value.setter
    def value(self, value):
        self._value = value

    def write_value(self, x_axis, y_axis):
        x_axis = -x_axis if self.invert_x_axis else x_axis
        y_axis = -y_axis if self.invert_y_axis else y_axis
        self._value = (x_axis, y_axis)

# ----------------------
# OUtput device


class PiCraftMotor(SourceMixin, Device):
    def __init__(self, _id=0):
        super(PiCraftMotor, self).__init__()
        self._lock = Lock()
        self._id = _id
        self._state = None
        Motor = get_motor_provider()
        self._motor = Motor(_id)

    def _write(self, value):
        try:
            self._state = self._motor.set_speed(value)
        except AttributeError:
            self._check_open()
            raise

    @property
github gpiozero / gpiozero / gpiozero / boards.py View on Github external
control.

    :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).

    .. _CamJam #3 EduKit: http://camjam.me/?page_id=1035
    """
    def __init__(self, pwm=True, pin_factory=None):
        super(CamJamKitRobot, self).__init__(
            left=(9, 10), right=(7, 8), pwm=pwm, pin_factory=pin_factory
        )


class PhaseEnableRobot(SourceMixin, CompositeDevice):
    """
    Extends :class:`CompositeDevice` to represent a dual-motor robot based
    around a Phase/Enable motor board.

    This class is constructed with two tuples representing the phase
    (direction) and enable (speed) pins of the left and right controllers
    respectively. For example, if the left motor's controller is connected to
    GPIOs 12 and 5, while the right motor's controller is connected to GPIOs 13
    and 6 so the following example will drive the robot forward::

        from gpiozero import PhaseEnableRobot

        robot = PhaseEnableRobot(left=(5, 12), right=(6, 13))
        robot.forward()

    :param tuple left: