The purpose of the example is demonstrate using a request with poll().
It provides a hint as to how the poll can be combined with other fds but,
as Python comes with batteries included, the Python version of the example
can be readily extended to actually demonstrate this, as well as how it
can be used in multi-threaded environments.
Extend the example to use an eventfd to allow the poll() to be run in
a background thread and be gracefully terminated by the main thread.
Signed-off-by: Kent Gibson <warthog618@gmail.com>
Link: https://lore.kernel.org/r/20240917125455.324551-1-warthog618@gmail.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
One of the example scripts is named differently than what the Makefile
lists it as. Change the EXTRA_DIST variable to fix it.
Fixes: 96c52d3e59 ("bindings: python: examples: replace tools examples with use case examples")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
A collection of minor changes to be more consistent with other examples:
- capitalize comments
- add line offset to value outputs
- drop comma from edge event outputs
- improve behaviour if run on a platform that does not match the
example configuration
- use with to cleanup request in toggle_line_value.py
Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Add new example scripts to EXTRA_DIST in the Makefile so that they end up
being shipped with the rest of the code.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
gpioset.py requests lines without setting their output value, and so
sets them all inactive, and subsequently sets them to their requested
value. This can result in glitches on lines which were active and
are set active.
As this is example code, it is also important to demonstrate that the
output value can be set by the request itself.
Request the lines with the correct output values set in the request
itself.
Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Add a new Python code examples - gpionotify.py - which is a simplified
reimplementation of the gpionotify tool.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
The read_edge_event() family of functions should actually be called
read_edge_events() as they universally allow to read more than one
event. We're converting wait_edge_event() too for consistency.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
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>
We're preparing to entirely remove the line objects from the API and
split their functionality between two new objects: line_info and
line_request. The lookup functions must be limited in the process.
This reworks all the find_line methods to: a) always assume that names
looked for are unique within a single chip (because while it's
technically possible for GPIO line names to be non-unique, it doesn't
make sense to look for two lines named the same) and b) return the
hardware offset within the chip instead of the line object.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
LGPL-2.1 is a license meant for shared libraries. Because I didn't know
any better I used it for all files in the repository, including programs
linking against libgpiod. The standard approach for many similar projects
is to use LGPL for library code and GPL for programs (and also Makefiles
and other files containing code or configuration).
Relicense all programs, tests, makefiles and autotools files under
GPL-2.0 or later.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Acked-by: Kent Gibson <warthog618@gmail.com>
In order to make the licensing situation of libgpiod clear and enable
easy reusing of the code, let's make all the files in the repository
compliant with the REUSE v3.0 specification.
In order to achieve that:
- put all used licenses into a separate LICENSES/ directory
- add SPDX license identifiers to all files that were missing them with
regular text files being licensed under CC-BY-SA-4.0, while files
containing code and configuration are licensed under GPL-2.0-or-later
- replace custom copyright texts with SPDX-FileCopyrightText
- update the gpio.h kernel uAPI header with the upstream version where
the SDPX license identifier was fixed to use 'GPL-2.0-only' variant
instead of the deprecated 'GPL-2.0'
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Acked-by: Kent Gibson <warthog618@gmail.com>
Unlike line direction - where input and output modes are equal, no signal
inversion (active-high) is the natural state of the line while active-low
is less likely. Let's drop the ACTIVE_STATE enum treewide and provide a
boolean property for lines - is_active_low() - to reflect that fact. This
function returning false means the line is "active-high".
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Chip iterators require the user to have permission to access all GPIO
chips. They also don't take into account symbolic links. In general
they're badly designed so remove them treewide in favor of scanning /dev
manually using the provided gpiod_is_gpiochip_device() helper.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Don't put a tab between the asignment and the first string - just move
the whole list one line below.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
The examples are programs meant to be executed directly and not
imported. Use the standard "if __name__ == '__main__':" before
any code.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
After setting the line values in gpioset.py: don't exit until the user
presses ENTER. This makes the example work the same as the 'wait' mode
in the original gpioset utility.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
In gpioset.py: when calling lines.set_values(), there's no point in
storing the return value as it's always None. It's also not used
anywhere.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Modify the gpiomon.py example to describe the detected events in
a format similar to the one used by the original gpiomon tool.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Currently the only tests we have for python bindings are in a simple
script that makes a lot of assumptions about the environment and
doesn't even work anymore with recent kernels.
Remove it and replace it with a proper test-suite implemented using
libgpiod and the standard python unittest package.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Currently we're not checking if the object returned from
gpiod.fine_line() is None which results in an attribute error being
raised when we try to call line methods on a non-line object.
Add an if and bail-out with an error code if the line with given name
was not found.
Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
Currently if we call gpiod_Chip_find_lines() with some names that
cannot be looked up, we end up getting an exception from the LineBulk
object's constructor (because None is not a Line and LineBulk can only
hold Line objects).
Fix the behavior of gpiod_Chip_find_lines() for nonexistent lines by
raising an explicit exception if the underlying gpiod_Chip_find_line()
call returns None. Also: add a relevant test case.
Fixes: 96c524c495 ("bindings: implement python bindings")
Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
Unlike the C++ bindings: we can't expect the python runtime to call
the Chip object's destructor when all references are dropped. We need
to close each chip explicitly. Either call the close() method directly
or use controlled execution in all examples.
Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
Add two new tests verifying the default_vals argument in line requests
for single and multiple lines.
Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
Strings with slashes trigger a warning in the kernel in
fs/proc/generic.c. Use a hardcoded consumer string.
Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
Provide __enter__ and __exit__ callbacks for Chip so that it can be
used with the 'with' statement.
Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
So far we rely on cpython's reference counting for closing the GPIO
chips and freeing the underlying resources. This is however wrong.
Python doesn't guarantee anything regarding the lifetime of an object.
Provide a method for closing the underlying chip handle and add
relevant checks to all methods using the resources associated with
a GPIO chip, so that we raise an error should the user use a chip
after closing it.
Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
Add more test cases/examples, implement some helper functions, tweak
the output messages, mention the need for a dummy chip in the system
in the module's pydoc string.
Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
Create a C extension module wrapping the functionality of libgpiod
in a set of Python classes.
Add code examples including simplified reimplementations of gpio-tools
in Python.
Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>