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