mirror of
https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git
synced 2026-10-24 00:48:53 +03:00
bindings: python: ext: add ability to query line name
Investigation has shown that the construction of the Enum values to create the LineInfo object is expensive [0]. Add a method to retrieve the line name from a given offset without the overhead of generating a full LineInfo object. [0]: https://github.com/brgl/libgpiod/issues/149#issuecomment-3319448712 Signed-off-by: Vincent Fazio <vfazio@gmail.com> Link: https://lore.kernel.org/r/20251009130516.3729433-8-vfazio@gmail.com Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
This commit is contained in:
committed by
Bartosz Golaszewski
parent
e2d7b6abaf
commit
0c0993569c
@@ -47,6 +47,7 @@ class Chip:
|
||||
def get_info(self) -> ChipInfo: ...
|
||||
def line_offset_from_id(self, id: str) -> int: ...
|
||||
def get_line_info(self, offset: int, watch: bool) -> LineInfo: ...
|
||||
def get_line_name(self, offset: int) -> Optional[str]: ...
|
||||
def request_lines(
|
||||
self,
|
||||
line_cfg: LineConfig,
|
||||
|
||||
@@ -147,6 +147,32 @@ static PyObject *chip_get_line_info(chip_object *self, PyObject *args)
|
||||
return info_obj;
|
||||
}
|
||||
|
||||
static PyObject *chip_get_line_name(chip_object *self, PyObject *args)
|
||||
{
|
||||
int ret;
|
||||
unsigned int offset;
|
||||
struct gpiod_line_info *info;
|
||||
PyObject *line_name;
|
||||
const char *name;
|
||||
|
||||
ret = PyArg_ParseTuple(args, "I", &offset);
|
||||
if (!ret)
|
||||
return NULL;
|
||||
|
||||
Py_BEGIN_ALLOW_THREADS;
|
||||
info = gpiod_chip_get_line_info(self->chip, offset);
|
||||
Py_END_ALLOW_THREADS;
|
||||
if (!info)
|
||||
return Py_gpiod_SetErrFromErrno();
|
||||
|
||||
name = gpiod_line_info_get_name(info);
|
||||
line_name = (name == NULL) ? Py_None : PyUnicode_FromString(name);
|
||||
|
||||
gpiod_line_info_free(info);
|
||||
|
||||
return line_name;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
chip_unwatch_line_info(chip_object *self, PyObject *args)
|
||||
{
|
||||
@@ -333,6 +359,11 @@ static PyMethodDef chip_methods[] = {
|
||||
.ml_meth = (PyCFunction)chip_request_lines,
|
||||
.ml_flags = METH_VARARGS,
|
||||
},
|
||||
{
|
||||
.ml_name = "get_line_name",
|
||||
.ml_meth = (PyCFunction)chip_get_line_name,
|
||||
.ml_flags = METH_VARARGS,
|
||||
},
|
||||
{ }
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user