Imt.Base C++ API V4.1.1.0
Loading...
Searching...
No Matches
RangeAlarmWithDelay.h
Go to the documentation of this file.
1// (c) IMT - Information Management Technology AG, CH-9470 Buchs, www.imt.ch.
2
3#ifndef IMT_BASE_LIB_ALARMS_RANGEALARMWITHDELAY_H
4#define IMT_BASE_LIB_ALARMS_RANGEALARMWITHDELAY_H
5
10
11namespace imt {
12namespace base {
13namespace lib {
14namespace alarms {
15
19template<typename ValueType>
21
22public:
23
28
32 static constexpr DelayTicks NULL_DELAY {0U};
33
47 AlarmSenderIfc& alarmSender,
49 AlarmIdentifier const lowerAlarmId,
50 DelayTicks const lowerAlarmDelayTicks,
51 AlarmIdentifier const upperAlarmId,
52 DelayTicks const upperAlarmDelayTicks) noexcept;
53
69 AlarmSenderIfc& alarmSender,
71 AlarmIdentifier const lowerAlarmId,
72 DelayTicks const lowerAlarmActivationDelayTicks,
73 DelayTicks const lowerAlarmDeactivationDelayTicks,
74 AlarmIdentifier const upperAlarmId,
75 DelayTicks const upperAlarmActivationDelayTicks,
76 DelayTicks const upperAlarmDeactivationDelayTicks) noexcept;
77
83 void updateValue(ValueType const& value);
84
90 void forceValue(ValueType const& value);
91
95 void deactivateAlarms();
96
101 bool isAlarmActive() const;
102
107 bool isLowerAlarmActive() const;
108
113 bool isUpperAlarmActive() const;
114
120 bool isInRange(ValueType const& value) const;
121
127
128private:
129
130 void activateLowerAlarm();
131 void activateUpperAlarm();
132 void deactivateLowerAlarm();
133 void deactivateUpperAlarm();
134 void resetAlarmTick(bool const isAlarmActive, bool const isInAlarmRange, DelayTicks* const pAlarmTick);
135 void updateValueUpperRange(ValueType const& value, bool const doForce);
136 void updateValueLowerRange(ValueType const& value, bool const doForce);
137
138 AlarmSenderIfc& m_alarmSender;
140 AlarmIdentifier const m_lowerAlarmId;
141 DelayTicks const m_lowerAlarmActivationDelayTicks;
142 DelayTicks const m_lowerAlarmDeactivationDelayTicks;
143 AlarmIdentifier const m_upperAlarmId;
144 DelayTicks const m_upperAlarmActivationDelayTicks;
145 DelayTicks const m_upperAlarmDeactivationDelayTicks;
146 // AXIVION Next Codeline AutosarC++19_03-A12.1.3: All data members initialized in custom constructor
147 bool m_isLowerAlarmActive;
148 DelayTicks m_lowerAlarmTick;
149 // AXIVION Next Codeline AutosarC++19_03-A12.1.3: All data members initialized in custom constructor
150 bool m_isUpperAlarmActive;
151 // AXIVION Next Codeline AutosarC++19_03-A12.1.3: All data members initialized in custom constructor
152 DelayTicks m_upperAlarmTick;
153 // AXIVION Next Codeline AutosarC++19_03-A12.1.3: All data members initialized in custom constructor
154 ValueType m_value;
155 // AXIVION Next Codeline AutosarC++19_03-A12.1.3: All data members initialized in custom constructor
156 DelayTicks m_tick;
157};
158
159// AXIVION Next Construct AutosarC++19_03-A12.1.6: false positive Noncopyable has not constructor with the same arguments
160template<typename ValueType>
162 AlarmSenderIfc& alarmSender,
164 AlarmIdentifier const lowerAlarmId,
165 DelayTicks const lowerAlarmDelayTicks,
166 AlarmIdentifier const upperAlarmId,
167 DelayTicks const upperAlarmDelayTicks) noexcept :
168 RangeAlarmWithDelay {alarmSender, range, lowerAlarmId, lowerAlarmDelayTicks, lowerAlarmDelayTicks, upperAlarmId, upperAlarmDelayTicks, upperAlarmDelayTicks} {
169}
170
171template<typename ValueType>
173 AlarmSenderIfc& alarmSender,
175 AlarmIdentifier const lowerAlarmId,
176 DelayTicks const lowerAlarmActivationDelayTicks,
177 DelayTicks const lowerAlarmDeactivationDelayTicks,
178 AlarmIdentifier const upperAlarmId,
179 DelayTicks const upperAlarmActivationDelayTicks,
180 DelayTicks const upperAlarmDeactivationDelayTicks) noexcept :
182 m_alarmSender {alarmSender},
183 m_range {range},
184 m_lowerAlarmId {lowerAlarmId},
185 m_lowerAlarmActivationDelayTicks {lowerAlarmActivationDelayTicks},
186 m_lowerAlarmDeactivationDelayTicks {lowerAlarmDeactivationDelayTicks},
187 m_upperAlarmId {upperAlarmId},
188 m_upperAlarmActivationDelayTicks {upperAlarmActivationDelayTicks},
189 m_upperAlarmDeactivationDelayTicks {upperAlarmDeactivationDelayTicks},
190 m_isLowerAlarmActive {false},
191 m_lowerAlarmTick {NULL_DELAY},
192 m_isUpperAlarmActive {false},
193 m_upperAlarmTick {NULL_DELAY},
194 m_value {0},
195 m_tick {NULL_DELAY} {
196}
197
198template<typename ValueType>
200 m_tick++;
201 m_value = value;
202
203 updateValueUpperRange(value, false);
204 updateValueLowerRange(value, false);
205}
206
207template<typename ValueType>
208void RangeAlarmWithDelay<ValueType>::forceValue(ValueType const& value) {
209 m_tick++;
210 m_value = value;
211
212 updateValueUpperRange(value, true);
213 updateValueLowerRange(value, true);
214}
215
216template<typename ValueType>
218 deactivateLowerAlarm();
219 deactivateUpperAlarm();
220}
221
222template<typename ValueType>
224 return m_isLowerAlarmActive || m_isUpperAlarmActive;
225}
226
227template<typename ValueType>
229 return m_isLowerAlarmActive;
230}
231
232template<typename ValueType>
234 return m_isUpperAlarmActive;
235}
236
237template<typename ValueType>
238bool RangeAlarmWithDelay<ValueType>::isInRange(ValueType const& value) const {
239 return m_range.isInRange(value);
240}
241
242template<typename ValueType>
244 m_range = range;
245}
246
247// private
248
249template<typename ValueType>
251 // reset delay
252 m_lowerAlarmTick = m_tick;
253
254 if (!m_isLowerAlarmActive) {
255 if (m_lowerAlarmId != NO_ALARM) {
256 m_alarmSender.activateAlarm(m_lowerAlarmId);
257 m_isLowerAlarmActive = true;
258 deactivateUpperAlarm();
259 }
260 }
261}
262
263template<typename ValueType>
264void RangeAlarmWithDelay<ValueType>::activateUpperAlarm() {
265 // reset delay
266 m_upperAlarmTick = m_tick;
267
268 if (!m_isUpperAlarmActive) {
269 if (m_upperAlarmId != NO_ALARM) {
270 m_alarmSender.activateAlarm(m_upperAlarmId);
271 m_isUpperAlarmActive = true;
272 deactivateLowerAlarm();
273 }
274 }
275}
276
277template<typename ValueType>
278void RangeAlarmWithDelay<ValueType>::deactivateLowerAlarm() {
279 // reset delay
280 m_lowerAlarmTick = m_tick;
281
282 if (m_isLowerAlarmActive) {
283 if (m_lowerAlarmId != NO_ALARM) {
284 m_alarmSender.deactivateAlarm(m_lowerAlarmId);
285 }
286 m_isLowerAlarmActive = false;
287 }
288}
289
290template<typename ValueType>
291void RangeAlarmWithDelay<ValueType>::deactivateUpperAlarm() {
292 // reset delay
293 m_upperAlarmTick = m_tick;
294
295 if (m_isUpperAlarmActive) {
296 if (m_upperAlarmId != NO_ALARM) {
297 m_alarmSender.deactivateAlarm(m_upperAlarmId);
298 }
299 m_isUpperAlarmActive = false;
300 }
301}
302
303template<typename ValueType>
304void RangeAlarmWithDelay<ValueType>::resetAlarmTick(bool const isAlarmActive, bool const isInAlarmRange, DelayTicks* const pAlarmTick) {
305 if (pAlarmTick == nullptr) {
306 return;
307 }
308
309 if (isAlarmActive) {
310 if (isInAlarmRange) {
311 // value in alarm range, reset delay for deactivation
312 *pAlarmTick = m_tick;
313 }
314 else {
315 // value not in alarm range, keep delay for deactivation running
316 }
317 }
318 else {
319 if (isInAlarmRange) {
320 // value in alarm range, keep delay for alarm activation running
321 }
322 else {
323 // value not in alarm range, reset delay for alarm activation
324 *pAlarmTick = m_tick;
325 }
326 }
327}
328
329template<typename ValueType>
330void RangeAlarmWithDelay<ValueType>::updateValueUpperRange(ValueType const& value, bool const doForce) {
331 resetAlarmTick(m_isUpperAlarmActive, m_range.isAbove(value), &m_upperAlarmTick);
332
333 DelayTicks const ticksPassedUpper {m_tick - m_upperAlarmTick};
334 if (m_range.isAbove(value)) {
335 if ((ticksPassedUpper >= m_upperAlarmActivationDelayTicks) || doForce) {
336 activateUpperAlarm();
337 }
338 }
339 else {
340 if ((ticksPassedUpper >= m_upperAlarmDeactivationDelayTicks) || doForce) {
341 deactivateUpperAlarm();
342 }
343 }
344}
345
346template<typename ValueType>
347void RangeAlarmWithDelay<ValueType>::updateValueLowerRange(ValueType const& value, bool const doForce) {
348 resetAlarmTick(m_isLowerAlarmActive, m_range.isBelow(value), &m_lowerAlarmTick);
349
350 DelayTicks const ticksPassedLower {m_tick - m_lowerAlarmTick};
351 if (m_range.isBelow(value)) {
352 if ((ticksPassedLower >= m_lowerAlarmActivationDelayTicks) || doForce) {
353 activateLowerAlarm();
354 }
355 }
356 else {
357 if ((ticksPassedLower >= m_lowerAlarmDeactivationDelayTicks) || doForce) {
358 deactivateLowerAlarm();
359 }
360 }
361}
362
363} // namespace alarms
364} // namespace lib
365} // namespace base
366} // namespace imt
367
368#endif // IMT_BASE_LIB_ALARMS_RANGEALARMWITHDELAY_H
Base class for a non copyable class that disables copy and assignment of instances.
Definition Noncopyable.h:51
A range of values limited by an lower and upper value that is included in the range.
Definition Range.h:51
Interface to send an alarm message to activate or deactivate a given alarm.
Activates the given alarms if the value is outside the range longer as the given delay.
bool isInRange(ValueType const &value) const
Returns true if the given value is inside the range.
void updateRange(::imt::base::core::util::Range< ValueType > const &range)
Changes the range of the alarm.
void deactivateAlarms()
Deactivates both alarms.
void forceValue(ValueType const &value)
Updates the value and checks for the alarm condition.
bool isLowerAlarmActive() const
Returns whether the lower alarm is currently activated.
void updateValue(ValueType const &value)
Updates the value and checks for the alarm condition.
RangeAlarmWithDelay(AlarmSenderIfc &alarmSender, ::imt::base::core::util::Range< ValueType > const &range, AlarmIdentifier const lowerAlarmId, DelayTicks const lowerAlarmDelayTicks, AlarmIdentifier const upperAlarmId, DelayTicks const upperAlarmDelayTicks) noexcept
Constructor.
static constexpr DelayTicks NULL_DELAY
A delay of 0 ticks.
bool isUpperAlarmActive() const
Returns whether the upper alarm is currently activated.
bool isAlarmActive() const
Returns if any alarm is currently activated.
uint32_t AlarmIdentifier
The alarm identifier.
This is a application specific file which is used to configure Imt.Base.Core.Math.
unsigned __int32 uint32_t
Definition stdint.h:64