UniSet  2.2.1
Pulse.h
00001 // --------------------------------------------------------------------------
00002 #ifndef Pulse_H_
00003 #define Pulse_H_
00004 // --------------------------------------------------------------------------
00005 #include <iostream>
00006 #include <algorithm>
00007 #include "PassiveTimer.h"
00008 // --------------------------------------------------------------------------
00018 class Pulse
00019 {
00020     public:
00021         Pulse(): ostate(false), isOn(false),
00022             t1_msec(0), t0_msec(0)
00023         {}
00024         ~Pulse() {}
00025 
00026         // t1_msec - интервал "вкл"
00027         // t0_msec - интерфал "откл"
00028         inline void run( int _t1_msec, int _t0_msec )
00029         {
00030             t1_msec = _t1_msec;
00031             t0_msec = _t0_msec;
00032             t1.setTiming(t1_msec);
00033             t0.setTiming(t0_msec);
00034             set(true);
00035         }
00036 
00037         inline void set_next( int _t1_msec, int _t0_msec )
00038         {
00039             t1_msec = _t1_msec;
00040             t0_msec = _t0_msec;
00041         }
00042 
00043         inline void reset()
00044         {
00045             set(true);
00046         }
00047 
00048         inline bool step()
00049         {
00050             if( !isOn )
00051             {
00052                 ostate = false;
00053                 return false;
00054             }
00055 
00056             if( ostate && t1.checkTime() )
00057             {
00058                 ostate = false;
00059                 t0.setTiming(t0_msec);
00060             }
00061 
00062             if( !ostate && t0.checkTime() )
00063             {
00064                 ostate = true;
00065                 t1.setTiming(t1_msec);
00066             }
00067 
00068             return ostate;
00069         }
00070 
00071         inline bool out()
00072         {
00073             return step(); // ostate;
00074         }
00075 
00076         inline void set( bool state )
00077         {
00078             isOn = state;
00079 
00080             if( !isOn )
00081                 ostate = false;
00082             else
00083             {
00084                 t1.reset();
00085                 t0.reset();
00086                 ostate     = true;
00087             }
00088         }
00089 
00090         friend std::ostream& operator<<(std::ostream& os, Pulse& p )
00091         {
00092             return os     << " idOn=" << p.isOn
00093                    << " t1=" << p.t1.getInterval()
00094                    << " t0=" << p.t0.getInterval()
00095                    << " out=" << p.out();
00096         }
00097 
00098         friend std::ostream& operator<<(std::ostream& os, Pulse* p )
00099         {
00100             return os << (*p);
00101         }
00102 
00103 
00104         inline long getT1()
00105         {
00106             return t1_msec;
00107         }
00108         inline long getT0()
00109         {
00110             return t0_msec;
00111         }
00112 
00113     protected:
00114         PassiveTimer t1;    // таймер "1"
00115         PassiveTimer t0;    // таймер "0"
00116         PassiveTimer tCorr;    // корректирующий таймер
00117         bool ostate;
00118         bool isOn;
00119         long t1_msec;
00120         long t0_msec;
00121 
00122 };
00123 // --------------------------------------------------------------------------
00124 #endif
00125 // --------------------------------------------------------------------------