Skip to content

Commit 805f7ea

Browse files
committed
docs/utime: Add docs for ticks_add(), improvements for other ticks_*().
1 parent 8679d9e commit 805f7ea

1 file changed

Lines changed: 44 additions & 8 deletions

File tree

docs/library/utime.rst

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,16 @@ Functions
8989
to be a power of two, but otherwise may differ from port to port. The same
9090
period value is used for all of ticks_ms(), ticks_us(), ticks_cpu() functions
9191
(for simplicity). Thus, these functions will return a value in range
92-
[0 .. `TICKS_MAX`], inclusive, total `TICKS_PERIOD` values. Not that only
92+
[0 .. `TICKS_MAX`], inclusive, total `TICKS_PERIOD` values. Note that only
9393
non-negative values are used. For the most part, you should treat values
94-
return by these functions as opaque. The only operations available for them
94+
returned by these functions as opaque. The only operations available for them
9595
are ``ticks_diff()`` and ``ticks_add()`` functions described below.
9696

97-
Note: Performing standard mathematical operations (+, -) on these value
98-
will lead to invalid result. Performing such operations and then passing
99-
results as arguments to ``ticks_diff()`` or ``ticks_add()`` will also lead to
100-
invalid result.
97+
Note: Performing standard mathematical operations (+, -) or relational
98+
operators (<, <=, >, >=) directly on these value will lead to invalid
99+
result. Performing mathematical operations and then passing their results
100+
as arguments to ``ticks_diff()`` or ``ticks_add()`` will also lead to
101+
invalid results from the latter functions.
101102

102103
.. function:: ticks_us()
103104

@@ -114,7 +115,34 @@ Functions
114115
function is intended for very fine benchmarking or very tight real-time loops.
115116
Avoid using it in portable code.
116117

117-
Availability: Not every port implement this function.
118+
Availability: Not every port implements this function.
119+
120+
121+
.. function:: ticks_add(ticks, delta)
122+
123+
Offset ticks value by a given number, which can be either positive or negative.
124+
Given a ``ticks`` value, this function allows to calculate ticks value ``delta``
125+
ticks before or after it, following modular-arithmetic definition of tick values
126+
(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
128+
call to ``ticks_add()``). However, ``delta`` can be an arbitrary integer number
129+
or numeric expression. ``ticks_add()`` is useful for calculating deadlines for
130+
events/tasks. (Note: you must use ``ticks_diff()`` function to work with
131+
deadlines.)
132+
133+
Examples::
134+
135+
# Find out what ticks value there was 100ms ago
136+
print(tick_add(time.ticks_ms(), -100))
137+
138+
# Calculate deadline for operation and test for it
139+
deadline = tick_add(time.ticks_ms(), 200)
140+
while ticks_diff(deadline, time.ticks_ms()) > 0:
141+
do_a_little_of_something()
142+
143+
# Find out TICKS_MAX used by this port
144+
print(tick_add(0, -1))
145+
118146

119147
.. function:: ticks_diff(ticks1, ticks2)
120148

@@ -128,13 +156,21 @@ Functions
128156
**signed** value in the range [`-TICKS_PERIOD/2` .. `TICKS_PERIOD/2-1`] (that's a typical
129157
range definition for two's-complement signed binary integers). If the result is negative,
130158
it means that `ticks1` occured earlier in time than `ticks2`. Otherwise, it means that
131-
`ticks1` was after `ticks2`. This holds `only` if `ticks1` and `ticks2` are apart from
159+
`ticks1` occured after `ticks2`. This holds `only` if `ticks1` and `ticks2` are apart from
132160
each other for no more than `TICKS_PERIOD/2-1` ticks. If that does not hold, incorrect
133161
result will be returned. Specifically, if 2 tick values are apart for `TICKS_PERIOD/2-1`
134162
ticks, that value will be returned by the function. However, if `TICKS_PERIOD/2` of
135163
real-time ticks has passed between them, the function will return `-TICKS_PERIOD/2`
136164
instead, i.e. result value will wrap around to the negative range of possible values.
137165

166+
Informal rationale of the constraints above: Suppose you are locked in a room with no
167+
means to monitor passing of time except a standard 12-notch clock. Then if you look at
168+
dial-plate now, and don't look again for another 13 hours (e.g., if you fall for a
169+
long sleep), then once you finally look again, it may seem to you that only 1 hour
170+
has passed. To avoid this mistake, just look at the clock regularly. Your application
171+
should do the same. "Too long sleep" metaphor also maps directly to application
172+
behavior: don't let your application run any single task for too long. Run tasks
173+
in steps, and do time-keeping inbetween.
138174

139175
``ticks_diff()`` is designed to accommodate various usage patterns, among them:
140176

0 commit comments

Comments
 (0)