Skip to content

Commit c805633

Browse files
committed
GPIODigitalOutputDriver: implement more protocol classes
Update the GPIODigitalOutputDriver to also implement the ResetProtocol, PowerProtocol, and ButtonProtocol. Update the GpioDigitalOutput agent to use the new invert( active-low) attribute Signed-off-by: Perry Melange <[email protected]>
1 parent 110e7f5 commit c805633

File tree

2 files changed

+62
-11
lines changed

2 files changed

+62
-11
lines changed

labgrid/driver/gpiodriver.py

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"""All GPIO-related drivers"""
22
import attr
3+
import time
34

45
from ..factory import target_factory
5-
from ..protocol import DigitalOutputProtocol
6+
from ..protocol import DigitalOutputProtocol, ResetProtocol, PowerProtocol, ButtonProtocol
67
from ..resource.remote import NetworkSysfsGPIO
78
from ..step import step
89
from .common import Driver
@@ -11,11 +12,12 @@
1112

1213
@target_factory.reg_driver
1314
@attr.s(eq=False)
14-
class GpioDigitalOutputDriver(Driver, DigitalOutputProtocol):
15+
class GpioDigitalOutputDriver(Driver, DigitalOutputProtocol, ResetProtocol, PowerProtocol, ButtonProtocol):
1516

1617
bindings = {
1718
"gpio": {"SysfsGPIO", "NetworkSysfsGPIO"},
1819
}
20+
delay = attr.ib(default=1.0, validator=attr.validators.instance_of(float))
1921

2022
def __attrs_post_init__(self):
2123
super().__attrs_post_init__()
@@ -37,9 +39,53 @@ def on_deactivate(self):
3739
@Driver.check_active
3840
@step(args=['status'])
3941
def set(self, status):
40-
self.proxy.set(self.gpio.index, status)
42+
self.proxy.set(self.gpio.index, self.gpio.invert, status)
4143

4244
@Driver.check_active
4345
@step(result=True)
4446
def get(self):
45-
return self.proxy.get(self.gpio.index)
47+
return self.proxy.get(self.gpio.index, self.gpio.invert)
48+
49+
@Driver.check_active
50+
@step(result=True)
51+
def invert(self):
52+
self.set(not self.get())
53+
54+
@Driver.check_active
55+
@step(result=True)
56+
def reset(self):
57+
self.cycle()
58+
59+
@Driver.check_active
60+
@step(result=True)
61+
def on(self):
62+
self.set(True)
63+
64+
@Driver.check_active
65+
@step(result=True)
66+
def off(self):
67+
self.set(False)
68+
69+
@Driver.check_active
70+
@step(result=True)
71+
def cycle(self):
72+
self.off()
73+
time.sleep(self.delay)
74+
self.on()
75+
76+
@Driver.check_active
77+
@step(result=True)
78+
def press(self):
79+
self.set(True)
80+
81+
@Driver.check_active
82+
@step(result=True)
83+
def release(self):
84+
self.set(False)
85+
86+
@Driver.check_active
87+
@step(result=True)
88+
def press_for(self):
89+
self.press()
90+
time.sleep(self.delay)
91+
self.release()

labgrid/util/agents/sysfsgpio.py

Lines changed: 12 additions & 7 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
@@ -69,18 +74,18 @@ def set(self, status):
6974

7075
_gpios = {}
7176

72-
def _get_gpio_line(index):
77+
def _get_gpio_line(index, invert):
7378
if index not in _gpios:
74-
_gpios[index] = GpioDigitalOutput(index=index)
79+
_gpios[index] = GpioDigitalOutput(index=index, invert=invert)
7580
return _gpios[index]
7681

77-
def handle_set(index, status):
78-
gpio_line = _get_gpio_line(index)
82+
def handle_set(index, invert, status):
83+
gpio_line = _get_gpio_line(index, invert)
7984
gpio_line.set(status)
8085

8186

82-
def handle_get(index):
83-
gpio_line = _get_gpio_line(index)
87+
def handle_get(index, invert):
88+
gpio_line = _get_gpio_line(index, invert)
8489
return gpio_line.get()
8590

8691

0 commit comments

Comments
 (0)