### Basic EncButton Usage Source: https://github.com/gyverlibs/encbutton/blob/main/README_EN.md Demonstrates the basic setup and loop for the EncButton library, attaching a callback function. ```cpp VOID setup () { Serial.Begin (115200); eb.attach (callback); } VOID loop () { eb.tick (); } ``` -------------------------------- ### Ultra-Minimal EncButton Configuration Source: https://context7.com/gyverlibs/encbutton/llms.txt Example of disabling all optional features and setting compile-time timeouts for an ultra-minimal EncButton instance. ```cpp // ── Ultra-minimal EncButton example ───────────────────────── #define EB_NO_FOR #define EB_NO_CALLBACK #define EB_NO_COUNTER #define EB_NO_BUFFER #define EB_DEB_TIME 50 #define EB_CLICK_TIME 500 #define EB_HOLD_TIME 600 #define EB_STEP_TIME 200 #define EB_FAST_TIME 30 #define EB_TOUT_TIME 1000 #include // EncButton with template pins: only 8 bytes SRAM total EncButtonT<2, 3, 4> eb; ``` -------------------------------- ### All Button Types Example Source: https://github.com/gyverlibs/encbutton/blob/main/README_EN.md Shows how to use different button types provided by the library: Button, Buttont, and Virtbutton. Ensure to call tick() for each button instance and check for clicks. ```cpp #include Button BTN (4); Buttont <5> btnt; Virtbutton BTNV; VOID setup () { Serial.Begin (115200); } VOID loop () { // Button btn.tick (); if (btn.click ()) serial.println ("btn click"); // Buttont btnt.tick (); if (btnt.click ()) serial.println ("BTNT CLICK"); // virtbutton btnv.tick (! DigitalRead (4));// transmit logical value if (btnv.click ()) serial.println ("btnv click"); } ``` -------------------------------- ### All Encoder Types Example Source: https://github.com/gyverlibs/encbutton/blob/main/README_EN.md Illustrates the usage of different encoder types: ENCODER, ENCODERT, and Virtencoder. Demonstrates how to read encoder ticks, turns, and direction. ```cpp #include ENCODER ENC (2, 3); ENCODERT <5, 6> ENCT; Virtencoder encv; VOID setup () { Serial.Begin (115200); } VOID loop () { // The survey is the same for everyone, 3 ways: // 1 // Tick will return 1 or -1, then this is a step if (enc.tick ()) serial.println (enc.counter); // 2 // can be interviewed through turn () enct.tick (); if (enct.turn ()) serial.println (enct.dir ()); // 3 // you can not use survey functions, but get the direction directly int8_t v = encv.tick (DigitalRead (2), DigitalRead (3)); if (v) serial.println (v);// Derive 1 or -1 } ``` -------------------------------- ### Initialize EncButton, Button, and Encoder Objects Source: https://github.com/gyverlibs/encbutton/blob/main/README_EN.md Demonstrates various ways to initialize EncButton, Button, and Encoder objects, including virtual and real instances, with different pin configurations and modes. Use these examples to set up your hardware interaction. ```CPP // virtual Virtencbutton eb;// Encoder with button Virtbutton b;// button Virtencoder e;// Encoder // Real // Encoder with button ENCBUTTON EB (ENC0, ENC1, BTN);// Encoder Pins and buttons ENCBUTTON EB (ENC0, ENC1, BTN, MODEENC);// + Pino Pino Encoder Pin (silence. Input) ENCBUTTON EB (ENC0, ENC1, BTN, MODEENC, MODEBTN);// + Pin mode buttons (silent. Input_pullup) ENCBUTTON EB (ENC0, ENC1, BTN, MODEENC, MODEBTN, BTNlevel);// + button level (silence) // template ENCBUTTON EB;// Encoder Pins and buttons ENCBUTTON EB (Modeenc);// + Pino Pino Encoder Pin (silence. Input) ENCBUTTON EB (Modeenc, Modebtn);// + Pin mode buttons (silent. Input_pullup) ENCBUTTON EB (Modeenc, Modebtn, Btnlevel);// + button level (silence) // button Button b (pin);// PIN Button b (PIN, Mode);// + Pin mode buttons (silent. Input_pullup) Button B (PIN, Mode, Btnlevel);// + button level (silence) // template Buttont b;// PIN Buttont b (mode);// + Pin mode buttons (silent. Input_pullup) Buttont b (mode, btnlevel);// + button level (silence) // Encoder ENCODER E (ENC0, ENC1);// Pines of Encoder ENCODER E (ENC0, ENC1, Mode);// + Pino Pino Encoder Pin (silence. Input) // template Encodert e;// Pines of Encoder Encodert E (Mode);// + Pino Pino Encoder Pin (silence. Input) ``` -------------------------------- ### Query Hold Durations with pressFor(), holdFor(), stepFor() Source: https://context7.com/gyverlibs/encbutton/llms.txt Use these functions to get elapsed milliseconds from different hold-phase start points. Disabled if EB_NO_FOR is defined. ```cpp #include Button btn(4); void loop() { btn.tick(); // pressFor() — ms since initial press if (btn.pressing()) { uint16_t t = btn.pressFor(); if (t > 3000) Serial.println("held more than 3 seconds"); } // holdFor() — ms since hold threshold was crossed if (btn.holding()) { if (btn.holdFor(1000)) Serial.println("held 1 s into hold phase"); } // stepFor() — ms since step phase began if (btn.step()) { // accelerate: larger increment after 2 seconds of step int delta = btn.stepFor(2000) ? 10 : 1; Serial.printf("delta = %d, steps so far = %d\n", delta, btn.getSteps()); } if (btn.release()) { Serial.printf("total press: %d ms, hold: %d ms, step: %d ms, steps: %d\n", btn.pressFor(), btn.holdFor(), btn.stepFor(), btn.getSteps()); } } ``` -------------------------------- ### Full EncButton Demo Source: https://github.com/gyverlibs/encbutton/blob/main/README_EN.md This example demonstrates the comprehensive functionality of the EncButton library, including configuration of timeouts, encoder behavior, and handling of various button and encoder events. It shows how to process general rotation, specific turns, button presses, clicks, holds, and step actions. Use this for a complete understanding of the library's capabilities. ```CPP // #define eb_no_for // Disable Pressfor/Holdfor/Stepfor support and Stepov counter (saves 2 bytes of RAM) // #define eb_no_callback // Disable the event processor Attach (saves 2 bytes of RAM) // #define eb_no_counter // Disable the enkoder counter (saves 4 bytes of RAM) // #define EB_NO_BUFFER // Disable the buffer of the encoder (saves 1 byte of RAM) // #define eb_deb_time 50 // Timesout to darebells button (button) // #define eb_click_time 500 // Clicks standstatics (button) // #define eb_hold_time 600 // Maintenance Times (button) // #define eb_step_time 200 // pulse retention rate (button) // #define EB_FAST_TIME 30 // Quick turn Timesout (Encoder) #include ENCBUTTON EB (2, 3, 4); // ENCBUTTON EB (2, 3, 4, Input);// + Pino Pino mode // ENCBUTTON EB (2, 3, 4, Input, Input_pullup);// + button pins mode // ENCBUTTON EB (2, 3, 4, Input, Input_pullup, Low);// + button level VOID setup () { Serial.Begin (115200); // shows the default values eb.setbtnlevel (Low); EB.SetClicktimeout (500); eb.Setdebtimeout (50); Eb.SetHoldtimeout (600); eb.setsteptimeout (200); eb.setencreverse (0); eb.setenctype (eb_step4_low); eb.setfasttimeout (30); // throw the Encoder counter eb.counter = 0; } VOID loop () { eb.tick (); // General rotation processing if.turn ()) { Serial.print ("Turn: Dir"); Serial.print (eb.dir ()); Serial.print (", fast"); Serial.print (eb.fast ()); Serial.print (", Hold"); Serial.print (eb.pressing ()); Serial.print (", Counter"); Serial.print (eb.counter); Serial.print (", clicks"); Serial.println (eb.getClicks ()); } // Turning rotation processing if.left ()) serial.println ("Left"); if.right ()) serial.println ("right"); if.left ()) serial.println ("Lefth"); if.righth ()) serial.println ("righth"); // button if.press ()) serial.println ("Press"); if.click ()) serial.println ("click"); if.release ()) { Serial.println ("Release"); Serial.print ("Clicks:"); Serial.print (eb.getClicks ()); Serial.print (", stps:"); Serial.print (eb.getsteps ()); Serial.print (", Press for:"); Serial.print (eb.pressfor ()); Serial.print (", Hold for:"); Serial.print (eb.holdfor ()); Serial.print (", step for:"); Serial.println (eb.stepfor ()); } // States // serial.println (eb.pressing ()); // serial.println (eb.holding ()); // serial.println (eb.busy ()); // serial.println (eb.waiting ()); // Timesout if (eb.timeout ()) serial.println ("Timeout!"); // Holding if.hold ()) serial.println ("Hold"); if.hold (3)) serial.println ("Hold 3"); // Impulse retention if.step ()) serial.println ("step"); if.step (3)) serial.println ("STEP 3"); // released after impulse deduction if (eb.releastep ()) serial.println ("Release Step"); if (eb.releastep (3)) serial.println ("Release Step 3"); // released after holding if.releasehold ()) serial.println ("Release Hold"); if (eb.releasehold (2)) serial.println ("Release Hold 2"); // Check for the number of clicks if.hasclicks (3)) Serial.println ("Has 3 Clicks"); // Bring the number of clicks if.hasclicks ()) { Serial.print ("Has Clicks:"); Serial.println (eb.getClicks ()); } } ``` -------------------------------- ### Custom Pin Reading and Time Functions Source: https://github.com/gyverlibs/encbutton/blob/main/README_EN.md Provides examples of implementing custom functions for reading pin states, setting pin modes, and getting uptime. These custom functions replace the library's defaults and should be defined in your project's .cpp or .h files. ```cpp #include Bool eb_read (uint8_t pin) { Return DigitalRead (PIN); } VOID eb_mode (uint8_t pin, uint8_t mode) { Pinmode (PIN, Mode); } uint32_t eb_uptime () { Return Millis (); } ``` -------------------------------- ### Array Initialization for Buttons and Encoders Source: https://github.com/gyverlibs/encbutton/blob/main/README_EN.md Shows how to create and initialize arrays of non-virtual button and encoder objects. Pin numbers are assigned individually using the `init()` method within the setup function. ```cpp Button btns [5]; ENCBUTTON EBS [3]; VOID setup () { btns [0] .init (2);// Indicate the pin btns [1] .init (5); btns [2] .init (10); // ... EBS [0] .init (11, 12, 13, Input); EBS [1] .init (14, 15, 16); // ... } VOID loop () { for (int i = 0; i <5; i ++) btns [i] .Tick (); for (int i = 0; i <3; i ++) EBS [i] .Tick (); if (btns [2] .Click ()) serial.println ("BTN2 click"); // ... } ``` -------------------------------- ### Connecting EncButton Event Handler Source: https://github.com/gyverlibs/encbutton/blob/main/README_EN.md This example shows how to connect a callback function to the EncButton library to handle various events. The callback function is triggered by actions such as presses, holds, steps, releases, clicks, and turns. It demonstrates how to differentiate between different event types and retrieve associated data like click counts. ```CPP #include ENCBUTTON EB (2, 3, 4); VOID callback () { Serial.print ("callback:"); switch (eb.action ()) { Case eb_press: Serial.println ("Press"); Break; Case eb_hold:serial.println ("Hold"); Break; Case eb_step: Serial.println ("STEP"); Break; Case eb_release: Serial.println ("Release"); Break; Case eb_click: Serial.println ("click"); Break; Case eb_clicks: Serial.print ("Clicks"); Serial.println (eb.getClicks ()); Break; Case eb_turn: Serial.print ("turn"); Serial.print (eb.dir ()); Serial.print (" "); Serial.print (eb.fast ()); Serial.print (" "); Serial.println (eb.pressing ()); Break; Case eb_rel_hold: Serial.println ("Release Hold"); Break; CASE EB_REL_HOLD_C: Serial.print ("Release Hold Clicks"); Serial.println (eb.getClicks ()); Break; CASE EB_REL_STEP: Serial.println ("Release Step"); Break; CASE EB_REL_STEP_C: Serial.print ("Release Step Clicks"); Serial.println (eb.getClicks ()); ``` -------------------------------- ### Interrupt Support Source: https://context7.com/gyverlibs/encbutton/llms.txt Explains how to use hardware interrupts with the EncButton library to prevent data loss during long `loop()` cycles. Covers `tickISR()`, `pressISR()`, `setEncISR()`, and provides examples for both `EncButton` and `VirtEncoder` with interrupt handling. ```APIDOC ## Interrupt Support ### Description For programs with long `loop()` cycles, connect encoder pins to hardware interrupts so no rotation steps are lost. Up to 5 encoder steps are buffered between `tick()` calls (disable with `EB_NO_BUFFER`). Button press can also be signalled from an ISR. ### EncButton with ISR ```cpp #include EncButton eb(2, 3, 4); // encA=D2(int0), encB=D3(int1), btn=D4 // For ESP8266/ESP32 add IRAM_ATTR: // IRAM_ATTR void encISR() { eb.tickISR(); } // IRAM_ATTR void btnISR() { eb.pressISR(); } void encISR() { eb.tickISR(); } // called on encoder pin CHANGE void btnISR() { eb.pressISR(); } // called on button press FALLING void setup() { Serial.begin(115200); eb.setEncISR(true); // tell library encoder is handled in ISR attachInterrupt(digitalPinToInterrupt(2), encISR, CHANGE); attachInterrupt(digitalPinToInterrupt(3), encISR, CHANGE); attachInterrupt(digitalPinToInterrupt(4), btnISR, FALLING); } void loop() { eb.tick(); // events are generated here, safe even with long delays if (eb.turn()) Serial.printf("turn dir=%+d ctr=%ld\n", eb.dir(), eb.counter); if (eb.click()) Serial.println("click"); delay(100); // simulate busy main loop — encoder steps still buffered } ``` ### VirtEncoder with ISR Standalone virtual encoder with ISR (for pin-expander scenarios): ```cpp VirtEncoder ve; void isr() { ve.tickISR(digitalRead(2), digitalRead(3)); } void setup() { attachInterrupt(0, isr, CHANGE); attachInterrupt(1, isr, CHANGE); ve.setEncISR(true); } void loop() { ve.tick(); // no pin arguments needed — ISR handled it if (ve.turn()) Serial.println(ve.dir()); } ``` ``` -------------------------------- ### Encoder variable manipulation Source: https://github.com/gyverlibs/encbutton/blob/main/README_EN.md Examples demonstrating how to change variable values based on encoder turns, including direction, speed, and press state. Adjustments can be made in steps of 1, 5, or 10. ```cpp // Change the values of the variables // Turn of the encoder if (enc.turn ()) { // Change in step 5 var += 5 * enc.dir (); // Change in step 1 with a normal turn, 10 with fast Var += ENC.FAST ()?10: 1; // Change in step 1 with a normal turn, 10 with pressed vAR += ENC.Pressing ()?10: 1; // Change one variable when turning, the other - with a pressed turn if (enc.pressing ()) Var0 ++; Else Var1 ++; // If the button is pressed - preliminary clicks are available // Choose a variable for changes in the premises.Clicks if (enc.pressing ()) { Switch (enc.getClicks ()) { CASE 1: VAR0 += ENC.DIR (); Break; CASE 2: VAR1 += ENC.DIR (); Break; CASE 3: VAR2 += ENC.DIR (); Break; } } } ``` -------------------------------- ### Button Processing with Interrupts Source: https://github.com/gyverlibs/encbutton/blob/main/README_EN.md Example for handling button presses using interrupts. Configure the interrupt mode ('Falling' or 'Rising') based on the button's physical connection and level. The `Pressisr()` function should be called within the ISR. ```cpp Button b (2); /* // ESP8266/ESP32 IRAM_ATTR VOID ISR () { B.Pressisr (); } */ VOID isr () { B.Pressisr (); } VOID setup () { Attachinterrupt (0, ISR, Falling); } VOID loop () { B.tick (); } ``` -------------------------------- ### Encoder Processing with Interrupts (Atmega328) Source: https://github.com/gyverlibs/encbutton/blob/main/README_EN.md Example of setting up an encoder with hardware interrupts for precise tracking. Ensure interrupts are configured for 'Change' mode and `setencisr(true)` is called. The main loop must call `tick()` for event generation. ```cpp // Example for Atmega328 and Encbutton ENCBUTTON EB (2, 3, 4); /* // ESP8266/ESP32 IRAM_ATTR VOID ISR () { eb.tickisr (); } */ VOID isr () { eb.tickisr (); } VOID setup () { Attachinterrupt (0, Isr, Change); Attachinterrupt (1, ISR, Change); eb.setencisr (true); } VOID loop () { eb.tick (); } ``` -------------------------------- ### Initialization Source: https://github.com/gyverlibs/encbutton/blob/main/README_EN.md Demonstrates various ways to initialize the EncButton, Button, and Encoder objects, including template-based initialization. ```APIDOC ## Initialization ### Virtual Objects ```cpp Virtencbutton eb; // Encoder with button Virtbutton b; // button Virtencoder e; // Encoder ``` ### Real Objects (ENCBUTTON) ```cpp // Basic initialization ENCBUTTON EB (ENC0, ENC1, BTN); // With Modeenc parameter ENCBUTTON EB (ENC0, ENC1, BTN, MODEENC); // With Modeenc and Modebtn parameters ENCBUTTON EB (ENC0, ENC1, BTN, MODEENC, MODEBTN); // With all parameters including Btnlevel ENCBUTTON EB (ENC0, ENC1, BTN, MODEENC, MODEBTN, BTNlevel); ``` ### Template Initialization (ENCBUTTON) ```cpp // Basic template initialization ENCBUTTON EB; // With Modeenc parameter ENCBUTTON EB (Modeenc); // With Modeenc and Modebtn parameters ENCBUTTON EB (Modeenc, Modebtn); // With all parameters including Btnlevel ENCBUTTON EB (Modeenc, Modebtn, Btnlevel); ``` ### Button Initialization ```cpp // Basic initialization Button b (pin); // With Mode parameter Button b (PIN, Mode); // With Mode and Btnlevel parameters Button B (PIN, Mode, Btnlevel); ``` ### Template Initialization (Button) ```cpp // Basic template initialization Buttont b; // With Mode parameter Buttont b (mode); // With Mode and Btnlevel parameters Buttont b (mode, btnlevel); ``` ### Encoder Initialization ```cpp // Basic initialization ENCODER E (ENC0, ENC1); // With Mode parameter ENCODER E (ENC0, ENC1, Mode); ``` ### Template Initialization (Encoder) ```cpp // Basic template initialization Encodert e; // With Mode parameter Encodert E (Mode); ``` ``` -------------------------------- ### Event Action Code Dispatch with action() and getAction() Source: https://context7.com/gyverlibs/encbutton/llms.txt Use action() or getAction() to get a numeric event code for switch-based dispatch. getAction() provides IDE autocompletion via EBAction enum. ```cpp #include EncButton eb(2, 3, 4); // encA=2, encB=3, btn=4 void loop() { if (eb.tick()) { // tick() returns true when any event fires switch (eb.action()) { case EB_PRESS: Serial.println("press"); break; case EB_HOLD: Serial.println("hold"); break; case EB_STEP: Serial.println("step"); break; case EB_RELEASE: Serial.println("release"); break; case EB_CLICK: Serial.println("click"); break; case EB_CLICKS: Serial.printf("multi-click: %d\n", eb.getClicks()); break; case EB_TURN: Serial.printf("turn dir=%d fast=%d pressed=%d ctr=%ld\n", eb.dir(), eb.fast(), eb.pressing(), eb.counter); break; case EB_REL_HOLD: Serial.println("released after hold"); break; case EB_REL_HOLD_C: Serial.printf("released after hold, %d pre-clicks\n", eb.getClicks()); break; case EB_REL_STEP: Serial.println("released after step"); break; case EB_REL_STEP_C: Serial.printf("released after step, %d pre-clicks\n", eb.getClicks()); break; case EB_TIMEOUT: Serial.println("inactivity timeout"); break; } } } ``` -------------------------------- ### Initialize and Use Button Input Source: https://context7.com/gyverlibs/encbutton/llms.txt Demonstrates basic initialization of a Button object with a runtime pin and its usage within the loop for detecting presses, releases, clicks, holds, and steps. Ensure `btn.tick()` is called once per loop. ```cpp #include // Runtime pin (pin 4, INPUT_PULLUP, active LOW — defaults) Button btn(4); // Template pin (saves 1 byte SRAM, slightly faster on AVR) ButtonT<5> btnt; void setup() { Serial.begin(115200); // Optional: change pin mode or active level // btn.init(4, INPUT_PULLUP, LOW); // Check if button is physically held at boot (no debounce) if (btn.read()) Serial.println("Button held at startup"); // Timeout before saving settings after last interaction btn.setTimeout(2000); } void loop() { btn.tick(); // must be called once per loop if (btn.press()) Serial.println("pressed"); if (btn.release()) Serial.println("released"); if (btn.click()) Serial.println("single click"); // Multi-click detection — fires after click-timeout expires if (btn.hasClicks(2)) Serial.println("double click"); if (btn.hasClicks(3)) Serial.println("triple click"); if (btn.hasClicks()) Serial.printf("clicks: %d\n", btn.getClicks()); // Hold detection if (btn.hold()) Serial.println("hold event (fired once)"); if (btn.holding()) Serial.println("currently held"); // Impulse step — fires repeatedly while held, period = stepTimeout if (btn.step()) Serial.println("step tick"); // Release-after events if (btn.releaseHold()) Serial.println("released after hold"); if (btn.releaseStep()) Serial.println("released after step"); if (btn.releaseHoldStep()) Serial.println("released after hold or step"); // State queries if (btn.pressing()) Serial.println("button is down right now"); if (btn.waiting()) Serial.println("waiting for more clicks"); if (btn.busy()) Serial.println("processing in progress"); // Inactivity timeout — fires once after setTimeout ms of no interaction if (btn.timeout()) Serial.println("no activity for 2000 ms"); } ``` -------------------------------- ### Button Class and Basic Usage Source: https://context7.com/gyverlibs/encbutton/llms.txt Demonstrates the basic usage of the Button class for runtime pin configuration and event handling like press, release, click, hold, and step. ```APIDOC ## Button — `Button` / `ButtonT` `Button` is the standard pin-bound button class. It reads the configured GPIO pin each `tick()` call and applies full button processing including debounce, click counting, hold detection, and impulse step repeat. `ButtonT` is the template variant where the pin is a compile-time constant, saving 1 byte SRAM and slightly speeding up pin reads on AVR. ```cpp #include // Runtime pin (pin 4, INPUT_PULLUP, active LOW — defaults) Button btn(4); // Template pin (saves 1 byte SRAM, slightly faster on AVR) ButtonT<5> btnt; void setup() { Serial.begin(115200); // Optional: change pin mode or active level // btn.init(4, INPUT_PULLUP, LOW); // Check if button is physically held at boot (no debounce) if (btn.read()) Serial.println("Button held at startup"); // Timeout before saving settings after last interaction btn.setTimeout(2000); } void loop() { btn.tick(); // must be called once per loop if (btn.press()) Serial.println("pressed"); if (btn.release()) Serial.println("released"); if (btn.click()) Serial.println("single click"); // Multi-click detection — fires after click-timeout expires if (btn.hasClicks(2)) Serial.println("double click"); if (btn.hasClicks(3)) Serial.println("triple click"); if (btn.hasClicks()) Serial.printf("clicks: %d\n", btn.getClicks()); // Hold detection if (btn.hold()) Serial.println("hold event (fired once)"); if (btn.holding()) Serial.println("currently held"); // Impulse step — fires repeatedly while held, period = stepTimeout if (btn.step()) Serial.println("step tick"); // Release-after events if (btn.releaseHold()) Serial.println("released after hold"); if (btn.releaseStep()) Serial.println("released after step"); if (btn.releaseHoldStep()) Serial.println("released after hold or step"); // State queries if (btn.pressing()) Serial.println("button is down right now"); if (btn.waiting()) Serial.println("waiting for more clicks"); if (btn.busy()) Serial.println("processing in progress"); // Inactivity timeout — fires once after setTimeout ms of no interaction if (btn.timeout()) Serial.println("no activity for 2000 ms"); } ``` ``` -------------------------------- ### Configure Button Timing and Active Level Source: https://context7.com/gyverlibs/encbutton/llms.txt Shows how to configure various timing parameters like debounce, click wait, hold, step, and inactivity timeouts, as well as the active signal level for a Button instance. These settings can be changed dynamically. ```cpp #include Button btn(4); void setup() { Serial.begin(115200); // Active level: LOW = button connects to GND (default for Button/EncButton) // HIGH = button connects to VCC (default for VirtButton) btn.setBtnLevel(LOW); // Debounce: ignore signal transitions shorter than this (default 50 ms, max 255 ms) btn.setDebTimeout(30); // Click wait: max gap between clicks when counting multi-clicks (default 500 ms, max 4000 ms) btn.setClickTimeout(400); // Hold: how long to hold before hold() fires (default 600 ms, max 4000 ms) btn.setHoldTimeout(800); // Step period: interval between step() impulses while held (default 200 ms, max 4000 ms) btn.setStepTimeout(150); // Inactivity timeout for timeout() (default 1000 ms, max 4000 ms) btn.setTimeout(3000); } void loop() { btn.tick(); if (btn.click()) Serial.println("click"); if (btn.hold()) Serial.println("held for 800 ms"); if (btn.step()) Serial.println("step every 150 ms"); if (btn.timeout()) Serial.println("3 seconds of silence"); } ``` -------------------------------- ### Include EncButton Library Source: https://context7.com/gyverlibs/encbutton/llms.txt Include the main header file to access all classes provided by the EncButton library. ```cpp #include ``` -------------------------------- ### Handle Preliminary Clicks with EncButton Source: https://github.com/gyverlibs/encbutton/blob/main/README_EN.md Process button clicks with preliminary click counts for advanced functionality. Option 1 manually checks click count, while Option 2 lets the library handle it if the count is known. ```CPP // 1 if (btn.hold ()) { if (btn.getclics () == 2) serial.println ("Hold 2 Clicks"); } // 2 if (btn.hold (2)) serial.println ("Hold 2 Clicks"); ``` -------------------------------- ### EncButton Class Methods Source: https://github.com/gyverlibs/encbutton/blob/main/README_EN.md Methods for detecting button presses, holds, steps, and clicks. ```APIDOC ## Pressing ### Description Checks if the button is currently being pressed. ### Method Bool ### Parameters - **Clicks** (Uint8_T) - Optional - The number of clicks to check for. ### Endpoint Pressing() Pressing(Uint8_T Clicks) ``` ```APIDOC ## Hold ### Description Checks if the button has been held for a specified duration or number of clicks. ### Method Bool ### Parameters - **Clicks** (Uint8_T) - Optional - The number of clicks to check for. ### Endpoint Hold() Hold(Uint8_T Clicks) ``` ```APIDOC ## Holding ### Description Checks if the button is continuously being held. ### Method Bool ### Parameters - **Clicks** (Uint8_T) - Optional - The number of clicks to check for. ### Endpoint Holding() Holding(Uint8_T Clicks) ``` ```APIDOC ## Step ### Description Detects a single step or a series of steps from the button. ### Method Bool ### Parameters - **Clicks** (Uint8_T) - Optional - The number of clicks to check for. ### Endpoint Step() Step(Uint8_T Clicks) ``` ```APIDOC ## HasClicks ### Description Checks if a specific number of clicks have been registered. ### Method Bool ### Parameters - **Clicks** (Uint8_T) - Optional - The number of clicks to check for. ### Endpoint HasClicks() HasClicks(Uint8_T Clicks) ``` ```APIDOC ## ReleaseHold ### Description Detects when the button is released after being held. ### Method Bool ### Parameters - **Clicks** (Uint8_T) - Optional - The number of clicks to check for. ### Endpoint ReleaseHold() ReleaseHold(Uint8_T Clicks) ``` ```APIDOC ## ReleaseStep ### Description Detects when the button is released after a step action. ### Method Bool ### Parameters - **Clicks** (Uint8_T) - Optional - The number of clicks to check for. ### Endpoint ReleaseStep() ReleaseStep(Uint8_T Clicks) ``` ```APIDOC ## getclicks ### Description Retrieves the total number of clicks registered. ### Method uint8_t ### Endpoint getclicks() ``` ```APIDOC ## getsteps ### Description Retrieves the total number of steps registered. ### Method uint16_t ### Endpoint getsteps() ``` ```APIDOC ## Waiting ### Description Checks if the system is waiting for repeated clicks. ### Method Bool ### Endpoint Waiting() ``` ```APIDOC ## Busy ### Description Checks if the button processing is currently active. ### Method Bool ### Endpoint Busy() ``` ```APIDOC ## Action ### Description Returns the event code associated with the button action. ### Method Uint16_T ### Endpoint Action() ``` ```APIDOC ## Timeout ### Description Checks if a specified timeout has elapsed since the last interaction. ### Method Bool ### Parameters - **MS** (Uint16_T) - The timeout duration in milliseconds. ### Endpoint Timeout(Uint16_T MS) ``` ```APIDOC ## Pressfor ### Description Returns the duration the button has been pressed. ### Method uint16_t ### Endpoint Pressfor() ``` ```APIDOC ## Pressfor (condition) ### Description Checks if the button has been held for longer than a specified duration. ### Method Bool ### Parameters - **MS** (Uint16_T) - The duration in milliseconds to check against. ### Endpoint Pressfor(Uint16_T MS) ``` ```APIDOC ## holdfor ### Description Returns the duration the button has been held since the beginning of retention. ### Method uint16_t ### Endpoint holdfor() ``` ```APIDOC ## Holdfor ### Description Checks if the button has been held for longer than a specified duration since the beginning of retention. ### Method Bool ### Parameters - **MS** (Uint16_T) - The duration in milliseconds to check against. ### Endpoint Holdfor(Uint16_T MS) ``` ```APIDOC ## stepfor ### Description Returns the duration the button has been held since the beginning of the step. ### Method uint16_t ### Endpoint stepfor() ``` ```APIDOC ## StepFor ### Description Checks if the button has been held for longer than a specified duration since the beginning of the step. ### Method Bool ### Parameters - **MS** (Uint16_T) - The duration in milliseconds to check against. ### Endpoint StepFor(Uint16_T MS) ``` -------------------------------- ### EncButton Class Methods Source: https://github.com/gyverlibs/encbutton/blob/main/README_EN.md Provides methods for detecting various button interactions like presses, holds, steps, and clicks. Use these to determine the state and events associated with a physical button. ```cpp Bool Pressing (); Bool Pressing (Uint8_T Clicks); // The button was withheld (more timeout) [event] Bool Hold (); Bool Hold (Uint8_T Clicks); // The button is held (more timeout) [condition] Bool Holding (); Bool Holding (Uint8_T Clicks); // Impulse retention [event] Bool Step (); Bool Step (Uint8_T Clicks); // Several clicks were recorded [event] Bool HasClicks (); Bool HasClicks (Uint8_T Clicks); // button released after holding [Event] Bool ReleaseHold (); Bool ReleaseHold (Uint8_T Clicks); // Button is released after impulse retention [Event] Bool ReleaseStep (); Bool ReleaseStep (Uint8_T Clicks); // get the number of clicks uint8_t getclicks (); // Get the number of steps uint16_t getsteps (); // button awaits repeated clicks (between Click () and HasClicks ()) [condition] Bool Waiting (); // processing (between the first press and after waiting for clicks) [condition] Bool Busy (); // there was an action from the button, the event code [event] will return Uint16_T Action (); ``` ```cpp // After interacting with the button (or enkoder Encbutton), the specified time has passed, ms [event] Bool Timeout (Uint16_T MS); // The time that the button is held (from the beginning of the press), ms uint16_t Pressfor (); // The button is held longer than (from the beginning of pressing), ms [condition] Bool Pressfor (Uint16_T MS); // The time that the button is held (from the beginning of retention), ms uint16_t holdfor (); // The button is held longer than (from the beginning of retention), ms [condition] Bool Holdfor (Uint16_T MS); // The time that the button is held (from the beginning of the step), ms uint16_t stepfor (); // The button is held longer than (from the beginning of the step), ms [condition] Bool StepFor (Uint16_T MS); ``` -------------------------------- ### Button step and release interactions Source: https://github.com/gyverlibs/encbutton/blob/main/README_EN.md Code snippets for handling button presses, including simple increments on step, changing direction on release, and incremental changes based on step duration or click count. ```cpp // Impulse retention at every step is increasing the variable if (btn.step ()) var ++; // Change the direction of change in the variable after letting go from STEP if (btn.step ()) var += dir; if (btn.releastep ()) die = -dir; // Change the selected variable using STEP if (btn.step (1)) Var1 ++;// Click-holding if (btn.step (2)) Var2 ++;// Click-Click-holding if (btn.step (3)) var3 ++;// Click-Click-Click-hold // if you keep the STEP for more than 2 seconds - an incremental +5, so far less - +1 if (btn.step ()) { if (btn.stepfor (2000)) var += 5; Else Var += 1; } // inclusion of the mode by the number of clicks if (btn.hasclicks ()) mode = btn.getclicks (); // inclusion of the mode by several clicks and retention if (btn.hold (1)) mode = 1;// Click-holding if (btn.hold (2)) mode = 2;// Click-Click-holding if (btn.hold (3)) mode = 3;// Click-Click-Click-hold // or so if (btn.hold ()) mode = btn.getclicks (); // Button is released, look how much it was held if (btn.release ()) { // from 1 to 2 seconds if (btn.pressfor ()> 1000 && btn.pressfor () <= 2000) mod = 1; // from 2 to 3 seconds Else if (BTN.PressFor ()> 2000 && BTN.PressFor () <= 3000) Mode = 2; } ``` -------------------------------- ### Button Configuration Source: https://context7.com/gyverlibs/encbutton/llms.txt Explains how to configure button behavior using methods like `setXxxTimeout()` and `setBtnLevel()` to adjust debounce, click, hold, step, and inactivity timeouts, as well as the active signal level. ```APIDOC ## Button Configuration — `setXxxTimeout()` / `setBtnLevel()` Runtime configuration of all timing parameters and active signal level. All timeouts are per-instance and can be changed at any time. ```cpp #include Button btn(4); void setup() { Serial.begin(115200); // Active level: LOW = button connects to GND (default for Button/EncButton) // HIGH = button connects to VCC (default for VirtButton) btn.setBtnLevel(LOW); // Debounce: ignore signal transitions shorter than this (default 50 ms, max 255 ms) btn.setDebTimeout(30); // Click wait: max gap between clicks when counting multi-clicks (default 500 ms, max 4000 ms) btn.setClickTimeout(400); // Hold: how long to hold before hold() fires (default 600 ms, max 4000 ms) btn.setHoldTimeout(800); // Step period: interval between step() impulses while held (default 200 ms, max 4000 ms) btn.setStepTimeout(150); // Inactivity timeout for timeout() (default 1000 ms, max 4000 ms) btn.setTimeout(3000); } void loop() { btn.tick(); if (btn.click()) Serial.println("click"); if (btn.hold()) Serial.println("held for 800 ms"); if (btn.step()) Serial.println("step every 150 ms"); if (btn.timeout()) Serial.println("3 seconds of silence"); } ``` ``` -------------------------------- ### Custom I/O Functions for Platform Integration Source: https://context7.com/gyverlibs/encbutton/llms.txt Override EB_read(), EB_mode(), and EB_uptime() to integrate EncButton with custom hardware or RTOS environments. Define these functions before the library uses them. ```cpp #include // Custom pin read — must return true when pin is logically HIGH bool EB_read(uint8_t pin) { return digitalRead(pin); // or: return myExpander.readPin(pin); } // Custom pin mode setter void EB_mode(uint8_t pin, uint8_t mode) { pinMode(pin, mode); // or: myExpander.setMode(pin, mode); } // Custom time source — must return milliseconds since boot uint32_t EB_uptime() { return millis(); // or: return xTaskGetTickCount() * portTICK_PERIOD_MS; } EncButton eb(2, 3, 4); void setup() { Serial.begin(115200); } void loop() { eb.tick(); if (eb.click()) Serial.println("click via custom I/O"); } ``` -------------------------------- ### Buttont Class Functions Source: https://github.com/gyverlibs/encbutton/blob/main/README_EN.md Outlines the core functions for the Buttont class, including initialization, reading, and ticking. ```CPP // specify the operating mode VOID Init (Uint8_t Mode); // Read the current value of the button (without debate) taking into account Setbtnlevel Bool Read (); // processing function, call in loop Bool Tick (); ``` -------------------------------- ### Encbuttont Class Functions Source: https://github.com/gyverlibs/encbutton/blob/main/README_EN.md Outlines the core functions for the Encbuttont class, including initialization, interrupt-based encoder ticking, loop-based ticking, and button reading. ```CPP // Configure Pino operation mode, button level VOID Init (Uint8_t Modeenc = Input, Uint8_t Modebtn = Input_pullup, Uint8_t Btnlevel = Low); // Function of processing for calling in an interruption of encoder int8_t tickisr (); // processing function, call in loop Bool Tick (); // Read the button value Bool Readbtn (); ``` -------------------------------- ### Logic of Work Source: https://github.com/gyverlibs/encbutton/blob/main/README_EN.md Explains the event handling logic in V3, emphasizing the importance of calling `Tick()` once per cycle. ```APIDOC ## Logic of Work In V3, event survey functions (Click, Turn, etc.) are not discarded immediately after being called. They are discarded at the next call to `Tick()`, thus retaining their meaning across all subsequent challenges within the current iteration of the main program loop. **Therefore, `Tick()` must be called only once per program cycle to avoid missing actions.** Read more about this above. ``` -------------------------------- ### Encbutton Class Constructors Source: https://github.com/gyverlibs/encbutton/blob/main/README_EN.md Defines the constructors for the Encbutton class, combining encoder and button functionality. ```CPP ENCBUTTON; // Set the Pins (ENK, ENK, button) ENCBUTTON (UINT8_T ENCA, UINT8_T ENCB, UINT8_T BTN); // Reference Pins (ENK, ENK, button, Pinmode ENK, Pinmode button, button level) ENCBUTTON (UINT8_T ENCA, UINT8_T ENCB, UINT8_T BTN, UINT8_T MODEENC = Input, Uint8_t Modebtn = Input_Pullup, Uint8_T BTNLEVEL = LOW); ``` -------------------------------- ### Encbutton Class Functions Source: https://github.com/gyverlibs/encbutton/blob/main/README_EN.md Outlines the core functions for the Encbutton class, including initialization, interrupt-based encoder ticking, loop-based ticking, and button reading. ```CPP // Reference Pins (ENK, ENK, button, Pinmode ENK, Pinmode button, button level) VOID Init (Uint8_t Enca, Uint8_t Encb, Uint8_t BTN, UINT8_T MODEENC = Input, Uint8_t Modebtn = Input_Pullup, Uint8_T BTNLEVEL = LOW); // Function of processing for calling in an interruption of encoder int8_t tickisr (); // processing function, call in loop Bool Tick (); // Read the value of the button taking into account Setbtnlevel Bool Readbtn (); ``` -------------------------------- ### Compile-time Settings for EncButton Source: https://github.com/gyverlibs/encbutton/blob/main/README_EN.md Define these preprocessor macros before including the library to disable specific features and reduce RAM usage. These settings affect all instances of buttons and encoders in the program. ```cpp #define eb_no_for // Disable the event processor attach (saves 2 bytes of RAM) #define eb_no_callback // Disable the encoder counter [Virtencoder, Encoder, Encbutton] (saves 4 bytes of RAM) #define eb_no_counter // Disconnect the buffer of the encoder (saves 2 bytes of RAM) #define eb_no_buffer /* Setting up timeouts for all classes - replaces the timauts constants, changeCranberries from the program (setxxxtimeout ()) will not be - Setting affects all buttons announced in the program/Encoders - saves 1 bytes of RAM for an object for each timeout - shows the default values in MS - values are not limited to 4000MS, as when installing from the program (SetXXXTimeout ()) */ #define eb_deb_time 50 // Timesout to extinguish the trim button (button) #define eb_click_time 500 // Click Stayout (button) #define eb_hold_time 600 // Maintenance Times (button) #define eb_step_time 200 // Impulse retention Timesout (button) #define EB_FAST_TIME 30 // TIMAUT RAM (ENCODER) ```