From b100e37e4063af9046348a3ee5bf54c32f1fadc1 Mon Sep 17 00:00:00 2001 From: Connor Fitzgerald Date: Sun, 17 Nov 2019 01:11:12 -0500 Subject: [PATCH 1/2] Fix underflow when task created inside another task --- src/timer.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/timer.h b/src/timer.h index c441420..d087124 100644 --- a/src/timer.h +++ b/src/timer.h @@ -92,7 +92,11 @@ class Timer { struct task * const task = &tasks[i]; const unsigned long duration = t - task->start; - if (task->handler && duration >= task->expires) { + // Tasks that were generated by a task earlier in the loop could have + // a start time larger than the "current time" given as an argument. + // + // Check if t >= task->start to prevent the underflow from triggering tasks early + if (task->handler && t >= task->start && duration >= task->expires) { task->repeat = task->handler(task->opaque) && task->repeat; if (task->repeat) task->start = t; From 8c8eba2300d74cc1c7c47813947da74e398f1e40 Mon Sep 17 00:00:00 2001 From: Connor Fitzgerald Date: Sun, 17 Nov 2019 01:24:16 -0500 Subject: [PATCH 2/2] Add github issue link --- src/timer.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/timer.h b/src/timer.h index d087124..4d45538 100644 --- a/src/timer.h +++ b/src/timer.h @@ -95,7 +95,9 @@ class Timer { // Tasks that were generated by a task earlier in the loop could have // a start time larger than the "current time" given as an argument. // - // Check if t >= task->start to prevent the underflow from triggering tasks early + // Check if t >= task->start to prevent the underflow from triggering tasks early. + // + // See https://github.com/contrem/arduino-timer/pull/7 for more info. if (task->handler && t >= task->start && duration >= task->expires) { task->repeat = task->handler(task->opaque) && task->repeat;