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