|
| 1 | +/* |
| 2 | + SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD |
| 3 | +
|
| 4 | + SPDX-License-Identifier: Apache-2.0 |
| 5 | +
|
| 6 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + you may not use this file except in compliance with the License. |
| 8 | + You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, software |
| 13 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + See the License for the specific language governing permissions and |
| 16 | + limitations under the License. |
| 17 | +
|
| 18 | + ESP32 Lambda FunctionalInterrupt Example |
| 19 | + ======================================== |
| 20 | +
|
| 21 | + This example demonstrates how to use lambda functions with FunctionalInterrupt |
| 22 | + for GPIO pin interrupt callbacks on ESP32. It shows CHANGE mode detection |
| 23 | + with LED toggle functionality and proper debouncing. |
| 24 | +
|
| 25 | + Hardware Setup: |
| 26 | + - Use BOOT Button or connect a button between BUTTON_PIN and GND (with internal pullup) |
| 27 | + - Use Builtin Board LED or connect an LED with resistor to GPIO 2 (LED_PIN) |
| 28 | +
|
| 29 | + Features Demonstrated: |
| 30 | + 1. CHANGE mode lambda to detect both RISING and FALLING edges |
| 31 | + 2. LED toggle on button press (FALLING edge) |
| 32 | + 3. Edge type detection using digitalRead() within ISR |
| 33 | + 4. Hardware debouncing with configurable timeout |
| 34 | +
|
| 35 | + IMPORTANT NOTE ABOUT ESP32 INTERRUPT BEHAVIOR: |
| 36 | + - Only ONE interrupt handler can be attached per GPIO pin at a time |
| 37 | + - Calling attachInterrupt() on a pin that already has an interrupt will override the previous one |
| 38 | + - This applies regardless of edge type (RISING, FALLING, CHANGE) |
| 39 | + - If you need both RISING and FALLING detection on the same pin, use CHANGE mode |
| 40 | + and determine the edge type within your handler by reading the pin state |
| 41 | +*/ |
| 42 | + |
| 43 | +#include <Arduino.h> |
| 44 | +#include <FunctionalInterrupt.h> |
| 45 | + |
| 46 | +// Pin definitions |
| 47 | +#define BUTTON_PIN BOOT_PIN // BOOT BUTTON - change as needed |
| 48 | +#ifdef LED_BUILTIN |
| 49 | +#define LED_PIN LED_BUILTIN |
| 50 | +#else |
| 51 | +#warning Using LED_PIN = GPIO 2 as default - change as needed |
| 52 | +#define LED_PIN 2 // change as needed |
| 53 | +#endif |
| 54 | + |
| 55 | +// Global variables for interrupt handling (volatile for ISR safety) |
| 56 | +volatile uint32_t buttonPressCount = 0; |
| 57 | +volatile uint32_t buttonReleaseCount = 0; |
| 58 | +volatile bool buttonPressed = false; |
| 59 | +volatile bool buttonReleased = false; |
| 60 | +volatile bool ledState = false; |
| 61 | +volatile bool ledStateChanged = false; // Flag to indicate LED needs updating |
| 62 | + |
| 63 | +// Debouncing variables (volatile for ISR safety) |
| 64 | +volatile unsigned long lastButtonInterruptTime = 0; |
| 65 | +const unsigned long DEBOUNCE_DELAY_MS = 50; // 50ms debounce delay |
| 66 | + |
| 67 | +// State-based debouncing to prevent hysteresis issues |
| 68 | +volatile bool lastButtonState = HIGH; // Track last stable state (HIGH = released) |
| 69 | + |
| 70 | +// Global lambda function (declared at file scope) - ISR in IRAM |
| 71 | +IRAM_ATTR std::function<void()> changeModeLambda = []() { |
| 72 | + // Simple debouncing: check if enough time has passed since last interrupt |
| 73 | + unsigned long currentTime = millis(); |
| 74 | + if (currentTime - lastButtonInterruptTime < DEBOUNCE_DELAY_MS) { |
| 75 | + return; // Ignore this interrupt due to bouncing |
| 76 | + } |
| 77 | + |
| 78 | + // Read current pin state to determine edge type |
| 79 | + bool currentState = digitalRead(BUTTON_PIN); |
| 80 | + |
| 81 | + // State-based debouncing: only process if state actually changed |
| 82 | + if (currentState == lastButtonState) { |
| 83 | + return; // No real state change, ignore (hysteresis/noise) |
| 84 | + } |
| 85 | + |
| 86 | + // Update timing and state |
| 87 | + lastButtonInterruptTime = currentTime; |
| 88 | + lastButtonState = currentState; |
| 89 | + |
| 90 | + if (currentState == LOW) { |
| 91 | + // FALLING edge detected (button pressed) - set flag for main loop |
| 92 | + // volatile variables require use of temporary value transfer |
| 93 | + uint32_t temp = buttonPressCount + 1; |
| 94 | + buttonPressCount = temp; |
| 95 | + buttonPressed = true; |
| 96 | + ledStateChanged = true; // Signal main loop to toggle LED |
| 97 | + } else { |
| 98 | + // RISING edge detected (button released) - set flag for main loop |
| 99 | + // volatile variables require use of temporary value transfer |
| 100 | + uint32_t temp = buttonReleaseCount + 1; |
| 101 | + buttonReleaseCount = temp; |
| 102 | + buttonReleased = true; |
| 103 | + } |
| 104 | +}; |
| 105 | + |
| 106 | +void setup() { |
| 107 | + Serial.begin(115200); |
| 108 | + delay(1000); // Allow serial monitor to connect |
| 109 | + |
| 110 | + Serial.println("ESP32 Lambda FunctionalInterrupt Example"); |
| 111 | + Serial.println("========================================"); |
| 112 | + |
| 113 | + // Configure pins |
| 114 | + pinMode(BUTTON_PIN, INPUT_PULLUP); |
| 115 | + pinMode(LED_PIN, OUTPUT); |
| 116 | + digitalWrite(LED_PIN, LOW); |
| 117 | + |
| 118 | + // CHANGE mode lambda to handle both RISING and FALLING edges |
| 119 | + // This toggles the LED on button press (FALLING edge) |
| 120 | + Serial.println("Setting up CHANGE mode lambda for LED toggle"); |
| 121 | + |
| 122 | + // Use the global lambda function |
| 123 | + attachInterrupt(BUTTON_PIN, changeModeLambda, CHANGE); |
| 124 | + |
| 125 | + Serial.println(); |
| 126 | + Serial.printf("Lambda interrupt configured on Pin %d (CHANGE mode)\r\n", BUTTON_PIN); |
| 127 | + Serial.printf("Debounce delay: %lu ms\r\n", DEBOUNCE_DELAY_MS); |
| 128 | + Serial.println(); |
| 129 | + Serial.println("Press the button to toggle the LED!"); |
| 130 | + Serial.println("Button press (FALLING edge) will toggle the LED."); |
| 131 | + Serial.println("Button release (RISING edge) will be detected and reported."); |
| 132 | + Serial.println("Button includes debouncing to prevent mechanical bounce issues."); |
| 133 | + Serial.println(); |
| 134 | +} |
| 135 | + |
| 136 | +void loop() { |
| 137 | + // Handle LED state changes (ISR-safe approach) |
| 138 | + if (ledStateChanged) { |
| 139 | + ledStateChanged = false; |
| 140 | + ledState = !ledState; // Toggle LED state in main loop |
| 141 | + digitalWrite(LED_PIN, ledState); |
| 142 | + } |
| 143 | + |
| 144 | + // Check for button presses |
| 145 | + if (buttonPressed) { |
| 146 | + buttonPressed = false; |
| 147 | + Serial.printf("==> Button PRESSED! Count: %lu, LED: %s (FALLING edge)\r\n", buttonPressCount, ledState ? "ON" : "OFF"); |
| 148 | + } |
| 149 | + |
| 150 | + // Check for button releases |
| 151 | + if (buttonReleased) { |
| 152 | + buttonReleased = false; |
| 153 | + Serial.printf("==> Button RELEASED! Count: %lu (RISING edge)\r\n", buttonReleaseCount); |
| 154 | + } |
| 155 | + |
| 156 | + delay(10); |
| 157 | +} |
0 commit comments