Call next() from a callback
Posted: Fri Aug 02, 2019 1:38 pm
I want to get the values of a cosine function in small steps and a regular interval.
To not calculate all values in advance and overfill the memory, I use a generator.
To get a regular value I use a timer as trigger.
Now my problem is, if I call next value in the callback function, I get the following error:
[code]
Traceback (most recent call last):
File "<stdin>", line 13, in get_cos_value
File "<stdin>", line 6, in cosine_generator
MemoryError: memory allocation failed, heap is locked
[/code]
My code:
[code]
import pyb
import math
from math import pi
def cosine_generator(n):
dx = 2*pi/(n-1)
for i in range(n):
yield math.cos(i*dx)
cos = cosine_generator(3000)
def get_cos_value(timer):
print(next(cos))
tim = pyb.Timer(3)
tim.init(freq=10)
tim.callback(get_cos_value)
[/code]
To not calculate all values in advance and overfill the memory, I use a generator.
To get a regular value I use a timer as trigger.
Now my problem is, if I call next value in the callback function, I get the following error:
[code]
Traceback (most recent call last):
File "<stdin>", line 13, in get_cos_value
File "<stdin>", line 6, in cosine_generator
MemoryError: memory allocation failed, heap is locked
[/code]
My code:
[code]
import pyb
import math
from math import pi
def cosine_generator(n):
dx = 2*pi/(n-1)
for i in range(n):
yield math.cos(i*dx)
cos = cosine_generator(3000)
def get_cos_value(timer):
print(next(cos))
tim = pyb.Timer(3)
tim.init(freq=10)
tim.callback(get_cos_value)
[/code]