From b63aa9aaa4c540ff20ed86aa736810a73255dd9b Mon Sep 17 00:00:00 2001 From: Matthew Campbell Date: Tue, 17 Mar 2020 15:51:09 -0400 Subject: [PATCH 1/8] Add active low support --- gpio.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/gpio.py b/gpio.py index 7f5cab3..0b3de84 100644 --- a/gpio.py +++ b/gpio.py @@ -17,9 +17,10 @@ class PinState(object): value: the file pointer to set/read value of pin. direction: the file pointer to set/read direction of the pin. """ - def __init__(self, value, direction): + def __init__(self, value, direction, active_low): self.value = value self.direction = direction + self.active_low = active_low path = os.path pjoin = os.path.join @@ -63,7 +64,8 @@ def wrapped(pin, *args, **kwargs): _write(f, pin) value = open(pjoin(ppath, 'value'), FMODE) direction = open(pjoin(ppath, 'direction'), FMODE) - _open[pin] = PinState(value=value, direction=direction) + active_low = open(pjoin(ppath, 'active_low'), FMODE) + _open[pin] = PinState(value=value, direction=direction, active_low=active_low) return function(pin, *args, **kwargs) return wrapped @@ -102,7 +104,7 @@ def cleanup(pin=None, assert_exists=False): @_verify -def setup(pin, mode, pullup=None, initial=False): +def setup(pin, mode, pullup=None, initial=False, active_low=False): '''Setup pin with mode IN or OUT. Args: @@ -110,7 +112,8 @@ def setup(pin, mode, pullup=None, initial=False): mode (str): use either gpio.OUT or gpio.IN pullup (None): rpio compatibility. If anything but None, raises value Error - pullup (bool, optional): Initial pin value. Default is False + initial (bool, optional): Initial pin value. Default is False + active_low (bool, optional): Set the pin to active low. Default is False ''' if pullup is not None: raise ValueError("sysfs does not support pullups") @@ -118,9 +121,19 @@ def setup(pin, mode, pullup=None, initial=False): if mode not in (IN, OUT, LOW, HIGH): raise ValueError(mode) + if not type(active_low) == bool: + raise ValueError("active_low argument must be True or False") + + log.debug("Set active_low {0}: {1}".format(pin, active_low)) + f_active_low = _open[pin].active_low + if active_low: + _write(f_active_low, 1) + else: + _write(f_active_low, 0) + log.debug("Setup {0}: {1}".format(pin, mode)) - f = _open[pin].direction - _write(f, mode) + f_direction = _open[pin].direction + _write(f_direction, mode) if mode == OUT: if initial: set(pin, 1) From 19848cf1c9d05a2f73b0445982a3dc021354bd94 Mon Sep 17 00:00:00 2001 From: Matthew Campbell Date: Thu, 19 Mar 2020 19:29:31 -0400 Subject: [PATCH 2/8] Better API backwards compatibility --- gpio.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/gpio.py b/gpio.py index 0b3de84..c747ded 100644 --- a/gpio.py +++ b/gpio.py @@ -16,6 +16,8 @@ class PinState(object): Args: value: the file pointer to set/read value of pin. direction: the file pointer to set/read direction of the pin. + active_now: the file pointer to set/read if the pin is active_low, + if None leave things as configured currently in sysfs. """ def __init__(self, value, direction, active_low): self.value = value @@ -104,7 +106,7 @@ def cleanup(pin=None, assert_exists=False): @_verify -def setup(pin, mode, pullup=None, initial=False, active_low=False): +def setup(pin, mode, pullup=None, initial=False, active_low=None): '''Setup pin with mode IN or OUT. Args: @@ -113,7 +115,8 @@ def setup(pin, mode, pullup=None, initial=False, active_low=False): pullup (None): rpio compatibility. If anything but None, raises value Error initial (bool, optional): Initial pin value. Default is False - active_low (bool, optional): Set the pin to active low. Default is False + active_low (bool, optional): Set the pin to active low. Default + is None which leaves things as configured in sysfs ''' if pullup is not None: raise ValueError("sysfs does not support pullups") @@ -124,12 +127,13 @@ def setup(pin, mode, pullup=None, initial=False, active_low=False): if not type(active_low) == bool: raise ValueError("active_low argument must be True or False") - log.debug("Set active_low {0}: {1}".format(pin, active_low)) - f_active_low = _open[pin].active_low - if active_low: - _write(f_active_low, 1) - else: - _write(f_active_low, 0) + if active_low is not None: + log.debug("Set active_low {0}: {1}".format(pin, active_low)) + f_active_low = _open[pin].active_low + if active_low: + _write(f_active_low, 1) + else: + _write(f_active_low, 0) log.debug("Setup {0}: {1}".format(pin, mode)) f_direction = _open[pin].direction From 3f3b6420dffd982b3dc1e9b9154ad96c83241797 Mon Sep 17 00:00:00 2001 From: Matthew Campbell Date: Thu, 19 Mar 2020 19:38:40 -0400 Subject: [PATCH 3/8] Purge docs about None as this can't happen --- gpio.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/gpio.py b/gpio.py index c747ded..ad161bf 100644 --- a/gpio.py +++ b/gpio.py @@ -16,8 +16,7 @@ class PinState(object): Args: value: the file pointer to set/read value of pin. direction: the file pointer to set/read direction of the pin. - active_now: the file pointer to set/read if the pin is active_low, - if None leave things as configured currently in sysfs. + active_now: the file pointer to set/read if the pin is active_low. """ def __init__(self, value, direction, active_low): self.value = value From 26bcdc9f56bfdaec18410989ab8c2d8eace500e2 Mon Sep 17 00:00:00 2001 From: Matthew Campbell Date: Fri, 20 Mar 2020 08:15:06 -0400 Subject: [PATCH 4/8] Close active_low on cleanup --- gpio.py | 1 + 1 file changed, 1 insertion(+) diff --git a/gpio.py b/gpio.py index ad161bf..62bc55c 100644 --- a/gpio.py +++ b/gpio.py @@ -95,6 +95,7 @@ def cleanup(pin=None, assert_exists=False): return state.value.close() state.direction.close() + state.active_low.close() if os.path.exists(gpiopath(pin)): log.debug("Unexporting pin {0}".format(pin)) with _export_lock: From 4ef2b99d0e8eda4474ae21545abd51a6996f4857 Mon Sep 17 00:00:00 2001 From: Matthew Campbell Date: Fri, 20 Mar 2020 08:16:14 -0400 Subject: [PATCH 5/8] put exception gaurd on file opens --- gpio.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/gpio.py b/gpio.py index 62bc55c..effa24f 100644 --- a/gpio.py +++ b/gpio.py @@ -63,9 +63,16 @@ def wrapped(pin, *args, **kwargs): with _export_lock: with open(pjoin(gpio_root, 'export'), 'w') as f: _write(f, pin) - value = open(pjoin(ppath, 'value'), FMODE) - direction = open(pjoin(ppath, 'direction'), FMODE) - active_low = open(pjoin(ppath, 'active_low'), FMODE) + value, direction, active_low = None, None, None + try: + value = open(pjoin(ppath, 'value'), FMODE) + direction = open(pjoin(ppath, 'direction'), FMODE) + active_low = open(pjoin(ppath, 'active_low'), FMODE) + except Exception as e: + if value: value.close() + if direction: direction.close() + if active_low: active_low.close() + throw e _open[pin] = PinState(value=value, direction=direction, active_low=active_low) return function(pin, *args, **kwargs) return wrapped From 5b3e06a31db4d1737cd68afa7833d6c88c2598bb Mon Sep 17 00:00:00 2001 From: Matthew Campbell Date: Fri, 20 Mar 2020 08:17:29 -0400 Subject: [PATCH 6/8] Clean up type checking on active_low --- gpio.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/gpio.py b/gpio.py index effa24f..fa13ad8 100644 --- a/gpio.py +++ b/gpio.py @@ -131,10 +131,9 @@ def setup(pin, mode, pullup=None, initial=False, active_low=None): if mode not in (IN, OUT, LOW, HIGH): raise ValueError(mode) - if not type(active_low) == bool: - raise ValueError("active_low argument must be True or False") - if active_low is not None: + if not isinstance(active_low, bool): + raise ValueError("active_low argument must be True or False") log.debug("Set active_low {0}: {1}".format(pin, active_low)) f_active_low = _open[pin].active_low if active_low: From 9062fccd77eedfcea7aab21b45b2af6a2cde1127 Mon Sep 17 00:00:00 2001 From: Matthew Campbell Date: Fri, 20 Mar 2020 08:19:06 -0400 Subject: [PATCH 7/8] use int cast for active_low->int --- gpio.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/gpio.py b/gpio.py index fa13ad8..bbedc85 100644 --- a/gpio.py +++ b/gpio.py @@ -136,10 +136,7 @@ def setup(pin, mode, pullup=None, initial=False, active_low=None): raise ValueError("active_low argument must be True or False") log.debug("Set active_low {0}: {1}".format(pin, active_low)) f_active_low = _open[pin].active_low - if active_low: - _write(f_active_low, 1) - else: - _write(f_active_low, 0) + _write(f_active_low, int(active_low)) log.debug("Setup {0}: {1}".format(pin, mode)) f_direction = _open[pin].direction From da0f98023e9f5cb54d62ed64b84802bf59b8f69d Mon Sep 17 00:00:00 2001 From: Matthew Campbell Date: Fri, 20 Mar 2020 08:38:04 -0400 Subject: [PATCH 8/8] Use python syntax --- gpio.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gpio.py b/gpio.py index bbedc85..1c893ba 100644 --- a/gpio.py +++ b/gpio.py @@ -72,7 +72,7 @@ def wrapped(pin, *args, **kwargs): if value: value.close() if direction: direction.close() if active_low: active_low.close() - throw e + raise e _open[pin] = PinState(value=value, direction=direction, active_low=active_low) return function(pin, *args, **kwargs) return wrapped