Skip to content

Commit 72381c8

Browse files
committed
use single constexpr definition instead of repeated expression.
1 parent 23ae385 commit 72381c8

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

cores/esp8266/Schedule.cpp

+11-9
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ static recurrent_fn_t* rLast = nullptr;
5050
// The target time for scheduling the next timed recurrent function
5151
static decltype(micros()) rTarget;
5252

53+
constexpr decltype(micros()) HALF_MAX_MICROS = ~static_cast<decltype(micros())>(0) >> 1;
54+
5355
// Returns a pointer to an unused sched_fn_t,
5456
// or if none are available allocates a new one,
5557
// or nullptr if limit is reached
@@ -106,7 +108,7 @@ bool schedule_function(const std::function<void(void)>& fn)
106108

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

@@ -126,7 +128,7 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
126128
const auto now = micros();
127129
const auto itemRemaining = item->callNow.remaining();
128130
const int32_t remaining = rTarget - now;
129-
if (!rFirst || (remaining > 0 && static_cast<uint32_t>(remaining) > itemRemaining))
131+
if (!rFirst || (remaining > 0 && static_cast<decltype(micros()>(remaining) > itemRemaining))
130132
{
131133
rTarget = now + itemRemaining;
132134
}
@@ -144,17 +146,17 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
144146
return true;
145147
}
146148

147-
uint32_t get_scheduled_recurrent_delay_us()
149+
decltype(micros()) get_scheduled_recurrent_delay_us()
148150
{
149-
if (!rFirst) return ~static_cast<decltype(micros())>(0) >> 1;
151+
if (!rFirst) return HALF_MAX_MICROS;
150152
// handle already expired rTarget.
151153
const int32_t remaining = rTarget - micros();
152-
return (remaining > 0) ? static_cast<uint32_t>(remaining) : 0;
154+
return (remaining > 0) ? static_cast<decltype(micros())>(remaining) : 0;
153155
}
154156

155-
uint32_t get_scheduled_delay_us()
157+
decltype(micros()) get_scheduled_delay_us()
156158
{
157-
return sFirst ? 0 : ~static_cast<decltype(micros())>(0) >> 1;
159+
return sFirst ? 0 : HALF_MAX_MICROS;
158160
}
159161

160162
void run_scheduled_functions()
@@ -223,7 +225,7 @@ void run_scheduled_recurrent_functions()
223225

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

228230
do
229231
{
@@ -262,7 +264,7 @@ void run_scheduled_recurrent_functions()
262264
const auto now = micros();
263265
const auto currentRemaining = current->callNow.remaining();
264266
const int32_t remaining = rTarget - now;
265-
if (remaining > 0 && static_cast<uint32_t>(remaining) > currentRemaining)
267+
if (remaining > 0 && static_cast<decltype(micros)>(remaining) > currentRemaining)
266268
{
267269
rTarget = now + currentRemaining;
268270
}

cores/esp8266/Schedule.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
// get_scheduled_recurrent_delay_us() is used by delay() to give a chance to
4343
// all recurrent functions to run per their timing requirement.
4444

45-
uint32_t get_scheduled_recurrent_delay_us();
45+
decltype(micros()) get_scheduled_recurrent_delay_us();
4646

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

68-
uint32_t get_scheduled_delay_us();
68+
decltype(micros()) get_scheduled_delay_us();
6969

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

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

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

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

0 commit comments

Comments
 (0)