Plaquette
 
Loading...
Searching...
No Matches
pq_time.h
1/*
2 * pq_time.h
3 *
4 * Time functions for Plaquette.
5 *
6 * (c) 2016 Sofian Audry :: info(@)sofianaudry(.)com
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#ifndef PQ_TIME_H_
23#define PQ_TIME_H_
24
25#include <stdint.h>
26
27namespace pq {
28
29// Time constants.
30#define MILLIS_PER_MICROS 1000
31#define MICROS_PER_MILLIS 1000
32#define MILLIS_PER_SECOND 1000
33#define MICROS_PER_SECOND 1000000UL
34
35#define SECONDS_PER_MINUTE 60
36#define SECONDS_PER_HOUR 3600
37#define SECONDS_PER_DAY 86400
38
39#define MINUTES_PER_HOUR 60
40#define HOURS_PER_DAY 24
41
42#define MILLIS_TO_MICROS 1e3f
43#define SECONDS_TO_MILLIS 1e3f
44#define SECONDS_TO_MICROS 1e6f
45
46#define MICROS_TO_MILLIS 1e-3f
47#define MILLIS_TO_SECONDS 1e-3f
48#define MICROS_TO_SECONDS 1e-6f
49
50constexpr float BPM_TO_HZ = 1.0f / SECONDS_PER_MINUTE;
51#define HZ_TO_BPM 60.0f
52
53// Special type to store time in microseconds.
54// Allows dealing with micros() timer overflow to represent time as a 64-bit value.
55typedef union {
56 // 64 bits version.
57 uint64_t micros64;
58
59 // 32 bits split version.
60 struct {
61 uint32_t base;
62 uint32_t overflows;
63 } micros32;
64}
65micro_seconds_t; // type name
66
74float seconds(bool referenceTime=true);
75
83uint32_t milliSeconds(bool referenceTime=true);
84
92uint64_t microSeconds(bool referenceTime=true);
93
99inline uint32_t microsToMillis(uint64_t micros) { return micros / MICROS_PER_MILLIS; }
100
106inline float microsToSeconds(uint64_t micros) { return micros * MICROS_TO_SECONDS; }
107
113inline uint64_t millisToMicros(uint32_t millis) { return static_cast<uint64_t>(millis * MICROS_PER_MILLIS); }
114
120inline float millisToSeconds(uint32_t millis) { return millis * MILLIS_TO_SECONDS; }
121
127inline uint64_t secondsToMicros(float secs) { return static_cast<uint64_t>(secs * SECONDS_TO_MICROS); }
128
134inline uint32_t secondsToMillis(float secs) { return static_cast<uint32_t>(secs * SECONDS_TO_MILLIS); }
135
141inline float minutesToSeconds(float minutes) { return minutes * SECONDS_PER_MINUTE; }
142
148inline float hoursToSeconds(float hours) { return hours * SECONDS_PER_HOUR; }
149
155inline float daysToSeconds(float days) { return days * SECONDS_PER_DAY; }
156
157} // namespace pq
158
159#endif
Definition pq_time.h:55