Button Callback Regular Expression
When a IRQ is called for a button press, we are passed an event that tell us what pin gets called. Unfortunately, MicroPython does not provide a method to just get the integer of the pin, which we need of all our buttons use a single IRQ function but need to do different tasks based on which button is being used.
The pin number is buried within the GPIO string like this:
1 |
|
We need to have a function that we can pass this string and it will just return the integer of the GPIO. It does this using the regular expression (re) function within MicroPython.
1 2 3 4 5 6 7 8 9 10 11 |
|
1 2 3 4 |
|
Here is the regular expression line
1 |
|
This function will look into the input_string for the letters "GPIO" followed by a decimal digit. It will then return that decimal digit as a string to the match variable.
It uses a regular expression pattern r"GPIO(\d+)"
to find and extract the GPIO number:
- GPIO
matches the literal characters "GPIO"
- (\d+)
is a capturing group that matches one or more digits:
- \d
matches any digit (0-9)
- +
means "one or more times"
- The parentheses ()
create a capturing group so we can extract just the number
The re.search()
function looks for this pattern anywhere in the input string and returns a match object if found
a match.
If a match is found (if match
):
- match.group(1)
gets the text from the first capturing group (the digits)
- int()
converts the string of digits to an integer
- This automatically removes any leading zeros (so "03" becomes 3)
If no match is found, it returns None.