UniSet  1.7.0
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 
00038                 onDelay(on_msec),offDelay(off_msec),waiting_on(false),waiting_off(false)
00039         {
00040         }
00041         
00042         ~DelayTimer(){}
00043     
00044         inline void set( timeout_t on_msec, timeout_t off_msec )
00045         {
00046             onDelay = on_msec;
00047             offDelay = off_msec;
00048             waiting_on = false;
00049             waiting_off = false;
00050             state = false;
00051         }
00052         
00053         // запустить часы (заново)
00054         inline void reset()
00055         {
00056             pt.reset();
00057             waiting_on = false;
00058             waiting_off = false;
00059             state = false;
00060         }
00061 
00062         inline bool check( bool st )
00063         {
00064             if( waiting_off )
00065             {
00066                 if( pt.checkTime() )
00067                 {
00068                     waiting_off = false;
00069                     if( !st )
00070                         state = false;
00071 
00072                     return state;
00073                 }
00074                 else if( st != prevState && !st )
00075                     pt.reset();
00076         
00077                 prevState = st;
00078                 return state;
00079             }
00080 
00081             if( waiting_on )
00082             {
00083                 if( pt.checkTime() )
00084                 {
00085                     waiting_on = false;
00086                     if( st )
00087                         state = true;
00088 
00089                     return state;
00090                 }
00091                 else if( st != prevState && st )
00092                     pt.reset();
00093 
00094                 prevState = st;
00095                 return state;
00096             }
00097 
00098             if( state != st )
00099             {
00100                 prevState = st;
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                 if( pt.checkTime() )
00113                     return st;
00114             }
00115 
00116             return state;
00117         }
00118 
00119         inline bool get(){ return state; }
00120 
00121     protected:
00122         PassiveTimer pt;
00123         bool prevState;
00124         bool state;
00125         timeout_t onDelay;
00126         timeout_t offDelay;
00127         bool waiting_on;
00128         bool waiting_off;
00129 };
00130 // --------------------------------------------------------------------------
00131 #endif
00132 // --------------------------------------------------------------------------