Callback help required #17905
Replies: 6 comments 1 reply
-
A callback does NOT return a value. It is a simplified ISR. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the prompt reply! Since I want the response to be immediate upon
the remote press I see no alternatives
than to keep both the remote_init() and its callback function located in
main.py.
Have a nice day... Denis Lebel
…On Tue, Aug 12, 2025 at 10:05 AM Norbert ***@***.***> wrote:
My question: is there a simple way to perhaps import a callback function
from a remote_driver.py so that upon it being triggered the remote key
value can be return to main.py without having the callback located in
main.py
A callback does NOT return a value. It is a simplified ISR.
And it's called asynchronously, so to whom would it return a value?
It could send a signal (to anyone who it may concern)
That signal recipient could then access a value from a predetermined
address (or a global value).
—
Reply to this email directly, view it on GitHub
<#17905 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AEFK5EL5CPGNCLQ676RYV3T3NHYCVAVCNFSM6AAAAACDWQXU52VHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTIMBYGMYDQNI>
.
You are receiving this because you authored the thread.Message ID:
***@***.***
com>
|
Beta Was this translation helpful? Give feedback.
-
Assuming you are using a module global variables to signal state, You then can move that class to its own module, and import and instantiate it. Then you can register the instance's method as the callback function. |
Beta Was this translation helpful? Give feedback.
-
Isn't it as easy as a callback is just a function or method that is passed to another function or method. (though I'm in grave risk of displaying my ignorance :) ) In python file tst_1.py :-
And in python file tst_2.py :-
Well thats my take on callbacks exhausted, and fair warning I'm only an old guy dabbling in this stuff. |
Beta Was this translation helpful? Give feedback.
-
@ADenislebel Thank you for using my IR receiver. The callback is a soft interrupt service routine (ISR). As others have pointed out, if the callback returned a value it would be passed to the driver and ignored. The simple approach is to have the callback populate a global object, with the main routine polling the object to see if anything was received. Adapting my demo code: import time
from machine import Pin
from ir_rx.nec import NEC_8 # NEC remote, 8 bit addresses
rx = [0, 0, 0, False] # [addr, data, ctrl, repeat]
def callback(data, addr, ctrl):
if data < 0: # NEC protocol sends repeat codes.
rx[3] = True
rx[0] = 1 # Use the addr field to indicate something has been received
else:
rx[0] = addr
rx[1] = data
rx[2] = ctrl
ir = NEC_8(Pin('X3', Pin.IN), callback)
while True:
while rx[0] == 0: # Wait for the next callback
time.sleep_ms(10)
# Something has been received: process it
rx[0] = 0 # Clear down the "received" flag The original demo flashes an LED at the same time. There are two ways to do this now, the amateur way and the professional ;-). The amateur way is to count iterations in the inner loop. When the count reaches say 50, toggle the LED and reset the count. The professional way is to use |
Beta Was this translation helpful? Give feedback.
-
Thanks again you guys, you're a valuable help for a Newbie as the raw condensed reference documentation is difficult to wrap my old C mind around it. One thing for sure I don't have Class (yet) in this language.... LOL |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have limited Micropython experience so I need some help in understanding how callbacks operate. Specifically I'm using the ir_rx.py and the Nec.py I got from github and they are working fine. However both the ir_init() and ir_callback() functions reside in my main.py. I'm attempting to tidy up (reduce main size) and move all I can into a separate PY. I understand that callbacks are driven by the underlying interrupt. Therefore the return value from a callback (in this case the remote key value) goes back to the where the callback function was created.
My question: is there a simple way to perhaps import a callback function from a remote_driver.py so that upon it being triggered the remote key value can be return to main.py without having the callback located in main.py
Thanks Denis Lebel
Beta Was this translation helpful? Give feedback.
All reactions