Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
timer strange ?
#1
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.
Reply
#2
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/l...Timer.init

it takes only keyword arguments and has 4 (and their default values): mode=Timer.PERIODIC, freq=-1, period=-1, callback=None
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
From the micropython documentation

https://docs.micropython.org/en/latest/l...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.
Reply
#4
(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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  email timer/rss feed timer ndiniz 1 2,090 Feb-02-2021, 07:18 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020