Created
June 17, 2018 16:09
-
-
Save rac2030/939cd59b5072a3b17bee851d313ca8b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <esp_sleep.h> | |
// Identifier of the badge, used for communication | |
uint64_t chipid; | |
// PINS | |
// Buttons | |
const byte BTN1 = D19; | |
const uint64_t BTN1_mask = 1ULL << BTN1; | |
const byte BTN2 = D8; | |
const uint64_t BTN2_mask = 1ULL << BTN2; | |
const byte BTN3 = D21; | |
const uint64_t BTN3_mask = 1ULL << BTN3; | |
const byte BTN4 = D20; | |
const uint64_t BTN4_mask = 1ULL << BTN4; | |
// interrupts | |
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED; | |
void IRAM_ATTR handleInterruptBTN1() { | |
handleInterrupButtonDebounce(BTN1, "Button 1 was pushed"); // Handle BTN1 | |
} | |
void IRAM_ATTR handleInterruptBTN2() { | |
handleInterrupButtonDebounce(BTN2, "Button 2 was pushed"); // Handle BTN2 | |
} | |
void IRAM_ATTR handleInterruptBTN3() { | |
handleInterrupButtonDebounce(BTN3, "Button 3 was pushed"); // Handle BTN3 | |
} | |
void IRAM_ATTR handleInterruptBTN4() { | |
handleInterrupButtonDebounce(BTN4, "Button 4 was pushed"); // Handle BTN4 | |
} | |
/** | |
* This is the handler actually doing the work, locking the mutex (one interrupt at a time) and | |
* debouncing it so it counts only if the time beetween both interrupts is greater than 200ms. | |
* */ | |
void handleInterrupButtonDebounce(byte buttonPin, String text) { | |
portENTER_CRITICAL_ISR(&mux); | |
static unsigned long last_interrupt_time = 0; | |
unsigned long interrupt_time = millis(); | |
// If interrupts come faster than 200ms, assume it's a bounce and ignore | |
if (interrupt_time - last_interrupt_time > 100) { | |
// distinguish between pressed and released state | |
if(digitalRead(buttonPin) == LOW) { | |
Serial.print("[pressed] "); | |
} else { | |
Serial.print("[released] "); | |
} | |
Serial.println(text); | |
last_interrupt_time = interrupt_time; | |
} | |
portEXIT_CRITICAL_ISR(&mux); | |
// Configure wakeup from deep sleep on all 4 buttons | |
//TODO works only with 1 pin in the mask :-/ why do you not work? | |
esp_sleep_enable_ext1_wakeup(BTN1_mask | BTN2_mask , ESP_EXT1_WAKEUP_ALL_LOW); | |
esp_deep_sleep_start(); | |
} | |
void setup() | |
{ | |
Serial.begin(115200); | |
// Get this devices ID | |
chipid=ESP.getEfuseMac(); | |
// Get wakeup reason and handle if it is external interrupt from pushbutton | |
if(esp_sleep_get_wakeup_cause() == 2) { // Wakeup caused by external signal using RTC_CNTL | |
Serial.print("[pressed] "); | |
} | |
// Setup interrupts for Buttons | |
pinMode(BTN1, INPUT_PULLUP); | |
attachInterrupt(digitalPinToInterrupt(BTN1), handleInterruptBTN1, FALLING); | |
pinMode(BTN2, INPUT_PULLUP); | |
attachInterrupt(digitalPinToInterrupt(BTN2), handleInterruptBTN2, FALLING); | |
pinMode(BTN3, INPUT_PULLUP); | |
attachInterrupt(digitalPinToInterrupt(BTN3), handleInterruptBTN3, FALLING); | |
pinMode(BTN4, INPUT_PULLUP); | |
attachInterrupt(digitalPinToInterrupt(BTN4), handleInterruptBTN4, FALLING); | |
// Configure wakeup from deep sleep on all 4 buttons | |
//Serial.println(esp_sleep_enable_ext1_wakeup(BTN1_mask | BTN2_mask | BTN3_mask | BTN4_mask, ESP_EXT1_WAKEUP_ALL_LOW)); | |
Serial.println("Initialized interrupts"); | |
// Go sleeping deep | |
//esp_deep_sleep_start(); | |
} | |
void loop(){} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment