NMEA2000 Library  0.1
Library to handle NMEA 2000 Communication written in C++
N2kTimer.h File Reference

The file contains function and classes for best timing performance. More...

#include <stdint.h>
#include <limits.h>
#include "N2kDef.h"
Include dependency graph for N2kTimer.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  tN2kScheduler
 Un synchronized Scheduler which can roll over. More...
 
class  tN2kSyncScheduler
 Synchronized Scheduler for timed message sending. More...
 

Macros

#define N2kScheduler64Disabled   0xffffffffffffffffULL
 
#define N2kSchedulerDisabled   0xffffffffUL
 Defined Value for disabling the scheduler. More...
 

Typedefs

using tN2kSchedulerTime = uint32_t
 

Functions

bool N2kHasElapsed (uint32_t Start, uint32_t Elapsed, uint32_t Now=N2kMillis())
 Has time elapsed since start. More...
 
bool N2kIsTimeBefore (uint32_t T1, uint32_t T2)
 Comparing 2 values even after 32 bit time roll over situation. More...
 
uint32_t N2kMillis ()
 
uint64_t N2kMillis64 ()
 

Detailed Description

The file contains function and classes for best timing performance.

Module is designed to hide millis() function from library and provide best performance way for timings.

N2kMillis64() is 64 bit millisecond timer, which practically never roll over.

tN2kSyncScheduler uses 64 bit timer for message scheduling. It has offset as defined by NMEA2000. It uses global offset for synchronizing messages to open. Use this for scheduling message sending.

For proper synchronization you define tN2kSyncScheduler for each of your messages as disabled (default constructor) at beginning. On library setup define OnOpen call back with tNMEA2000::SetOnOpen. When OnOpen will be called, it initializes each message scheduler with period and offset. As default offsets it is good to use different value for each message so that your system will not try to send them exactly at same time. For single frame message 5-10 ms offset difference is enough. For message after multiframe message offset should be (multiframe message frame count)/2+(5 to 10). NMEA encourages manufacturers to provide the capability of altering, save and restore period and offset for NMEA certified products on the network. See tN2kGroupFunctionHandlerForPGN126993::HandleRequest in module N2kGroupFunctionDefaultHandlers

tN2kScheduler uses fastest method for scheduling next event. Tested for Teensy and ESP32.

Both schedulers solves roll over problem, which was caused with simple use of millis()

Definition in file N2kTimer.h.

Macro Definition Documentation

◆ N2kScheduler64Disabled

#define N2kScheduler64Disabled   0xffffffffffffffffULL

Definition at line 83 of file N2kTimer.h.

◆ N2kSchedulerDisabled

#define N2kSchedulerDisabled   0xffffffffUL

Defined Value for disabling the scheduler.

Definition at line 271 of file N2kTimer.h.

Typedef Documentation

◆ tN2kSchedulerTime

using tN2kSchedulerTime = uint32_t

Definition at line 272 of file N2kTimer.h.

Function Documentation

◆ N2kHasElapsed()

bool N2kHasElapsed ( uint32_t  Start,
uint32_t  Elapsed,
uint32_t  Now = N2kMillis() 
)
inline

Has time elapsed since start.

N2kHasElapsed tests has time elapsed since start. Test works even after 32 bit time roll over situation.

Parameters
StartStart time
ElapsedHow much should be elapsed from start
NowCompare (current) time, optional, default N2kMillis()
Returns
true when Now>Start+Elapsed
false

Definition at line 111 of file N2kTimer.h.

◆ N2kIsTimeBefore()

bool N2kIsTimeBefore ( uint32_t  T1,
uint32_t  T2 
)
inline

Comparing 2 values even after 32 bit time roll over situation.

N2kIsTimeBefore is for comparing T1 and T2 even after 32 bit time roll over situation.

Parameters
T1Time value 1
T2Time value 2
Returns
true when T1<T2
false

Definition at line 96 of file N2kTimer.h.

◆ N2kMillis()

uint32_t N2kMillis ( )

Definition at line 48 of file N2kTimer.cpp.

◆ N2kMillis64()

uint64_t N2kMillis64 ( )

Definition at line 51 of file N2kTimer.cpp.