@@ -58,7 +58,7 @@ Functions
5858.. only :: port_unix or port_pyboard or port_esp8266
5959
6060 .. function :: sleep(seconds)
61-
61+
6262 Sleep for the given number of seconds. Seconds can be a floating-point number to
6363 sleep for a fractional number of seconds. Note that other MicroPython ports may
6464 not accept floating-point argument, for compatibility with them use ``sleep_ms() ``
@@ -67,48 +67,48 @@ Functions
6767.. only :: port_wipy
6868
6969 .. function :: sleep(seconds)
70-
70+
7171 Sleep for the given number of seconds.
7272
7373.. only :: port_unix or port_pyboard or port_wipy or port_esp8266
7474
75- .. function :: sleep_ms(ms)
75+ .. function :: sleep_ms(ms)
7676
7777 Delay for given number of milliseconds, should be positive or 0.
7878
79- .. function :: sleep_us(us)
79+ .. function :: sleep_us(us)
8080
81- Delay for given number of microseconds, should be positive or 0
81+ Delay for given number of microseconds, should be positive or 0.
8282
83- .. function :: ticks_ms()
83+ .. function :: ticks_ms()
8484
85- Returns an increasing millisecond counter with an arbitrary reference point,
86- that wraps around after some value. This value is not explicitly exposed,
87- but we will refer to it as `TICKS_MAX ` to simplify discussion. Period of
88- the values is ` TICKS_PERIOD = TICKS_MAX + 1 `. `TICKS_PERIOD ` is guaranteed
89- to be a power of two, but otherwise may differ from port to port. The same
90- period value is used for all of ticks_ms(), ticks_us(), ticks_cpu() functions
91- (for simplicity). Thus, these functions will return a value in range
92- [0 .. ` TICKS_MAX `], inclusive, total `TICKS_PERIOD ` values. Note that only
93- non-negative values are used. For the most part, you should treat values
94- returned by these functions as opaque. The only operations available for them
95- are ``ticks_diff() `` and ``ticks_add() `` functions described below.
85+ Returns an increasing millisecond counter with an arbitrary reference point, that
86+ wraps around after some value. This value is not explicitly exposed, but we will
87+ refer to it as `` TICKS_MAX `` to simplify discussion. Period of the values is
88+ `` TICKS_PERIOD = TICKS_MAX + 1 `` . `` TICKS_PERIOD `` is guaranteed to be a power of
89+ two, but otherwise may differ from port to port. The same period value is used
90+ for all of `` ticks_ms() ``, `` ticks_us() ``, `` ticks_cpu() `` functions (for
91+ simplicity). Thus, these functions will return a value in range [`` 0 `` ..
92+ `` TICKS_MAX `` ], inclusive, total `` TICKS_PERIOD ` ` values. Note that only
93+ non-negative values are used. For the most part, you should treat values returned
94+ by these functions as opaque. The only operations available for them are
95+ ``ticks_diff() `` and ``ticks_add() `` functions described below.
9696
9797 Note: Performing standard mathematical operations (+, -) or relational
9898 operators (<, <=, >, >=) directly on these value will lead to invalid
9999 result. Performing mathematical operations and then passing their results
100100 as arguments to ``ticks_diff() `` or ``ticks_add() `` will also lead to
101101 invalid results from the latter functions.
102102
103- .. function :: ticks_us()
103+ .. function :: ticks_us()
104104
105- Just like ``ticks_ms `` above, but in microseconds.
105+ Just like ``ticks_ms() `` above, but in microseconds.
106106
107- .. function :: ticks_cpu()
107+ .. function :: ticks_cpu()
108108
109- Similar to ``ticks_ms `` and ``ticks_us ``, but with the highest possible resolution
109+ Similar to ``ticks_ms() `` and ``ticks_us() ``, but with the highest possible resolution
110110 in the system. This is usually CPU clocks, and that's why the function is named that
111- way. But it doesn't have to a CPU clock, some other timing source available in a
111+ way. But it doesn't have to be a CPU clock, some other timing source available in a
112112 system (e.g. high-resolution timer) can be used instead. The exact timing unit
113113 (resolution) of this function is not specified on ``utime `` module level, but
114114 documentation for a specific port may provide more specific information. This
@@ -118,13 +118,13 @@ Functions
118118 Availability: Not every port implements this function.
119119
120120
121- .. function :: ticks_add(ticks, delta)
121+ .. function :: ticks_add(ticks, delta)
122122
123123 Offset ticks value by a given number, which can be either positive or negative.
124124 Given a ``ticks `` value, this function allows to calculate ticks value ``delta ``
125125 ticks before or after it, following modular-arithmetic definition of tick values
126126 (see ``ticks_ms() `` above). ``ticks `` parameter must be a direct result of call
127- to ``tick_ms () ``, ``ticks_us() ``, ``ticks_cpu() `` functions (or from previous
127+ to ``ticks_ms () ``, ``ticks_us() ``, or ``ticks_cpu() `` functions (or from previous
128128 call to ``ticks_add() ``). However, ``delta `` can be an arbitrary integer number
129129 or numeric expression. ``ticks_add() `` is useful for calculating deadlines for
130130 events/tasks. (Note: you must use ``ticks_diff() `` function to work with
@@ -133,35 +133,37 @@ Functions
133133 Examples::
134134
135135 # Find out what ticks value there was 100ms ago
136- print(tick_add (time.ticks_ms(), -100))
136+ print(ticks_add (time.ticks_ms(), -100))
137137
138138 # Calculate deadline for operation and test for it
139- deadline = tick_add (time.ticks_ms(), 200)
139+ deadline = ticks_add (time.ticks_ms(), 200)
140140 while ticks_diff(deadline, time.ticks_ms()) > 0:
141141 do_a_little_of_something()
142142
143143 # Find out TICKS_MAX used by this port
144- print(tick_add(0, -1))
145-
146-
147- .. function :: ticks_diff(ticks1, ticks2)
148-
149- Measure ticks difference between values returned from ticks_ms(), ticks_us(), or ticks_cpu()
150- functions. The argument order is the same as for subtraction operator,
151- ``tick_diff(ticks1, ticks2) `` has the same meaning as ``ticks1 - ticks2 ``. However, values returned by
152- ticks_ms(), etc. functions may wrap around, so directly using subtraction on them will
153- produce incorrect result. That is why ticks_diff() is needed, it implements modular
154- (or more specifically, ring) arithmetics to produce correct result even for wrap-around
155- values (as long as they not too distant inbetween, see below). The function returns
156- **signed ** value in the range [`-TICKS_PERIOD/2 ` .. `TICKS_PERIOD/2-1 `] (that's a typical
157- range definition for two's-complement signed binary integers). If the result is negative,
158- it means that `ticks1 ` occured earlier in time than `ticks2 `. Otherwise, it means that
159- `ticks1 ` occured after `ticks2 `. This holds `only ` if `ticks1 ` and `ticks2 ` are apart from
160- each other for no more than `TICKS_PERIOD/2-1 ` ticks. If that does not hold, incorrect
161- result will be returned. Specifically, if 2 tick values are apart for `TICKS_PERIOD/2-1 `
162- ticks, that value will be returned by the function. However, if `TICKS_PERIOD/2 ` of
163- real-time ticks has passed between them, the function will return `-TICKS_PERIOD/2 `
164- instead, i.e. result value will wrap around to the negative range of possible values.
144+ print(ticks_add(0, -1))
145+
146+
147+ .. function :: ticks_diff(ticks1, ticks2)
148+
149+ Measure ticks difference between values returned from ``ticks_ms() ``, ``ticks_us() ``,
150+ or ``ticks_cpu() `` functions. The argument order is the same as for subtraction
151+ operator, ``ticks_diff(ticks1, ticks2) `` has the same meaning as ``ticks1 - ticks2 ``.
152+ However, values returned by ``ticks_ms() ``, etc. functions may wrap around, so
153+ directly using subtraction on them will produce incorrect result. That is why
154+ ``ticks_diff() `` is needed, it implements modular (or more specifically, ring)
155+ arithmetics to produce correct result even for wrap-around values (as long as they not
156+ too distant inbetween, see below). The function returns **signed ** value in the range
157+ [``-TICKS_PERIOD/2 `` .. ``TICKS_PERIOD/2-1 ``] (that's a typical range definition for
158+ two's-complement signed binary integers). If the result is negative, it means that
159+ ``ticks1 `` occured earlier in time than ``ticks2 ``. Otherwise, it means that
160+ ``ticks1 `` occured after ``ticks2 ``. This holds ``only `` if ``ticks1 `` and ``ticks2 ``
161+ are apart from each other for no more than ``TICKS_PERIOD/2-1 `` ticks. If that does
162+ not hold, incorrect result will be returned. Specifically, if two tick values are
163+ apart for ``TICKS_PERIOD/2-1 `` ticks, that value will be returned by the function.
164+ However, if ``TICKS_PERIOD/2 `` of real-time ticks has passed between them, the
165+ function will return ``-TICKS_PERIOD/2 `` instead, i.e. result value will wrap around
166+ to the negative range of possible values.
165167
166168 Informal rationale of the constraints above: Suppose you are locked in a room with no
167169 means to monitor passing of time except a standard 12-notch clock. Then if you look at
@@ -200,20 +202,21 @@ Functions
200202 print("Oops, running late, tell task to run faster!")
201203 task.run(run_faster=true)
202204
203- Note: Do not pass ``time() `` values to ``ticks_diff() ``, and should use
205+ Note: Do not pass ``time() `` values to ``ticks_diff() ``, you should use
204206 normal mathematical operations on them. But note that ``time() `` may (and will)
205207 also overflow. This is known as https://en.wikipedia.org/wiki/Year_2038_problem .
206208
207209
208210.. function :: time()
209211
210- Returns the number of seconds, as an integer, since the Epoch, assuming that underlying
211- RTC is set and maintained as described above. If an RTC is not set, this function returns
212- number of seconds since a port-specific reference point in time (for embedded boards without
213- a battery-backed RTC, usually since power up or reset). If you want to develop portable
214- MicroPython application, you should not rely on this function to provide higher than second
215- precision. If you need higher precision, use ``ticks_ms() `` and ``ticks_us() `` functions,
216- if you need calendar time, ``localtime() `` without an argument is a better choice.
212+ Returns the number of seconds, as an integer, since the Epoch, assuming that
213+ underlying RTC is set and maintained as described above. If an RTC is not set, this
214+ function returns number of seconds since a port-specific reference point in time (for
215+ embedded boards without a battery-backed RTC, usually since power up or reset). If you
216+ want to develop portable MicroPython application, you should not rely on this function
217+ to provide higher than second precision. If you need higher precision, use
218+ ``ticks_ms() `` and ``ticks_us() `` functions, if you need calendar time,
219+ ``localtime() `` without an argument is a better choice.
217220
218221 .. admonition :: Difference to CPython
219222 :class: attention
0 commit comments