| 1 | // SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 | // SPDX-FileCopyrightText: 2021-2022 Bartosz Golaszewski <[email protected]> |
| 3 | |
| 4 | #include <iterator> |
| 5 | #include <ostream> |
| 6 | |
| 7 | #include "internal.hpp" |
| 8 | |
| 9 | namespace gpiod { |
| 10 | namespace line { |
| 11 | |
| 12 | namespace { |
| 13 | |
| 14 | const ::std::map<line::value, ::std::string> value_names = { |
| 15 | { line::value::INACTIVE, "INACTIVE" }, |
| 16 | { line::value::ACTIVE, "ACTIVE" }, |
| 17 | }; |
| 18 | |
| 19 | const ::std::map<line::direction, ::std::string> direction_names = { |
| 20 | { line::direction::AS_IS, "AS_IS" }, |
| 21 | { line::direction::INPUT, "INPUT" }, |
| 22 | { line::direction::OUTPUT, "OUTPUT" }, |
| 23 | }; |
| 24 | |
| 25 | const ::std::map<line::bias, ::std::string> bias_names = { |
| 26 | { line::bias::AS_IS, "AS_IS" }, |
| 27 | { line::bias::UNKNOWN, "UNKNOWN" }, |
| 28 | { line::bias::DISABLED, "DISABLED" }, |
| 29 | { line::bias::PULL_UP, "PULL_UP" }, |
| 30 | { line::bias::PULL_DOWN, "PULL_DOWN" }, |
| 31 | }; |
| 32 | |
| 33 | const ::std::map<line::drive, ::std::string> drive_names = { |
| 34 | { line::drive::PUSH_PULL, "PUSH_PULL" }, |
| 35 | { line::drive::OPEN_DRAIN, "OPEN_DRAIN" }, |
| 36 | { line::drive::OPEN_SOURCE, "OPEN_SOURCE" }, |
| 37 | }; |
| 38 | |
| 39 | const ::std::map<line::edge, ::std::string> edge_names = { |
| 40 | { line::edge::NONE, "NONE" }, |
| 41 | { line::edge::RISING, "RISING_EDGE" }, |
| 42 | { line::edge::FALLING, "FALLING_EDGE" }, |
| 43 | { line::edge::BOTH, "BOTH_EDGES" }, |
| 44 | }; |
| 45 | |
| 46 | const ::std::map<line::clock, ::std::string> clock_names = { |
| 47 | { line::clock::MONOTONIC, "MONOTONIC" }, |
| 48 | { line::clock::REALTIME, "REALTIME" }, |
| 49 | { line::clock::HTE, "HTE" }, |
| 50 | }; |
| 51 | |
| 52 | } /* namespace */ |
| 53 | |
| 54 | GPIOD_CXX_API ::std::ostream& operator<<(::std::ostream& out, line::value val) |
| 55 | { |
| 56 | out << value_names.at(val); |
| 57 | |
| 58 | return out; |
| 59 | } |
| 60 | |
| 61 | GPIOD_CXX_API ::std::ostream& operator<<(::std::ostream& out, line::direction dir) |
| 62 | { |
| 63 | out << direction_names.at(dir); |
| 64 | |
| 65 | return out; |
| 66 | } |
| 67 | |
| 68 | GPIOD_CXX_API ::std::ostream& operator<<(::std::ostream& out, line::edge edge) |
| 69 | { |
| 70 | out << edge_names.at(edge); |
| 71 | |
| 72 | return out; |
| 73 | } |
| 74 | |
| 75 | GPIOD_CXX_API ::std::ostream& operator<<(::std::ostream& out, line::bias bias) |
| 76 | { |
| 77 | out << bias_names.at(bias); |
| 78 | |
| 79 | return out; |
| 80 | } |
| 81 | |
| 82 | GPIOD_CXX_API ::std::ostream& operator<<(::std::ostream& out, line::drive drive) |
| 83 | { |
| 84 | out << drive_names.at(drive); |
| 85 | |
| 86 | return out; |
| 87 | } |
| 88 | |
| 89 | GPIOD_CXX_API ::std::ostream& operator<<(::std::ostream& out, line::clock clock) |
| 90 | { |
| 91 | out << clock_names.at(clock); |
| 92 | |
| 93 | return out; |
| 94 | } |
| 95 | |
| 96 | template<typename T> |
| 97 | ::std::ostream& insert_vector(::std::ostream& out, |
| 98 | const ::std::string& name, const ::std::vector<T>& vec) |
| 99 | { |
| 100 | out << name << "("; |
| 101 | ::std::copy(vec.begin(), ::std::prev(vec.end()), |
| 102 | ::std::ostream_iterator<T>(out, ", ")); |
| 103 | out << vec.back(); |
| 104 | out << ")"; |
| 105 | |
| 106 | return out; |
| 107 | } |
| 108 | |
| 109 | GPIOD_CXX_API ::std::ostream& operator<<(::std::ostream& out, const offsets& offs) |
| 110 | { |
| 111 | return insert_vector(out, "gpiod::offsets", offs); |
| 112 | } |
| 113 | |
| 114 | GPIOD_CXX_API ::std::ostream& operator<<(::std::ostream& out, const line::values& vals) |
| 115 | { |
| 116 | return insert_vector(out, "gpiod::values", vals); |
| 117 | } |
| 118 | |
| 119 | GPIOD_CXX_API ::std::ostream& operator<<(::std::ostream& out, const line::value_mapping& mapping) |
| 120 | { |
| 121 | out << "gpiod::value_mapping(" << mapping.first << ": " << mapping.second << ")"; |
| 122 | |
| 123 | return out; |
| 124 | } |
| 125 | |
| 126 | GPIOD_CXX_API ::std::ostream& operator<<(::std::ostream& out, const line::value_mappings& mappings) |
| 127 | { |
| 128 | return insert_vector(out, "gpiod::value_mappings", mappings); |
| 129 | } |
| 130 | |
| 131 | } /* namespace line */ |
| 132 | } /* namespace gpiod */ |