Callback use, global flags and location #13713
Replies: 4 comments 3 replies
-
Have you tried to access that flag using ›remote.flagname‹ from within main.py? |
Beta Was this translation helpful? Give feedback.
-
In MicroPython callbacks can be methods of a user defined class. Hence they can access bound variables as required. |
Beta Was this translation helpful? Give feedback.
-
Before I use a |
Beta Was this translation helpful? Give feedback.
-
Thanks in my former life I was a C programmer so I get that using
interrupts can screw up the higher level code. In my case here I'm only
using callback timers at intervals in the second or more range. They set a
single flag (bool) and the code in main.py running and endless loop
responds and clears the flag. So thanks again.
Have a nice day...Denis
…On Wed, Feb 21, 2024 at 9:15 PM glenn20 ***@***.***> wrote:
One note of caution. If your callback handles events that may be bursty
and you care about ensuring you recognise every single event, you may want
to push the data from each event onto a queue, so you are sure each one
won't be missed by the main execution path.
If all you care about is the data from the most recent event, then you can
ignore this advice
|
Beta Was this translation helpful? Give feedback.
-
I understand that callback functions cannot return a variable. Therefore setting a flag (True or False) seems like the only way to signal other code that the callback occurred. I got some infrared remote code from Github that work fine on my hardware. However the callback example only prints the remote code. I need to get access to it from my main.py where I wish to deal with it. So instead of printing the code just set a flag. However the flag is only a global within the remote module (py) where the callback resides. My problem is how do I get the flag passed to my main.py so that I can respond to the remote hit and clear the flag for the next hit. Another approach would be to have my callback reside in main.py but as a rule I want to keep my main small. Any suggestions on how best to resolve this issue. I've tried to import a the flag from the remote.py into my main.py but although the interpreter doesn't report an import error the resulting flag in main never gets set while the global flag in remote.py does!
P.S. I could have copy and paste a bunch of my code here but I'm more interested is in learning and understanding.
Regards Denis Lebel
Beta Was this translation helpful? Give feedback.
All reactions