UniSet  2.2.1
DelayTimer.h
00001 /* This file is part of the UniSet project
00002  * Copyright (c) 2002 Free Software Foundation, Inc.
00003  * Copyright (c) 2002 Pavel Vainerman
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018  */
00019 // --------------------------------------------------------------------------
00020 #ifndef DelayTimer_H_
00021 #define DelayTimer_H_
00022 // --------------------------------------------------------------------------
00023 #include "PassiveTimer.h"
00024 // --------------------------------------------------------------------------
00030 class DelayTimer
00031 {
00032     public:
00033         DelayTimer(): prevState(false), state(false),
00034             onDelay(0), offDelay(0), waiting_on(false), waiting_off(false) {}
00035 
00036         DelayTimer( timeout_t on_msec, timeout_t off_msec ): prevState(false), state(false),
00037             onDelay(on_msec), offDelay(off_msec), waiting_on(false), waiting_off(false)
00038         {
00039         }
00040 
00041         ~DelayTimer() {}
00042 
00043         inline void set( timeout_t on_msec, timeout_t off_msec )
00044         {
00045             onDelay = on_msec;
00046             offDelay = off_msec;
00047             waiting_on = false;
00048             waiting_off = false;
00049             state = false;
00050         }
00051 
00052         // запустить часы (заново)
00053         inline void reset()
00054         {
00055             pt.reset();
00056             waiting_on = false;
00057             waiting_off = false;
00058             state = false;
00059         }
00060 
00061         inline bool check( bool st )
00062         {
00063             prevState = st;
00064 
00065             if( waiting_off )
00066             {
00067                 if( pt.checkTime() )
00068                 {
00069                     waiting_off = false;
00070 
00071                     if( !st )
00072                         state = false;
00073 
00074                     return state;
00075                 }
00076                 else if( st )
00077                     waiting_off = false;
00078 
00079                 return state;
00080             }
00081 
00082             if( waiting_on )
00083             {
00084                 if( pt.checkTime() )
00085                 {
00086                     waiting_on = false;
00087 
00088                     if( st )
00089                         state = true;
00090 
00091                     return state;
00092                 }
00093                 else if( !st )
00094                     waiting_on = false;
00095 
00096                 return state;
00097             }
00098 
00099             if( state != st )
00100             {
00101                 if( st )
00102                 {
00103                     pt.setTiming(onDelay);
00104                     waiting_on = true;
00105                 }
00106                 else
00107                 {
00108                     pt.setTiming(offDelay);
00109                     waiting_off = true;
00110                 }
00111 
00112                 // на случае если таймеры  = 0..
00113                 if( pt.checkTime() )
00114                 {
00115                     state = st;
00116                     return st;
00117                 }
00118             }
00119 
00120             return state;
00121         }
00122 
00123         inline bool get()
00124         {
00125             return check(prevState);
00126         }
00127 
00128         inline timeout_t getOnDelay() const
00129         {
00130             return onDelay;
00131         }
00132         inline timeout_t getOffDelay() const
00133         {
00134             return offDelay;
00135         }
00136 
00137         inline timeout_t getCurrent() const
00138         {
00139             return pt.getCurrent();
00140         }
00141 
00142     protected:
00143         PassiveTimer pt;
00144         bool prevState;
00145         bool state;
00146         timeout_t onDelay;
00147         timeout_t offDelay;
00148         bool waiting_on;
00149         bool waiting_off;
00150 };
00151 // --------------------------------------------------------------------------
00152 #endif
00153 // --------------------------------------------------------------------------