forked from espressif/arduino-esp32
Functional interrupt (#1728)
* Initial * Implementation * Add to CMakelist.txt * Add example * Add IRAM_ATTR
This commit is contained in:
@ -0,0 +1,47 @@
|
||||
#include <Arduino.h>
|
||||
#include <FunctionalInterrupt.h>
|
||||
|
||||
#define BUTTON1 16
|
||||
#define BUTTON2 17
|
||||
|
||||
class Button
|
||||
{
|
||||
public:
|
||||
Button(uint8_t reqPin) : PIN(reqPin){
|
||||
pinMode(PIN, INPUT_PULLUP);
|
||||
attachInterrupt(PIN, std::bind(&Button::isr,this), FALLING);
|
||||
};
|
||||
~Button() {
|
||||
detachInterrupt(PIN);
|
||||
}
|
||||
|
||||
void IRAM_ATTR isr() {
|
||||
numberKeyPresses += 1;
|
||||
pressed = true;
|
||||
}
|
||||
|
||||
void checkPressed() {
|
||||
if (pressed) {
|
||||
Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses);
|
||||
pressed = false;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
const uint8_t PIN;
|
||||
volatile uint32_t numberKeyPresses;
|
||||
volatile bool pressed;
|
||||
};
|
||||
|
||||
Button button1(BUTTON1);
|
||||
Button button2(BUTTON2);
|
||||
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
button1.checkPressed();
|
||||
button2.checkPressed();
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
struct Button {
|
||||
const uint8_t PIN;
|
||||
uint32_t numberKeyPresses;
|
||||
bool pressed;
|
||||
};
|
||||
|
||||
Button button1 = {23, 0, false};
|
||||
Button button2 = {18, 0, false};
|
||||
|
||||
void IRAM_ATTR isr(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, isr, &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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user