UniSet @VERSION@
DelayTimer.h
1/*
2 * Copyright (c) 2015 Pavel Vainerman.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation, version 2.1.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Lesser Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16// --------------------------------------------------------------------------
17#ifndef DelayTimer_H_
18#define DelayTimer_H_
19// --------------------------------------------------------------------------
20#include "PassiveTimer.h"
21// --------------------------------------------------------------------------
22namespace uniset
23{
30 {
31 public:
32 DelayTimer() {}
33
34 DelayTimer( timeout_t on_msec, timeout_t off_msec ) noexcept:
35 onDelay(on_msec), offDelay(off_msec) {}
36
37 ~DelayTimer() noexcept {}
38
39 inline void set( timeout_t on_msec, timeout_t off_msec ) noexcept
40 {
41 onDelay = on_msec;
42 offDelay = off_msec;
43 waiting_on = false;
44 waiting_off = false;
45 state = false;
46 }
47
48 // запустить часы (заново)
49 inline void reset() noexcept
50 {
51 pt.reset();
52 prevState = false;
53 waiting_on = false;
54 waiting_off = false;
55 state = false;
56 }
57
58 inline bool check( bool st ) noexcept
59 {
60 prevState = st;
61
62 if( waiting_off )
63 {
64 if( pt.checkTime() )
65 {
66 waiting_off = false;
67
68 if( !st )
69 state = false;
70
71 return state;
72 }
73 else if( st )
74 waiting_off = false;
75
76 return state;
77 }
78
79 if( waiting_on )
80 {
81 if( pt.checkTime() )
82 {
83 waiting_on = false;
84
85 if( st )
86 state = true;
87
88 return state;
89 }
90 else if( !st )
91 waiting_on = false;
92
93 return state;
94 }
95
96 if( state != st )
97 {
98 waiting_on = false;
99 waiting_off = false;
100
101 if( st )
102 {
103 if( onDelay <= 0 )
104 {
105 pt.setTiming(0);
106 state = st;
107 return st;
108 }
109
110 pt.setTiming(onDelay);
111 waiting_on = true;
112 }
113 else
114 {
115 if( offDelay <= 0 )
116 {
117 pt.setTiming(0);
118 state = st;
119 return st;
120 }
121
122 pt.setTiming(offDelay);
123 waiting_off = true;
124 }
125 }
126
127 return state;
128 }
129
130 inline bool get() noexcept
131 {
132 return check(prevState);
133 }
134
135 inline timeout_t getOnDelay() const noexcept
136 {
137 return onDelay;
138 }
139 inline timeout_t getOffDelay() const noexcept
140 {
141 return offDelay;
142 }
143
144 inline timeout_t getCurrent() const noexcept
145 {
146 return pt.getCurrent();
147 }
148
149 inline bool isWaitingOn() noexcept
150 {
151 return !get() && waiting_on;
152 }
153
154 inline bool isWaitingOff() noexcept
155 {
156 return get() && waiting_off;
157 }
158
159 inline bool isWaiting() noexcept
160 {
161 check(prevState);
162 return (waiting_off || waiting_on);
163 }
164
165 protected:
166 PassiveTimer pt;
167 bool prevState = { false };
168 bool state = { false };
169 timeout_t onDelay = { 0 };
170 timeout_t offDelay = { 0 };
171 bool waiting_on = { false };
172 bool waiting_off = { false };
173 };
174 // -------------------------------------------------------------------------
175} // end of uniset namespace
176// --------------------------------------------------------------------------
177#endif
178// --------------------------------------------------------------------------
Definition DelayTimer.h:30
Пассивный таймер
Definition PassiveTimer.h:94
virtual timeout_t setTiming(timeout_t msec) noexcept override
Definition PassiveTimer.cc:59
virtual bool checkTime() const noexcept override
Definition PassiveTimer.cc:46
virtual void reset() noexcept override
Definition PassiveTimer.cc:73
virtual timeout_t getCurrent() const noexcept override
Definition PassiveTimer.cc:79
Definition AccessConfig.h:30