Issue with ExtInt callback and snapshot

Discussion related to "under the hood" OpenMV topics.
alina
Posts: 2
Joined: Sun Oct 23, 2016 4:16 pm
Location: Montreal
Contact:

Issue with ExtInt callback and snapshot

Postby alina » Sun Oct 23, 2016 4:24 pm

Hi all,

I am basically trying to have a triggered snapshot.
For this, I use et ExtInt set up on a rising edge of an IO (feed by my Arduino), then inside my callback func, I take a snapshot.
In other words:

Code: Select all

def callback(line):
    global img
    img = sensor.snapshot()
    
extint = pyb.ExtInt(Pin('P1'), pyb.ExtInt.IRQ_RISING, pyb.Pin.PULL_NONE, callback)
However I get a "MemoryError: memory allocation failed, heap is locked", and as far as I understand, it seems that the snapshot call is allocating some memory, which is forbidden in the ExtInt callback function.

I wonder why the snapshot call do allocate any memory, and if there is a workaround to make this work.
Many thanks,
AL
User avatar
iabdalkader
Posts: 1214
Joined: Sun May 24, 2015 3:53 pm

Re: Issue with ExtInt callback and snapshot

Postby iabdalkader » Sun Oct 23, 2016 4:49 pm

The snapshot call allocates memory for the return object (the image). Memory allocation is not allowed in interrupts because it might corrupt the heap if it's being scanned or collected. An easy workaround is to set a flag in the IRQ handler and check that flag in the main loop.
alina
Posts: 2
Joined: Sun Oct 23, 2016 4:16 pm
Location: Montreal
Contact:

Re: Issue with ExtInt callback and snapshot

Postby alina » Sun Oct 23, 2016 5:34 pm

Oh I should have think twice...
Thank you for your answer!

Return to “Technical Discussion”

Who is online

Users browsing this forum: No registered users and 11 guests