## Generic Button Example This example demonstrates how to utilize the `generic_button` component to read button events. ## Hardware * Any GPIO button on any development board can be used in this example. ## Build and Flash Build the project and flash it to the board, then run the monitor tool to view the serial output: * Run `. <directory with ESP-IDF version>/export.sh` to set IDF environment * Run `idf.py set-target esp32xx` to set target chip * Run `idf.py -p PORT flash monitor` to build, flash and monitor the project (To exit the serial monitor, type `Ctrl-]` (or on a Mac `Ctrl-Option-6`). See the Getting Started Guide for all the steps to configure and use the ESP-IDF to build projects. ## Function A button is defined. Four callback functions are defined for the events * BUTTON_SINGLE_CLICK, * BUTTON_DOUBLE_CLICK, * BUTTON_MULTIPLE_CLICK_3 (triple click) and * BUTTON_LONG_PRESS_START_5000 (long press for at least 5000 ms). In case of a single_click the onboard LED is flashed three times (if LED type is "LED strip": red, green ,blue). In case of a double click the onboard LED is flashed six times (if LED type is "LED strip": red, red, green, green, blue, blue). In case of a triple click the onboard LED is flashed nine times (if LED type is "LED strip": red, red, red, green, green, green, blue, blue, blue). In case of a long press of at least 5000 ms the onboard LED is flashed two times (if LED type is "LED strip": white, white). ## Example Output ``` I (300) main_task: Calling app_main() I (300) onBoard Button: onBoardButton Example Program I (300) M5 Atom Lite onBoardLed: configured to use addressable LED strip! I (300) onBoardButton: Button Type GPIO I (310) button: IoT Button Version: 4.1.6 I (310) onBoardButton: RegisterCallbackForEvent called I (310) onBoardButton: RegisterCallbackForEvent called I (320) onBoardButton: RegisterCallbackForEvent called with args I (320) onBoardButton: RegisterCallbackForEvent called with args I (330) onBoard Button: wait for button callback events I (7620) onBoardButton Callback: for Event BUTTON_SINGLE_CLICK called! I (7620) button: BUTTON_SINGLE_CLICK I (7620) M5 Atom Lite onBoardLed: Turning the LED ON! I (8130) M5 Atom Lite onBoardLed: Turning the LED OFF! I (8630) M5 Atom Lite onBoardLed: Turning the LED ON! I (9130) M5 Atom Lite onBoardLed: Turning the LED OFF! I (9630) M5 Atom Lite onBoardLed: Turning the LED ON! I (10130) M5 Atom Lite onBoardLed: Turning the LED OFF! I (12710) Button Callback: for Event BUTTON_DOUBLE_CLICK called! I (12710) button: BUTTON_DOUBLE_CLICK I (12710) M5 Atom Lite onBoardLed: Turning the LED ON! I (13210) M5 Atom Lite onBoardLed: Turning the LED OFF! I (13710) M5 Atom Lite onBoardLed: Turning the LED ON! I (14210) M5 Atom Lite onBoardLed: Turning the LED OFF! I (14710) M5 Atom Lite onBoardLed: Turning the LED ON! I (15210) M5 Atom Lite onBoardLed: Turning the LED OFF! I (15710) M5 Atom Lite onBoardLed: Turning the LED ON! I (16210) M5 Atom Lite onBoardLed: Turning the LED OFF! I (16710) M5 Atom Lite onBoardLed: Turning the LED ON! I (17210) M5 Atom Lite onBoardLed: Turning the LED OFF! I (17710) M5 Atom Lite onBoardLed: Turning the LED ON! I (18210) M5 Atom Lite onBoardLed: Turning the LED OFF! I (20300) Button Callback: for Event BUTTON_MULTIPLE_CLICK called! I (20300) button: BUTTON_MULTIPLE_CLICK I (20300) M5 Atom Lite onBoardLed: Turning the LED ON! I (20800) M5 Atom Lite onBoardLed: Turning the LED OFF! I (21300) M5 Atom Lite onBoardLed: Turning the LED ON! I (21800) M5 Atom Lite onBoardLed: Turning the LED OFF! I (22300) M5 Atom Lite onBoardLed: Turning the LED ON! I (22800) M5 Atom Lite onBoardLed: Turning the LED OFF! I (23300) M5 Atom Lite onBoardLed: Turning the LED ON! I (23800) M5 Atom Lite onBoardLed: Turning the LED OFF! I (24300) M5 Atom Lite onBoardLed: Turning the LED ON! I (24800) M5 Atom Lite onBoardLed: Turning the LED OFF! I (25300) M5 Atom Lite onBoardLed: Turning the LED ON! I (25800) M5 Atom Lite onBoardLed: Turning the LED OFF! I (26300) M5 Atom Lite onBoardLed: Turning the LED ON! I (26800) M5 Atom Lite onBoardLed: Turning the LED OFF! I (27300) M5 Atom Lite onBoardLed: Turning the LED ON! I (27800) M5 Atom Lite onBoardLed: Turning the LED OFF! I (28300) M5 Atom Lite onBoardLed: Turning the LED ON! I (28800) M5 Atom Lite onBoardLed: Turning the LED OFF! I (35100) Button Callback: for Event BUTTON_LONG_PRESS_START_5000 called! I (35100) button: BUTTON_LONG_PRESS_START I (35100) M5 Atom Lite onBoardLed: Turning the LED ON! I (35600) M5 Atom Lite onBoardLed: Turning the LED OFF! I (36100) M5 Atom Lite onBoardLed: Turning the LED ON! I (36600) M5 Atom Lite onBoardLed: Turning the LED OFF! ```
To create a project from this example, run:
idf.py create-project-from-example "elrebo-de/generic_button=1.1.0:onBoardButton"