NMEA2000 Library  0.1
Library to handle NMEA 2000 Communication written in C++
N2kTimer.h
Go to the documentation of this file.
1#ifndef _N2k_TIMER_H_
2#define _N2k_TIMER_H_
3
4/*
5 * N2kTimer.h
6 *
7 * Copyright (c) 2022-2024 Timo Lappalainen, Kave Oy, www.kave.fi
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25*/
26
27/*************************************************************************/
63#include <stdint.h>
64#include <limits.h>
65#include "N2kDef.h"
66
67#if defined(ARDUINO_ARCH_ESP32) || defined(ESP_PLATFORM)
68#include <esp_timer.h>
69#include <esp_attr.h>
70 inline uint64_t N2kMillis64() {
71 return esp_timer_get_time()/1000ULL;
72 }
73 inline uint32_t N2kMillis() { return N2kMillis64(); }
74#elif defined(ARDUINO)
75 #include <Arduino.h>
76 uint64_t N2kMillis64();
77 inline uint32_t N2kMillis() { return millis(); }
78#else
79 uint64_t N2kMillis64();
80 uint32_t N2kMillis();
81#endif
82
83#define N2kScheduler64Disabled 0xffffffffffffffffULL
84
85/************************************************************************/
96inline bool N2kIsTimeBefore(uint32_t T1, uint32_t T2) { return (T2-T1)<INT32_MAX; }
97
98/************************************************************************/
111inline bool N2kHasElapsed(uint32_t Start, uint32_t Elapsed, uint32_t Now=N2kMillis()) {
112 return Now-(Start+Elapsed)<INT32_MAX;
113}
114
115/************************************************************************/
125protected:
127 static uint64_t SyncOffset;
129 uint64_t NextTime;
131 uint32_t Offset;
133 uint32_t Period;
134public:
135
136 /************************************************************************/
145 tN2kSyncScheduler(bool Enable=false, uint32_t _Period=0, uint32_t _Offset=0) {
146 Offset=_Offset; Period=_Period;
147 if ( Enable ) {
149 } else {
150 Disable();
151 }
152 }
153
154 /************************************************************************/
158
159 /************************************************************************/
165 inline bool IsDisabled() const { return NextTime==N2kScheduler64Disabled; }
166
167 /************************************************************************/
173 inline bool IsEnabled() const { return NextTime!=N2kScheduler64Disabled; }
174
175 /************************************************************************/
181 inline bool IsTime() { return N2kMillis64()>NextTime; }
182
183 /************************************************************************/
188 inline uint64_t Remaining() { return IsTime()?0:NextTime-N2kMillis64(); }
189
190 /************************************************************************/
195 void SetPeriod(uint32_t _Period) { Period=_Period; if ( Period<1 ) Period=1; UpdateNextTime(); }
196
197 /************************************************************************/
202 void SetOffset(uint32_t _Offset) { Offset=_Offset; UpdateNextTime(); }
203
204 /************************************************************************/
210 void SetPeriodAndOffset(uint32_t _Period, uint32_t _Offset) {
211 Period=_Period;
212 Offset=_Offset;
213 if ( Period==0 ) Disable();
215 }
216
217 /************************************************************************/
222 uint32_t GetOffset() const { return Offset; }
223
224 /************************************************************************/
229 uint32_t GetPeriod() const { return Period; }
230
231 /************************************************************************/
236 uint64_t GetNextTime() const { return NextTime; }
237
238 /************************************************************************/
243 if ( Period==0 ) {
244 Disable();
245 return;
246 }
247 uint64_t now=N2kMillis64();
248 if ( (Offset+SyncOffset)>now ) {
250 } else {
251 uint64_t n=(now-(Offset+SyncOffset))/Period;
253 }
254 }
255 /************************************************************************/
260};
261
262#if defined(ARDUINO_ARCH_ESP32) || defined(ESP_PLATFORM) || defined(__linux__) || defined(__linux) || defined(linux)
264 #define N2kUse64bitSchedulerTime 1
266 #define N2kSchedulerDisabled 0xffffffffffffffffULL
267 using tN2kSchedulerTime=uint64_t;
268#else
269 #include <limits.h>
271 #define N2kSchedulerDisabled 0xffffffffUL
272 using tN2kSchedulerTime=uint32_t;
273#endif
274
275//------------------------------------------------------------------------------
276//
277/************************************************************************/
287protected:
288
291 #if !defined(N2kUse64bitSchedulerTime)
293 #endif
294public:
295
296 /************************************************************************/
302
303 /************************************************************************/
306 void Disable() {
308 }
309
310 /************************************************************************/
316 bool IsDisabled() const { return NextTime==N2kSchedulerDisabled; }
317
318 /************************************************************************/
324 bool IsEnabled() const { return NextTime!=N2kSchedulerDisabled; }
325
326 /************************************************************************/
332 bool IsTime() const {
333 #if defined(N2kUse64bitSchedulerTime)
334 return N2kMillis64()>NextTime;
335 #else
336 return !IsDisabled() && ( N2kMillis()-NextTime<INT32_MAX );
337 #endif
338 }
339
340 /************************************************************************/
345 void FromNow(uint32_t _Add) {
346 #if defined(N2kUse64bitSchedulerTime)
347 NextTime=N2kMillis64()+_Add;
348 #else
349 NextTime=Now()+_Add;
350 if ( NextTime==N2kSchedulerDisabled ) NextTime=0; // Roll over
351 #endif
352 }
353};
354
355#endif
Type definitions and utility macros used in the NMEA2000 libraries.
uint32_t millis()
#define N2kSchedulerDisabled
Defined Value for disabling the scheduler.
Definition: N2kTimer.h:271
#define N2kScheduler64Disabled
Definition: N2kTimer.h:83
uint64_t N2kMillis64()
Definition: N2kTimer.cpp:51
bool N2kHasElapsed(uint32_t Start, uint32_t Elapsed, uint32_t Now=N2kMillis())
Has time elapsed since start.
Definition: N2kTimer.h:111
uint32_t N2kMillis()
Definition: N2kTimer.cpp:48
bool N2kIsTimeBefore(uint32_t T1, uint32_t T2)
Comparing 2 values even after 32 bit time roll over situation.
Definition: N2kTimer.h:96
uint32_t tN2kSchedulerTime
Definition: N2kTimer.h:272
Un synchronized Scheduler which can roll over.
Definition: N2kTimer.h:286
tN2kSchedulerTime Now()
Definition: N2kTimer.cpp:63
void Disable()
Disable the Scheduler.
Definition: N2kTimer.h:306
bool IsDisabled() const
Check if the scheduler is disabled.
Definition: N2kTimer.h:316
bool IsEnabled() const
Check if the scheduler is enabled.
Definition: N2kTimer.h:324
tN2kSchedulerTime NextTime
Timestamp for next event.
Definition: N2kTimer.h:290
tN2kScheduler(tN2kSchedulerTime _NextTime=N2kSchedulerDisabled)
Constructor.
Definition: N2kTimer.h:301
void FromNow(uint32_t _Add)
Set Timestamp for next event relative to now.
Definition: N2kTimer.h:345
bool IsTime() const
Is it time for the next event.
Definition: N2kTimer.h:332
Synchronized Scheduler for timed message sending.
Definition: N2kTimer.h:124
uint32_t Period
Period for scheduler.
Definition: N2kTimer.h:133
static uint64_t SyncOffset
???
Definition: N2kTimer.h:127
uint32_t Offset
Offset to synchronize with others.
Definition: N2kTimer.h:131
uint64_t NextTime
Timestamp for next event.
Definition: N2kTimer.h:129
static void SetSyncOffset()
Set the SyncOffset of the scheduler.
Definition: N2kTimer.h:259
uint64_t GetNextTime() const
Get the Timestamp of the NextTime.
Definition: N2kTimer.h:236
void SetOffset(uint32_t _Offset)
Set the Offset of the Scheduler.
Definition: N2kTimer.h:202
bool IsDisabled() const
Checks if Scheduler is disabled.
Definition: N2kTimer.h:165
uint64_t Remaining()
Calculate remaining time.
Definition: N2kTimer.h:188
void SetPeriodAndOffset(uint32_t _Period, uint32_t _Offset)
Set the Period And Offset of the Scheduler.
Definition: N2kTimer.h:210
bool IsTime()
Check if ít is time.
Definition: N2kTimer.h:181
bool IsEnabled() const
Checks if Scheduler is enabled.
Definition: N2kTimer.h:173
void SetPeriod(uint32_t _Period)
Set the Period of the Scheduler.
Definition: N2kTimer.h:195
uint32_t GetOffset() const
Get the Offset of the Scheduler.
Definition: N2kTimer.h:222
uint32_t GetPeriod() const
Get the Period of the Scheduler.
Definition: N2kTimer.h:229
void UpdateNextTime()
Update the timestamp for NextTime.
Definition: N2kTimer.h:242
tN2kSyncScheduler(bool Enable=false, uint32_t _Period=0, uint32_t _Offset=0)
Constructor for sync scheduler.
Definition: N2kTimer.h:145
void Disable()
Disable Scheduler.
Definition: N2kTimer.h:157