Skip to content

Commit 1a7b246

Browse files
committed
SysfsGpio: add invert attribute (active-low) to the SysfsGPIO classes
The 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 <[email protected]>
1 parent cea524d commit 1a7b246

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"]
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
@@ -758,8 +758,10 @@ class MatchedSysfsGPIO(USBResource):
758758
"""The MatchedSysfsGPIO described a SysfsGPIO matched by Udev
759759
760760
Args:
761-
pin (int): gpio pin number within the matched gpiochip."""
761+
pin (int): gpio pin number within the matched gpiochip.
762+
invert (bool): optional, whether the logic level is inverted (active-low)"""
762763
pin = attr.ib(default=None, validator=attr.validators.instance_of(int))
764+
invert = attr.ib(default=False, validator=attr.validators.instance_of(bool))
763765
index = None
764766

765767
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._logger = logging.getLogger("Device: ")
2829
GpioDigitalOutput._assert_gpio_line_is_exported(index)
2930
gpio_sysfs_path = os.path.join(GpioDigitalOutput._gpio_sysfs_path_prefix,
@@ -40,6 +41,10 @@ def __init__(self, index):
4041
gpio_sysfs_value_path = os.path.join(gpio_sysfs_path, 'value')
4142
self.gpio_sysfs_value_fd = os.open(gpio_sysfs_value_path, flags=(os.O_RDWR | os.O_SYNC))
4243

44+
gpio_sysfs_active_low_path = os.path.join(gpio_sysfs_path, 'active_low')
45+
with open(gpio_sysfs_active_low_path, 'w') as active_low_fd:
46+
active_low_fd.write(str(int(invert)))
47+
4348
def __del__(self):
4449
os.close(self.gpio_sysfs_value_fd)
4550
self.gpio_sysfs_value_fd = None
@@ -66,24 +71,22 @@ def set(self, status):
6671

6772
os.write(self.gpio_sysfs_value_fd, binary_value)
6873

69-
7074
_gpios = {}
7175

72-
def _get_gpio_line(index):
76+
def _get_gpio_line(index, invert):
7377
if index not in _gpios:
74-
_gpios[index] = GpioDigitalOutput(index=index)
78+
_gpios[index] = GpioDigitalOutput(index=index, invert=invert)
7579
return _gpios[index]
7680

77-
def handle_set(index, status):
78-
gpio_line = _get_gpio_line(index)
81+
def handle_set(index, invert, status):
82+
gpio_line = _get_gpio_line(index, invert)
7983
gpio_line.set(status)
8084

8185

82-
def handle_get(index):
83-
gpio_line = _get_gpio_line(index)
86+
def handle_get(index, invert):
87+
gpio_line = _get_gpio_line(index, invert)
8488
return gpio_line.get()
8589

86-
8790
methods = {
8891
'set': handle_set,
8992
'get': handle_get,

0 commit comments

Comments
 (0)