| 1 | // SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 | // SPDX-FileCopyrightText: 2021-2022 Bartosz Golaszewski <[email protected]> |
| 3 | |
| 4 | #include <map> |
| 5 | #include <ostream> |
| 6 | #include <utility> |
| 7 | |
| 8 | #include "internal.hpp" |
| 9 | |
| 10 | namespace gpiod { |
| 11 | |
| 12 | namespace { |
| 13 | |
| 14 | const ::std::map<int, line::direction> direction_mapping = { |
| 15 | { GPIOD_LINE_DIRECTION_INPUT, line::direction::INPUT }, |
| 16 | { GPIOD_LINE_DIRECTION_OUTPUT, line::direction::OUTPUT }, |
| 17 | }; |
| 18 | |
| 19 | const ::std::map<int, line::bias> bias_mapping = { |
| 20 | { GPIOD_LINE_BIAS_UNKNOWN, line::bias::UNKNOWN }, |
| 21 | { GPIOD_LINE_BIAS_DISABLED, line::bias::DISABLED }, |
| 22 | { GPIOD_LINE_BIAS_PULL_UP, line::bias::PULL_UP }, |
| 23 | { GPIOD_LINE_BIAS_PULL_DOWN, line::bias::PULL_DOWN }, |
| 24 | }; |
| 25 | |
| 26 | const ::std::map<int, line::drive> drive_mapping = { |
| 27 | { GPIOD_LINE_DRIVE_PUSH_PULL, line::drive::PUSH_PULL }, |
| 28 | { GPIOD_LINE_DRIVE_OPEN_DRAIN, line::drive::OPEN_DRAIN }, |
| 29 | { GPIOD_LINE_DRIVE_OPEN_SOURCE, line::drive::OPEN_SOURCE }, |
| 30 | }; |
| 31 | |
| 32 | const ::std::map<int, line::edge> edge_mapping = { |
| 33 | { GPIOD_LINE_EDGE_NONE, line::edge::NONE }, |
| 34 | { GPIOD_LINE_EDGE_RISING, line::edge::RISING }, |
| 35 | { GPIOD_LINE_EDGE_FALLING, line::edge::FALLING }, |
| 36 | { GPIOD_LINE_EDGE_BOTH, line::edge::BOTH }, |
| 37 | }; |
| 38 | |
| 39 | const ::std::map<int, line::clock> clock_mapping = { |
| 40 | { GPIOD_LINE_CLOCK_MONOTONIC, line::clock::MONOTONIC }, |
| 41 | { GPIOD_LINE_CLOCK_REALTIME, line::clock::REALTIME }, |
| 42 | { GPIOD_LINE_CLOCK_HTE, line::clock::HTE }, |
| 43 | }; |
| 44 | |
| 45 | } /* namespace */ |
| 46 | |
| 47 | void line_info::impl::set_info_ptr(line_info_ptr& new_info) |
| 48 | { |
| 49 | this->info = ::std::move(new_info); |
| 50 | } |
| 51 | |
| 52 | line_info::line_info() |
| 53 | : _m_priv(new impl) |
| 54 | { |
| 55 | |
| 56 | } |
| 57 | |
| 58 | GPIOD_CXX_API line_info::line_info(const line_info& other) noexcept |
| 59 | : _m_priv(other._m_priv) |
| 60 | { |
| 61 | |
| 62 | } |
| 63 | |
| 64 | GPIOD_CXX_API line_info::line_info(line_info&& other) noexcept |
| 65 | : _m_priv(::std::move(other._m_priv)) |
| 66 | { |
| 67 | |
| 68 | } |
| 69 | |
| 70 | GPIOD_CXX_API line_info::~line_info() |
| 71 | { |
| 72 | |
| 73 | } |
| 74 | |
| 75 | GPIOD_CXX_API line_info& line_info::operator=(const line_info& other) noexcept |
| 76 | { |
| 77 | this->_m_priv = other._m_priv; |
| 78 | |
| 79 | return *this; |
| 80 | } |
| 81 | |
| 82 | GPIOD_CXX_API line_info& line_info::operator=(line_info&& other) noexcept |
| 83 | { |
| 84 | this->_m_priv = ::std::move(other._m_priv); |
| 85 | |
| 86 | return *this; |
| 87 | } |
| 88 | |
| 89 | GPIOD_CXX_API line::offset line_info::offset() const noexcept |
| 90 | { |
| 91 | return ::gpiod_line_info_get_offset(this->_m_priv->info.get()); |
| 92 | } |
| 93 | |
| 94 | GPIOD_CXX_API ::std::string line_info::name() const noexcept |
| 95 | { |
| 96 | const char* name = ::gpiod_line_info_get_name(this->_m_priv->info.get()); |
| 97 | |
| 98 | return name ?: ""; |
| 99 | } |
| 100 | |
| 101 | GPIOD_CXX_API bool line_info::used() const noexcept |
| 102 | { |
| 103 | return ::gpiod_line_info_is_used(this->_m_priv->info.get()); |
| 104 | } |
| 105 | |
| 106 | GPIOD_CXX_API ::std::string line_info::consumer() const noexcept |
| 107 | { |
| 108 | const char* consumer = ::gpiod_line_info_get_consumer(this->_m_priv->info.get()); |
| 109 | |
| 110 | return consumer ?: ""; |
| 111 | } |
| 112 | |
| 113 | GPIOD_CXX_API line::direction line_info::direction() const |
| 114 | { |
| 115 | int direction = ::gpiod_line_info_get_direction(this->_m_priv->info.get()); |
| 116 | |
| 117 | return get_mapped_value(direction, direction_mapping); |
| 118 | } |
| 119 | |
| 120 | GPIOD_CXX_API bool line_info::active_low() const noexcept |
| 121 | { |
| 122 | return ::gpiod_line_info_is_active_low(this->_m_priv->info.get()); |
| 123 | } |
| 124 | |
| 125 | GPIOD_CXX_API line::bias line_info::bias() const |
| 126 | { |
| 127 | int bias = ::gpiod_line_info_get_bias(this->_m_priv->info.get()); |
| 128 | |
| 129 | return get_mapped_value(bias, bias_mapping); |
| 130 | } |
| 131 | |
| 132 | GPIOD_CXX_API line::drive line_info::drive() const |
| 133 | { |
| 134 | int drive = ::gpiod_line_info_get_drive(this->_m_priv->info.get()); |
| 135 | |
| 136 | return get_mapped_value(drive, drive_mapping); |
| 137 | } |
| 138 | |
| 139 | GPIOD_CXX_API line::edge line_info::edge_detection() const |
| 140 | { |
| 141 | int edge = ::gpiod_line_info_get_edge_detection(this->_m_priv->info.get()); |
| 142 | |
| 143 | return get_mapped_value(edge, edge_mapping); |
| 144 | } |
| 145 | |
| 146 | GPIOD_CXX_API line::clock line_info::event_clock() const |
| 147 | { |
| 148 | int clock = ::gpiod_line_info_get_event_clock(this->_m_priv->info.get()); |
| 149 | |
| 150 | return get_mapped_value(clock, clock_mapping); |
| 151 | } |
| 152 | |
| 153 | GPIOD_CXX_API bool line_info::debounced() const noexcept |
| 154 | { |
| 155 | return ::gpiod_line_info_is_debounced(this->_m_priv->info.get()); |
| 156 | } |
| 157 | |
| 158 | GPIOD_CXX_API ::std::chrono::microseconds line_info::debounce_period() const noexcept |
| 159 | { |
| 160 | return ::std::chrono::microseconds( |
| 161 | ::gpiod_line_info_get_debounce_period_us(this->_m_priv->info.get())); |
| 162 | } |
| 163 | |
| 164 | GPIOD_CXX_API ::std::ostream& operator<<(::std::ostream& out, const line_info& info) |
| 165 | { |
| 166 | ::std::string name, consumer; |
| 167 | |
| 168 | name = info.name().empty() ? "unnamed" : ::std::string("'") + info.name() + "'"; |
| 169 | consumer = info.consumer().empty() ? "unused" : ::std::string("'") + info.consumer() + "'"; |
| 170 | |
| 171 | out << "gpiod::line_info(offset=" << info.offset() << |
| 172 | ", name=" << name << |
| 173 | ", used=" << ::std::boolalpha << info.used() << |
| 174 | ", consumer=" << consumer << |
| 175 | ", direction=" << info.direction() << |
| 176 | ", active_low=" << ::std::boolalpha << info.active_low() << |
| 177 | ", bias=" << info.bias() << |
| 178 | ", drive=" << info.drive() << |
| 179 | ", edge_detection=" << info.edge_detection() << |
| 180 | ", event_clock=" << info.event_clock() << |
| 181 | ", debounced=" << ::std::boolalpha << info.debounced(); |
| 182 | |
| 183 | if (info.debounced()) |
| 184 | out << ", debounce_period=" << info.debounce_period().count() << "us"; |
| 185 | |
| 186 | out << ")"; |
| 187 | |
| 188 | return out; |
| 189 | } |
| 190 | |
| 191 | } /* namespace gpiod */ |