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
50// Number of seconds in one full 32-bit micros() overflow period (2^32 microseconds).
51// Used to convert the overflow counter to seconds without 64-bit arithmetic.
52constexpr float MICROS32_OVERFLOW_TO_SECONDS = 4294967296.0f * MICROS_TO_SECONDS;
53
54constexpr float BPM_TO_HZ = 1.0f / SECONDS_PER_MINUTE;
55#define HZ_TO_BPM 60.0f
56
57// Special type to store time in microseconds.
58// Allows dealing with micros() timer overflow to represent time as a 64-bit value.
59typedef union {
60 // 64 bits version.
61 uint64_t micros64;
62
63 // 32 bits split version.
64 struct {
65 uint32_t base;
66 uint32_t overflows;
67 } micros32;
68}
69micro_seconds_t; // type name
70
78float seconds(bool referenceTime=true);
79
87uint32_t milliSeconds(bool referenceTime=true);
88
96uint64_t microSeconds(bool referenceTime=true);
97
103inline uint32_t microsToMillis(uint64_t micros) { return micros / MICROS_PER_MILLIS; }
104
110inline float microsToSeconds(uint64_t micros) { return micros * MICROS_TO_SECONDS; }
111
117inline uint64_t millisToMicros(uint32_t millis) { return static_cast<uint64_t>(millis * MICROS_PER_MILLIS); }
118
124inline float millisToSeconds(uint32_t millis) { return millis * MILLIS_TO_SECONDS; }
125
131inline uint64_t secondsToMicros(float secs) { return static_cast<uint64_t>(secs * SECONDS_TO_MICROS); }
132
138inline uint32_t secondsToMillis(float secs) { return static_cast<uint32_t>(secs * SECONDS_TO_MILLIS); }
139
145inline float minutesToSeconds(float minutes) { return minutes * SECONDS_PER_MINUTE; }
146
152inline float hoursToSeconds(float hours) { return hours * SECONDS_PER_HOUR; }
153
159inline float daysToSeconds(float days) { return days * SECONDS_PER_DAY; }
160
161} // namespace pq
162
163#endif
Definition pq_time.h:59