UniSet  2.24.2
Pulse.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 Pulse_H_
18 #define Pulse_H_
19 // --------------------------------------------------------------------------
20 #include <iostream>
21 #include <algorithm>
22 #include "PassiveTimer.h"
23 // --------------------------------------------------------------------------
24 namespace uniset
25 {
26  // header only
27 
37  class Pulse
38  {
39  public:
40 
41  // t1_msec - интервал "вкл"
42  // t0_msec - интервал "откл"
43  inline void run( timeout_t _t1_msec, timeout_t _t0_msec ) noexcept
44  {
45  setTiming(_t1_msec, _t0_msec, true);
46  }
47 
48  inline void setTiming( timeout_t _t1_msec, timeout_t _t0_msec, bool run = false ) noexcept
49  {
50  t1_msec = _t1_msec;
51  t0_msec = _t0_msec;
52  t1.setTiming(t1_msec);
53  t0.setTiming(t0_msec);
54  set(run);
55  }
56 
57  inline void reset() noexcept
58  {
59  set(true);
60  }
61 
62  inline bool step() noexcept
63  {
64  if( !enabled )
65  {
66  ostate = false;
67  return false;
68  }
69 
70  if( ostate && t1.checkTime() )
71  {
72  ostate = false;
73  t0.setTiming(t0_msec);
74  }
75 
76  if( !ostate && t0.checkTime() )
77  {
78  ostate = true;
79  t1.setTiming(t1_msec);
80  }
81 
82  return ostate;
83  }
84 
85  inline bool out() noexcept
86  {
87  return step(); // ostate;
88  }
89 
90  inline bool out() const noexcept
91  {
92  return ostate;
93  }
94 
95  inline void set( bool state ) noexcept
96  {
97  enabled = state;
98 
99  if( !enabled )
100  ostate = false;
101  else
102  {
103  t1.reset();
104  t0.reset();
105  ostate = true;
106  }
107  }
108 
109  friend std::ostream& operator<<(std::ostream& os, Pulse& p )
110  {
111  return os << " idOn=" << p.enabled
112  << " t1=" << p.t1.getInterval()
113  << " t0=" << p.t0.getInterval()
114  << " out=" << p.out();
115  }
116 
117  friend std::ostream& operator<<(std::ostream& os, Pulse* p )
118  {
119  return os << (*p);
120  }
121 
122  inline timeout_t getT1() const noexcept
123  {
124  return t1_msec;
125  }
126  inline timeout_t getT0() const noexcept
127  {
128  return t0_msec;
129  }
130 
131  bool isOn() const noexcept
132  {
133  return enabled;
134  }
135 
136  protected:
137  PassiveTimer t1; // таймер "1"
138  PassiveTimer t0; // таймер "0"
139  bool ostate = { false };
140  bool enabled = { false };
141  timeout_t t1_msec = { 0 };
142  timeout_t t0_msec = { 0 };
143 
144  };
145  // -------------------------------------------------------------------------
146 } // end of uniset namespace
147 // --------------------------------------------------------------------------
148 #endif
149 // --------------------------------------------------------------------------
Пассивный таймер
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
Definition: Pulse.h:38
Definition: CommonEventLoop.h:15