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

ClickVersusDoubleClickUsingSuppression.ino (2532B)

      1 /*
      2  * A demo that uses kFeatureSuppressClickBeforeDoubleClick to distinguish a
      3  * Clicked event from a DoubleClicked event. Click turns on the LED. A
      4  * DoubleClick turns off the LED.
      5  *
      6  * The only way to suppress the Clicked "after" a DoubleClicked is to postpone
      7  * the sending of the Clicked event until getDoubleClickDelay() time after the
      8  * Clicked. At that time, we can tell if a DoubleClicked has occurred or not.
      9  * But this means that every Clicked event is delayed by (kClickDelay +
     10  * kDoubleClickDelay + 2 * kDebounceDelay) which is 700 ms using the default
     11  * values, and you'll notice this delay in the LED turning on.
     12  *
     13  * The other side-effect is that if a user doesn't input a clean Click (which
     14  * results in a normal Press/Release sequence), then nothing happens to the LED.
     15  * Depending on the application, this may or may not be the desirable result.
     16 
     17  * See Also:
     18  *    examples/ClickVersusDoubleClickUsingReleased/
     19  *      - uses the Released event instead of the Clicked event
     20  */
     21 
     22 #include <AceButton.h>
     23 using namespace ace_button;
     24 
     25 // The pin number attached to the button.
     26 const int BUTTON_PIN = 2;
     27 
     28 #ifdef ESP32
     29   // Different ESP32 boards use different pins
     30   const int LED_PIN = 2;
     31 #else
     32   const int LED_PIN = LED_BUILTIN;
     33 #endif
     34 
     35 // LED states. Some microcontrollers wire their built-in LED the reverse.
     36 const int LED_ON = HIGH;
     37 const int LED_OFF = LOW;
     38 
     39 // One button wired to the pin at BUTTON_PIN. Automatically uses the default
     40 // ButtonConfig. The alternative is to call the AceButton::init() method in
     41 // setup() below.
     42 AceButton button(BUTTON_PIN);
     43 
     44 void handleEvent(AceButton*, uint8_t, uint8_t);
     45 
     46 void setup() {
     47   // initialize built-in LED as an output
     48   pinMode(LED_PIN, OUTPUT);
     49 
     50   // Button uses the built-in pull up register.
     51   pinMode(BUTTON_PIN, INPUT_PULLUP);
     52 
     53   ButtonConfig* buttonConfig = button.getButtonConfig();
     54   buttonConfig->setEventHandler(handleEvent);
     55   buttonConfig->setFeature(ButtonConfig::kFeatureDoubleClick);
     56   buttonConfig->setFeature(
     57       ButtonConfig::kFeatureSuppressClickBeforeDoubleClick);
     58 }
     59 
     60 void loop() {
     61   // Should be called every 20ms or faster for the default debouncing time
     62   // of ~50ms.
     63   button.check();
     64 }
     65 
     66 // The event handler for the button.
     67 void handleEvent(AceButton* /* button */, uint8_t eventType,
     68     uint8_t /* buttonState */) {
     69   switch (eventType) {
     70     case AceButton::kEventClicked:
     71       digitalWrite(LED_PIN, LED_ON);
     72       break;
     73     case AceButton::kEventDoubleClicked:
     74       digitalWrite(LED_PIN, LED_OFF);
     75       break;
     76   }
     77 }