39
39
40
40
#include "shared-bindings/microcontroller/__init__.h"
41
41
#include "shared-bindings/supervisor/__init__.h"
42
+ #include "shared-bindings/time/__init__.h"
42
43
#include "shared-bindings/supervisor/Runtime.h"
43
44
44
45
//| """Supervisor settings"""
@@ -207,6 +208,55 @@ STATIC mp_obj_t supervisor_set_next_code_file(size_t n_args, const mp_obj_t *pos
207
208
}
208
209
MP_DEFINE_CONST_FUN_OBJ_KW (supervisor_set_next_code_file_obj , 0 , supervisor_set_next_code_file );
209
210
211
+ //| def ticks_ms() -> int:
212
+ //| """Return the time in milliseconds since an unspecified reference point, wrapping after 2**29ms.
213
+ //|
214
+ //| The value is initialized so that the first overflow occurs about 65
215
+ //| seconds after power-on, making it feasible to check that your program
216
+ //| works properly around an overflow.
217
+ //|
218
+ //| The wrap value was chosen so that it is always possible to add
219
+ //| or subtract two `ticks_ms` values without overflow on a board without
220
+ //| long ints (or without allocating any long integer objects, on boards with
221
+ //| long ints).
222
+ //|
223
+ //| This ticks value comes from a low-accuracy clock internal to the
224
+ //| microcontroller, just like `time.monotonic`. Due to its low accuracy
225
+ //| and the fact that it "wraps around" every few days, it is intended
226
+ //| for working with short term events like advancing an LED animation,
227
+ //| not for long term events like counting down the time until a holiday.
228
+ //|
229
+ //| Addition, subtraction, and comparison of ticks values can be done
230
+ //| with routines like the following::
231
+ //|
232
+ //| _TICKS_PERIOD = const(1<<29)
233
+ //| _TICKS_MAX = const(_TICKS_PERIOD-1)
234
+ //| _TICKS_HALFPERIOD = const(_TICKS_PERIOD//2)
235
+ //|
236
+ //| def ticks_add(ticks, delta):
237
+ //| "Add a delta to a base number of ticks, performing wraparound at 2**29ms."
238
+ //| return (a + b) % _TICKS_PERIOD
239
+ //|
240
+ //| def ticks_diff(ticks1, ticks2):
241
+ //| "Compute the signed difference between two ticks values, assuming that they are within 2**28 ticks"
242
+ //| diff = (ticks1 - ticks2) & _TICKS_MAX
243
+ //| diff = ((diff + _TICKS_HALFPERIOD) & _TICKS_MAX) - _TICKS_HALFPERIOD
244
+ //| return diff
245
+ //|
246
+ //| def ticks_less(ticks1, ticks2):
247
+ //| "Return true iff ticks1 is less than ticks2, assuming that they are within 2**28 ticks"
248
+ //| return ticks_diff(ticks1, ticks2) < 0
249
+ //|
250
+ //| """
251
+ //| ...
252
+ STATIC mp_obj_t supervisor_ticks_ms (void ) {
253
+ uint64_t ticks_ms = common_hal_time_monotonic_ms ();
254
+ return mp_obj_new_int ((ticks_ms + 0x1fff0000 ) % (1 << 29 ));
255
+ }
256
+ MP_DEFINE_CONST_FUN_OBJ_0 (supervisor_ticks_ms_obj , supervisor_ticks_ms );
257
+
258
+
259
+
210
260
STATIC const mp_rom_map_elem_t supervisor_module_globals_table [] = {
211
261
{ MP_ROM_QSTR (MP_QSTR___name__ ), MP_ROM_QSTR (MP_QSTR_supervisor ) },
212
262
{ MP_ROM_QSTR (MP_QSTR_enable_autoreload ), MP_ROM_PTR (& supervisor_enable_autoreload_obj ) },
@@ -217,6 +267,7 @@ STATIC const mp_rom_map_elem_t supervisor_module_globals_table[] = {
217
267
{ MP_ROM_QSTR (MP_QSTR_RunReason ), MP_ROM_PTR (& supervisor_run_reason_type ) },
218
268
{ MP_ROM_QSTR (MP_QSTR_set_next_stack_limit ), MP_ROM_PTR (& supervisor_set_next_stack_limit_obj ) },
219
269
{ MP_ROM_QSTR (MP_QSTR_set_next_code_file ), MP_ROM_PTR (& supervisor_set_next_code_file_obj ) },
270
+ { MP_ROM_QSTR (MP_QSTR_ticks_ms ), MP_ROM_PTR (& supervisor_ticks_ms_obj ) },
220
271
};
221
272
222
273
STATIC MP_DEFINE_CONST_DICT (supervisor_module_globals , supervisor_module_globals_table );
0 commit comments