Debian Codegraph @

bindings/cxx/examples/find_line_by_name.cpp

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]>
3
4 /* Minimal example of finding a line with the given name. */
5
6 #include <cstdlib>
7 #include <filesystem>
8 #include <gpiod.hpp>
9 #include <iostream>
10
11 namespace {
12
13 /* Example configuration - customize to suit your situation */
14 const ::std::string line_name = "GPIO19";
15
16 } /* namespace */
17
18 int main()
19 {
20 /*
21 * Names are not guaranteed unique, so this finds the first line with
22 * the given name.
23 */
24 for (const auto &entry :
25 ::std::filesystem::directory_iterator("/dev/")) {
26 if (::gpiod::is_gpiochip_device(entry.path())) {
27 ::gpiod::chip chip(entry.path());
28
29 auto offset = chip.get_line_offset_from_name(line_name);
30 if (offset >= 0) {
31 ::std::cout << line_name << ": "
32 << chip.get_info().name() << " "
33 << offset << ::std::endl;
34 return EXIT_SUCCESS;
35 }
36 }
37 }
38 ::std::cout << "line '" << line_name << "' not found" << ::std::endl;
39
40 return EXIT_FAILURE;
41 }