Skip to content

Commit

Permalink
Port to gpiod/gpiodevice.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Feb 15, 2024
1 parent 3ffcef5 commit 47ed3a9
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions automationhat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, Edge, Value

from .pins import AsyncWorker, ObjectCollection, StoppableThread # noqa: F401

Expand Down Expand Up @@ -36,6 +38,7 @@
_is_setup = False
_t_update_lights = None
_ads1015 = None
_gpiochip = None


class SNLight(object):
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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)

Expand Down Expand Up @@ -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

Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 47ed3a9

Please sign in to comment.