Skip to content

Commit

Permalink
use single constexpr definition instead of repeated expression.
Browse files Browse the repository at this point in the history
  • Loading branch information
dok-net committed Jul 26, 2023
1 parent 23ae385 commit 72381c8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
20 changes: 11 additions & 9 deletions cores/esp8266/Schedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ static recurrent_fn_t* rLast = nullptr;
// The target time for scheduling the next timed recurrent function
static decltype(micros()) rTarget;

constexpr decltype(micros()) HALF_MAX_MICROS = ~static_cast<decltype(micros())>(0) >> 1;

// Returns a pointer to an unused sched_fn_t,
// or if none are available allocates a new one,
// or nullptr if limit is reached
Expand Down Expand Up @@ -106,7 +108,7 @@ bool schedule_function(const std::function<void(void)>& fn)

IRAM_ATTR // (not only) called from ISR
bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
uint32_t repeat_us, const std::function<bool(void)>& alarm)
decltype(micros()) repeat_us, const std::function<bool(void)>& alarm)
{
assert(repeat_us < decltype(recurrent_fn_t::callNow)::neverExpires); //~26800000us (26.8s)

Expand All @@ -126,7 +128,7 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
const auto now = micros();
const auto itemRemaining = item->callNow.remaining();
const int32_t remaining = rTarget - now;
if (!rFirst || (remaining > 0 && static_cast<uint32_t>(remaining) > itemRemaining))
if (!rFirst || (remaining > 0 && static_cast<decltype(micros()>(remaining) > itemRemaining))
{
rTarget = now + itemRemaining;
}
Expand All @@ -144,17 +146,17 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
return true;
}

uint32_t get_scheduled_recurrent_delay_us()
decltype(micros()) get_scheduled_recurrent_delay_us()
{
if (!rFirst) return ~static_cast<decltype(micros())>(0) >> 1;
if (!rFirst) return HALF_MAX_MICROS;
// handle already expired rTarget.
const int32_t remaining = rTarget - micros();
return (remaining > 0) ? static_cast<uint32_t>(remaining) : 0;
return (remaining > 0) ? static_cast<decltype(micros())>(remaining) : 0;
}

uint32_t get_scheduled_delay_us()
decltype(micros()) get_scheduled_delay_us()
{
return sFirst ? 0 : ~static_cast<decltype(micros())>(0) >> 1;
return sFirst ? 0 : HALF_MAX_MICROS;
}

void run_scheduled_functions()
Expand Down Expand Up @@ -223,7 +225,7 @@ void run_scheduled_recurrent_functions()

// prevent scheduling of new functions during this run
stop = rLast;
rTarget = micros() + (~static_cast<decltype(micros())>(0) >> 1);
rTarget = micros() + HALF_MAX_MICROS;

do
{
Expand Down Expand Up @@ -262,7 +264,7 @@ void run_scheduled_recurrent_functions()
const auto now = micros();
const auto currentRemaining = current->callNow.remaining();
const int32_t remaining = rTarget - now;
if (remaining > 0 && static_cast<uint32_t>(remaining) > currentRemaining)
if (remaining > 0 && static_cast<decltype(micros)>(remaining) > currentRemaining)
{
rTarget = now + currentRemaining;
}
Expand Down
6 changes: 3 additions & 3 deletions cores/esp8266/Schedule.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
// get_scheduled_recurrent_delay_us() is used by delay() to give a chance to
// all recurrent functions to run per their timing requirement.

uint32_t get_scheduled_recurrent_delay_us();
decltype(micros()) get_scheduled_recurrent_delay_us();

// scheduled functions called once:
//
Expand All @@ -65,7 +65,7 @@ uint32_t get_scheduled_recurrent_delay_us();
// values, viz. 0 in case of any pending scheduled functions, or a large delay time if
// there is no function in the queue.

uint32_t get_scheduled_delay_us();
decltype(micros()) get_scheduled_delay_us();

bool schedule_function (const std::function<void(void)>& fn);

Expand Down Expand Up @@ -93,7 +93,7 @@ void run_scheduled_functions();
// any remaining delay from repeat_us is disregarded, and fn is executed.

bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
uint32_t repeat_us, const std::function<bool(void)>& alarm = nullptr);
decltype(micros()) repeat_us, const std::function<bool(void)>& alarm = nullptr);

// Test recurrence and run recurrent scheduled functions.
// (internally called at every `yield()` and `loop()`)
Expand Down

0 comments on commit 72381c8

Please sign in to comment.