Python Forum
timer strange ? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: timer strange ? (/thread-41160.html)



timer strange ? - trix - Nov-20-2023

hello,
i,am busy to adjust the code from a libary (micropython-stepper 1.0.3),
to do the things witch i want it to do (acc & decc).
no i see that the configure the timer on a different way then what i learn.
i learn something like:
timer_0.init(mode=, period=, callback=)
where you have by mode the options:
periodic
one_shot

now i see in the libary that te used a timer configuration:
timer.init(freq = self.steps_per_sec,callback = self._timer_callback)
do someone now why the 2e way is also working ?
tnx.


RE: timer strange ? - buran - Nov-20-2023

where did you see the first one, which by the way is not valid python code?

Have a look at machine.Timer.init() docs:

https://docs.micropython.org/en/latest/library/machine.Timer.html#machine.Timer.init

it takes only keyword arguments and has 4 (and their default values): mode=Timer.PERIODIC, freq=-1, period=-1, callback=None


RE: timer strange ? - deanhystad - Nov-20-2023

From the micropython documentation

https://docs.micropython.org/en/latest/library/machine.Timer.html
Quote:classmachine.Timer

Methods¶
Timer.init(*, mode=Timer.PERIODIC, freq=-1, period=-1, callback=None)¶

Keyword arguments:

mode can be one of:

Timer.ONE_SHOT - The timer runs once until the configured period of the channel expires.

Timer.PERIODIC - The timer runs periodically at the configured frequency of the channel.

freq - The timer frequency, in units of Hz. The upper bound of the frequency is dependent on the port. When both the freq and period arguments are given, freq has a higher priority and period is ignored.

period - The timer period, in milliseconds.

callback - The callable to call upon expiration of the timer period. The callback must take one argument, which is passed the Timer object. The callback argument shall be specified. Otherwise an exception will occur upon timer expiration: TypeError: 'NoneType' object isn't callable

mode is an optional argument. If you don't supply a mode argument the mode is Timer.PERIODIC.

freq and period set how often the callback function is called. You can use either. If you provide both freq and period, freq is used and period is ignored. If you don't use freq or period the default freq is used (1000 Hz).

So there is no reason to use mode=Timer.PERIODIC. Not using the mode argument achieves the same thing. And you are free to use either freq or period.


RE: timer strange ? - DeaD_EyE - Nov-20-2023

(Nov-20-2023, 08:40 PM)deanhystad Wrote: So there is no reason to use mode=Timer.PERIODIC.

Explicit is often better than implicit. Someone who doesn't know Micropython, but knows Python very well, still understands the code. But if there are hidden default arguments, this information is hidden.


RE: timer strange ? - trix - Nov-21-2023

thanks for the lessons Clap
the lesson of all this is that i still have a lot to learn ;)
I'll read it again at home tonight.
tnx again.