Entropy Piano Tuner  1.1.3 (documentation not yet complete)
An open-source experimental software for piano tuning by entropy minimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
timer.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright 2015 Haye Hinrichsen, Christoph Wick
3  *
4  * This file is part of Entropy Piano Tuner.
5  *
6  * Entropy Piano Tuner is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * Entropy Piano Tuner is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * Entropy Piano Tuner. If not, see http://www.gnu.org/licenses/.
18  *****************************************************************************/
19 
20 //=============================================================================
21 // Timer
22 //=============================================================================
23 
24 #include "timer.h"
25 
26 #include <thread>
27 
28 #include "prerequisites.h"
29 
30 //-----------------------------------------------------------------------------
31 // constructor
32 //-----------------------------------------------------------------------------
33 
35 
37 {
38  reset();
39 }
40 
41 
42 //-----------------------------------------------------------------------------
43 // reset timer
44 //-----------------------------------------------------------------------------
45 
49 
50 void Timer::reset ()
51 {
52  mStart = std::chrono::system_clock::now();
53 }
54 
55 //-----------------------------------------------------------------------------
56 // sleep in idle mode
57 //-----------------------------------------------------------------------------
58 
66 
67 void Timer::wait(int milliseconds)
68 {
69  std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
70 }
71 
72 
73 //-----------------------------------------------------------------------------
74 // Get elapsed time in milliseconds
75 //-----------------------------------------------------------------------------
76 
78 {
79  auto now = std::chrono::system_clock::now();
80  return std::chrono::duration_cast<std::chrono::milliseconds>(now-mStart).count();
81 }
82 
83 
84 //-----------------------------------------------------------------------------
85 // Check for timeout
86 //-----------------------------------------------------------------------------
87 
93 
94 bool Timer::timeout (int64_t milliseconds)
95 {
96  int64_t delta_t = getMilliseconds();
97  return (delta_t > milliseconds);
98 }
99 
100 
101 //-----------------------------------------------------------------------------
102 // Wait a certain minimum time span since last reset
103 //-----------------------------------------------------------------------------
104 
111 
112 void Timer::waitUntil (int64_t milliseconds, int interval_ms)
113 {
114  while (not timeout(milliseconds)) wait(interval_ms);
115 }
116 
117 
std::chrono::time_point< std::chrono::system_clock > mStart
Definition: timer.h:52
Timer()
Constructor, resets the timer.
Definition: timer.cpp:36
bool timeout(int64_t milliseconds)
Check for timeout.
Definition: timer.cpp:94
void waitUntil(int64_t milliseconds, int interval_ms=5)
Wait in idle mode for a certain minimum time span since last reset.
Definition: timer.cpp:112
void wait(int milliseconds)
Wait in idle mode without consuming CPU time.
Definition: timer.cpp:67
int64_t getMilliseconds()
Definition: timer.cpp:77
void reset()
Reset the timer and remember the current time locally.
Definition: timer.cpp:50