Important Update:
- FunctionCore/SelectableCore: - Replace Channel->State with InState - Add Channel-OutState (ready for output) updated by ChannelStateEvent() - TimingCore: - Add GetUpCounter() for calculating uptime string (in seconds)
This commit is contained in:
35
TimingCore.h
35
TimingCore.h
@@ -12,6 +12,7 @@
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// redA Libraries
|
||||
/* none */
|
||||
@@ -23,19 +24,22 @@ inline void SetInterval( timeval * Time, long MilliSeconds ) {
|
||||
Time->tv_sec = MilliSeconds / 1000;
|
||||
Time->tv_usec = (MilliSeconds % 1000) * 1000;
|
||||
};
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Mark start time
|
||||
inline void SetStartTime( timeval *StartTime ) {
|
||||
gettimeofday( StartTime, NULL );
|
||||
};
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Clear timer
|
||||
inline void ClearStartTime( timeval * StartTime ) {
|
||||
StartTime->tv_sec = 0;
|
||||
StartTime->tv_usec = 0;
|
||||
};
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Time passed since start time
|
||||
// Milli-seconds passed since start time
|
||||
inline long TimePassed( timeval StartTime ) {
|
||||
timeval CurrTime;
|
||||
long Duration;
|
||||
@@ -47,17 +51,46 @@ inline long TimePassed( timeval StartTime ) {
|
||||
Duration = LONG_MAX;
|
||||
return Duration;
|
||||
};
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Time remaining from Start time to given time out
|
||||
inline long TimeLeft( timeval StartTime, long MilliSeconds ) {
|
||||
return (MilliSeconds - TimePassed(StartTime));
|
||||
};
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Has give time expired after start time
|
||||
inline bool Timeout( timeval StartTime, long MilliSeconds ) {
|
||||
return ((TimePassed(StartTime) > MilliSeconds)? true : false);
|
||||
};
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Get Upcount in seconds from start time (with string output)
|
||||
inline long GetUpCounter( timeval StartTime, char * TextStr ) {
|
||||
long Duration;
|
||||
int Days, Hours, Minutes, Seconds;
|
||||
timeval CurrTime;
|
||||
|
||||
// Get duration
|
||||
gettimeofday( &CurrTime, NULL );
|
||||
Duration = (!StartTime.tv_sec)? 0 : (CurrTime.tv_sec - StartTime.tv_sec); // Handle zero start
|
||||
|
||||
// Create string
|
||||
if (TextStr) {
|
||||
// Get Parts
|
||||
Minutes = Duration / 60;
|
||||
Hours = Minutes / 60;
|
||||
Days = Hours / 24;
|
||||
|
||||
// Get modulus
|
||||
Seconds = Duration % 60;
|
||||
Minutes = Minutes % 60;
|
||||
Hours = Hours % 24;
|
||||
|
||||
sprintf( TextStr, "%dd %02d:%02d:%02d", Days, Hours, Minutes, Seconds );
|
||||
}
|
||||
return Duration;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
#endif /* REDACORE_TIMINGCORE_H_ */
|
||||
|
||||
Reference in New Issue
Block a user