@@ -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,
+ },
{ }
};
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> --- bindings/python/gpiod/_ext.pyi | 1 + bindings/python/gpiod/ext/chip.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+)