Skip to content

Commit 71c085d

Browse files
committed
use single constexpr definition instead of repeated expression.
1 parent 5ae08f4 commit 71c085d

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

cores/esp8266/Schedule.cpp

+11-10
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "Schedule.h"
2222
#include "PolledTimeout.h"
2323
#include "interrupts.h"
24-
#include "coredecls.h"
2524

2625
typedef std::function<void(void)> mSchedFuncT;
2726
struct scheduled_fn_t
@@ -50,6 +49,8 @@ static recurrent_fn_t* rLast = nullptr;
5049
// The target time for scheduling the next timed recurrent function
5150
static decltype(micros()) rTarget;
5251

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

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

@@ -126,7 +127,7 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
126127
const auto now = micros();
127128
const auto itemRemaining = item->callNow.remaining();
128129
const int32_t remaining = rTarget - now;
129-
if (!rFirst || (remaining > 0 && static_cast<uint32_t>(remaining) > itemRemaining))
130+
if (!rFirst || (remaining > 0 && static_cast<decltype(micros())>(remaining) > itemRemaining))
130131
{
131132
rTarget = now + itemRemaining;
132133
}
@@ -144,17 +145,17 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
144145
return true;
145146
}
146147

147-
uint32_t get_scheduled_recurrent_delay_us()
148+
decltype(micros()) get_scheduled_recurrent_delay_us()
148149
{
149-
if (!rFirst) return ~static_cast<decltype(micros())>(0) >> 1;
150+
if (!rFirst) return HALF_MAX_MICROS;
150151
// handle already expired rTarget.
151152
const int32_t remaining = rTarget - micros();
152-
return (remaining > 0) ? static_cast<uint32_t>(remaining) : 0;
153+
return (remaining > 0) ? static_cast<decltype(micros())>(remaining) : 0;
153154
}
154155

155-
uint32_t get_scheduled_delay_us()
156+
decltype(micros()) get_scheduled_delay_us()
156157
{
157-
return sFirst ? 0 : ~static_cast<decltype(micros())>(0) >> 1;
158+
return sFirst ? 0 : HALF_MAX_MICROS;
158159
}
159160

160161
void run_scheduled_functions()
@@ -223,7 +224,7 @@ void run_scheduled_recurrent_functions()
223224

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

228229
do
229230
{
@@ -262,7 +263,7 @@ void run_scheduled_recurrent_functions()
262263
const auto now = micros();
263264
const auto currentRemaining = current->callNow.remaining();
264265
const int32_t remaining = rTarget - now;
265-
if (remaining > 0 && static_cast<uint32_t>(remaining) > currentRemaining)
266+
if (remaining > 0 && static_cast<decltype(micros())>(remaining) > currentRemaining)
266267
{
267268
rTarget = now + currentRemaining;
268269
}

cores/esp8266/Schedule.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <functional>
2323
#include <stdint.h>
2424

25+
#include "coredecls.h"
26+
2527
#define SCHEDULED_FN_MAX_COUNT 32
2628

2729
// The purpose of scheduled functions is to trigger, from SYS stack (like in
@@ -42,7 +44,7 @@
4244
// get_scheduled_recurrent_delay_us() is used by delay() to give a chance to
4345
// all recurrent functions to run per their timing requirement.
4446

45-
uint32_t get_scheduled_recurrent_delay_us();
47+
decltype(micros()) get_scheduled_recurrent_delay_us();
4648

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

68-
uint32_t get_scheduled_delay_us();
70+
decltype(micros()) get_scheduled_delay_us();
6971

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

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

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

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

cores/esp8266/core_esp8266_main.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ bool esp_try_delay(const uint32_t start_ms, const uint32_t timeout_ms, const uin
174174
}
175175

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

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

0 commit comments

Comments
 (0)