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