define multiple GPIO Pins / IRQ in a "smart" (Pythonic ?) way #11540
-
Am using a dedicated Pico / Rp2040 to control a large number of sensors. Essentially what I need is a detection of a "falling edge" signal and corrolate that to the GPIO Pin-id. Each Pin has an ISR that essentially writes the GPIO Pin (id) to its corresponding spot in a bytearray (taking the recommendations from the MP "writing Interrupts handlers" documentation). The above is part of a model train set-up in which I control signals, speed, position, sounds etc of multiple trains. As far as I know is there no direct way to extract the GPIO id from the Pin object. What I had to do was create say 20 unique ISR's only tasked with writing an unique number in the range 0..19 to the bytearray. Which means spelling out 20 times:
The process runs as a black box, so nobody but myself will be concerned that the code looks like being written by a first-grader. Still, I keep asking myself if there is no smarter way to accomplish the above. Tried various things to bring the required steps together in a loop construct but can't get it to work. H2 ? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
There have been a few discussions over the years about adding a way to extract some sort of ID from a Anyway. The first idea that comes to mind is that if you have a list of the "registered" pins, then from machine import *
pins = [Pin(7), Pin(2), Pin(9)]
def on_edge(p):
pin_index = pins.index(p)
...
for p in pins:
p.irq(handler=on_edge, ...) |
Beta Was this translation helpful? Give feedback.
-
Jim, thank your quick response AND your very helpful suggestion. I created a list of all instantiated Pin instances and in the ISR did an index_search on that list using the Pin object passed to in by the IRQ. The index I could then use to find the actual Pin number in a 2nd list containing the assigned pins. It is "clean" and quick. |
Beta Was this translation helpful? Give feedback.
-
Thank you, Robert. WIll that try as well. |
Beta Was this translation helpful? Give feedback.
There have been a few discussions over the years about adding a way to extract some sort of ID from a
machine.Pin
instance. It's difficult to do this in a portable way though -- on ports like rp2 and esp32 this is straightforward because the underlying IDs are numeric, but on say stm32 where a pin is a port and a number. Also there's both the ID that the board gives it versus the actual underlying pin (e.g. the GPIO 7 pin on rp2 is actually pin 9 on the package).Anyway. The first idea that comes to mind is that if you have a list of the "registered" pins, then
pins.index(p)
wherep
is the argument to the common IRQ handler will give you an index you can use into your bytearray. e.g.