Skip to content

Commit

Permalink
Merge pull request #47 from tiagocoutinho/gpio-tests
Browse files Browse the repository at this point in the history
Gpio fixes find
  • Loading branch information
tiagocoutinho authored Oct 12, 2024
2 parents 6a9dd69 + a719d49 commit 5cbaae3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
![License][license]
[![CI][CI]](https://github.com/tiagocoutinho/linuxpy/actions/workflows/ci.yml)

[![Source][source]](https://github.com/tiagocoutinho/linuxpy/)
[![Documentation][documentation]](https://tiagocoutinho.github.io/linuxpy/)

Human friendly interface to linux subsystems using python.

Provides python access to several linux subsystems like V4L2, GPIO, Led, thermal,
Expand Down Expand Up @@ -348,3 +351,5 @@ $ python -m linuxpy.midi.cli listen 0:1 14:0
[pypi-status]: https://img.shields.io/pypi/status/linuxpy.svg
[license]: https://img.shields.io/pypi/l/linuxpy.svg
[CI]: https://github.com/tiagocoutinho/linuxpy/actions/workflows/ci.yml/badge.svg
[documentation]: https://img.shields.io/badge/Documentation-blue?color=grey&logo=mdBook
[source]: https://img.shields.io/badge/Source-grey?logo=git
5 changes: 5 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ hide:
[![License][license]]()
[![CI][CI]](https://github.com/tiagocoutinho/linuxpy/actions/workflows/ci.yml)

[![Source][source]](https://github.com/tiagocoutinho/linuxpy/)
[![Documentation][documentation]](https://tiagocoutinho.github.io/linuxpy/)

Human friendly interface to linux subsystems using python.

Provides python access to several linux subsystems like V4L2, input, GPIO and MIDI.
Expand Down Expand Up @@ -101,3 +104,5 @@ with providing a common API on different platforms.
[pypi-status]: https://img.shields.io/pypi/status/linuxpy.svg
[license]: https://img.shields.io/pypi/l/linuxpy.svg
[CI]: https://github.com/tiagocoutinho/linuxpy/actions/workflows/ci.yml/badge.svg
[documentation]: https://img.shields.io/badge/Documentation-blue?color=grey&logo=mdBook
[source]: https://img.shields.io/badge/Source-grey?logo=git
15 changes: 14 additions & 1 deletion linuxpy/gpio/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ def set_values(self, values: dict[int, Union[int, bool]]):


class Device(BaseDevice):
"""
A device represents a connection to the underlying gpio chip
"""

PREFIX = "/dev/gpiochip"

def __len__(self) -> int:
Expand All @@ -306,6 +312,13 @@ def __getitem__(self, key: Union[int, tuple, slice]) -> Request:
return self.request(lines)

def get_info(self) -> Info:
"""
Reads all information available including chip info and detailed information
about each chip line information
Returns:
Info: The full chip information
"""
return get_info(self)

def request(self, lines: Optional[Sequence[int]] = None, name: str = "", flags: LineFlag = LineFlag(0)) -> Request:
Expand All @@ -324,4 +337,4 @@ def iter_devices(path: PathLike = "/dev", **kwargs) -> Iterable[Device]:
return (Device(name, **kwargs) for name in iter_gpio_files(path=path))


find = make_find(iter_devices)
find = make_find(iter_devices, needs_open=False)
1 change: 1 addition & 0 deletions scripts/setup-gpio-sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def Line(name, direction=None):
Line("L-I2", "input"),
Line("L-O0", "output-high"),
Line("L-O1", "output-low"),
Line("L-O2"),
]
},
],
Expand Down
13 changes: 13 additions & 0 deletions tests/test_gpio.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,31 +437,36 @@ def _():
assert l0.flags == raw.LineFlag.INPUT
assert l0.name == "L-I0"
assert l0.consumer == ""
assert l0.offset == 0
assert l0.attributes.flags == 0
assert isclose(l0.attributes.debounce_period, 0)

l1 = info.lines[1]
assert l1.flags == raw.LineFlag.INPUT
assert l1.name == "L-I1"
assert l1.consumer == ""
assert l1.offset == 1
assert l1.attributes.flags == 0

l2 = info.lines[2]
assert l2.flags == raw.LineFlag.USED | raw.LineFlag.INPUT
assert l2.name == "L-I2"
assert l2.consumer == "L-I2-hog"
assert l2.offset == 2
assert l2.attributes.flags == 0

l3 = info.lines[3]
assert l3.flags == raw.LineFlag.USED | raw.LineFlag.OUTPUT
assert l3.name == "L-O0"
assert l3.consumer == "L-O0-hog"
assert l3.offset == 3
assert l3.attributes.flags == 0

l4 = info.lines[4]
assert l4.flags == raw.LineFlag.USED | raw.LineFlag.OUTPUT
assert l4.name == "L-O1"
assert l4.consumer == "L-O1-hog"
assert l4.offset == 4
assert l4.attributes.flags == 0


Expand Down Expand Up @@ -495,6 +500,14 @@ def _():
# close the request to make sure it always succeeds
request.close()

with device.request([5, 12], "myself", raw.LineFlag.OUTPUT) as request:
info = device.get_info()
l5 = info.lines[5]
assert l5.flags == raw.LineFlag.USED | raw.LineFlag.OUTPUT
assert l5.name == "L-O2"
assert l5.consumer == "myself"
assert l5.attributes.flags == 0


@test("sim get value")
def _():
Expand Down

0 comments on commit 5cbaae3

Please sign in to comment.