PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
PlatformTimer Class Reference

#include <PlatformTimer.h>

Inheritance diagram for PlatformTimer:
PTimer PTimer PDevice PDevice

Public Member Functions

 PlatformTimer ()
 The Standard constructor.
 
virtual ~PlatformTimer ()
 The Standard destructor.
 
virtual void Wait (unsigned long int msecs)
 
virtual void Sleep (unsigned long int msecs)
 
virtual unsigned long int GetTime () const
 
virtual void GetTimeOfDay (unsigned long &secs, unsigned long &msecs)
 
virtual int GetState (int iface) const
 
 PlatformTimer ()
 
virtual ~PlatformTimer ()
 
virtual void Wait (unsigned long int msecs)
 
virtual void Sleep (unsigned long int msecs)
 
virtual unsigned long int GetTime () const
 
virtual void GetTimeOfDay (unsigned long &secs, unsigned long &msecs)
 
virtual int GetState (int iface) const
 
- Public Member Functions inherited from PTimer
 PTimer ()
 The Standard constructor.
 
virtual ~PTimer ()
 The Standard destructor.
 
virtual PEBL_DEVICE_TYPE GetDeviceType ()
 
- Public Member Functions inherited from PDevice
 PDevice ()
 The Standard constructor.
 
virtual ~PDevice ()
 The Standard destructor.
 

Detailed Description

This device is the SDL-Specific interface for two types of platform functions: Querying the actual time, in msec, since the program started (which will wrap after a while) and waiting a specified period of time. It uses SDL function calls for this. For better accuracy, use system-specific timer calls,

Headless platform timer implementation using C++ chrono library This is a real implementation, not a stub, since timing is platform-independent

Definition at line 41 of file sdl/PlatformTimer.h.

Constructor & Destructor Documentation

◆ PlatformTimer() [1/2]

PlatformTimer::PlatformTimer ( )

The Standard constructor.

This is the standard PlatformTimer constructor.

Definition at line 57 of file sdl/PlatformTimer.cpp.

58{
59
60}

◆ ~PlatformTimer() [1/2]

PlatformTimer::~PlatformTimer ( )
virtual

The Standard destructor.

This is the standard pNode destructor.

Definition at line 63 of file sdl/PlatformTimer.cpp.

64{
65 // Standard Destructor
66}

◆ PlatformTimer() [2/2]

PlatformTimer::PlatformTimer ( )

◆ ~PlatformTimer() [2/2]

virtual PlatformTimer::~PlatformTimer ( )
virtual

Member Function Documentation

◆ GetState() [1/2]

int PlatformTimer::GetState ( int  iface) const
virtual

Implements PTimer.

Definition at line 158 of file sdl/PlatformTimer.cpp.

159{
160 //There is no interface for the timer.
161 return SDL_GetTicks();
162}

◆ GetState() [2/2]

virtual int PlatformTimer::GetState ( int  iface) const
virtual

Implements PTimer.

◆ GetTime() [1/2]

◆ GetTime() [2/2]

virtual unsigned long int PlatformTimer::GetTime ( ) const
virtual

Implements PTimer.

◆ GetTimeOfDay() [1/2]

void PlatformTimer::GetTimeOfDay ( unsigned long &  secs,
unsigned long &  msecs 
)
virtual

Implements PTimer.

Definition at line 171 of file sdl/PlatformTimer.cpp.

172{
173#if defined( PEBL_UNIX) || defined(PEBL_EMSCRIPTEN)
174 struct timeval * tp=NULL;
175 gettimeofday(tp,NULL);
176
177 time_t secs1 = tp->tv_sec;
178 suseconds_t usecs = tp->tv_usec;
179
180 secs = secs1;
181 msecs = usecs/100;
182#else
183
184 SYSTEMTIME st;
185 ::GetSystemTime(&st);
186
187
188 std::cerr << std::setw(2) << st.wHour << ':'
189 << std::setw(2) << st.wMinute << ':'
190 << std::setw(2) << st.wSecond << '.'
191 << std::setw(3) << st.wMilliseconds << '\n';
192
193// return timeGetTime();
194#endif
195}
#define NULL
Definition BinReloc.cpp:317

References NULL.

Referenced by PEBLEnvironment::GetTimeOfDay().

◆ GetTimeOfDay() [2/2]

virtual void PlatformTimer::GetTimeOfDay ( unsigned long &  secs,
unsigned long &  msecs 
)
virtual

Implements PTimer.

◆ Sleep() [1/2]

void PlatformTimer::Sleep ( unsigned long int  msecs)
virtual

Sleep using OS-specific sleep functions (yields CPU, unlike Wait which busy-waits) This is appropriate for polling loops, avoiding CPU burn

Implements PTimer.

Definition at line 132 of file sdl/PlatformTimer.cpp.

133{
134#ifdef PEBL_EMSCRIPTEN
135 // Emscripten: yield to browser to allow browser events to be processed
136 emscripten_sleep(msecs);
137#elif defined(PEBL_UNIX)
138 // Unix/Linux: nanosleep (high precision, yields CPU)
139 struct timespec a, b;
140 a.tv_sec = msecs / 1000;
141 a.tv_nsec = (msecs % 1000) * 1000000; // Convert remaining ms to nanoseconds
142 nanosleep(&a, &b);
143#elif defined(PEBL_WIN32) || defined(PEBL_WINDOWS)
144 // Windows: SDL_Delay (yields CPU, ~1ms precision)
145 SDL_Delay(msecs);
146#else
147 // Fallback: use SDL_Delay on unknown platforms
148 SDL_Delay(msecs);
149#endif
150}

Referenced by PEventLoop::Loop(), and PEBLObjects::RecordToBuffer().

◆ Sleep() [2/2]

virtual void PlatformTimer::Sleep ( unsigned long int  msecs)
virtual

Implements PTimer.

◆ Wait() [1/2]

void PlatformTimer::Wait ( unsigned long int  msecs)
virtual

Implements PTimer.

Definition at line 73 of file sdl/PlatformTimer.cpp.

78{
80
81 //This should check for wrapping at ~49 days.
82
83 unsigned long int timeX = SDL_GetTicks() + msecs;
84 unsigned long int missedticks = 0;
85 unsigned long int missedms = 0;
86 unsigned long int lasttime = 0;
87
88 unsigned long int time=SDL_GetTicks();
89
90 while(true)
91 {
92 lasttime = time;
93
94 time = SDL_GetTicks();
95 if((time-lasttime) > 1)
96 {
97 missedticks++;
98 missedms += (time-lasttime);
99 }
100
101
102 if(time > timeX)
103 {
104
105 cerr << "Timer missed [" << missedticks << "] ticks, for a total of [" << missedms << "] ms, on a wait of [" << msecs <<"] ms.\n";
106 break;
107 }
108 }
109}
110#else
111{
112 //This does a busy wait; could be augmented with interrupts or RTC calls if necessary.
113 //This should check for wrapping at ~49 days.
114
115 unsigned long int timeX = SDL_GetTicks() + msecs;
116 unsigned long int time;
117
118 while(true)
119 {
120 time = SDL_GetTicks();
121 if(time > timeX)
122 break;
123
124 }
125}

◆ Wait() [2/2]

virtual void PlatformTimer::Wait ( unsigned long int  msecs)
virtual

Implements PTimer.


The documentation for this class was generated from the following files: