Module plugins.gpio.Platform

Expand source code
import gpiozero as gpiozero
from pydantic.class_validators import validator

from modules.base.Configuration import *
from modules.base.Instances import *

@configuration
class GpioPlatformConfiguration(PlatformConfiguration):
    '''The GPIO platform is based on gpiozero, which is a wrapper for different GPIO libraries.
    Most of the libraries are untested in piTomation, please file an issue if you find a problem with one of the factories.'''

    factory: str = "mock" #"pigpio"
    '''gpio zero factory name, please see https://gpiozero.readthedocs.io/en/stable/api_pins.html'''

    @validator('platform')
    def check_platform(cls, v):
        if "plugins.gpio" not in v:
            raise ValueError("wrong platform: plugins.gpio, is: " + v)
        return v


class Platform(BasePlatform, Logging):
    '''GPIO platform'''
    
    def __init__(self, parent: Stackable, config: GpioPlatformConfiguration) -> None:
        super().__init__(parent, config)
        self.configuration = config

        def mockFactory():
            from gpiozero.pins.mock import MockFactory
            return MockFactory()

        def rpigpioFactory():
            from gpiozero.pins.rpigpio import RPiGPIOFactory
            return RPiGPIOFactory()

        def lgpioFactory():
            from gpiozero.pins.lgpio import LGPIOFactory
            return LGPIOFactory()

        def rpioFactory():
            from gpiozero.pins.rpio import RPIOFactory
            return RPIOFactory()

        def pigpioFactory():
            import pigpio        
            import time
            import os    
            pi = pigpio.pi() #type: ignore

            while not pi.connected: #type: ignore
                self.log_warning("make sure that pigpio is installed. 'sudo apt install pigpio' ")
                self.log_info("trying to connect to pipgio..")
                os.system("sudo pigpiod")
                time.sleep(1)

            from gpiozero.pins.pigpio import PiGPIOFactory
            return PiGPIOFactory()

        def nativeFactory():
            from gpiozero.pins.native import NativeFactory
            return NativeFactory()


        factories = {
            "mock": mockFactory,
            "rpigpio": rpigpioFactory,
            "lgpio": lgpioFactory,
            "rpio": rpioFactory,
            "pigpio": pigpioFactory,
            "native":   nativeFactory
        }

        gpiozero.Device.pin_factory = factories[self.configuration.factory]()

        self.gpio = gpiozero

    def dispose(self):
        self.gpio.Device.close() #type: ignore
        super().dispose()

Classes

class GpioPlatformConfiguration (**data: Any)

The GPIO platform is based on gpiozero, which is a wrapper for different GPIO libraries. Most of the libraries are untested in piTomation, please file an issue if you find a problem with one of the factories.

YAML configuration

Expand source code
@configuration
class GpioPlatformConfiguration(PlatformConfiguration):
    '''The GPIO platform is based on gpiozero, which is a wrapper for different GPIO libraries.
    Most of the libraries are untested in piTomation, please file an issue if you find a problem with one of the factories.'''

    factory: str = "mock" #"pigpio"
    '''gpio zero factory name, please see https://gpiozero.readthedocs.io/en/stable/api_pins.html'''

    @validator('platform')
    def check_platform(cls, v):
        if "plugins.gpio" not in v:
            raise ValueError("wrong platform: plugins.gpio, is: " + v)
        return v

Ancestors

Class variables

var factory : str

Static methods

def check_platform(v)
Expand source code
@validator('platform')
def check_platform(cls, v):
    if "plugins.gpio" not in v:
        raise ValueError("wrong platform: plugins.gpio, is: " + v)
    return v

Inherited members

class Platform (parent: Stackable, config: GpioPlatformConfiguration)

GPIO platform

Expand source code
class Platform(BasePlatform, Logging):
    '''GPIO platform'''
    
    def __init__(self, parent: Stackable, config: GpioPlatformConfiguration) -> None:
        super().__init__(parent, config)
        self.configuration = config

        def mockFactory():
            from gpiozero.pins.mock import MockFactory
            return MockFactory()

        def rpigpioFactory():
            from gpiozero.pins.rpigpio import RPiGPIOFactory
            return RPiGPIOFactory()

        def lgpioFactory():
            from gpiozero.pins.lgpio import LGPIOFactory
            return LGPIOFactory()

        def rpioFactory():
            from gpiozero.pins.rpio import RPIOFactory
            return RPIOFactory()

        def pigpioFactory():
            import pigpio        
            import time
            import os    
            pi = pigpio.pi() #type: ignore

            while not pi.connected: #type: ignore
                self.log_warning("make sure that pigpio is installed. 'sudo apt install pigpio' ")
                self.log_info("trying to connect to pipgio..")
                os.system("sudo pigpiod")
                time.sleep(1)

            from gpiozero.pins.pigpio import PiGPIOFactory
            return PiGPIOFactory()

        def nativeFactory():
            from gpiozero.pins.native import NativeFactory
            return NativeFactory()


        factories = {
            "mock": mockFactory,
            "rpigpio": rpigpioFactory,
            "lgpio": lgpioFactory,
            "rpio": rpioFactory,
            "pigpio": pigpioFactory,
            "native":   nativeFactory
        }

        gpiozero.Device.pin_factory = factories[self.configuration.factory]()

        self.gpio = gpiozero

    def dispose(self):
        self.gpio.Device.close() #type: ignore
        super().dispose()

Ancestors

Methods

def dispose(self)
Expand source code
def dispose(self):
    self.gpio.Device.close() #type: ignore
    super().dispose()

Inherited members