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         }
00048         
00049         // запустить часы (заново)
00050         inline void reset()
00051         {
00052             pt.reset();
00053         }
00054 
00055         inline bool check( bool st )
00056         {
00057             if( waiting_off )
00058             {
00059                 if( !st && pt.checkTime() )
00060                 {
00061                     waiting_off = false;
00062                     state = false;
00063                     return state;
00064                 }
00065                 else if( st != prevState )
00066                     pt.reset();
00067         
00068                 prevState = st;
00069                 return state;
00070             }
00071 
00072             if( waiting_on )
00073             {
00074                 if( st && pt.checkTime() )
00075                 {
00076                     waiting_on = false;
00077                     state = true;
00078                     return state;
00079                 }
00080                 else if( st != prevState )
00081                     pt.reset();
00082 
00083                 prevState = st;
00084                 return state;
00085             }
00086 
00087             if( state != st )
00088             {
00089                 prevState = st;
00090                 if( st )
00091                 {
00092                     pt.setTiming(onDelay);
00093                     waiting_on = true;
00094                 }
00095                 else
00096                 {
00097                     pt.setTiming(offDelay);
00098                     waiting_off = true;
00099                 }
00100 
00101                 if( pt.checkTime() )
00102                     return st;
00103             }
00104 
00105             return state;
00106         }
00107 
00108     protected:
00109         PassiveTimer pt;
00110         bool prevState;
00111         bool state;
00112         timeout_t onDelay;
00113         timeout_t offDelay;
00114         bool waiting_on;
00115         bool waiting_off;
00116 };
00117 // --------------------------------------------------------------------------
00118 #endif
00119 // --------------------------------------------------------------------------

Документация по UniSet. Последние изменения: Mon Dec 3 16:14:25 2012. Создано системой  doxygen 1.5.9