Files
redAcore/TimingCore.h
Charl Wentzel e9b50f1af9 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)
2019-06-23 10:38:27 +02:00

97 lines
2.8 KiB
C

/*
* TimingCore.h
*
* Created on: 13 May 2016
* Author: wentzelc
*/
#ifndef REDACORE_TIMINGCORE_H_
#define REDACORE_TIMINGCORE_H_
// Standard C/C++ Libraries
#include <sys/time.h>
#include <time.h>
#include <limits.h>
#include <stdio.h>
// redA Libraries
/* none */
//---------------------------------------------------------------------------
// Set interval (not for timer)
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;
};
//---------------------------------------------------------------------------
// Milli-seconds passed since start time
inline long TimePassed( timeval StartTime ) {
timeval CurrTime;
long Duration;
gettimeofday( &CurrTime, NULL );
Duration = (CurrTime.tv_sec - StartTime.tv_sec) * (time_t)1000 +
(CurrTime.tv_usec - StartTime.tv_usec) / (time_t)1000;
if (Duration < 0)
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_ */