ChimeraTK-ApplicationCore 04.06.00
Loading...
Searching...
No Matches
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
10namespace 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
77 }
78 }
79
80 private:
81 uint32_t _defaultPeriod{};
82 };
83} // namespace ChimeraTK
static Application & getInstance()
Obtain instance of the application.
void setCurrentVersionNumber(VersionNumber versionNumber) override
Set the current version number.
bool write(ChimeraTK::VersionNumber versionNumber)=delete
InvalidityTracer application module.
Simple periodic trigger that fires a variable once per second.
ScalarPollInput< uint32_t > period
void mainLoop() override
To be implemented by the user: function called in a separate thread executing the main loop of the mo...
ScalarOutput< uint64_t > tick
void prepare() override
Prepare the execution of the module.
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.
Convenience class for output scalar accessors (always UpdateMode::push)
Convenience class for input scalar accessors with UpdateMode::poll.