ChimeraTK-ApplicationCore  04.01.00
PeriodicTrigger.h
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 #pragma once
4 
5 #include "ApplicationModule.h"
6 #include "ScalarAccessor.h"
7 
8 #include <chrono>
9 
10 namespace ChimeraTK {
11 
34  PeriodicTrigger(ModuleGroup* owner, const std::string& name, const std::string& description,
35  const uint32_t defaultPeriod = 1000, const std::unordered_set<std::string>& tags = {},
36  const std::string& periodName = "period", const std::string& tickName = "tick")
37  : ApplicationModule(owner, name, description, tags),
38  period(this, periodName, "ms", "period in milliseconds. The trigger is sent once per the specified duration."),
39  tick(this, tickName, "", "Timer tick. Counts the trigger number starting from 0."),
40  _defaultPeriod(defaultPeriod) {}
41 
42  PeriodicTrigger() = default;
43 
46 
47  void prepare() override {
49  tick.write(); // send initial value
50  }
51 
52  void sendTrigger() {
54  ++tick;
55  tick.write();
56  }
57 
58  void mainLoop() override {
59  if(Application::getInstance().getTestableMode().isEnabled()) {
60  return;
61  }
62  tick = 0;
63  std::chrono::time_point<std::chrono::steady_clock> t = std::chrono::steady_clock::now();
64 
65  while(true) {
66  period.read();
67  if(period == 0) {
68  // set receiving end of timeout. Will only be overwritten if there is
69  // new data.
70  period = _defaultPeriod;
71  }
72  t += std::chrono::milliseconds(static_cast<uint32_t>(period));
73  boost::this_thread::interruption_point();
74  std::this_thread::sleep_until(t);
75 
76  sendTrigger();
77  }
78  }
79 
80  private:
81  uint32_t _defaultPeriod{};
82  };
83 } // namespace ChimeraTK
ChimeraTK::PeriodicTrigger::sendTrigger
void sendTrigger()
Definition: PeriodicTrigger.h:52
ChimeraTK::ModuleGroup
Definition: ModuleGroup.h:16
ChimeraTK::PeriodicTrigger::period
ScalarPollInput< uint32_t > period
Definition: PeriodicTrigger.h:44
ChimeraTK::ApplicationModule::setCurrentVersionNumber
void setCurrentVersionNumber(VersionNumber versionNumber) override
Set the current version number.
Definition: ApplicationModule.cc:89
ChimeraTK::PeriodicTrigger::prepare
void prepare() override
Prepare the execution of the module.
Definition: PeriodicTrigger.h:47
ChimeraTK::Application::getInstance
static Application & getInstance()
Obtain instance of the application.
Definition: Application.cc:261
ChimeraTK::ApplicationModule
Definition: ApplicationModule.h:24
ChimeraTK::PeriodicTrigger::mainLoop
void mainLoop() override
To be implemented by the user: function called in a separate thread executing the main loop of the mo...
Definition: PeriodicTrigger.h:58
ChimeraTK::PeriodicTrigger::PeriodicTrigger
PeriodicTrigger()=default
ChimeraTK::ScalarPollInput< uint32_t >
ScalarAccessor.h
ChimeraTK::PeriodicTrigger::PeriodicTrigger
PeriodicTrigger(ModuleGroup *owner, const std::string &name, const std::string &description, const uint32_t defaultPeriod=1000, const std::unordered_set< std::string > &tags={}, const std::string &periodName="period", const std::string &tickName="tick")
Create periodic trigger module.
Definition: PeriodicTrigger.h:34
ApplicationModule.h
ChimeraTK::ScalarAccessor::write
bool write(ChimeraTK::VersionNumber versionNumber)=delete
ChimeraTK::PeriodicTrigger::tick
ScalarOutput< uint64_t > tick
Definition: PeriodicTrigger.h:45
ChimeraTK::ScalarPollInput::read
void read()
Definition: ScalarAccessor.h:84
ChimeraTK::ApplicationModule::ApplicationModule
ApplicationModule()=default
Default constructor: Allows late initialisation of modules (e.g.
ChimeraTK
InvalidityTracer application module.
Definition: spec_dataValidityPropagation.dox:2
ChimeraTK::ScalarOutput< uint64_t >
ChimeraTK::PeriodicTrigger
Simple periodic trigger that fires a variable once per second.
Definition: PeriodicTrigger.h:16