@@ -35,34 +35,34 @@ TimerTask::TimerTask() {
3535 tm_internal::atomicWriteBool (&taskInUse, false );
3636}
3737
38- void TimerTask::initialise (sched_t execInfo, TimerUnit unit, TimerFn execCallback) {
39- this ->myTimingSchedule = execInfo;
40- this ->timingInformation = unit;
38+ void TimerTask::initialise (sched_t when, TimerUnit unit, TimerFn execCallback, bool repeating) {
39+ handleScheduling (when, unit, repeating);
4140 this ->callback = execCallback;
4241 this ->executeMode = EXECTYPE_FUNCTION;
42+ }
4343
44- this -> scheduledAt = ( isMicrosSchedule ()) ? micros () : millis ();
44+ void TimerTask::handleScheduling ( sched_t when, TimerUnit unit, bool repeating) {
4545 tm_internal::atomicWritePtr (&next, nullptr );
46+
47+ if (unit == TIME_SECONDS) {
48+ when = when * sched_t (1000 );
49+ unit = TIME_MILLIS;
50+ }
51+ this ->myTimingSchedule = when;
52+ this ->timingInformation = repeating ? TimerUnit (unit | TM_TIME_REPEATING) : unit;
53+ this ->scheduledAt = (isMicrosSchedule ()) ? micros () : millis ();
4654}
4755
48- void TimerTask::initialise (uint32_t execInfo, TimerUnit unit, Executable* execCallback, bool deleteWhenDone) {
49- this ->myTimingSchedule = execInfo;
50- this ->timingInformation = unit;
56+ void TimerTask::initialise (uint32_t when, TimerUnit unit, Executable* execCallback, bool deleteWhenDone, bool repeating) {
57+ handleScheduling (when, unit, repeating);
5158 this ->taskRef = execCallback;
5259 this ->executeMode = deleteWhenDone ? ExecutionType (EXECTYPE_EXECUTABLE | EXECTYPE_DELETE_ON_DONE) : EXECTYPE_EXECUTABLE;
53-
54- this ->scheduledAt = (isMicrosSchedule ()) ? micros () : millis ();
55- tm_internal::atomicWritePtr (&next, nullptr );
5660}
5761
5862void TimerTask::initialiseEvent (BaseEvent* event, bool deleteWhenDone) {
59- this ->timingInformation = (TimerUnit)(TIME_MICROS | TM_TIME_REPEATING);
60- this ->myTimingSchedule = 0 ;
63+ handleScheduling (0 , TIME_MICROS, true );
6164 this ->eventRef = event;
6265 this ->executeMode = deleteWhenDone ? ExecutionType (EXECTYPE_EVENT | EXECTYPE_DELETE_ON_DONE) : EXECTYPE_EVENT;
63-
64- this ->scheduledAt = micros ();
65- tm_internal::atomicWritePtr (&next, nullptr );
6666}
6767
6868bool TimerTask::isReady () {
@@ -72,10 +72,6 @@ bool TimerTask::isReady() {
7272 uint32_t delay = myTimingSchedule;
7373 return (micros () - scheduledAt) >= delay;
7474 }
75- else if (isSecondsSchedule ()) {
76- uint32_t delay = uint32_t (myTimingSchedule) * 1000UL ;
77- return (millis () - scheduledAt) >= delay;
78- }
7975 else {
8076 uint32_t delay = myTimingSchedule;
8177 return (millis () - scheduledAt) >= delay;
@@ -91,9 +87,6 @@ unsigned long TimerTask::microsFromNow() {
9187 }
9288 else {
9389 uint32_t delay = myTimingSchedule;
94- if (isSecondsSchedule ()) {
95- delay *= 1000UL ;
96- }
9790 uint32_t alreadyTaken = (millis () - scheduledAt);
9891 microsFromNow = (delay < alreadyTaken) ? 0 : ((delay - alreadyTaken) * 1000UL );
9992 }
0 commit comments