Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit eaf05e9

Browse filesBrowse files
committed
Finalize scheduled recurrent function delay logic.
1 parent 81b90c9 commit eaf05e9
Copy full SHA for eaf05e9

File tree

Expand file treeCollapse file tree

3 files changed

+24
-13
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+24
-13
lines changed

‎cores/esp8266/Schedule.cpp

Copy file name to clipboardExpand all lines: cores/esp8266/Schedule.cpp
+20-7Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct recurrent_fn_t
4747

4848
static recurrent_fn_t* rFirst = nullptr;
4949
static recurrent_fn_t* rLast = nullptr;
50-
static uint32_t rScheduleTarget = 0;
50+
static uint32_t rTarget;
5151

5252
// Returns a pointer to an unused sched_fn_t,
5353
// or if none are available allocates a new one,
@@ -118,10 +118,13 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
118118

119119
item->mFunc = fn;
120120
item->alarm = alarm;
121-
// if (!rScheduleTarget || rScheduleTarget > item.callNow.remaining())
122-
// {
123-
// rScheduleTarget = item.callNow.remaining();
124-
// }
121+
122+
// prevent new item overwriting an already expired rTarget.
123+
const int32_t rRemaining = rTarget - millis();
124+
if (!rFirst || (rRemaining > 0 && static_cast<uint32_t>(rRemaining) > item->callNow.remaining()))
125+
{
126+
rTarget = millis() + item->callNow.remaining();
127+
}
125128

126129
esp8266::InterruptLock lockAllInterruptsInThisScope;
127130

@@ -138,9 +141,12 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
138141
return true;
139142
}
140143

141-
uint32_t compute_scheduled_recurrent_grain()
144+
uint32_t get_scheduled_recurrent_delay()
142145
{
143-
return 0;
146+
if (!rFirst) return ~static_cast<uint32_t>(0);
147+
// handle already expired rTarget.
148+
const int32_t rRemaining = rTarget - millis();
149+
return (rRemaining > 0) ? static_cast<uint32_t>(rRemaining) : 0;
144150
}
145151

146152
void run_scheduled_functions()
@@ -203,6 +209,7 @@ void run_scheduled_recurrent_functions()
203209
fence = true;
204210
}
205211

212+
rTarget = millis() + current->callNow.remaining();
206213
recurrent_fn_t* prev = nullptr;
207214
// prevent scheduling of new functions during this run
208215
auto stop = rLast;
@@ -241,6 +248,12 @@ void run_scheduled_recurrent_functions()
241248
{
242249
prev = current;
243250
current = current->mNext;
251+
// prevent current item overwriting an already expired rTarget.
252+
const int32_t rRemaining = rTarget - millis();
253+
if (rRemaining > 0 && static_cast<uint32_t>(rRemaining) > current->callNow.remaining())
254+
{
255+
rTarget = millis() + current->callNow.remaining();
256+
}
244257
}
245258

246259
if (yieldNow)

‎cores/esp8266/Schedule.h

Copy file name to clipboardExpand all lines: cores/esp8266/Schedule.h
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@
3939
// scheduled function happen more often: every yield() (vs every loop()),
4040
// and time resolution is microsecond (vs millisecond). Details are below.
4141

42-
// compute_scheduled_recurrent_grain() is used by delay() to give a chance to
42+
// get_scheduled_recurrent_delay() is used by delay() to give a chance to
4343
// all recurrent functions to run per their timing requirement.
4444

45-
uint32_t compute_scheduled_recurrent_grain ();
45+
uint32_t get_scheduled_recurrent_delay();
4646

4747
// scheduled functions called once:
4848
//

‎cores/esp8266/core_esp8266_main.cpp

Copy file name to clipboardExpand all lines: cores/esp8266/core_esp8266_main.cpp
+2-4Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,10 @@ 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-
uint32_t grain_ms = std::min(intvl_ms, compute_scheduled_recurrent_grain());
177+
const uint32_t max_delay_ms = std::min(intvl_ms, get_scheduled_recurrent_delay());
178178

179179
// recurrent scheduled functions will be called from esp_delay()->esp_suspend()
180-
esp_delay(grain_ms > 0 ?
181-
std::min((timeout_ms - expired), grain_ms) :
182-
(timeout_ms - expired));
180+
esp_delay(std::min((timeout_ms - expired), max_delay_ms));
183181

184182
return false; // expiration must be checked again
185183
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.