Skip to content

File setup.py#

File List > build > lib.linux-armv7l-cpython-311 > pypisoundmicro > setup.py

Go to the documentation of this file

"""Setup utilities for Element configuration in pypisoundmicro."""
from ._utils import copy_doc
from .swig import pypisoundmicro as psm
from typing import Optional, Union, Self
from .types import ElementType, Pin, PinDirection, PinPull, ActivityType
from .name import ElementName
from . import Element

class Setup:
    """Wrapper for upisnd_setup_t type that encapsulates Element configuration.

    This class provides a Pythonic interface for working with element setup options
    that are passed to Element.setup() to create new elements.
    """

    def __init__(self) -> None:
        """Initialize an empty Setup object."""
        self._setup = 0

    @property
    def element_type(self) -> ElementType:
        """ Extracts the Element type from the setup container."""
        return ElementType(psm.upisnd_setup_get_element_type(self._setup))

    @element_type.setter
    def element_type(self, value: ElementType) -> None:
        """
     Sets the Element type in the setup container.

    Always set the Element Type before setting any other property of setup, as
    the type is double-checked to know if the set operation is valid for this type.

    :rtype: int
    :return: 0 on success, `-errno` on error.
    """
        result, setup = psm.upisnd_setup_set_element_type(self._setup, value)
        if result != 0:
            raise ValueError(f'Failed to set element type: {result}')
        self._setup = setup

    @property
    def pin(self) -> Pin:
        """ Extracts the main pin from the setup container."""
        return Pin(psm.upisnd_setup_get_pin_id(self._setup))

    @pin.setter
    def pin(self, value: Pin) -> None:
        """
     Sets the main pin id in the setup container.

    :rtype: int
    :return: 0 on success, `-errno` on error.
    """
        result, setup = psm.upisnd_setup_set_pin_id(self._setup, value)
        if result != 0:
            raise ValueError(f'Failed to set pin: {result}')
        self._setup = setup

    @property
    def gpio_direction(self) -> PinDirection:
        """ Extracts the GPIO direction from the setup container, applies only to GPIO Input or Output."""
        return PinDirection(psm.upisnd_setup_get_gpio_dir(self._setup))

    @gpio_direction.setter
    def gpio_direction(self, value: PinDirection) -> None:
        """
     Sets the GPIO direction in the setup container, applies only to GPIO Elements.

    Always set the GPIO dir before setting the pull and output, as the direction is double-checked
    to know whether pull (only input) or output level (only output) properties are valid.

    :rtype: int
    :return: 0 on success, `-errno` on error.
    """
        result, setup = psm.upisnd_setup_set_gpio_dir(self._setup, value)
        if result != 0:
            raise ValueError(f'Failed to set GPIO direction: {result}')
        self._setup = setup

    @property
    def gpio_pull(self) -> PinPull:
        """ Extracts the GPIO pull from the setup container, applies to GPIO Input and the first pin of Encoder."""
        return PinPull(psm.upisnd_setup_get_gpio_pull(self._setup))

    @gpio_pull.setter
    def gpio_pull(self, value: PinPull) -> None:
        """
     Sets the GPIO pull in the setup container, applies only to GPIO Input.

    Make sure to first set the GPIO dir to #UPISND_PIN_DIR_INPUT.

    :rtype: int
    :return: 0 on success, `-errno` on error.
    """
        result, setup = psm.upisnd_setup_set_gpio_pull(self._setup, value)
        if result != 0:
            raise ValueError(f'Failed to set GPIO pull: {result}')
        self._setup = setup

    @property
    def gpio_output(self) -> int:
        """ Extracts the GPIO output level from the setup container, applies only to GPIO Output."""
        return psm.upisnd_setup_get_gpio_output(self._setup)

    @gpio_output.setter
    def gpio_output(self, value: int) -> None:
        """
     Sets the GPIO output level in the setup container, applies only to GPIO Output.

    Make sure to first set the GPIO dir to #UPISND_PIN_DIR_OUTPUT.

    :rtype: int
    :return: 0 on success, `-errno` on error.
    """
        result, setup = psm.upisnd_setup_set_gpio_output(self._setup, value)
        if result != 0:
            raise ValueError(f'Failed to set GPIO output: {result}')
        self._setup = setup

    @property
    def encoder_pin_b(self) -> Pin:
        """ Extracts the Encoder's second pin from the setup container, applies only to Encoders."""
        return Pin(psm.upisnd_setup_get_encoder_pin_b_id(self._setup))

    @encoder_pin_b.setter
    def encoder_pin_b(self, value: Pin) -> None:
        """
     Sets the Encoder's second pin id in the setup container, applies only to Encoders.

    :rtype: int
    :return: 0 on success, `-errno on error.
    """
        result, setup = psm.upisnd_setup_set_encoder_pin_b_id(self._setup, value)
        if result != 0:
            raise ValueError(f'Failed to set encoder pin B: {result}')
        self._setup = setup

    @property
    def encoder_pin_b_pull(self) -> PinPull:
        """ Extracts the Encoder's second pin pull from the setup container, applies only to Encoders."""
        return PinPull(psm.upisnd_setup_get_encoder_pin_b_pull(self._setup))

    @encoder_pin_b_pull.setter
    def encoder_pin_b_pull(self, value: PinPull) -> None:
        """
     Sets the Encoder's second pin pull in the setup container, applies only to Encoders.

    :rtype: int
    :return: 0 on success, `-errno` on error.
    """
        result, setup = psm.upisnd_setup_set_encoder_pin_b_pull(self._setup, value)
        if result != 0:
            raise ValueError(f'Failed to set encoder pin B pull: {result}')
        self._setup = setup

    @property
    def activity_type(self) -> ActivityType:
        """ Extracts the Activity type from the setup container, applies only to Activity Elements."""
        return ActivityType(psm.upisnd_setup_get_activity_type(self._setup))

    @activity_type.setter
    def activity_type(self, value: ActivityType) -> None:
        """
     Sets the Activity type in the setup container, applies only to Activity Elements.

    :rtype: int
    :return: 0 on success, `-errno` on error.
    """
        result, setup = psm.upisnd_setup_set_activity_type(self._setup, value)
        if result != 0:
            raise ValueError(f'Failed to set activity type: {result}')
        self._setup = setup

    def to_int(self) -> int:
        """Get the raw setup integer value."""
        return self._setup

    @classmethod
    def from_int(cls, setup_int: int) -> Self:
        """Create a Setup object from an existing integer setup value.

        Args:
            setup_int: An integer representing a upisnd_setup_t value

        Returns:
            A new Setup object initialized with the given setup value
        """
        setup = cls()
        setup._setup = setup_int
        return setup

    @staticmethod
    def for_encoder(pin_a: Pin, pull_a: PinPull, pin_b: Pin, pull_b: PinPull) -> 'Setup':
        """ Initialize upisnd_setup_t with options for Encoder."""
        setup = Setup()
        result, setup_value = psm.upisnd_setup_for_encoder(setup._setup, pin_a, pull_a, pin_b, pull_b)
        if result != 0:
            raise ValueError(f'Failed to setup encoder: {result}')
        setup._setup = setup_value
        return setup

    @staticmethod
    def for_analog_input(pin: Pin) -> 'Setup':
        """ Initialize upisnd_setup_t with options for Analog Input."""
        setup = Setup()
        result, setup_value = psm.upisnd_setup_for_analog_input(setup._setup, pin)
        if result != 0:
            raise ValueError(f'Failed to setup analog input: {result}')
        setup._setup = setup_value
        return setup

    @staticmethod
    def for_gpio_input(pin: Pin, pull: PinPull) -> 'Setup':
        """ Initialize upisnd_setup_t with options for GPIO Input."""
        setup = Setup()
        result, setup_value = psm.upisnd_setup_for_gpio_input(setup._setup, pin, pull)
        if result != 0:
            raise ValueError(f'Failed to setup GPIO input: {result}')
        setup._setup = setup_value
        return setup

    @staticmethod
    def for_gpio_output(pin: Pin, high: int) -> 'Setup':
        """ Initialize upisnd_setup_t with options for GPIO Output."""
        setup = Setup()
        result, setup_value = psm.upisnd_setup_for_gpio_output(setup._setup, pin, high)
        if result != 0:
            raise ValueError(f'Failed to setup GPIO output: {result}')
        setup._setup = setup_value
        return setup

    @staticmethod
    def for_activity(pin: Pin, activity: ActivityType) -> 'Setup':
        """ Initialize upisnd_setup_t with options for Activity Element."""
        setup = Setup()
        result, setup_value = psm.upisnd_setup_for_activity(setup._setup, pin, activity)
        if result != 0:
            raise ValueError(f'Failed to setup activity: {result}')
        setup._setup = setup_value
        return setup

def setup_element(name: Union[str, ElementName], setup: Union[Setup, int]) -> Element:
    """
     Set up an Element with the provided name and setup options container.

    :type name: string
    :param name: The name of the Element to set up.
    :type setup: int
    :param setup: The setup options container for the Element.

    In case an Element already exists with the same name, and the requested configuration is the same,
    the existing Element will get returned and its refcount will get incremented. Otherwise, NULL
    will be returned, and `errno` set appropriately.

    :rtype: upisnd_elements_list_node_t
    :return: A valid Element reference on success, NULL on error, inspect `errno` for details.
    """
    return Element.setup(name, setup)