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, bool init_state=false )
00045         {
00046             onDelay = on_msec;
00047             offDelay = off_msec;
00048             waiting_on = false;
00049             waiting_off = false;
00050             state = init_state;
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             prevState = st;
00065             if( waiting_off )
00066             {
00067                 if( pt.checkTime() )
00068                 {
00069                     waiting_off = false;
00070                     if( !st )
00071                         state = false;
00072 
00073                     return state;
00074                 }
00075                 else if( st )
00076                     waiting_off = false;
00077 
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 )
00092                     waiting_on = false;
00093 
00094                 return state;
00095             }
00096 
00097             if( state != st )
00098             {
00099                 if( st )
00100                 {
00101                     pt.setTiming(onDelay);
00102                     waiting_on = true;
00103                 }
00104                 else
00105                 {
00106                     pt.setTiming(offDelay);
00107                     waiting_off = true;
00108                 }
00109 
00110                 // на случай если время = 0
00111                 if( pt.checkTime() )
00112                 {
00113                     state = st;
00114                     return st;
00115                 }
00116             }
00117 
00118             return state;
00119         }
00120 
00121         inline bool get()
00122         {
00123             return check(prevState);
00124         }
00125 
00126         inline timeout_t getOffDelay(){ return offDelay; }
00127         inline timeout_t getOnDelay(){ return onDelay; }
00128 
00129     protected:
00130         PassiveTimer pt;
00131         bool prevState;
00132         bool state;
00133         timeout_t onDelay;
00134         timeout_t offDelay;
00135         bool waiting_on;
00136         bool waiting_off;
00137 };
00138 // --------------------------------------------------------------------------
00139 #endif
00140 // --------------------------------------------------------------------------