Archive and discussion of the MicroPython Newsletter
Damien
Posts: 634
Joined: Mon Dec 09, 2013 5:02 pm

Dear community,

Welcome! In this week's MicroPython newsletter we discuss the basics of timing intervals, and a few important developments with the main Python language.

Measuring time intervals

Timing things is very common, and it's important to get it right. MicroPython provides dedicated functions in the `utime` module (also available as `time`) to time intervals. The following functions can be used: `utime.ticks_ms()` and `utime.ticks_us()`.

These functions return a small integer which means two things: 1) they are limited to a maximum value (30 bits on a 32-bit microcontroller); and 2) they do not allocate any (heap) memory. It is crucial that they don't allocate memory because memory allocation is not deterministic, and determinism is very important in embedded systems. For example, if you are timing intervals in the range of 100 microseconds you don't want it to have variations in the measurement due to memory allocation of the `ticks_us()` function taking an uncertain amount of time. Since the ticks functions always take the same amount of time to execute you can be certain to get accurate time intervals.

Regarding point (1): because ticks return values are restricted they will wrap around once they reach their maximum value. Although this might seem like a limitation it is actually a feature because, as long as you are measuring intervals which are less than the maximum (for example, roughly 1000 seconds using `ticks_us`), the intervals will be calculated correctly for an indefinite amount of time. So you can be sure that your code will continue to run correctly, and give the same results, as long as it runs.

A few extra functions are provided to help you perform arithmetic with ticks that wrap around. They are `ticks_diff(t1, t0)`, for computing differences between ticks, and `ticks_add(t, delta)`, for computing new ticks values. `ticks_diff(t1, t0)` should be used to compute the actual time interval and the arguments to this function can be results from any of the other ticks functions. There is also `ticks_cpu()` which gives the highest possible tick resolution, usually in correspondence to the number of CPU cycles that have passed.

You can read more about these functions in the documentation: http://docs.micropython.org/en/latest/p ... utime.html.

Note: in the early days of MicroPython there were the functions `pyb.millis()` and `pyb.elapsed_millis(t0)` to measure intervals. The newer `utime` functions should be used instead of these.

Python language developments

There has been some big news over the past few weeks in the wider Python world: the creator of Python -- Guido van Rossum -- has announced his effective retirement as the Python "leader", or BDFL. You can read his announcement here: https://mail.python.org/pipermail/pytho ... 05664.html. We would like to extend a huge thanks to Guido for all the hard work he put into Python over the many years he spend designing, building, extending and nurturing it. And also for the hard work fostering such a fantastic community around it. Without him and his legacy there would definitely be no MicroPython!

Related to this announcement is the acceptance of a new feature in Python that is set to appear in Python 3.8: assignment expressions https://www.python.org/dev/peps/pep-0572/ with the new `:=` operator. If this appears in MicroPython it won't be for some time, so don't get comfortable with it just yet!

Events

MicroPython events this week:
From Damien and the MicroPython team.