Object method as timer callback

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
matiase
Posts: 1
Joined: Wed Jan 23, 2019 7:11 pm

Object method as timer callback

Post by matiase » Sun Jan 27, 2019 1:13 pm

Hello,
I'm trying to start timer and set object method as collback, but i receive:
TypeError: 'NonType' object is not callable

I supported this:
https://docs.micropython.org/en/latest/ ... -callbacks
and this:
https://github.com/dhylands/upy-example ... eat_irq.py

My program:

from machine import Timer
import micropython

class Data(object):
def __init__(self):
timer = Timer(1)
timer.init(period=2000, mode=Timer.PERIODIC)
timer.callback(self.cb_timer)

def cb_timer(self, timer):
print("CB_TIMER")

micropython.alloc_emergency_exception_buf(100)
Data()

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Object method as timer callback

Post by Roberthh » Sun Jan 27, 2019 5:50 pm

Try:

Code: Select all

from machine import Timer
import micropython

class Data(object):
    def __init__(self):
        timer = Timer(1)
        timer.init(period=2000, mode=Timer.PERIODIC, callback=self.cb_timer)

    def cb_timer(self, timer):
        print("CB_TIMER")

micropython.alloc_emergency_exception_buf(100)
Data()
The error message your code gets first is:
AttributeError: 'Timer' object has no attribute 'callback

Post Reply