Page 1 of 1
Timer callback problem
Posted: Mon Sep 12, 2016 6:10 pm
by stmfresser
Hello,
i'm trying to toggle the leds every 1 second. i get the this error message
Code: Select all
uncaught exception in Timer(2) interrupt handler
TypeError: function takes 1 positional arguments but 2 were given
Code: Select all
import pyb
import micropython
micropython.alloc_emergency_exception_buf(200)
class A:
def __init__(self):
self.leds = [pyb.LED(i) for i in range(1,5)]
tim = pyb.Timer(2, freq=1)
tim.callback(self.ledToggle)
def ledToggle(self):
for led in self.leds:
led.toggle()
if __name__ == '__main__':
a = A()
does anyone know how to solve it?
thanks in advance
Re: Timer callback problem
Posted: Mon Sep 12, 2016 6:36 pm
by deshipu
I think the ledToggle callback needs to take a parameter.
Re: Timer callback problem
Posted: Mon Sep 12, 2016 6:38 pm
by dhylands
Re: Timer callback problem
Posted: Mon Sep 12, 2016 7:01 pm
by stmfresser
thanks.
now i get a another error message
Code: Select all
MemoryError: memory allocation failed, heap is locked
micropython runs on a STM32F407 Discovery Board
Code: Select all
MicroPython v1.8.3-26-ga56a683 on 2016-08-16; F4DISC with STM32F407
Re: Timer callback problem
Posted: Mon Sep 12, 2016 10:35 pm
by dhylands
You should read this:
http://docs.micropython.org/en/latest/p ... =interrupt
You're not allowed to allocate memory inside an interrupt handler (and timer callbacks are interrupt handlers).
The for loop is trying to allocate an iterator object. If you rewrite it like this:
Code: Select all
import pyb
import micropython
micropython.alloc_emergency_exception_buf(200)
class A:
def __init__(self):
self.leds = [pyb.LED(i) for i in range(1,5)]
tim = pyb.Timer(2, freq=1)
tim.callback(self.ledToggle)
def ledToggle(self, tim):
for i in range(len(self.leds)):
self.leds[i].toggle()
A()
Re: Timer callback problem
Posted: Mon Sep 12, 2016 11:56 pm
by stmfresser
dhylands wrote:You should read this:
http://docs.micropython.org/en/latest/p ... =interrupt
You're not allowed to allocate memory inside an interrupt handler (and timer callbacks are interrupt handlers).
The for loop is trying to allocate an iterator object. If you rewrite it like this:
Code: Select all
import pyb
import micropython
micropython.alloc_emergency_exception_buf(200)
class A:
def __init__(self):
self.leds = [pyb.LED(i) for i in range(1,5)]
tim = pyb.Timer(2, freq=1)
tim.callback(self.ledToggle)
def ledToggle(self, tim):
for i in range(len(self.leds)):
self.leds[i].toggle()
A()
Thank you!