FastLED 3.9.7
Loading...
Searching...
No Matches
Timekeeping Functions

Detailed Description

Tools for tracking and working with time

Classes

class  CEveryNTime
 Time interval checking class. More...
 
class  CEveryNMillisDynamic
 Create the CEveryNMillisDynamic class for dynamic millisecond intervals. More...
 

Macros

#define GET_MILLIS   millis
 The a number of functions need access to a millisecond counter in order to keep time.
 
#define INSTANTIATE_EVERY_N_TIME_PERIODS(NAME, TIMETYPE, TIMEGETTER)
 Preprocessor-based class "template" for CEveryNTime, used with EVERY_N_TIME timekeepers.
 
#define USE_GET_MILLISECOND_TIMER
 Set this flag to use the get_millisecond_timer() function in place of the default millis() function.
 

Functions

LIB8STATIC uint16_t seconds16 ()
 Return the current seconds since boot in a 16-bit value.
 
LIB8STATIC uint16_t minutes16 ()
 Return the current minutes since boot in a 16-bit value.
 
LIB8STATIC uint8_t hours8 ()
 Return the current hours since boot in an 8-bit value.
 
LIB8STATIC uint16_t div1024_32_16 (uint32_t in32)
 Helper routine to divide a 32-bit value by 1024, returning only the low 16 bits.
 
LIB8STATIC uint16_t bseconds16 ()
 Returns the current time-since-boot in "binary seconds", which are actually 1024/1000 of a second long.
 

CEveryNTime Base Classes

These macros define the time interval checking classes used in the EVERY_N_TIME time macros.

 INSTANTIATE_EVERY_N_TIME_PERIODS (CEveryNMillis, uint32_t, GET_MILLIS)
 Create the CEveryNMillis class for millisecond intervals.
 
 INSTANTIATE_EVERY_N_TIME_PERIODS (CEveryNSeconds, uint16_t, seconds16)
 Create the CEveryNSeconds class for second intervals.
 
 INSTANTIATE_EVERY_N_TIME_PERIODS (CEveryNBSeconds, uint16_t, bseconds16)
 Create the CEveryNBSeconds class for bsecond intervals.
 
 INSTANTIATE_EVERY_N_TIME_PERIODS (CEveryNMinutes, uint16_t, minutes16)
 Create the CEveryNMinutes class for minutes intervals.
 
 INSTANTIATE_EVERY_N_TIME_PERIODS (CEveryNHours, uint8_t, hours8)
 Create the CEveryNHours class for hours intervals.
 
#define CEveryNMilliseconds   CEveryNMillis
 Alias for CEveryNMillis.
 

"EVERY_N_TIME" Macros

Check whether to excecute a block of code every N amount of time.

These are useful for limiting how often code runs. For example, you can use fill_rainbow() to fill a strip of LEDs with color, combined with an EVERY_N_MILLIS block to limit how fast the colors change:

static uint8_t hue = 0;
fill_rainbow(leds, NUM_LEDS, hue);
EVERY_N_MILLIS(20) { hue++; } // advances hue every 20 milliseconds
void fill_rainbow(struct CRGB *targetArray, int numToFill, uint8_t initialhue, uint8_t deltahue)
Fill a range of LEDs with a rainbow of colors.
#define EVERY_N_MILLIS(N)
Checks whether to execute a block of code every N milliseconds.
Definition lib8tion.h:1274

Note that in order for these to be accurate, the EVERY_N block must be evaluated at a regular basis.

#define EVERY_N_MILLIS(N)   EVERY_N_MILLIS_I(CONCAT_MACRO(PER, __COUNTER__ ),N)
 Checks whether to execute a block of code every N milliseconds.
 
#define EVERY_N_MILLIS_I(NAME, N)   static CEveryNMillis NAME(N); if( NAME )
 Checks whether to execute a block of code every N milliseconds, using a custom instance name.
 
#define EVERY_N_SECONDS(N)   EVERY_N_SECONDS_I(CONCAT_MACRO(PER, __COUNTER__ ),N)
 Checks whether to execute a block of code every N seconds.
 
#define EVERY_N_SECONDS_I(NAME, N)   static CEveryNSeconds NAME(N); if( NAME )
 Checks whether to execute a block of code every N seconds, using a custom instance name.
 
#define EVERY_N_BSECONDS(N)   EVERY_N_BSECONDS_I(CONCAT_MACRO(PER, __COUNTER__ ),N)
 Checks whether to execute a block of code every N bseconds.
 
#define EVERY_N_BSECONDS_I(NAME, N)   static CEveryNBSeconds NAME(N); if( NAME )
 Checks whether to execute a block of code every N bseconds, using a custom instance name.
 
#define EVERY_N_MINUTES(N)   EVERY_N_MINUTES_I(CONCAT_MACRO(PER, __COUNTER__ ),N)
 Checks whether to execute a block of code every N minutes.
 
#define EVERY_N_MINUTES_I(NAME, N)   static CEveryNMinutes NAME(N); if( NAME )
 Checks whether to execute a block of code every N minutes, using a custom instance name.
 
#define EVERY_N_HOURS(N)   EVERY_N_HOURS_I(CONCAT_MACRO(PER, __COUNTER__ ),N)
 Checks whether to execute a block of code every N hours.
 
#define EVERY_N_HOURS_I(NAME, N)   static CEveryNHours NAME(N); if( NAME )
 Checks whether to execute a block of code every N hours, using a custom instance name.
 
#define EVERY_N_MILLISECONDS(N)   EVERY_N_MILLIS(N)
 Alias for EVERY_N_MILLIS.
 
#define EVERY_N_MILLISECONDS_I(NAME, N)   EVERY_N_MILLIS_I(NAME,N)
 Alias for EVERY_N_MILLIS_I.
 
#define EVERY_N_MILLISECONDS_DYNAMIC(PERIOD_FUNC)   EVERY_N_MILLISECONDS_DYNAMIC_I(CONCAT_MACRO(__dynamic_millis_timer, __COUNTER__ ), (PERIOD_FUNC))
 Checks whether to execute a block of code every N milliseconds, where N is determined dynamically.
 
#define EVERY_N_MILLISECONDS_DYNAMIC_I(NAME, PERIOD_FUNC)
 Checks whether to execute a block of code every N milliseconds, where N is determined dynamically, using a custom instance name.
 

Macro Definition Documentation

◆ CEveryNMilliseconds

#define CEveryNMilliseconds   CEveryNMillis

Alias for CEveryNMillis.

Definition at line 1183 of file lib8tion.h.

◆ EVERY_N_BSECONDS

#define EVERY_N_BSECONDS ( N)    EVERY_N_BSECONDS_I(CONCAT_MACRO(PER, __COUNTER__ ),N)

Checks whether to execute a block of code every N bseconds.

See also
bseconds16()

Definition at line 1292 of file lib8tion.h.

◆ EVERY_N_BSECONDS_I

#define EVERY_N_BSECONDS_I ( NAME,
N )   static CEveryNBSeconds NAME(N); if( NAME )

Checks whether to execute a block of code every N bseconds, using a custom instance name.

See also
bseconds16()

Definition at line 1296 of file lib8tion.h.

◆ EVERY_N_HOURS

#define EVERY_N_HOURS ( N)    EVERY_N_HOURS_I(CONCAT_MACRO(PER, __COUNTER__ ),N)

Checks whether to execute a block of code every N hours.

See also
hours8()

Definition at line 1310 of file lib8tion.h.

◆ EVERY_N_HOURS_I

#define EVERY_N_HOURS_I ( NAME,
N )   static CEveryNHours NAME(N); if( NAME )

Checks whether to execute a block of code every N hours, using a custom instance name.

See also
hours8()

Definition at line 1314 of file lib8tion.h.

◆ EVERY_N_MILLIS

#define EVERY_N_MILLIS ( N)    EVERY_N_MILLIS_I(CONCAT_MACRO(PER, __COUNTER__ ),N)

Checks whether to execute a block of code every N milliseconds.

See also
GET_MILLIS

Definition at line 1274 of file lib8tion.h.

◆ EVERY_N_MILLIS_I

#define EVERY_N_MILLIS_I ( NAME,
N )   static CEveryNMillis NAME(N); if( NAME )

Checks whether to execute a block of code every N milliseconds, using a custom instance name.

See also
GET_MILLIS

Definition at line 1278 of file lib8tion.h.

◆ EVERY_N_MILLISECONDS

#define EVERY_N_MILLISECONDS ( N)    EVERY_N_MILLIS(N)

Alias for EVERY_N_MILLIS.

Definition at line 1318 of file lib8tion.h.

◆ EVERY_N_MILLISECONDS_DYNAMIC

#define EVERY_N_MILLISECONDS_DYNAMIC ( PERIOD_FUNC)    EVERY_N_MILLISECONDS_DYNAMIC_I(CONCAT_MACRO(__dynamic_millis_timer, __COUNTER__ ), (PERIOD_FUNC))

Checks whether to execute a block of code every N milliseconds, where N is determined dynamically.

Definition at line 1323 of file lib8tion.h.

◆ EVERY_N_MILLISECONDS_DYNAMIC_I

#define EVERY_N_MILLISECONDS_DYNAMIC_I ( NAME,
PERIOD_FUNC )
Value:
static CEveryNMillisDynamic NAME(1); \
NAME.setPeriod(PERIOD_FUNC); \
if( NAME )
Create the CEveryNMillisDynamic class for dynamic millisecond intervals.
Definition lib8tion.h:1186

Checks whether to execute a block of code every N milliseconds, where N is determined dynamically, using a custom instance name.

Definition at line 1326 of file lib8tion.h.

◆ EVERY_N_MILLISECONDS_I

#define EVERY_N_MILLISECONDS_I ( NAME,
N )   EVERY_N_MILLIS_I(NAME,N)

Alias for EVERY_N_MILLIS_I.

Definition at line 1320 of file lib8tion.h.

◆ EVERY_N_MINUTES

#define EVERY_N_MINUTES ( N)    EVERY_N_MINUTES_I(CONCAT_MACRO(PER, __COUNTER__ ),N)

Checks whether to execute a block of code every N minutes.

See also
minutes16()

Definition at line 1301 of file lib8tion.h.

◆ EVERY_N_MINUTES_I

#define EVERY_N_MINUTES_I ( NAME,
N )   static CEveryNMinutes NAME(N); if( NAME )

Checks whether to execute a block of code every N minutes, using a custom instance name.

See also
minutes16()

Definition at line 1305 of file lib8tion.h.

◆ EVERY_N_SECONDS

#define EVERY_N_SECONDS ( N)    EVERY_N_SECONDS_I(CONCAT_MACRO(PER, __COUNTER__ ),N)

Checks whether to execute a block of code every N seconds.

See also
seconds16()
Examples
FxEngine.ino.

Definition at line 1283 of file lib8tion.h.

◆ EVERY_N_SECONDS_I

#define EVERY_N_SECONDS_I ( NAME,
N )   static CEveryNSeconds NAME(N); if( NAME )

Checks whether to execute a block of code every N seconds, using a custom instance name.

See also
seconds16()

Definition at line 1287 of file lib8tion.h.

◆ GET_MILLIS

#define GET_MILLIS   millis

The a number of functions need access to a millisecond counter in order to keep time.

On Arduino, this is "millis()". On other platforms, you'll need to provide a function with this signature which provides similar functionality:

uint32_t get_millisecond_timer();

You can also force use of the get_millisecond_timer() function by #defining USE_GET_MILLISECOND_TIMER.

Definition at line 837 of file lib8tion.h.

◆ INSTANTIATE_EVERY_N_TIME_PERIODS

#define INSTANTIATE_EVERY_N_TIME_PERIODS ( NAME,
TIMETYPE,
TIMEGETTER )
Value:
class NAME { \
public: \
TIMETYPE mPrevTrigger; \
TIMETYPE mPeriod; \
\
NAME() { reset(); mPeriod = 1; }; \
NAME(TIMETYPE period) { reset(); setPeriod(period); }; \
void setPeriod( TIMETYPE period) { mPeriod = period; }; \
TIMETYPE getTime() { return (TIMETYPE)(TIMEGETTER()); }; \
TIMETYPE getPeriod() { return mPeriod; }; \
TIMETYPE getElapsed() { return getTime() - mPrevTrigger; } \
TIMETYPE getRemaining() { return mPeriod - getElapsed(); } \
TIMETYPE getLastTriggerTime() { return mPrevTrigger; } \
bool ready() { \
bool isReady = (getElapsed() >= mPeriod); \
if( isReady ) { reset(); } \
return isReady; \
} \
void reset() { mPrevTrigger = getTime(); }; \
void trigger() { mPrevTrigger = getTime() - mPeriod; }; \
\
operator bool() { return ready(); } \
};

Preprocessor-based class "template" for CEveryNTime, used with EVERY_N_TIME timekeepers.

Classes to implement EVERY_N_MILLIS, EVERY_N_SECONDS, EVERY_N_MINUTES, EVERY_N_HOURS, and EVERY_N_BSECONDS.

Definition at line 1077 of file lib8tion.h.

◆ USE_GET_MILLISECOND_TIMER

#define USE_GET_MILLISECOND_TIMER

Set this flag to use the get_millisecond_timer() function in place of the default millis() function.

Definition at line 1341 of file lib8tion.h.

Function Documentation

◆ bseconds16()

LIB8STATIC uint16_t bseconds16 ( )

Returns the current time-since-boot in "binary seconds", which are actually 1024/1000 of a second long.

Definition at line 1065 of file lib8tion.h.

◆ div1024_32_16()

LIB8STATIC uint16_t div1024_32_16 ( uint32_t in32)

Helper routine to divide a 32-bit value by 1024, returning only the low 16 bits.

You'd think this would be just

result = (in32 >> 10) & 0xFFFF;

And on ARM, that's what you want and all is well. But on AVR that code turns into a loop that executes a four-byte shift ten times: 40 shifts in all, plus loop overhead. This routine gets exactly the same result with just six shifts (vs 40), and no loop overhead. Used to convert millis to "binary seconds" aka bseconds: one bsecond == 1024 millis.

Definition at line 1040 of file lib8tion.h.

◆ hours8()

LIB8STATIC uint8_t hours8 ( )

Return the current hours since boot in an 8-bit value.

Used as part of the "every N time-periods" mechanism

Definition at line 1018 of file lib8tion.h.

◆ minutes16()

LIB8STATIC uint16_t minutes16 ( )

Return the current minutes since boot in a 16-bit value.

Used as part of the "every N time-periods" mechanism

Definition at line 1008 of file lib8tion.h.

◆ seconds16()

LIB8STATIC uint16_t seconds16 ( )

Return the current seconds since boot in a 16-bit value.

Used as part of the "every N time-periods" mechanism

Definition at line 998 of file lib8tion.h.