roo_time
API Documentation for roo_time
Loading...
Searching...
No Matches
uptime_now.cpp
Go to the documentation of this file.
1#include "roo_time.h"
2
3#if defined(ROO_TESTING)
4
5#include "roo_testing/system/timer.h"
6
7inline static int64_t __uptime() { return system_time_get_micros(); }
8
9inline static void __delayMicros(int64_t micros) {
10 system_time_delay_micros(micros);
11}
12
13#elif defined(ESP32)
14
15#include <Arduino.h>
16
17#include "esp_attr.h"
18
19extern "C" {
20int64_t esp_timer_get_time();
21}
22
23inline static IRAM_ATTR int64_t __uptime() { return esp_timer_get_time(); }
24
25#define ROO_TIME_UPTIME_MONOTONE 1
26
27inline static void __delayMicros(int64_t micros) {
28 if (micros < 0) {
29 return;
30 } else if (micros < 2000) {
31 delayMicroseconds(micros);
32 } else {
33 delay(micros / 1000);
34 delayMicroseconds(micros % 1000);
35 }
36}
37
38#elif defined(ESP_PLATFORM)
39
40#include <esp_attr.h>
41#include <freertos/FreeRTOS.h>
42#include <freertos/task.h>
43#include <rom/ets_sys.h>
44
45extern "C" {
46int64_t esp_timer_get_time();
47}
48
49inline static IRAM_ATTR int64_t __uptime() { return esp_timer_get_time(); }
50
51#define ROO_TIME_UPTIME_MONOTONE 1
52
53inline static void __delayMicros(int64_t micros) {
54 if (micros < 0) {
55 return;
56 } else if (micros < 2000) {
57 ets_delay_us(micros);
58 } else {
59 vTaskDelay(pdMS_TO_TICKS(micros / 1000));
60 ets_delay_us(micros % 1000);
61 }
62}
63
64#elif defined(ARDUINO)
65
66#include <Arduino.h>
67
68inline static int64_t __uptime() { return micros(); }
69
70inline static void __delayMicros(int64_t micros) {
71 if (micros < 0) {
72 return;
73 } else if (micros < 2000) {
74 delayMicroseconds(micros);
75 } else {
76 delay(micros / 1000);
77 delayMicroseconds(micros % 1000);
78 }
79}
80
81#elif defined(__linux__)
82
83#include <chrono>
84#include <thread>
85
86inline static int64_t __uptime() {
87 auto now = std::chrono::high_resolution_clock::now();
88 return std::chrono::duration_cast<std::chrono::microseconds>(
89 now.time_since_epoch())
90 .count();
91}
92
93inline static void __delayMicros(int64_t micros) {
94 std::this_thread::sleep_for(std::chrono::microseconds(micros));
95}
96
97#endif
98
99#ifndef IRAM_ATTR
100#define IRAM_ATTR
101#endif
102
103namespace roo_time {
104
105#ifndef ROO_TIME_UPTIME_MONOTONE
106#define ROO_TIME_UPTIME_MONOTONE 0
107#endif
108
109#if ROO_TIME_UPTIME_MONOTONE
110
111const Uptime IRAM_ATTR Uptime::Now() { return Uptime(__uptime()); }
112
113#else // e.g. on Arduino platforms with 32-bit millisecond resolution.
114
115static int64_t last_reading = 0;
116
117// Offset to make sure the time is monotone.
118static int64_t offset = 0;
119
121 int64_t now = __uptime() + offset;
122 int64_t diff = last_reading - now;
123 if (diff > 0) {
124 offset += diff;
125 now += diff;
126 }
127 last_reading = now;
128 return Uptime(now);
129}
130
131#endif
132
133void IRAM_ATTR Delay(Duration duration) { __delayMicros(duration.inMicros()); }
134void IRAM_ATTR DelayUntil(Uptime deadline) { Delay(deadline - Uptime::Now()); }
135
136} // namespace roo_time
Represents an amount of time (e.g. 5s, 10min).
Definition roo_time.h:25
constexpr int64_t inMicros() const
Returns duration in microseconds.
Definition roo_time.h:44
Represents an instant relative to process/boot start time.
Definition roo_time.h:444
Uptime()
Constructs zero uptime value.
Definition roo_time.h:456
static const Uptime Now()
Returns current monotonic process uptime.
Umbrella header for the roo_time module.
Definition roo_time.cpp:3
void DelayUntil(Uptime deadline)
Delays execution until deadline.
static int64_t last_reading
void Delay(Duration duration)
Delays execution for duration.
static int64_t offset
#define IRAM_ATTR