UniSet  2.8.0
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 // --------------------------------------------------------------------------
22 namespace uniset
23 {
29  class DelayTimer
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  waiting_on = false;
53  waiting_off = false;
54  state = false;
55  }
56 
57  inline bool check( bool st ) noexcept
58  {
59  prevState = st;
60 
61  if( waiting_off )
62  {
63  if( pt.checkTime() )
64  {
65  waiting_off = false;
66 
67  if( !st )
68  state = false;
69 
70  return state;
71  }
72  else if( st )
73  waiting_off = false;
74 
75  return state;
76  }
77 
78  if( waiting_on )
79  {
80  if( pt.checkTime() )
81  {
82  waiting_on = false;
83 
84  if( st )
85  state = true;
86 
87  return state;
88  }
89  else if( !st )
90  waiting_on = false;
91 
92  return state;
93  }
94 
95  if( state != st )
96  {
97  waiting_on = false;
98  waiting_off = false;
99 
100  if( st )
101  {
102  if( onDelay <= 0 )
103  {
104  pt.setTiming(0);
105  state = st;
106  return st;
107  }
108 
109  pt.setTiming(onDelay);
110  waiting_on = true;
111  }
112  else
113  {
114  if( offDelay <= 0 )
115  {
116  pt.setTiming(0);
117  state = st;
118  return st;
119  }
120 
121  pt.setTiming(offDelay);
122  waiting_off = true;
123  }
124  }
125 
126  return state;
127  }
128 
129  inline bool get() noexcept
130  {
131  return check(prevState);
132  }
133 
134  inline timeout_t getOnDelay() const noexcept
135  {
136  return onDelay;
137  }
138  inline timeout_t getOffDelay() const noexcept
139  {
140  return offDelay;
141  }
142 
143  inline timeout_t getCurrent() const noexcept
144  {
145  return pt.getCurrent();
146  }
147 
148  inline bool isWaitingOn() noexcept
149  {
150  return !get() && waiting_on;
151  }
152 
153  inline bool isWaitingOff() noexcept
154  {
155  return get() && waiting_off;
156  }
157 
158  inline bool isWaiting() noexcept
159  {
160  check(prevState);
161  return (waiting_off || waiting_on);
162  }
163 
164  protected:
165  PassiveTimer pt;
166  bool prevState = { false };
167  bool state = { false };
168  timeout_t onDelay = { 0 };
169  timeout_t offDelay = { 0 };
170  bool waiting_on = { false };
171  bool waiting_off = { false };
172  };
173  // -------------------------------------------------------------------------
174 } // end of uniset namespace
175 // --------------------------------------------------------------------------
176 #endif
177 // --------------------------------------------------------------------------
Пассивный таймер
Definition: PassiveTimer.h:92
virtual void reset() noexcept override
Definition: PassiveTimer.cc:73
virtual bool checkTime() const noexcept override
Definition: PassiveTimer.cc:46
Definition: CommonEventLoop.h:14
virtual timeout_t getCurrent() const noexcept override
Definition: PassiveTimer.cc:79
virtual timeout_t setTiming(timeout_t msec) noexcept override
Definition: PassiveTimer.cc:59
Definition: DelayTimer.h:29