1
0
mirror of https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git synced 2026-10-25 12:39:18 +03:00
Files
libgpiod/bindings/python/tests/tests_line_info.py
Bartosz Golaszewski b7ba732e6a treewide: libgpiod v2 implementation
This commit is the result of a squash of all the commits created during
the development of libgpiod v2. This is done to preserve bisectability
of the git tree. It contains significant re-writes of the core C library
as well as C++ and python bindings. The tools have been adjusted to work
with the new API but have not undergone significant changes yet.

This commit contains the bulk of the work on libgpiod v2 but the
development continues with more changes planned.

Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
[Ben: fix format specifier for uint64_t]
Signed-off-by: Ben Hutchings <ben.hutchings@mind.be>
[Viresh: Fix ioctl number for gpiod_line_request_reconfigure_lines()]
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
[Kent: bug fixes, improvements, better pointer contracts, type strictness]
Signed-off-by: Kent Gibson <warthog618@gmail.com>
2022-10-31 11:15:14 +01:00

102 lines
3.4 KiB
Python

# SPDX-License-Identifier: GPL-2.0-or-later
# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@bgdev.pl>
import errno
import gpiod
import unittest
from . import gpiosim
from gpiod.line import Direction, Bias, Drive, Clock
HogDir = gpiosim.Chip.Direction
class GetLineInfo(unittest.TestCase):
def setUp(self):
self.sim = gpiosim.Chip(
num_lines=4,
line_names={0: "foobar"},
)
self.chip = gpiod.Chip(self.sim.dev_path)
def tearDown(self):
self.chip.close()
self.chip = None
self.sim = None
def test_get_line_info_by_offset(self):
self.chip.get_line_info(0)
def test_get_line_info_by_offset_keyword(self):
self.chip.get_line_info(line=0)
def test_get_line_info_by_name(self):
self.chip.get_line_info("foobar")
def test_get_line_info_by_name_keyword(self):
self.chip.get_line_info(line="foobar")
def test_get_line_info_by_offset_string(self):
self.chip.get_line_info("2")
def test_offset_out_of_range(self):
with self.assertRaises(ValueError) as ex:
self.chip.get_line_info(4)
def test_no_offset(self):
with self.assertRaises(TypeError):
self.chip.get_line_info()
class LinePropertiesCanBeRead(unittest.TestCase):
def test_basic_properties(self):
sim = gpiosim.Chip(
num_lines=8,
line_names={1: "foo", 2: "bar", 4: "baz", 5: "xyz"},
hogs={3: ("hog3", HogDir.OUTPUT_HIGH), 4: ("hog4", HogDir.OUTPUT_LOW)},
)
with gpiod.Chip(sim.dev_path) as chip:
info4 = chip.get_line_info(4)
info6 = chip.get_line_info(6)
self.assertEqual(info4.offset, 4)
self.assertEqual(info4.name, "baz")
self.assertTrue(info4.used)
self.assertEqual(info4.consumer, "hog4")
self.assertEqual(info4.direction, Direction.OUTPUT)
self.assertFalse(info4.active_low)
self.assertEqual(info4.bias, Bias.UNKNOWN)
self.assertEqual(info4.drive, Drive.PUSH_PULL)
self.assertEqual(info4.event_clock, Clock.MONOTONIC)
self.assertFalse(info4.debounced)
self.assertEqual(info4.debounce_period.total_seconds(), 0.0)
self.assertEqual(info6.offset, 6)
self.assertEqual(info6.name, None)
self.assertFalse(info6.used)
self.assertEqual(info6.consumer, None)
self.assertEqual(info6.direction, Direction.INPUT)
self.assertFalse(info6.active_low)
self.assertEqual(info6.bias, Bias.UNKNOWN)
self.assertEqual(info6.drive, Drive.PUSH_PULL)
self.assertEqual(info6.event_clock, Clock.MONOTONIC)
self.assertFalse(info6.debounced)
self.assertEqual(info6.debounce_period.total_seconds(), 0.0)
class LineInfoStringRepresentation(unittest.TestCase):
def test_line_info_str(self):
sim = gpiosim.Chip(
line_names={0: "foo"}, hogs={0: ("hogger", HogDir.OUTPUT_HIGH)}
)
with gpiod.Chip(sim.dev_path) as chip:
info = chip.get_line_info(0)
self.assertEqual(
str(info),
'<LineInfo offset=0 name="foo" used=True consumer="hogger" direction=Direction.OUTPUT active_low=False bias=Bias.UNKNOWN drive=Drive.PUSH_PULL edge_detection=Edge.NONE event_clock=Clock.MONOTONIC debounced=False debounce_period=0:00:00>',
)