Library provides periodical calling of a procedure (timer handler) without input and output parameters.
- The library does not use system interrupts, just the
millis()
function. - The timer realizes timing at the principle
not sooner than
.
- Arduino.h: Main include file for the Arduino SDK.
- inttypes.h: Integer type conversions. This header file includes the exact-width integer definitions and extends them with additional facilities provided by the implementation.
- Arduino.h: Main include file for the Arduino platform.
The template or the signature of a timer handler function, which is called by a timer at the end of a timer period, or immediatelly at the begining of it respectivelly.
- A handler is just a bare function without any input parameters and returning nothing.
- A handler can be declared just as
void
type. It is not needed to be declared asgbj_timer::Handler
type.
void gbj_timer::Handler()
None
None
Constructor creates the class instance object and initiate a timer.
- Timer handler is a procedure (function) within a sketch that receives no parameters and returns no value.
- Timer without a time handler is suitable for created internal timer objects in classes and run their member functions as handlers separately.
gbj_timer(uint32_t timerPeriod, Handler *timerHandler, bool start, bool halt)
-
timerPeriod: Duration of a repeating interval in milliseconds.
- Valid values: 0 ~ 2^32 - 1
- Default value: none
-
timerHandler: Pointer to a procedure that is called periodically by the timer. If no handler is provided, the timer just runs.
- Valid values: valid address space
- Default value: nullptr
-
start: Flag about immediate starting the timer.
- Valid values: true, false
- true: The timer handler is called at the very first calling the method run().
- false: The timer handler is called the first time after set time period counting from the setting period by this method.
- Default value: false
- Valid values: true, false
-
halt: Flag about immediate halting the timer.
- Valid values: true, false
- true: The timer is halted for the very beginning and the method run() is not called even if the immidiate start is enabled. In order to activate the timer, it should be restarted by restart() or just resumed resume(), if not entire new period is needed.
- false: The timer is in active mode from the very beginning.
- Default value: false
- Valid values: true, false
None
void timerTest() {}
gbj_timer timer = gbj_timer(1000L, timerTest, true);
void loop()
{
timer.run();
}
void timerTest() {}
gbj_timer timer = gbj_timer(1000L);
void loop()
{
if (timer.run()) timerTest();
}
void timerTest() {}
gbj_timer timer = gbj_timer(1000L, timerTest, false, true);
void setup()
{
timer.restart();
}
void loop()
{
timer.run();
}
The method should be called in the LOOP section of a sketch. It evaluates a timer and calls its handler at appripriate time.
- The method does not run a halted timer and one with zero time period.
- The method still measures time, i.e., saves timestamp of its recent running even if in inactive mode.
bool run()
None
Flag about reaching a timer period and some action should be taken.
The method starts new timer period and runs the timer's handler, if defined.
void fire()
None
None
The method suspends (temporary stops) the timer and makes it inactive, so that the timer's handler is not run more.
void halt()
None
None
The method resumes halted (inactive) timer and makes it active, so that the timer runs again.
- Because the timer measures time always, although it is in inactive, it calls next run after timer period, not imediately after resuming.
- Resuming still active timer has no effect.
void resume()
None
None
The method sets the timestamp of the timer to the current time, so that the timer starts counting from beginning of its period.
void reset()
None
None
The wrapper method for resuming and resetting the timer.
void restart()
None
None
The method sets a new timer period. It allows to dynamically change a timer frequency in an application.
void setPeriod(uint32_t timerPeriod)
- timerPeriod: Duration of a repeating interval in milliseconds.
- Valid values: 0 ~ 2^32 - 1
- Default value: none
None
The method returns current timer period, usually set in timerPeriod parameter of the constructor.
uint32_t getPeriod()
None
Current timer period in milliseconds.
The method returns flag about active (running, not halted) timer.
bool isActive()
None
Flag about current timer activity.