Skip to content

Functional interrupt #1728

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 17, 2018
Merged

Conversation

hreintke
Copy link
Contributor

@hreintke hreintke commented Aug 5, 2018

Add the functional option to attachInterrupt.
Implementation is similar to ESP8266

@me-no-dev
Copy link
Member

Please add example :)

@hreintke
Copy link
Contributor Author

I will make the example.
Where do you want me to put it ?
I only see example directories with libraries.

@me-no-dev
Copy link
Member

https://github.com/espressif/arduino-esp32/tree/master/libraries/ESP32/examples
the ESP32 library is used exactly for examples related to the core :) add a GPIO folder there, then move the GPIOInterrupt sketch and folder there, renaming both to BasicGPIOInterrupt, then add your example with new name. If you do it all good, both examples will show in the IDE under File->Examples->ESP32->GPIO

@me-no-dev me-no-dev merged commit ea61563 into espressif:master Sep 17, 2018
@Elijahg
Copy link

Elijahg commented Jan 10, 2019

Your example doesn't actually work, it only ever calls button2's interrupt. It fails to compile when renaming isr to isr2, forcing it to use the function with the void* arg parameter instead of the overloaded isr function.

#include <Arduino.h>

struct Button {
    const uint8_t PIN;
    uint32_t numberKeyPresses;
    bool pressed;
};

Button button1 = {13, 0, false};
Button button2 = {12, 0, false};

void IRAM_ATTR isr2(void* arg) {
    Button* s = static_cast<Button*>(arg);
    s->numberKeyPresses += 1;
    s->pressed = true;
}

void IRAM_ATTR isr() {
    button2.numberKeyPresses += 1;
    button2.pressed = true;
}

void setup() {
    Serial.begin(115200);
    pinMode(button1.PIN, INPUT_PULLUP);
    attachInterruptArg(button1.PIN, isr2, &button1, FALLING);
    pinMode(button2.PIN, INPUT_PULLUP);
    attachInterrupt(button2.PIN, isr, FALLING);
}

void loop() {
    if (button1.pressed) {
        Serial.printf("Button 1 has been pressed %u times\n", button1.numberKeyPresses);
        button1.pressed = false;
    }
    if (button2.pressed) {
        Serial.printf("Button 2 has been pressed %u times\n", button2.numberKeyPresses);
        button2.pressed = false;
    }
    static uint32_t lastMillis = 0;
    if (millis() - lastMillis > 10000) {
      lastMillis = millis();
      detachInterrupt(button1.PIN);
    }
}

Results in invalid conversion from 'void (*)(void*)' to 'void (*)()' [-fpermissive]

@hreintke
Copy link
Contributor Author

hreintke commented Jan 11, 2019

@Elijahg :
I don't know what you want to achieve but ...

The sketch in your post works for me.
Be aware that after 10 seconds the button1 interrupt is disabled, from that moment onwards only button2 will be in use.

You post this in the Functional Interrupt PR, however your sketch doesn't use that functionality.

Using functional interrupt you can do f.e.

#include "FunctionalInterrupt.h"

struct Button {
    const uint8_t PIN;
    uint32_t numberKeyPresses;
    bool pressed;
};

Button button1 = {13, 0, false};
Button button2 = {12, 0, false};

void IRAM_ATTR isr2(void* arg) {
    Button* s = static_cast<Button*>(arg);
    s->numberKeyPresses += 1;
    s->pressed = true;
}

void setup() {
    Serial.begin(115200);
    pinMode(button1.PIN, INPUT_PULLUP);
    attachInterrupt(button1.PIN, std::bind(isr2,&button1), FALLING);
    pinMode(button2.PIN, INPUT_PULLUP);
    attachInterrupt(button2.PIN, std::bind(isr2, &button2), FALLING);
}

void loop() {
    if (button1.pressed) {
        Serial.printf("Button 1 has been pressed %u times\n", button1.numberKeyPresses);
        button1.pressed = false;
    }
    if (button2.pressed) {
        Serial.printf("Button 2 has been pressed %u times\n", button2.numberKeyPresses);
        button2.pressed = false;
    }
}

@hreintke hreintke deleted the FunctionalInterrupt branch January 11, 2019 14:54
@m0x72
Copy link

m0x72 commented Jan 18, 2019

@Elijahg Commit 879388e should have fixed the error you're experiencing with attachInterruptArg. (Release 1.0.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants