UniSet  2.6.0
Pulse.h
00001 /*
00002  * Copyright (c) 2015 Pavel Vainerman.
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU Lesser General Public License as
00006  * published by the Free Software Foundation, version 2.1.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00011  * Lesser General Lesser Public License for more details.
00012  *
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00015  */
00016 // --------------------------------------------------------------------------
00017 #ifndef Pulse_H_
00018 #define Pulse_H_
00019 // --------------------------------------------------------------------------
00020 #include <iostream>
00021 #include <algorithm>
00022 #include "PassiveTimer.h"
00023 // --------------------------------------------------------------------------
00024 namespace uniset
00025 {
00035 class Pulse
00036 {
00037     public:
00038         Pulse() noexcept {}
00039         ~Pulse() noexcept {}
00040 
00041         // t1_msec - интервал "вкл"
00042         // t0_msec - интерфал "откл"
00043         inline void run( timeout_t _t1_msec, timeout_t _t0_msec ) noexcept
00044         {
00045             t1_msec = _t1_msec;
00046             t0_msec = _t0_msec;
00047             t1.setTiming(t1_msec);
00048             t0.setTiming(t0_msec);
00049             set(true);
00050         }
00051 
00052         inline void set_next( timeout_t _t1_msec, timeout_t _t0_msec ) noexcept
00053         {
00054             t1_msec = _t1_msec;
00055             t0_msec = _t0_msec;
00056         }
00057 
00058         inline void reset() noexcept
00059         {
00060             set(true);
00061         }
00062 
00063         inline bool step() noexcept
00064         {
00065             if( !isOn )
00066             {
00067                 ostate = false;
00068                 return false;
00069             }
00070 
00071             if( ostate && t1.checkTime() )
00072             {
00073                 ostate = false;
00074                 t0.setTiming(t0_msec);
00075             }
00076 
00077             if( !ostate && t0.checkTime() )
00078             {
00079                 ostate = true;
00080                 t1.setTiming(t1_msec);
00081             }
00082 
00083             return ostate;
00084         }
00085 
00086         inline bool out() noexcept
00087         {
00088             return step(); // ostate;
00089         }
00090 
00091         inline void set( bool state ) noexcept
00092         {
00093             isOn = state;
00094 
00095             if( !isOn )
00096                 ostate = false;
00097             else
00098             {
00099                 t1.reset();
00100                 t0.reset();
00101                 ostate     = true;
00102             }
00103         }
00104 
00105         friend std::ostream& operator<<(std::ostream& os, Pulse& p )
00106         {
00107             return os << " idOn=" << p.isOn
00108                    << " t1=" << p.t1.getInterval()
00109                    << " t0=" << p.t0.getInterval()
00110                    << " out=" << p.out();
00111         }
00112 
00113         friend std::ostream& operator<<(std::ostream& os, Pulse* p )
00114         {
00115             return os << (*p);
00116         }
00117 
00118         inline timeout_t getT1() const noexcept
00119         {
00120             return t1_msec;
00121         }
00122         inline timeout_t getT0() const noexcept
00123         {
00124             return t0_msec;
00125         }
00126 
00127     protected:
00128         PassiveTimer t1;    // таймер "1"
00129         PassiveTimer t0;    // таймер "0"
00130         PassiveTimer tCorr;    // корректирующий таймер
00131         bool ostate = { false };
00132         bool isOn = { false };
00133         timeout_t t1_msec = { 0 };
00134         timeout_t t0_msec = { 0 };
00135 
00136 };
00137 // -------------------------------------------------------------------------
00138 } // end of uniset namespace
00139 // --------------------------------------------------------------------------
00140 #endif
00141 // --------------------------------------------------------------------------