-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESP_INTERUPT_TEST.ino
105 lines (77 loc) · 2.28 KB
/
ESP_INTERUPT_TEST.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <Arduino.h>
// ==================== TUNEABLE PARAMETERS ====================
// Uncomment these lines in order to activate Serial debug
#define DEBUG
#define DEBUGLIB_BAUDS 115200
// Button is used to permit MAC or enter WEB server mode
#define BUTTON_PIN D6
// button press durations:
// regular: Add last MAC
// long: Go AP Server mode (creates its own WiFi)
#define PIN_LONG_PRESS_MS 1000
volatile bool lastPinStatus = true;
volatile bool pinStatus = true;
volatile unsigned long pinLastTrigger = 0;
volatile bool shortPress = false;
volatile bool longPress = false;
// ==================== Function definitions ====================
void setup();
void loop();
void pinInterrupt();
// ==================== Debug functionality; simplified, taken from: https://github.com/Naguissa/uDebugLib ====================
#ifdef DEBUG
#define uDebugLibInit() Serial.begin(DEBUGLIB_BAUDS)
#define DEBUG_PRINT Serial.print
#define DEBUG_PRINTLN Serial.println
#else
#define uDebugLibInit()
#define DEBUG_PRINT
#define DEBUG_PRINTLN
#endif
// ==================== Setup functions ====================
// This section of code runs only once at start-up.
void setup() {
uDebugLibInit();
DEBUG_PRINTLN("Serial OK (if debug enabled)");
pinMode(BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), pinInterrupt, CHANGE);
}
//
// ==================== LOOP ====================
//
void loop() {
if (lastPinStatus != pinStatus) {
lastPinStatus = pinStatus;
DEBUG_PRINT("Pin status changed; new status: ");
DEBUG_PRINTLN(pinStatus ? "ON" : "OFF");
}
if (shortPress) {
shortPress = false;
DEBUG_PRINTLN("Short press detected");
}
if (longPress) {
longPress = false;
DEBUG_PRINTLN("Long press detected");
}
}
// ==================== PIN CHANGE INTERRUPT ====================
ICACHE_RAM_ATTR void pinInterrupt() {
bool prev = pinStatus;
pinStatus = digitalRead(BUTTON_PIN);
if (prev == pinStatus) {
return; // Skip, duplicate
}
if (pinStatus) { // Pressed
pinLastTrigger = millis();
} else { // release
if (pinLastTrigger == 0) {
return; // Release without press? Any failure
}
if (millis() > pinLastTrigger + PIN_LONG_PRESS_MS) { // Long press
longPress = true;
} else { // Short press
shortPress = true;
}
pinLastTrigger = 0;
}
}