Skip to content

Button press detection with debounce, for esp32 idf

License

Notifications You must be signed in to change notification settings

JadElClemens/esp32-button

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Button press detector

This implements a version of THE ULTIMATE DEBOUNCER(TM) from hackaday.

It can monitor multiple pins, and sends button events over a queue for your application to process.

Changes in Fork

Forked from craftmetrics's implementation.

This fork introduces the following changes:

  • A CMakeLists.txt file for compatibility with the new Python/CMake-based ESP-IDF build system
  • Added __cplusplus preprocessor check for inclusion in C++ projects
  • Posts to the ESP-IDF Default Event Loop instead of a FreeRTOS queue.
  • Adds KConfig options for long press duration/repeat time, core affinity, task stack size, and priority

Available input GPIO pins

Only the following pins can be used as inputs on the ESP32:

0-19, 21-23, 25-27, 32-39

Example Usage

void buttonHandler(void*, esp_event_base_t, int32_t, void*);

ESP_ERROR_CHECK(esp_event_loop_create_default());
button_init(PIN_BIT(BUTTON_1) | PIN_BIT(BUTTON_2));
ESP_ERROR_CHECK(esp_event_handler_register(BUTTON_EVENT, ESP_EVENT_ANY_ID, buttonHandler, nullptr));

void buttonHandler(void* arg, esp_event_base_t eventbase, int32_t eventid, void* data) {
    switch(eventid) {
	    case EVT_BUTTON_UP:
		    ...
			break;
		case EVT_BUTTON_DOWN:
		    ...
			break;
	}
}

Event Types

EVT_BUTTON_DOWN

Triggered when the button is first considered pressed.

Also triggered every 50ms during a long press.

EVT_BUTTON_UP

Triggered when the button is considered released. I recommend using EVT_BUTTON_DOWN to check/mark a press vs. a hold and checking this state in EVT_BUTTON_UP to perform some action.

About

Button press detection with debounce, for esp32 idf

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 97.8%
  • CMake 1.8%
  • Makefile 0.4%