libGizmod/GizmodTimer.cpp

Go to the documentation of this file.
00001 
00012 /*
00013   
00014   Copyright (c) 2007, Tim Burrell
00015   Licensed under the Apache License, Version 2.0 (the "License");
00016   you may not use this file except in compliance with the License.
00017   You may obtain a copy of the License at 
00018 
00019         http://www.apache.org/licenses/LICENSE-2.0
00020 
00021   Unless required by applicable law or agreed to in writing, software
00022   distributed under the License is distributed on an "AS IS" BASIS,
00023   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00024   See the License for the specific language governing permissions and 
00025   limitations under the License. 
00026   
00027 */
00028 
00029 #include "GizmodTimer.hpp"
00030 #include "GizmoEventLIRC.hpp"
00031 #include "../libH/Debug.hpp"
00032 #include "../libH/Exception.hpp"
00033 #include "../libH/UtilTime.hpp"
00034 #include <boost/shared_ptr.hpp>
00035 #include <boost/thread/thread.hpp>
00036 
00037 using namespace std;
00038 using namespace boost;
00039 using namespace H;
00040 using namespace Gizmod;
00041 
00043 // Type Defs
00045         
00050 #define TIMER_GRANULARITY       0.05f
00051 
00053 // Construction
00055 
00059 GizmodTimer::GizmodTimer(float Seconds, boost::python::object TimerFunction) : mThreadProc(this) {
00060         mCancel = false;
00061         mRepeats = 0;
00062         mTotalRepeats = 0;
00063         mSleepTime = Seconds;
00064         mTimerFunction = TimerFunction;
00065 }
00066 
00070 GizmodTimer::GizmodTimer(float Seconds, boost::python::object TimerFunction, boost::python::object UserData) : mThreadProc(this) {
00071         mCancel = false;
00072         mRepeats = 0;
00073         mTotalRepeats = 0;
00074         mSleepTime = Seconds;
00075         mTimerFunction = TimerFunction;
00076         setUserData(UserData);
00077 }
00078 
00082 GizmodTimer::GizmodTimer(float Seconds, boost::python::object TimerFunction, int Repeats, boost::python::object UserData) : mThreadProc(this) {
00083         mCancel = false;
00084         mRepeats = 0;
00085         mTotalRepeats = Repeats;
00086         mSleepTime = Seconds;
00087         mTimerFunction = TimerFunction;
00088         setUserData(UserData);
00089 }
00090 
00094 GizmodTimer::~GizmodTimer() {
00095 }
00096 
00098 // Class Body
00100 
00104 void GizmodTimer::start() {
00105         cdbg4 << "GizmodTimer :: start" << endl;
00106         boost::thread thrd(mThreadProc);
00107 }
00108 
00112 void GizmodTimer::cancel() {
00113         cdbg4 << "GizmodTimer :: cancel" << endl;
00114         mCancel = true;
00115 }
00116 
00120 void GizmodTimer::resetTimer() {
00121         mTotalSlept = 0.0f;
00122 }
00123 
00128 void GizmodTimer::setUserData(boost::python::object UserData) {
00129         mUserData = UserData;
00130 }
00131 
00136 void GizmodTimer::setTime(float Seconds) {
00137         mSleepTime = Seconds;
00138 }
00139 
00143 void GizmodTimer::threadProc() {
00144         cdbg4 << "GizmodTimer :: Sleeping [" << mSleepTime << "s]" << endl;
00145         
00146         mRepeats = -1;
00147         do {
00148                 // count the number of times we've repeated
00149                 mRepeats ++;
00150                 
00151                 // sleep the desired amount of time
00152                 mTotalSlept = 0.0f;
00153                 mCancel = false;
00154                 while ( (!mCancel) && (mTotalSlept < mSleepTime) ) {
00155                         float SleepStep = TIMER_GRANULARITY;
00156                         if (mTotalSlept + SleepStep > mSleepTime)
00157                                 SleepStep = mSleepTime - mTotalSlept;
00158                         cdbg5 << "GizmodTimer :: Slept [" << mTotalSlept<< "s] of [" << mSleepTime << "s] -- Sleeping [" << SleepStep << "s]" << endl;
00159                         UtilTime::sleep(SleepStep);
00160                         mTotalSlept += SleepStep + 0.0001f;
00161                 }
00162                 
00163                 if (mCancel) {
00164                         cdbg5 << "GizmodTimer :: Cancel Called" << endl;
00165                         return;
00166                 }               
00167                 
00168                 // call the python timer function
00169                 cdbg4 << "GizmodTimer :: Calling Python timerFunction" << endl;
00170                 mutex::scoped_lock lock(mMutexScript);
00171                 mTimerFunction(mUserData);              
00172                 cdbg5 << "GizmodTimer :: Python timerFunction exited." << endl;
00173                 cdbg5 << "GizmodTimer :: Repeating: " << mRepeats << " of " << mTotalRepeats << endl;
00174         } while ( (!mCancel) && ( (mTotalRepeats == -1) || (mRepeats <= mTotalRepeats) ) );
00175 }

Generated on Wed Nov 7 10:04:16 2007 for gizmod by  doxygen 1.5.3