Instrument Neutral Distributed Interface INDI  2.0.2
fpsmeter.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2020 by Pawel Soja <kernel32.pl@gmail.com>
3  FPS Meter
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 
19 */
20 #include "fpsmeter.h"
21 
22 namespace INDI
23 {
24 
25 FPSMeter::FPSMeter(double timeWindow)
26  : mTimeWindow(timeWindow)
27 {
28  reset();
29 }
30 
32 {
33  mFrameTime2 = mFrameTime1;
34  mFrameTime1 = std::chrono::steady_clock::now();
35 
36  ++mTotalFrames;
37  ++mFramesPerElapsedTime;
38 
39  double dt = deltaTime();
40 
41  mElapsedTime += dt;
42  mTotalTime += dt;
43 
44  if (mElapsedTime >= mTimeWindow)
45  {
46  mFramesPerSecond = mFramesPerElapsedTime / mElapsedTime * 1000;
47  mElapsedTime = 0;
48  mFramesPerElapsedTime = 0;
49  return true;
50  }
51 
52  return false;
53 }
54 
55 void FPSMeter::setTimeWindow(double timeWindow)
56 {
57  mTimeWindow = timeWindow;
58 }
59 
61 {
62  return mFramesPerSecond;
63 }
64 
65 double FPSMeter::deltaTime() const
66 {
67  return std::chrono::duration<double>(mFrameTime1 - mFrameTime2).count() * 1000;
68 }
69 
70 uint64_t FPSMeter::totalFrames() const
71 {
72  return mTotalFrames;
73 }
74 
75 double FPSMeter::totalTime() const
76 {
77  return mTotalTime;
78 }
79 
81 {
82  mFramesPerElapsedTime = 0;
83  mElapsedTime = 0;
84  mFrameTime1 = std::chrono::steady_clock::now();
85  mFrameTime2 = mFrameTime1;
86  mFramesPerSecond = 0;
87  mTotalFrames = 0;
88  mTotalTime = 0;
89 }
90 
91 }
FPSMeter(double timeWindow=1000)
Definition: fpsmeter.cpp:25
void reset()
Reset all frame information.
Definition: fpsmeter.cpp:80
void setTimeWindow(double timeWindow)
Time window setup.
Definition: fpsmeter.cpp:55
double deltaTime() const
Time in milliseconds between last frames.
Definition: fpsmeter.cpp:65
double totalTime() const
Total time.
Definition: fpsmeter.cpp:75
uint64_t totalFrames() const
Total frames.
Definition: fpsmeter.cpp:70
double framesPerSecond() const
Number of frames per second counted in the time window.
Definition: fpsmeter.cpp:60
bool newFrame()
When you get a frame, call the function to count.
Definition: fpsmeter.cpp:31
Namespace to encapsulate INDI client, drivers, and mediator classes.