12 years, 10 months ago.

Feature request: Argument to interrupt handler

Hi:

Possible to update InterruptIn class to include argument to interrupt handler? Instead of

void rise(void (*fptr)(void));

I propose this

void rise(void (*fptr)(void *arg = NULL), void *arg);

The new rise() will assign pointer to the function to its internal rise interrupt handler, and also assign the address or arg into it.

For example,

#include "mbed.h"
 
InterruptIn button(p5);
DigitalOut  Leds[] = { (LED1), (LED2), (LED3)};
DigitalOut  flash(LED4);
int         i = 0; 

// LED1 .. LED4 will be lighted up in sequence at each press of the button 
void flip(void *argv) {
    int n = (int) *argv;
    Leds[n] = !leds[n];
    n = ++n % 3;
    (int) (*argv) = n;
}
 
int main() {
    button.rise(&flip, &i);
    while(1) {           // wait around, interrupts will interrupt this!
        flash = !flash;
        wait(0.25);
    }
}

Suggestion?

Question relating to:

errored program

posted by siddhartha A R 24 Jul 2017

2 Answers

HM Yoong
poster
12 years, 10 months ago.

I think it again, when an interrupt is trigger, no one is there to pass argument to a generic interrupt handler.

In other words, each interrupt handler has to be created for each InterruptIn object if it is intended to do different actions.

Accepted Answer
12 years, 10 months ago.

Well I can see it sometimes being useful, but not for interruptIn. Why not just use 'i' directly here in your interrupt handler?

Precisely i was using "i"

posted by HM Yoong 24 Jul 2017