ChimeraTK-DeviceAccess  03.18.00
LNMMonostableTriggerPlugin.cc
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2 // SPDX-License-Identifier: LGPL-3.0-or-later
3 
4 #include "LNMAccessorPlugin.h"
6 
7 #include <boost/make_shared.hpp>
8 
9 #include <thread>
10 
11 namespace ChimeraTK::LNMBackend {
12 
13  /********************************************************************************************************************/
14 
16  LNMBackendRegisterInfo info, size_t pluginIndex, const std::map<std::string, std::string>& parameters)
17  : AccessorPlugin(info, pluginIndex) {
18  // extract parameters
19  if(parameters.find("milliseconds") == parameters.end()) {
21  "LogicalNameMappingBackend MonostableTriggerPluginPlugin: Missing parameter 'milliseconds'.");
22  }
23  _milliseconds = std::stod(parameters.at("milliseconds"));
24  if(parameters.find("active") != parameters.end()) {
25  _active = std::stoul(parameters.at("active"));
26  }
27  if(parameters.find("inactive") != parameters.end()) {
28  _inactive = std::stoul(parameters.at("inactive"));
29  }
30 
31  // Change register info to write-only and data type nodata
32  info.readable = false;
34  }
35 
36  /********************************************************************************************************************/
37 
39  // Change register info to write-only and data type nodata
40  _info.readable = false;
43  }
44 
45  /********************************************************************************************************************/
46 
47  template<typename UserType>
50 
52  double milliseconds, uint32_t active, uint32_t inactive)
53  : ChimeraTK::NDRegisterAccessorDecorator<UserType, uint32_t>(target), _delay(milliseconds), _active(active),
54  _inactive(inactive) {
55  // make sure the target register is writeable and scalar
56  if(!_target->isWriteable()) {
58  "LogicalNameMappingBackend MonostableTriggerPluginPlugin: Cannot target non-writeable register.");
59  }
60  if(_target->getNumberOfChannels() > 1 || _target->getNumberOfSamples() > 1) {
62  "LogicalNameMappingBackend MonostableTriggerPluginPlugin: Cannot target non-scalar registers.");
63  }
64  }
65 
66  [[nodiscard]] bool isReadable() const override { return false; }
67 
68  void doPreRead(TransferType) override {
69  throw ChimeraTK::logic_error("LogicalNameMappingBackend MonostableTriggerPluginPlugin: Reading is not allowed.");
70  }
71 
72  void doPostRead(TransferType, bool /*hasNewData*/) override {}
73 
74  void doPreWrite(TransferType /*type*/, VersionNumber versionNumber) override {
75  _target->accessData(0, 0) = _active;
76  _target->setDataValidity(this->_dataValidity);
77  _target->preWrite(TransferType::write, versionNumber);
78  }
79 
80  bool doWriteTransfer(ChimeraTK::VersionNumber versionNumber) override;
81  bool doWriteTransferDestructively(ChimeraTK::VersionNumber versionNumber) override;
82 
83  void doPostWrite(TransferType, VersionNumber versionNumber) override {
84  _target->postWrite(TransferType::write, versionNumber);
85  }
86 
87  std::chrono::duration<double, std::milli> _delay;
88  uint32_t _active, _inactive;
89 
90  bool dataLossInInactivate{false};
91 
93  };
94 
95  template<typename UserType>
97  // Note: since we have called _target->preWrite() in our doPreWrite(), there cannot be a logic_error at this point
98  // any more. This also holds for the second transfer initiated here: if the first transfer is allowed, so is the
99  // second. In case the target backend screws this up, we will run into std::terminate.
100 
101  bool a = _target->writeTransfer(versionNumber);
102  _target->postWrite(TransferType::write, versionNumber);
103 
104  std::this_thread::sleep_for(_delay);
105 
106  _target->accessData(0, 0) = _inactive;
107  _target->preWrite(TransferType::write, versionNumber);
108  dataLossInInactivate = _target->writeTransfer(versionNumber);
109  return a || dataLossInInactivate;
110  }
111 
112  template<typename UserType>
114  ChimeraTK::VersionNumber versionNumber) {
115  return doWriteTransfer(versionNumber);
116  }
117 
118  /********************************************************************************************************************/
119 
120  template<typename UserType, typename TargetType>
121  boost::shared_ptr<NDRegisterAccessor<UserType>> MonostableTriggerPlugin::decorateAccessor(
122  boost::shared_ptr<LogicalNameMappingBackend>&, boost::shared_ptr<NDRegisterAccessor<TargetType>>& target,
123  const UndecoratedParams&) {
124  if constexpr(std::is_same<TargetType, uint32_t>::value) {
125  return boost::make_shared<MonostableTriggerPluginDecorator<UserType>>(target, _milliseconds, _active, _inactive);
126  }
127 
128  assert(false);
129 
130  return {};
131  }
132 } // namespace ChimeraTK::LNMBackend
ChimeraTK::LNMBackend::MonostableTriggerPluginDecorator::doPreWrite
void doPreWrite(TransferType, VersionNumber versionNumber) override
Definition: LNMMonostableTriggerPlugin.cc:74
ChimeraTK::AccessMode::raw
@ raw
Raw access: disable any possible conversion from the original hardware data type into the given UserT...
ChimeraTK::LNMBackend::MonostableTriggerPluginDecorator::doWriteTransfer
bool doWriteTransfer(ChimeraTK::VersionNumber versionNumber) override
Definition: LNMMonostableTriggerPlugin.cc:96
ChimeraTK::LNMBackend::MonostableTriggerPlugin::decorateAccessor
boost::shared_ptr< NDRegisterAccessor< UserType > > decorateAccessor(boost::shared_ptr< LogicalNameMappingBackend > &backend, boost::shared_ptr< NDRegisterAccessor< TargetType >> &target, const UndecoratedParams &accessorParams)
Definition: LNMMonostableTriggerPlugin.cc:121
ChimeraTK::LNMBackend::AccessorPlugin
Base class for plugins that modify the behaviour of accessors in the logical name mapping backend.
Definition: LNMAccessorPlugin.h:104
ChimeraTK::LNMBackend::MonostableTriggerPluginDecorator
Definition: LNMMonostableTriggerPlugin.cc:48
ChimeraTK::LNMBackend
Definition: LNMMathPluginFormulaHelper.h:16
ChimeraTK::AccessModeFlags::remove
void remove(AccessMode flag)
Remove the given flag from the set.
Definition: AccessMode.cc:56
ChimeraTK::DataDescriptor::FundamentalType::nodata
@ nodata
ChimeraTK::LNMBackend::MonostableTriggerPlugin::doRegisterInfoUpdate
void doRegisterInfoUpdate() override
Implementation of the plugin specific register information update.
Definition: LNMMonostableTriggerPlugin.cc:38
ChimeraTK::LNMBackend::MonostableTriggerPluginDecorator::isReadable
bool isReadable() const override
Definition: LNMMonostableTriggerPlugin.cc:66
ChimeraTK::LNMBackend::MonostableTriggerPlugin::MonostableTriggerPlugin
MonostableTriggerPlugin(LNMBackendRegisterInfo info, size_t pluginIndex, const std::map< std::string, std::string > &parameters)
Definition: LNMMonostableTriggerPlugin.cc:15
ChimeraTK::LNMBackend::MonostableTriggerPluginDecorator::doPostWrite
void doPostWrite(TransferType, VersionNumber versionNumber) override
Definition: LNMMonostableTriggerPlugin.cc:83
NDRegisterAccessorDecorator.h
ChimeraTK::LNMBackend::UndecoratedParams
Helper struct to hold extra parameters needed by some plugins, used in decorateAccessor()
Definition: LNMAccessorPlugin.h:13
ChimeraTK::DataDescriptor
Class describing the actual payload data format of a register in an abstract manner.
Definition: DataDescriptor.h:19
ChimeraTK::LNMBackend::MonostableTriggerPluginDecorator::dataLossInInactivate
bool dataLossInInactivate
Definition: LNMMonostableTriggerPlugin.cc:90
ChimeraTK::LNMBackend::MonostableTriggerPluginDecorator::_inactive
uint32_t _inactive
Definition: LNMMonostableTriggerPlugin.cc:88
ChimeraTK::LNMBackend::MonostableTriggerPluginDecorator::_delay
std::chrono::duration< double, std::milli > _delay
Definition: LNMMonostableTriggerPlugin.cc:87
ChimeraTK::LNMBackendRegisterInfo
RegisterInfo structure for the LogicalNameMappingBackend.
Definition: LNMBackendRegisterInfo.h:22
ChimeraTK::LNMBackend::MonostableTriggerPluginDecorator::doPostRead
void doPostRead(TransferType, bool) override
Definition: LNMMonostableTriggerPlugin.cc:72
ChimeraTK::LNMBackend::AccessorPluginBase::_info
LNMBackendRegisterInfo _info
RegisterInfo describing the the target register for which this plugin instance should work.
Definition: LNMAccessorPlugin.h:93
ChimeraTK::TransferType
TransferType
Used to indicate the applicable operation on a Transferelement.
Definition: TransferElement.h:51
ChimeraTK::LNMBackend::MonostableTriggerPluginDecorator::MonostableTriggerPluginDecorator
MonostableTriggerPluginDecorator(const boost::shared_ptr< ChimeraTK::NDRegisterAccessor< uint32_t >> &target, double milliseconds, uint32_t active, uint32_t inactive)
Definition: LNMMonostableTriggerPlugin.cc:51
ChimeraTK::LNMBackend::MonostableTriggerPluginDecorator::doWriteTransferDestructively
bool doWriteTransferDestructively(ChimeraTK::VersionNumber versionNumber) override
Definition: LNMMonostableTriggerPlugin.cc:113
ChimeraTK::NDRegisterAccessorDecorator
Base class for decorators of the NDRegisterAccessor.
Definition: NDRegisterAccessorDecorator.h:120
ChimeraTK::LNMBackendRegisterInfo::_dataDescriptor
DataDescriptor _dataDescriptor
Definition: LNMBackendRegisterInfo.h:111
ChimeraTK::TransferType::write
@ write
ChimeraTK::LNMBackend::MonostableTriggerPlugin::_milliseconds
double _milliseconds
Definition: LNMAccessorPlugin.h:201
ChimeraTK::LNMBackendRegisterInfo::supportedFlags
AccessModeFlags supportedFlags
Supported AccessMode flags.
Definition: LNMBackendRegisterInfo.h:106
ChimeraTK::VersionNumber
Class for generating and holding version numbers without exposing a numeric representation.
Definition: VersionNumber.h:23
ChimeraTK::LNMBackend::MonostableTriggerPluginDecorator::doPreRead
void doPreRead(TransferType) override
Definition: LNMMonostableTriggerPlugin.cc:68
ChimeraTK::LNMBackend::MonostableTriggerPlugin::_active
uint32_t _active
Definition: LNMAccessorPlugin.h:202
ChimeraTK
Definition: DummyBackend.h:16
ChimeraTK::LNMBackend::MonostableTriggerPluginDecorator::_active
uint32_t _active
Definition: LNMMonostableTriggerPlugin.cc:88
ChimeraTK::NDRegisterAccessor
N-dimensional register accessor.
Definition: ForwardDeclarations.h:17
LNMAccessorPlugin.h
ChimeraTK::LNMBackendRegisterInfo::readable
bool readable
Flag if the register is readable.
Definition: LNMBackendRegisterInfo.h:99
ChimeraTK::logic_error
Exception thrown when a logic error has occured.
Definition: Exception.h:51
ChimeraTK::LNMBackend::MonostableTriggerPlugin::_inactive
uint32_t _inactive
Definition: LNMAccessorPlugin.h:203