acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

ClickVersusDoubleClickUsingReleased.ino (2636B)

      1 /*
      2  * A demo that distinguishes a "Clicked" from a DoubleClicked by using a
      3  * Released event instead of a Clicked. Released turns on the LED. A DoubleClick
      4  * turns off the LED.
      5  *
      6  * Normally, AceButton cannot separate a Clicked from a DoubleClicked because
      7  * the Clicked event will always trigger if a DoubleClicked occurs. We cannot
      8  * suppress the first Clicked because it has already occurred by the time the
      9  * DoubleClicked occurs, and the first Clicked cannot predict the future.
     10  *
     11  * This version uses a Released event instead of a Clicked to turn the LED on,
     12  * while suppressing the Released after a DoubleClicked, and we ignore the
     13  * Clicked event that we can't suppress. The disadvantage of this version is
     14  * that if a user accidentally makes a normal Clicked event (a rapid
     15  * Pressed/Released), nothing happens to the LED. Depending on the application,
     16  * this may or may not be the desirable result.
     17  *
     18  * See Also:
     19  *    examples/ClickVersusDoubleClickUsingSuppression/
     20  *      - uses the kFeatureSuppressClickBeforeDoubleClick
     21  */
     22 
     23 #include <AceButton.h>
     24 using namespace ace_button;
     25 
     26 // The pin number attached to the button.
     27 const int BUTTON_PIN = 2;
     28 
     29 #ifdef ESP32
     30   // Different ESP32 boards use different pins
     31   const int LED_PIN = 2;
     32 #else
     33   const int LED_PIN = LED_BUILTIN;
     34 #endif
     35 
     36 // LED states. Some microcontrollers wire their built-in LED the reverse.
     37 const int LED_ON = HIGH;
     38 const int LED_OFF = LOW;
     39 
     40 // One button wired to the pin at BUTTON_PIN. Automatically uses the default
     41 // ButtonConfig. The alternative is to call the AceButton::init() method in
     42 // setup() below.
     43 AceButton button(BUTTON_PIN);
     44 
     45 void handleEvent(AceButton*, uint8_t, uint8_t);
     46 
     47 void setup() {
     48   // initialize built-in LED as an output
     49   pinMode(LED_PIN, OUTPUT);
     50 
     51   // Button uses the built-in pull up register.
     52   pinMode(BUTTON_PIN, INPUT_PULLUP);
     53 
     54   ButtonConfig* buttonConfig = button.getButtonConfig();
     55   buttonConfig->setEventHandler(handleEvent);
     56   buttonConfig->setFeature(ButtonConfig::kFeatureDoubleClick);
     57   buttonConfig->setFeature(ButtonConfig::kFeatureSuppressAfterClick);
     58   buttonConfig->setFeature(ButtonConfig::kFeatureSuppressAfterDoubleClick);
     59 }
     60 
     61 void loop() {
     62   // Should be called every 20ms or faster for the default debouncing time
     63   // of ~50ms.
     64   button.check();
     65 }
     66 
     67 // The event handler for the button.
     68 void handleEvent(AceButton* /* button */, uint8_t eventType,
     69     uint8_t /* buttonState */) {
     70   switch (eventType) {
     71     case AceButton::kEventReleased:
     72       digitalWrite(LED_PIN, LED_ON);
     73       break;
     74     case AceButton::kEventDoubleClicked:
     75       digitalWrite(LED_PIN, LED_OFF);
     76       break;
     77   }
     78 }