Issue with a timer callback

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
pipponefrancone
Posts: 9
Joined: Sat Sep 12, 2020 6:02 pm

Issue with a timer callback

Post by pipponefrancone » Tue Oct 20, 2020 9:15 pm

Hello there,
I am trying to use an object's method as a timer callback:

Code: Select all

#creating a new object a
>>> a = test()
>>> a.mymethod
<bound_method>

>>> timer = Timer(0)
>>> timer.init(period=50, mode=1, callback=a.mymethod)
At this point, each time the timer runs it gives me this error:

Code: Select all

TypeError: function takes 1 positional arguments but 2 were given 
How do I solve this? :D

User avatar
jimmo
Posts: 2015
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Issue with a timer callback

Post by jimmo » Tue Oct 20, 2020 10:21 pm

pipponefrancone wrote:
Tue Oct 20, 2020 9:15 pm
How do I solve this?
What does mymethod look like? The Timer will expect that it takes an instance of the timer that fired the IRQ.

Code: Select all

class test:
  ...

  def mymethod(self, timer):
      print('hello')
I'm guessing you just have "def mymethod(self):" ?

pipponefrancone
Posts: 9
Joined: Sat Sep 12, 2020 6:02 pm

Re: Issue with a timer callback

Post by pipponefrancone » Wed Oct 21, 2020 11:32 am

Yes the function only takes the self parameter, I totally forgot to add one for the timer instance.
jimmo wrote:
Tue Oct 20, 2020 10:21 pm

Code: Select all

  def mymethod(self, timer):
      print('hello')
This solves it, thank you. But what if I want to pass other parameters? :)

User avatar
jimmo
Posts: 2015
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Issue with a timer callback

Post by jimmo » Wed Oct 21, 2020 10:37 pm

As long as to the Timer, it looks like a method that takes one argument, then you can. This is already happening with the "self" argument, but ignoring classes for a second, you can bind arguments any way you like. Lambdas are my favourite way but there are other alteratives.

Code: Select all

def handler(timer):
  pass

timer.irq(callback=handler)

Code: Select all

def handler_with_other_args(timer, x, y):
  pass

timer.irq(callback=lambda t: handler(t, 1, 2))

pipponefrancone
Posts: 9
Joined: Sat Sep 12, 2020 6:02 pm

Re: Issue with a timer callback

Post by pipponefrancone » Fri Oct 23, 2020 8:53 am

That's very interesting, thank for your help!
I would also like to ask another question about timers, should I add it here or crate a new thread? Sorry I'm not very familiar with forums :?

User avatar
jimmo
Posts: 2015
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Issue with a timer callback

Post by jimmo » Fri Oct 23, 2020 11:30 am

pipponefrancone wrote:
Fri Oct 23, 2020 8:53 am
I would also like to ask another question about timers, should I add it here or crate a new thread?
Probably best make a new thread.

Post Reply