UniSet  2.0.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                 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             if( waiting_off )
00064             {
00065                 if( pt.checkTime() )
00066                 {
00067                     waiting_off = false;
00068                     if( !st )
00069                         state = false;
00070 
00071                     return state;
00072                 }
00073                 else if( st != prevState && !st )
00074                     pt.reset();
00075 
00076                 prevState = st;
00077                 return state;
00078             }
00079 
00080             if( waiting_on )
00081             {
00082                 if( pt.checkTime() )
00083                 {
00084                     waiting_on = false;
00085                     if( st )
00086                         state = true;
00087 
00088                         return state;
00089                 }
00090                 else if( st != prevState && st )
00091                     pt.reset();
00092 
00093                 prevState = st;
00094                 return state;
00095             }
00096 
00097             if( state != st )
00098             {
00099                 prevState = st;
00100                 if( st )
00101                 {
00102                     pt.setTiming(onDelay);
00103                     waiting_on = true;
00104                 }
00105                 else
00106                 {
00107                     pt.setTiming(offDelay);
00108                     waiting_off = true;
00109                 }
00110 
00111                 if( pt.checkTime() )
00112                     return st;
00113             }
00114 
00115             return state;
00116         }
00117 
00118         inline bool get(){ return check(prevState); }
00119 
00120         inline timeout_t getOnDelay(){ return onDelay; }
00121         inline timeout_t getOffDelay(){ return offDelay; }
00122 
00123     protected:
00124         PassiveTimer pt;
00125         bool prevState;
00126         bool state;
00127         timeout_t onDelay;
00128         timeout_t offDelay;
00129         bool waiting_on;
00130         bool waiting_off;
00131 };
00132 // --------------------------------------------------------------------------
00133 #endif
00134 // --------------------------------------------------------------------------