-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimedthresholdcounter.cpp
More file actions
103 lines (80 loc) · 1.95 KB
/
timedthresholdcounter.cpp
File metadata and controls
103 lines (80 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "cpp-utils/timedthresholdcounter.hpp"
#include <iostream>
namespace cu
{
TimedThresholdCounter::TimedThresholdCounter()
{
}
void TimedThresholdCounter::enable()
{
m_enabled = true;
}
void TimedThresholdCounter::disable()
{
m_enabled = false;
}
void TimedThresholdCounter::increment()
{
if(!m_enabled)
return;
m_bouncing = false;
auto now = std::chrono::steady_clock::now();
if(std::chrono::duration_cast<std::chrono::milliseconds>(m_timePointLastIncrement.time_since_epoch()).count() > 0)
{
if(((now - m_timePointLastIncrement) < m_debounceTime))
{
m_bouncing = true;
return;
}
}
if((m_thresholdTimeWindow.count() > 0) && ((now - m_timePointLastIncrement) > m_thresholdTimeWindow))
m_counter = 0;
m_counter++;
m_timePointLastIncrement = now;
}
void TimedThresholdCounter::clear()
{
m_counter = 0;
m_bouncing = false;
}
bool TimedThresholdCounter::isBouncing()
{
return m_bouncing;
}
bool TimedThresholdCounter::isThresholdExceeded()
{
return (m_counter > m_threshold);
}
const std::chrono::duration<double> &TimedThresholdCounter::debounceTime() const
{
return m_debounceTime;
}
void TimedThresholdCounter::setDebounceTime(const std::chrono::duration<double> &newDebounceTime)
{
m_debounceTime = newDebounceTime;
}
const std::chrono::duration<double> &TimedThresholdCounter::thresholdTimeWindow() const
{
return m_thresholdTimeWindow;
}
void TimedThresholdCounter::setThresholdTimeWindow(const std::chrono::duration<double> &newThresholdTimeWindow)
{
m_thresholdTimeWindow = newThresholdTimeWindow;
}
bool TimedThresholdCounter::isEnabled() const
{
return m_enabled;
}
uint32_t TimedThresholdCounter::value() const
{
return m_counter;
}
uint32_t TimedThresholdCounter::threshold() const
{
return m_threshold;
}
void TimedThresholdCounter::setThreshold(uint32_t newThreshold)
{
m_threshold = newThreshold;
}
}