00001 /*************************************************************************** 00002 tag: Peter Soetens Wed Apr 17 16:01:31 CEST 2002 TimeService.cpp 00003 00004 TimeService.cpp - description 00005 ------------------- 00006 begin : Wed April 17 2002 00007 copyright : (C) 2002 Peter Soetens 00008 email : peter.soetens@mech.kuleuven.ac.be 00009 00010 *************************************************************************** 00011 * * 00012 * This program is free software; you can redistribute it and/or modify * 00013 * it under the terms of the GNU General Public License as published by * 00014 * the Free Software Foundation; either version 2 of the License, or * 00015 * (at your option) any later version. * 00016 * * 00017 ***************************************************************************/ 00018 00019 #include "os/fosi.h" 00020 #include "TimeService.hpp" 00021 00022 namespace RTT { 00023 using namespace os; 00024 00025 TimeService* TimeService::_instance = 0; 00026 00027 const TimeService::ticks TimeService::InfiniteTicks = ::InfiniteTicks; 00028 const TimeService::nsecs TimeService::InfiniteNSecs = ::InfiniteNSecs; 00029 const TimeService::Seconds TimeService::InfiniteSeconds = ::InfiniteSeconds; 00030 00031 TimeService::ticks TimeService::nsecs2ticks( const nsecs m ) 00032 { 00033 return nano2ticks( m ); 00034 } 00035 00036 TimeService::nsecs TimeService::ticks2nsecs( const ticks t ) 00037 { 00038 return ticks2nano( t ); 00039 } 00040 00041 TimeService* TimeService::Instance() 00042 { 00043 if ( _instance == 0 ) 00044 { 00045 _instance = new TimeService(); 00046 } 00047 00048 return _instance; 00049 } 00050 00051 00052 bool TimeService::Release() 00053 { 00054 if ( _instance != 0 ) 00055 { 00056 delete _instance; 00057 _instance = 0; 00058 return true; 00059 } 00060 00061 return false; 00062 } 00063 00064 00065 TimeService::TimeService() 00066 : offset(0), use_clock(true) 00067 { 00068 //os::cout << "HeartBeat Created\n"; 00069 } 00070 00071 TimeService::~TimeService() 00072 { 00073 //os::cout << "HB DESTRUCTOR\n"; 00074 } 00075 00076 void TimeService::enableSystemClock( bool yes_no ) 00077 { 00078 // guarantee monotonous time increase : 00079 if ( yes_no == use_clock ) 00080 return; 00081 // we switched from one time domain to the other 00082 use_clock=yes_no; 00083 if ( use_clock == true ) 00084 { 00085 // if offset is X, then start counting from X. 00086 offset = offset - rtos_get_time_ticks(); 00087 } 00088 else 00089 { 00090 // start counting from _now_ + old offset 00091 offset = offset + rtos_get_time_ticks(); 00092 } 00093 } 00094 00095 bool TimeService::systemClockEnabled() const 00096 { 00097 return use_clock; 00098 } 00099 00100 TimeService::ticks 00101 TimeService::getTicks() const 00102 { 00103 return use_clock ? rtos_get_time_ticks() + offset : 0 + offset; 00104 } 00105 00106 TimeService::ticks 00107 TimeService::getTicks( TimeService::ticks &relativeTime ) const 00108 { 00109 if ( relativeTime == 0 ) 00110 { 00111 relativeTime = getTicks(); 00112 return 0; 00113 } 00114 return ( getTicks() - relativeTime ); 00115 } 00116 00117 TimeService::ticks 00118 TimeService::ticksSince( TimeService::ticks relativeTime ) const 00119 { 00120 return ( getTicks() - relativeTime ); 00121 } 00122 00123 00124 TimeService::Seconds 00125 TimeService::getSeconds( TimeService::ticks &relativeTime ) const 00126 { 00127 return nsecs_to_Seconds( ticks2nsecs( getTicks( relativeTime ) ) ) ; 00128 } 00129 00130 TimeService::Seconds 00131 TimeService::secondsSince( TimeService::ticks relativeTime ) const 00132 { 00133 return nsecs_to_Seconds( ticks2nsecs( ticksSince( relativeTime ) ) ) ; 00134 } 00135 00136 TimeService::Seconds 00137 TimeService::secondsChange( TimeService::Seconds delta ) 00138 { 00139 offset += nsecs2ticks( Seconds_to_nsecs( delta ) ); 00140 return nsecs_to_Seconds( ticks2nsecs( ticksSince( 0 ) ) ) ; 00141 } 00142 00143 TimeService::ticks 00144 TimeService::ticksChange( TimeService::ticks delta ) 00145 { 00146 offset += delta; 00147 return ticksSince( 0 ); 00148 } 00149 00150 TimeService::nsecs 00151 TimeService::getNSecs() const 00152 { 00153 return ticks2nsecs(offset) + (use_clock) ? (rtos_get_time_ns()) : (0); 00154 } 00155 00156 TimeService::nsecs 00157 TimeService::getNSecs( TimeService::nsecs &relativeTime ) const 00158 { 00159 if ( relativeTime == 0 ) 00160 { 00161 relativeTime = getNSecs(); 00162 return 0; 00163 } 00164 return ( getNSecs() - relativeTime ); 00165 } 00166 }