Skip to content

Commit 5dcd2cf

Browse files
committed
SysfsGpio: add invert attribute (active-low) to the SysfsGPIO classes
The sysfsgpio resources (SysfsGPIO, NetworkSysfsGPIO and MatchedSysfsGPIO), the GPIOSysFSExport, the driver GpioDigitalOutputDriver and the GpioDigitalOutput agent have been modified to have an additional optional invert (active-low) attribute (default False) which can be used to invert the logical value used on the gpio line. Signed-off-by: Perry Melange <isprotejesvalkata@gmail.com>
1 parent 22f4b81 commit 5dcd2cf

File tree

6 files changed

+24
-13
lines changed

6 files changed

+24
-13
lines changed

labgrid/driver/gpiodriver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ def on_deactivate(self):
3737
@Driver.check_active
3838
@step(args=['status'])
3939
def set(self, status):
40-
self.proxy.set(self.gpio.index, status)
40+
self.proxy.set(self.gpio.index, self.gpio.invert, status)
4141

4242
@Driver.check_active
4343
@step(result=True)
4444
def get(self):
45-
return self.proxy.get(self.gpio.index)
45+
return self.proxy.get(self.gpio.index, self.gpio.invert)

labgrid/remote/exporter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,16 +642,19 @@ def _get_params(self):
642642
return {
643643
"host": self.host,
644644
"index": self.local.index,
645+
"invert": self.local.invert,
645646
}
646647

647648
def _get_start_params(self):
648649
return {
649650
"index": self.local.index,
651+
"invert": self.local.invert,
650652
}
651653

652654
def _start(self, start_params):
653655
"""Start a GPIO export to userspace"""
654656
index = start_params["index"]
657+
invert = start_params["invert"] # pylint: disable=unused-variable
655658

656659
if self.export_path.exists():
657660
self.system_exported = True

labgrid/resource/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,7 @@ class SysfsGPIO(Resource):
4242
"""The basic SysfsGPIO contains an index
4343
4444
Args:
45-
index (int): index of target gpio line."""
45+
index (int): index of target gpio line.
46+
invert (bool) : optional, whether the logic level is inverted (active-low)"""
4647
index = attr.ib(default=None, validator=attr.validators.instance_of(int))
48+
invert = attr.ib(default=False, validator=attr.validators.instance_of(bool))

labgrid/resource/remote.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ class NetworkSysfsGPIO(NetworkResource, ManagedResource):
340340

341341
"""The NetworkSysfsGPIO describes a remotely accessible gpio line"""
342342
index = attr.ib(validator=attr.validators.optional(attr.validators.instance_of(int)))
343+
invert = attr.ib(default=False, validator=attr.validators.instance_of(bool))
343344
def __attrs_post_init__(self):
344345
self.timeout = 10.0
345346
super().__attrs_post_init__()

labgrid/resource/udev.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,10 @@ class MatchedSysfsGPIO(USBResource):
778778
"""The MatchedSysfsGPIO described a SysfsGPIO matched by Udev
779779
780780
Args:
781-
pin (int): gpio pin number within the matched gpiochip."""
781+
pin (int): gpio pin number within the matched gpiochip.
782+
invert (bool): optional, whether the logic level is inverted (active-low)"""
782783
pin = attr.ib(default=None, validator=attr.validators.instance_of(int))
784+
invert = attr.ib(default=False, validator=attr.validators.instance_of(bool))
783785
index = None
784786

785787
def __attrs_post_init__(self):

labgrid/util/agents/sysfsgpio.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
This module implements switching GPIOs via sysfs GPIO kernel interface.
33
44
Takes an integer property 'index' which refers to the already exported GPIO device.
5+
Takes a boolean property 'invert' which inverts logical values if set to True (active-low)
56
67
"""
78
import logging
@@ -23,7 +24,7 @@ def _assert_gpio_line_is_exported(index):
2324
if not os.path.exists(gpio_sysfs_path):
2425
raise ValueError("Device not found")
2526

26-
def __init__(self, index):
27+
def __init__(self, index, invert):
2728
self.gpio_sysfs_value_fd = None
2829
self._logger = logging.getLogger("Device: ")
2930
GpioDigitalOutput._assert_gpio_line_is_exported(index)
@@ -41,6 +42,10 @@ def __init__(self, index):
4142
gpio_sysfs_value_path = os.path.join(gpio_sysfs_path, 'value')
4243
self.gpio_sysfs_value_fd = os.open(gpio_sysfs_value_path, flags=(os.O_RDWR | os.O_SYNC))
4344

45+
gpio_sysfs_active_low_path = os.path.join(gpio_sysfs_path, 'active_low')
46+
with open(gpio_sysfs_active_low_path, 'w') as active_low_fd:
47+
active_low_fd.write(str(int(invert)))
48+
4449
def __del__(self):
4550
if self.gpio_sysfs_value_fd:
4651
os.close(self.gpio_sysfs_value_fd)
@@ -68,24 +73,22 @@ def set(self, status):
6873

6974
os.write(self.gpio_sysfs_value_fd, binary_value)
7075

71-
7276
_gpios = {}
7377

74-
def _get_gpio_line(index):
78+
def _get_gpio_line(index, invert):
7579
if index not in _gpios:
76-
_gpios[index] = GpioDigitalOutput(index=index)
80+
_gpios[index] = GpioDigitalOutput(index=index, invert=invert)
7781
return _gpios[index]
7882

79-
def handle_set(index, status):
80-
gpio_line = _get_gpio_line(index)
83+
def handle_set(index, invert, status):
84+
gpio_line = _get_gpio_line(index, invert)
8185
gpio_line.set(status)
8286

8387

84-
def handle_get(index):
85-
gpio_line = _get_gpio_line(index)
88+
def handle_get(index, invert):
89+
gpio_line = _get_gpio_line(index, invert)
8690
return gpio_line.get()
8791

88-
8992
methods = {
9093
'set': handle_set,
9194
'get': handle_get,

0 commit comments

Comments
 (0)