| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | // SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> |
| 3 | |
| 4 | /* Minimal example of watching for requests on particular lines. */ |
| 5 | |
| 6 | #include <cstdlib> |
| 7 | #include <gpiod.hpp> |
| 8 | #include <iomanip> |
| 9 | #include <iostream> |
| 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 | const char *event_type(const ::gpiod::info_event &event) |
| 18 | { |
| 19 | switch (event.type()) { |
| 20 | case ::gpiod::info_event::event_type::LINE_REQUESTED: |
| 21 | return "Requested"; |
| 22 | case ::gpiod::info_event::event_type::LINE_RELEASED: |
| 23 | return "Released"; |
| 24 | case ::gpiod::info_event::event_type::LINE_CONFIG_CHANGED: |
| 25 | return "Reconfig"; |
| 26 | default: |
| 27 | return "Unknown"; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | } /* namespace */ |
| 32 | |
| 33 | int main() |
| 34 | { |
| 35 | ::gpiod::chip chip(chip_path); |
| 36 | |
| 37 | for (auto offset :line_offsets) |
| 38 | chip.watch_line_info(offset); |
| 39 | |
| 40 | for (;;) { |
| 41 | /* Blocks until at least one event is available */ |
| 42 | auto event = chip.read_info_event(); |
| 43 | ::std::cout << "line: " << event.get_line_info().offset() << " " |
| 44 | << ::std::setw(9) << ::std::left |
| 45 | << event_type(event) << " " |
| 46 | << event.timestamp_ns() / 1000000000 << "." |
| 47 | << event.timestamp_ns() % 1000000000 << ::std::endl; |
| 48 | } |
| 49 | } |