Skip to content

Commit 76a79b5

Browse files
committed
Core can't reference to libraries, need to move FunctionalInterrupt to libraries, too.
1 parent e786dc2 commit 76a79b5

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <FunctionalInterrupt.h>
2+
#include <Arduino.h>
3+
4+
#if defined(ESP32)
5+
#define BUTTON1 16
6+
#define BUTTON2 17
7+
#elif defined(ARDUINO_ESP8266_WEMOS_D1MINI)
8+
#define BUTTON1 D4
9+
#define BUTTON2 D3
10+
#else
11+
#define BUTTON1 2
12+
#define BUTTON2 0
13+
#endif
14+
15+
class Button {
16+
public:
17+
Button(uint8_t reqPin) : PIN(reqPin) {
18+
pinMode(PIN, INPUT_PULLUP);
19+
// Arduino C API:
20+
//attachInterruptArg(PIN, [](void* self) {
21+
// static_cast<Button*>(self)->isr();
22+
//}, this, FALLING); // works on ESP32; fails on ESP8266: "ISR not in IRAM"
23+
//attachInterruptArg(PIN, reinterpret_cast<void(*)(void*)>(&isr_static), this, FALLING); // works on ESP32; works on ESP8266
24+
// FunctionalInterrupts API:
25+
attachScheduledInterrupt(PIN, [this](InterruptInfo ii) {
26+
Serial.print("Pin ");
27+
Serial.println(ii.pin);
28+
isr();
29+
}, FALLING); // works on ESP32; works on ESP8266
30+
};
31+
~Button() {
32+
detachInterrupt(PIN);
33+
}
34+
35+
#if defined(ESP8266)
36+
void ICACHE_RAM_ATTR isr()
37+
#elif defined(ESP32)
38+
void IRAM_ATTR isr()
39+
#endif
40+
{
41+
numberKeyPresses += 1;
42+
pressed = true;
43+
}
44+
45+
#if defined(ESP8266)
46+
static void ICACHE_RAM_ATTR isr_static(Button* const self)
47+
#elif defined(ESP32)
48+
static void IRAM_ATTR isr_static(Button* const self)
49+
#endif
50+
{
51+
self->isr();
52+
}
53+
54+
void checkPressed() {
55+
if (pressed) {
56+
Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses);
57+
pressed = false;
58+
}
59+
}
60+
61+
private:
62+
const uint8_t PIN;
63+
volatile uint32_t numberKeyPresses = 0;
64+
volatile bool pressed = false;
65+
};
66+
67+
Button* button1;
68+
Button* button2;
69+
70+
71+
void setup() {
72+
Serial.begin(115200);
73+
Serial.println("FunctionalInterrupt test/example");
74+
75+
button1 = new Button(BUTTON1);
76+
button2 = new Button(BUTTON2);
77+
78+
Serial.println("setup() complete");
79+
}
80+
81+
void loop() {
82+
button1->checkPressed();
83+
button2->checkPressed();
84+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#######################################
2+
# Datatypes (KEYWORD1)
3+
#######################################
4+
5+
InterruptInfo KEYWORD1
6+
ArgStructure KEYWORD1
7+
8+
#######################################
9+
# Methods and Functions (KEYWORD2)
10+
#######################################
11+
12+
attachInterrupt KEYWORD2
13+
attachScheduledInterrupt KEYWORD2
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=FunctionalInterrupt
2+
version=1.0
3+
author=hreintke <[email protected]>
4+
maintainer=hreintke <[email protected]>
5+
sentence=C++ functional and scheduled interrupt handling
6+
paragraph=
7+
category=Other
8+
url=https://github.com/esp8266/Arduino
9+
architectures=esp8266
10+
dot_a_linkage=true

0 commit comments

Comments
 (0)