Debian Codegraph @

bindings/cxx/examples/toggle_line_value.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 toggling a single line. */
5
6 #include <cstdlib>
7 #include <chrono>
8 #include <filesystem>
9 #include <gpiod.hpp>
10 #include <iostream>
11 #include <thread>
12
13 namespace {
14
15 /* Example configuration - customize to suit your situation. */
16 const ::std::filesystem::path chip_path("/dev/gpiochip0");
17 const ::gpiod::line::offset line_offset = 5;
18
19 ::gpiod::line::value toggle_value(::gpiod::line::value v)
20 {
21 return (v == ::gpiod::line::value::ACTIVE) ?
22 ::gpiod::line::value::INACTIVE :
23 ::gpiod::line::value::ACTIVE;
24 }
25
26 } /* namespace */
27
28 int main()
29 {
30 ::gpiod::line::value value = ::gpiod::line::value::ACTIVE;
31
32 auto request =
33 ::gpiod::chip(chip_path)
34 .prepare_request()
35 .set_consumer("toggle-line-value")
36 .add_line_settings(
37 line_offset,
38 ::gpiod::line_settings().set_direction(
39 ::gpiod::line::direction::OUTPUT))
40 .do_request();
41
42 for (;;) {
43 ::std::cout << line_offset << "=" << value << ::std::endl;
44
45 std::this_thread::sleep_for(std::chrono::seconds(1));
46 value = toggle_value(value);
47 request.set_value(line_offset, value);
48 }
49 }