Learning ExtInt interrupts

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
synimak
Posts: 20
Joined: Sun Feb 28, 2016 5:11 am
Contact:

Learning ExtInt interrupts

Post by synimak » Thu Mar 24, 2016 12:13 pm

I'm trying to learn how to use interrupts but but getting no where. Everything I do results in the callback being initiated when the pyboard boots and nothing happening when the pin does actually go low. I had some more complex code that I started with but now I'm just trying to just print "Hello" when a pin goes low.

Code: Select all

from pyb import Pin, ExtInt

def call():
	print("Hello")
	
extint = ExtInt(Pin('X1'), ExtInt.IRQ_FALLING, Pin.PULL_UP, call())

while True:
	pass
	
	
What am I missing?

User avatar
marfis
Posts: 215
Joined: Fri Oct 31, 2014 10:29 am
Location: Zurich / Switzerland

Re: Learning ExtInt... ???

Post by marfis » Thu Mar 24, 2016 12:49 pm

check out the doc:
https://micropython.org/doc/module/pyb/ExtInt

you'll notice that your callback must provide one argument. And in the setup function you have to remove the "()" from call.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Learning ExtInt... ???

Post by dhylands » Thu Mar 24, 2016 10:50 pm

What @marfis said.

By passing call() you're calling the function call once, and passing None (the return value from calling call) to ExtInt which tells it to disable ExtInt handling.

The one call you're seeing is the call that you're making by adding the parenthesis (i.e. call() actually calls the function).

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: Learning ExtInt... ???

Post by SpotlightKid » Thu Mar 24, 2016 10:52 pm

When you pass a callback function as an argument to another function, you have to pass the function object and not the return value of calling the function. I.e. you don't put () behind the function name, because that means calling the function. You just put the function name as the argument. In Python almost everything is an object, including functions, so when you define a function, you are basically just creating a variable with the function name, which holds a reference to the function object.

Post Reply