diff --git a/automationhat/__init__.py b/automationhat/__init__.py index 6070b5e..8c84558 100644 --- a/automationhat/__init__.py +++ b/automationhat/__init__.py @@ -3,8 +3,10 @@ import warnings import ads1015 -import RPi.GPIO as GPIO +import gpiod +import gpiodevice import sn3218 +from gpiod.line import Bias, Direction, Value from .pins import AsyncWorker, ObjectCollection, StoppableThread # noqa: F401 @@ -36,6 +38,7 @@ _is_setup = False _t_update_lights = None _ads1015 = None +_gpiochip = None class SNLight(object): @@ -133,13 +136,14 @@ def __init__(self, pin): self.pin = pin self._last_value = None self._is_setup = False + self._gpioline = None def __call__(self): return filter(lambda x: x[0] != '_', dir(self)) def read(self): self.setup() - return GPIO.input(self.pin) + return self._gpioline.get_value(self.pin) == Value.ACTIVE def setup(self): pass @@ -157,10 +161,10 @@ def has_changed(self): return False def is_on(self): - return self.read() == 1 + return self.read() is True def is_off(self): - return self.read() == 0 + return self.read() is False class Input(Pin): @@ -176,7 +180,13 @@ def setup(self): return False setup() - GPIO.setup(self.pin, GPIO.IN) + + self.pin = _gpiochip.line_offset_from_id(self.pin) + + self._gpioline = _gpiochip.request_lines(consumer="AH", config={ + self.pin: gpiod.LineSettings(direction=Direction.INPUT, bias=Bias.DISABLED) + }) + self._is_setup = True def auto_light(self, value=None): @@ -185,7 +195,8 @@ def auto_light(self, value=None): return self._en_auto_lights def read(self): - value = Pin.read(self) + self.setup() + value = self._gpioline.get_value(self.pin) == Value.ACTIVE if self._en_auto_lights: self.light.write(value) return value @@ -204,7 +215,13 @@ def setup(self): return False setup() - GPIO.setup(self.pin, GPIO.OUT, initial=0) + + self.pin = _gpiochip.line_offset_from_id(self.pin) + + self._gpioline = _gpiochip.request_lines(consumer="AH", config={ + self.pin: gpiod.LineSettings(direction=Direction.OUTPUT, bias=Bias.DISABLED, output_value=Value.INACTIVE) + }) + self._is_setup = True return True @@ -219,7 +236,7 @@ def write(self, value): :param value: Value to write, either 1 for HIGH or 0 for LOW """ self.setup() - GPIO.output(self.pin, value) + self._gpioline.set_value(self.pin, Value.ACTIVE if value else Value.INACTIVE) if self._en_auto_lights: self.light.write(1 if value else 0) @@ -256,10 +273,15 @@ def setup(self): setup() + self.pin = _gpiochip.line_offset_from_id(self.pin) + if is_automation_phat() and self.name == "one": self.pin = RELAY_3 - GPIO.setup(self.pin, GPIO.OUT, initial=0) + self._gpioline = _gpiochip.request_lines(consumer="AH", config={ + self.pin: gpiod.LineSettings(direction=Direction.OUTPUT, bias=Bias.DISABLED, output_value=Value.INACTIVE) + }) + self._is_setup = True return True @@ -273,7 +295,7 @@ def write(self, value): if is_automation_phat() and self.name in ["two", "three"]: warnings.warn("Relay '{}' is not supported on Automation pHAT".format(self.name)) - GPIO.output(self.pin, value) + self._gpioline.set_value(self.pin, Value.ACTIVE if value else Value.INACTIVE) if self._en_auto_lights: if value: @@ -331,15 +353,14 @@ def enable_auto_lights(state): def setup(): - global automation_hat, automation_phat, lights, _ads1015, _is_setup, _t_update_lights + global automation_hat, automation_phat, lights, _ads1015, _is_setup, _t_update_lights, _gpiochip if _is_setup: return True _is_setup = True - GPIO.setmode(GPIO.BCM) - GPIO.setwarnings(False) + _gpiochip = gpiodevice.find_chip_by_platform() _ads1015 = ads1015.ADS1015() try: @@ -379,8 +400,6 @@ def _exit(): if lights is not None: lights.output([0] * 18) - GPIO.cleanup() - analog = ObjectCollection() analog._add(one=AnalogInput(0, 25.85, 0))