| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | // SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> |
| 3 | |
| 4 | /* Minimal example of toggling multiple lines. */ |
| 5 | |
| 6 | #include <cstdlib> |
| 7 | #include <gpiod.hpp> |
| 8 | #include <iostream> |
| 9 | #include <thread> |
| 10 | |
| 11 | namespace { |
| 12 | |
| 13 | /* Example configuration - customize to suit your situation */ |
| 14 | const ::std::filesystem::path chip_path("/dev/gpiochip0"); |
| 15 | const ::gpiod::line::offsets line_offsets = { 5, 3, 7 }; |
| 16 | |
| 17 | ::gpiod::line::value toggle_value(::gpiod::line::value v) |
| 18 | { |
| 19 | return (v == ::gpiod::line::value::ACTIVE) ? |
| 20 | ::gpiod::line::value::INACTIVE : |
| 21 | ::gpiod::line::value::ACTIVE; |
| 22 | } |
| 23 | |
| 24 | void toggle_values(::gpiod::line::values &values) |
| 25 | { |
| 26 | for (size_t i = 0; i < values.size(); i++) |
| 27 | values[i] = toggle_value(values[i]); |
| 28 | } |
| 29 | |
| 30 | void print_values(::gpiod::line::offsets const &offsets, |
| 31 | ::gpiod::line::values const &values) |
| 32 | { |
| 33 | for (size_t i = 0; i < offsets.size(); i++) |
| 34 | ::std::cout << offsets[i] << "=" << values[i] << ' '; |
| 35 | ::std::cout << ::std::endl; |
| 36 | } |
| 37 | |
| 38 | } /* namespace */ |
| 39 | |
| 40 | int main() |
| 41 | { |
| 42 | ::gpiod::line::values values = { ::gpiod::line::value::ACTIVE, |
| 43 | ::gpiod::line::value::ACTIVE, |
| 44 | ::gpiod::line::value::INACTIVE }; |
| 45 | |
| 46 | auto request = |
| 47 | ::gpiod::chip(chip_path) |
| 48 | .prepare_request() |
| 49 | .set_consumer("toggle-multiple-line-values") |
| 50 | .add_line_settings( |
| 51 | line_offsets, |
| 52 | ::gpiod::line_settings().set_direction( |
| 53 | ::gpiod::line::direction::OUTPUT)) |
| 54 | .set_output_values(values) |
| 55 | .do_request(); |
| 56 | |
| 57 | for (;;) { |
| 58 | print_values(line_offsets, values); |
| 59 | std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 60 | toggle_values(values); |
| 61 | request.set_values(line_offsets, values); |
| 62 | } |
| 63 | } |