|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Analog Devices Inc. |
| 3 | + * |
| 4 | + * This file is part of libm2k |
| 5 | + * (see http://www.github.com/analogdevicesinc/libm2k). |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Lesser General Public License as published by |
| 9 | + * the Free Software Foundation, either version 2.1 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Lesser General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +#include "m2khardwaretrigger_v0.26_impl.hpp" |
| 23 | +#include <libm2k/m2kexceptions.hpp> |
| 24 | +#include <algorithm> |
| 25 | + |
| 26 | + |
| 27 | +const std::string libm2k::M2kHardwareTriggerV026Impl::m_source_attr = "trigger_src"; |
| 28 | +const std::string libm2k::M2kHardwareTriggerV026Impl::m_condition_attr = "trigger_condition"; |
| 29 | + |
| 30 | + |
| 31 | +std::vector<std::string> libm2k::M2kHardwareTriggerV026Impl::m_trigger_source = { |
| 32 | + "none", |
| 33 | + "trigger_i_0", |
| 34 | + "trigger_i_1", |
| 35 | + "trigger-adc", |
| 36 | + "trigger-la", |
| 37 | +}; |
| 38 | + |
| 39 | +std::vector<std::string> libm2k::M2kHardwareTriggerV026Impl::m_trigger_cond = { |
| 40 | + "edge-rising", |
| 41 | + "edge-falling", |
| 42 | + "level-low", |
| 43 | + "level-high", |
| 44 | + "edge-any", |
| 45 | + "none" |
| 46 | +}; |
| 47 | + |
| 48 | +libm2k::M2kHardwareTriggerV026Impl::M2kHardwareTriggerV026Impl(struct iio_context *ctx, bool init) : |
| 49 | + M2kHardwareTriggerV024Impl(ctx) |
| 50 | +{ |
| 51 | + if (init) { |
| 52 | + reset(); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +void libm2k::M2kHardwareTriggerV026Impl::reset() |
| 57 | +{ |
| 58 | + M2kHardwareTriggerV024Impl::reset(); |
| 59 | +} |
| 60 | + |
| 61 | +void libm2k::M2kHardwareTriggerV026Impl::setTriggerOutSource(libm2k::M2K_TRIGGER_OUT_SOURCE src, |
| 62 | + const std::shared_ptr<libm2k::utils::DeviceOut>& device) |
| 63 | +{ |
| 64 | + if (src >= m_trigger_source.size()) { |
| 65 | + THROW_M2K_EXCEPTION("M2kHardwareTrigger: " |
| 66 | + "the provided source is not supported on " |
| 67 | + "the current board; Check the firmware version.", |
| 68 | + libm2k::EXC_INVALID_PARAMETER); |
| 69 | + } |
| 70 | + std::string src_str = m_trigger_source[src]; |
| 71 | + device->setStringValue(m_source_attr, src_str); |
| 72 | +} |
| 73 | + |
| 74 | +libm2k::M2K_TRIGGER_OUT_SOURCE |
| 75 | +libm2k::M2kHardwareTriggerV026Impl::getTriggerOutSource(const std::shared_ptr<libm2k::utils::DeviceOut>& device) const |
| 76 | +{ |
| 77 | + std::string buf = device->getStringValue(m_source_attr); |
| 78 | + |
| 79 | + auto it = std::find(m_trigger_source.begin(), |
| 80 | + m_trigger_source.end(), buf.c_str()); |
| 81 | + if (it == m_trigger_source.end()) { |
| 82 | + THROW_M2K_EXCEPTION("Unexpected value read from attribute: " + m_source_attr, libm2k::EXC_INVALID_PARAMETER); |
| 83 | + } |
| 84 | + |
| 85 | + return static_cast<libm2k::M2K_TRIGGER_OUT_SOURCE>(it - m_trigger_source.begin()); |
| 86 | +} |
| 87 | + |
| 88 | +void libm2k::M2kHardwareTriggerV026Impl::setTriggerOutCondition( |
| 89 | + libm2k::M2K_TRIGGER_CONDITION_DIGITAL cond, const std::shared_ptr<libm2k::utils::DeviceOut>& device) |
| 90 | +{ |
| 91 | + if (cond >= m_trigger_cond.size()) { |
| 92 | + THROW_M2K_EXCEPTION("M2kHardwareTrigger: " |
| 93 | + "the provided condition is not supported on " |
| 94 | + "the current board; Check the firmware version.", |
| 95 | + libm2k::EXC_INVALID_PARAMETER); |
| 96 | + } |
| 97 | + std::string src_str = m_trigger_cond[cond]; |
| 98 | + device->setStringValue(m_condition_attr, src_str); |
| 99 | +} |
| 100 | + |
| 101 | +libm2k::M2K_TRIGGER_CONDITION_DIGITAL |
| 102 | +libm2k::M2kHardwareTriggerV026Impl::getTriggerOutCondition(const std::shared_ptr<libm2k::utils::DeviceOut>& device) const |
| 103 | +{ |
| 104 | + std::string buf = device->getStringValue(m_condition_attr); |
| 105 | + |
| 106 | + auto it = std::find(m_trigger_cond.begin(), |
| 107 | + m_trigger_cond.end(), buf.c_str()); |
| 108 | + if (it == m_trigger_cond.end()) { |
| 109 | + THROW_M2K_EXCEPTION("Unexpected value read from attribute: " + m_condition_attr, libm2k::EXC_INVALID_PARAMETER); |
| 110 | + } |
| 111 | + |
| 112 | + return static_cast<libm2k::M2K_TRIGGER_CONDITION_DIGITAL>(it - m_trigger_cond.begin()); |
| 113 | +} |
0 commit comments