4
4
5
5
/*
6
6
PolledTimeout.h - Encapsulation of a polled Timeout
7
-
7
+
8
8
Copyright (c) 2018 Daniel Salazar. All rights reserved.
9
9
This file is part of the esp8266 core for Arduino environment.
10
10
23
23
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
24
*/
25
25
26
- #include < limits>
27
-
28
- #include < Arduino.h>
26
+ #include < c_types.h> // IRAM_ATTR
27
+ #include < limits> // std::numeric_limits
28
+ #include < type_traits> // std::is_unsigned
29
+ #include < core_esp8266_features.h>
29
30
30
31
namespace esp8266
31
32
{
@@ -70,13 +71,13 @@ struct TimeSourceMillis
70
71
71
72
struct TimeSourceCycles
72
73
{
73
- // time policy based on ESP.getCycleCount ()
74
+ // time policy based on esp_get_cycle_count ()
74
75
// this particular time measurement is intended to be called very often
75
76
// (every loop, every yield)
76
77
77
- using timeType = decltype(ESP.getCycleCount ());
78
- static timeType time () {return ESP. getCycleCount ();}
79
- static constexpr timeType ticksPerSecond = ESP.getCpuFreqMHz () * 1000000UL ; // 80'000'000 or 160'000'000 Hz
78
+ using timeType = decltype(esp_get_cycle_count ());
79
+ static timeType time () {return esp_get_cycle_count ();}
80
+ static constexpr timeType ticksPerSecond = esp_get_cpu_freq_mhz () * 1000000UL ; // 80'000'000 or 160'000'000 Hz
80
81
static constexpr timeType ticksPerSecondMax = 160000000 ; // 160MHz
81
82
};
82
83
@@ -161,13 +162,13 @@ class timeoutTemplate
161
162
return expiredRetrigger ();
162
163
return expiredOneShot ();
163
164
}
164
-
165
+
165
166
IRAM_ATTR // fast
166
167
operator bool ()
167
168
{
168
- return expired ();
169
+ return expired ();
169
170
}
170
-
171
+
171
172
bool canExpire () const
172
173
{
173
174
return !_neverExpires;
@@ -178,6 +179,7 @@ class timeoutTemplate
178
179
return _timeout != alwaysExpired;
179
180
}
180
181
182
+ // Resets, will trigger after this new timeout.
181
183
IRAM_ATTR // called from ISR
182
184
void reset (const timeType newUserTimeout)
183
185
{
@@ -186,12 +188,30 @@ class timeoutTemplate
186
188
_neverExpires = (newUserTimeout < 0 ) || (newUserTimeout > timeMax ());
187
189
}
188
190
191
+ // Resets, will trigger after the timeout previously set.
189
192
IRAM_ATTR // called from ISR
190
193
void reset ()
191
194
{
192
195
_start = TimePolicyT::time ();
193
196
}
194
197
198
+ // Resets to just expired so that on next poll the check will immediately trigger for the user,
199
+ // also change timeout (after next immediate trigger).
200
+ IRAM_ATTR // called from ISR
201
+ void resetAndSetExpired (const timeType newUserTimeout)
202
+ {
203
+ reset (newUserTimeout);
204
+ _start -= _timeout;
205
+ }
206
+
207
+ // Resets to just expired so that on next poll the check will immediately trigger for the user.
208
+ IRAM_ATTR // called from ISR
209
+ void resetAndSetExpired ()
210
+ {
211
+ reset ();
212
+ _start -= _timeout;
213
+ }
214
+
195
215
void resetToNeverExpires ()
196
216
{
197
217
_timeout = alwaysExpired + 1 ; // because canWait() has precedence
@@ -202,7 +222,7 @@ class timeoutTemplate
202
222
{
203
223
return TimePolicyT::toUserUnit (_timeout);
204
224
}
205
-
225
+
206
226
static constexpr timeType timeMax ()
207
227
{
208
228
return TimePolicyT::timeMax;
@@ -235,14 +255,14 @@ class timeoutTemplate
235
255
}
236
256
return false ;
237
257
}
238
-
258
+
239
259
IRAM_ATTR // fast
240
260
bool expiredOneShot () const
241
261
{
242
262
// returns "always expired" or "has expired"
243
263
return !canWait () || checkExpired (TimePolicyT::time ());
244
264
}
245
-
265
+
246
266
timeType _timeout;
247
267
timeType _start;
248
268
bool _neverExpires;
@@ -259,14 +279,14 @@ using periodic = polledTimeout::timeoutTemplate<true> /*__attribute__((deprecate
259
279
using oneShotMs = polledTimeout::timeoutTemplate<false >;
260
280
using periodicMs = polledTimeout::timeoutTemplate<true >;
261
281
262
- // Time policy based on ESP.getCycleCount (), and intended to be called very often:
282
+ // Time policy based on esp_get_cycle_count (), and intended to be called very often:
263
283
// "Fast" versions sacrifices time range for improved precision and reduced execution time (by 86%)
264
- // (cpu cycles for ::expired(): 372 (millis()) vs 52 (ESP.getCycleCount ()))
284
+ // (cpu cycles for ::expired(): 372 (millis()) vs 52 (esp_get_cycle_count ()))
265
285
// timeMax() values:
266
286
// Ms: max is 26843 ms (26.8 s)
267
287
// Us: max is 26843545 us (26.8 s)
268
288
// Ns: max is 1073741823 ns ( 1.07 s)
269
- // (time policy based on ESP.getCycleCount () is intended to be called very often)
289
+ // (time policy based on esp_get_cycle_count () is intended to be called very often)
270
290
271
291
using oneShotFastMs = polledTimeout::timeoutTemplate<false , YieldPolicy::DoNothing, TimePolicy::TimeFastMillis>;
272
292
using periodicFastMs = polledTimeout::timeoutTemplate<true , YieldPolicy::DoNothing, TimePolicy::TimeFastMillis>;
0 commit comments