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 27, 2023
1 parent 5ae08f4 commit 71c085d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
21 changes: 11 additions & 10 deletions cores/esp8266/Schedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "Schedule.h"
#include "PolledTimeout.h"
#include "interrupts.h"
#include "coredecls.h"

typedef std::function<void(void)> mSchedFuncT;
struct scheduled_fn_t
Expand Down Expand Up @@ -50,6 +49,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 +107,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 +127,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 +145,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 +224,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 +263,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
8 changes: 5 additions & 3 deletions cores/esp8266/Schedule.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <functional>
#include <stdint.h>

#include "coredecls.h"

#define SCHEDULED_FN_MAX_COUNT 32

// The purpose of scheduled functions is to trigger, from SYS stack (like in
Expand All @@ -42,7 +44,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 +67,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 +95,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
3 changes: 2 additions & 1 deletion cores/esp8266/core_esp8266_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ bool esp_try_delay(const uint32_t start_ms, const uint32_t timeout_ms, const uin
}

// compute greatest delay interval with respect to scheduled recurrent functions
const uint32_t max_delay_ms = std::min(intvl_ms, get_scheduled_recurrent_delay_us() / 1000);
const uint32_t scheduled_recurrent_delay_ms = get_scheduled_recurrent_delay_us() / 1000UL;
const uint32_t max_delay_ms = std::min(intvl_ms, scheduled_recurrent_delay_ms);

// recurrent scheduled functions will be called from esp_delay()->esp_suspend()
esp_delay(std::min((timeout_ms - expired), max_delay_ms));
Expand Down

0 comments on commit 71c085d

Please sign in to comment.