| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | // SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> |
| 3 | |
| 4 | /* |
| 5 | * Example of a bi-directional line requested as input and then switched |
| 6 | * to output. |
| 7 | */ |
| 8 | |
| 9 | #include <cstdlib> |
| 10 | #include <filesystem> |
| 11 | #include <gpiod.hpp> |
| 12 | #include <iostream> |
| 13 | |
| 14 | namespace { |
| 15 | |
| 16 | /* Example configuration - customize to suit your situation */ |
| 17 | const ::std::filesystem::path chip_path("/dev/gpiochip0"); |
| 18 | const ::gpiod::line::offset line_offset = 5; |
| 19 | |
| 20 | } /* namespace */ |
| 21 | |
| 22 | int main() |
| 23 | { |
| 24 | /* request the line initially as an input */ |
| 25 | auto request = ::gpiod::chip(chip_path) |
| 26 | .prepare_request() |
| 27 | .set_consumer("reconfigure-input-to-output") |
| 28 | .add_line_settings( |
| 29 | line_offset, |
| 30 | ::gpiod::line_settings().set_direction( |
| 31 | ::gpiod::line::direction::INPUT)) |
| 32 | .do_request(); |
| 33 | |
| 34 | /* read the current line value */ |
| 35 | ::std::cout << line_offset << "=" |
| 36 | << (request.get_value(line_offset) == |
| 37 | ::gpiod::line::value::ACTIVE ? |
| 38 | "Active" : |
| 39 | "Inactive") |
| 40 | << " (input)" << ::std::endl; |
| 41 | |
| 42 | /* switch the line to an output and drive it low */ |
| 43 | request.reconfigure_lines(::gpiod::line_config().add_line_settings( |
| 44 | line_offset, |
| 45 | ::gpiod::line_settings() |
| 46 | .set_direction(::gpiod::line::direction::OUTPUT) |
| 47 | .set_output_value(::gpiod::line::value::INACTIVE))); |
| 48 | |
| 49 | /* report the current driven value */ |
| 50 | ::std::cout << line_offset << "=" |
| 51 | << (request.get_value(line_offset) == |
| 52 | ::gpiod::line::value::ACTIVE ? |
| 53 | "Active" : |
| 54 | "Inactive") |
| 55 | << " (output)" << ::std::endl; |
| 56 | |
| 57 | return EXIT_SUCCESS; |
| 58 | } |