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

ClickVersusDoubleClickUsingBoth.ino (2244B)

      1 /*
      2  * A demo that combines the techniques of ClickVersusDoubleClickUsingReleased
      3  * and ClickVersusDoubleClickUsingSuppression by using both event types to
      4  * trigger a "Clicked". I think this is the best of both worlds. If someone does
      5  * a simple Press/Release, the Release gets triggered. If someone does a quick
      6  * click, then a Click gets triggers (after a delay to wait for the potential
      7  * DoubleClick).
      8  *
      9  * See Also:
     10  *    examples/ClickVersusDoubleClickUsingReleased/
     11  *    examples/ClickVersusDoubleClickUsingSuppression/
     12  */
     13 
     14 #include <AceButton.h>
     15 using namespace ace_button;
     16 
     17 // The pin number attached to the button.
     18 const int BUTTON_PIN = 2;
     19 
     20 #ifdef ESP32
     21   // Different ESP32 boards use different pins
     22   const int LED_PIN = 2;
     23 #else
     24   const int LED_PIN = LED_BUILTIN;
     25 #endif
     26 
     27 // LED states. Some microcontrollers wire their built-in LED the reverse.
     28 const int LED_ON = HIGH;
     29 const int LED_OFF = LOW;
     30 
     31 // One button wired to the pin at BUTTON_PIN. Automatically uses the default
     32 // ButtonConfig. The alternative is to call the AceButton::init() method in
     33 // setup() below.
     34 AceButton button(BUTTON_PIN);
     35 
     36 void handleEvent(AceButton*, uint8_t, uint8_t);
     37 
     38 void setup() {
     39   // initialize built-in LED as an output
     40   pinMode(LED_PIN, OUTPUT);
     41 
     42   // Button uses the built-in pull up register.
     43   pinMode(BUTTON_PIN, INPUT_PULLUP);
     44 
     45   ButtonConfig* buttonConfig = button.getButtonConfig();
     46   buttonConfig->setEventHandler(handleEvent);
     47   buttonConfig->setFeature(ButtonConfig::kFeatureDoubleClick);
     48   buttonConfig->setFeature(
     49       ButtonConfig::kFeatureSuppressClickBeforeDoubleClick);
     50   buttonConfig->setFeature(ButtonConfig::kFeatureSuppressAfterClick);
     51   buttonConfig->setFeature(ButtonConfig::kFeatureSuppressAfterDoubleClick);
     52 }
     53 
     54 void loop() {
     55   // Should be called every 20ms or faster for the default debouncing time
     56   // of ~50ms.
     57   button.check();
     58 }
     59 
     60 // The event handler for the button.
     61 void handleEvent(AceButton* /* button */, uint8_t eventType,
     62     uint8_t /* buttonState */) {
     63   switch (eventType) {
     64     case AceButton::kEventClicked:
     65     case AceButton::kEventReleased:
     66       digitalWrite(LED_PIN, LED_ON);
     67       break;
     68     case AceButton::kEventDoubleClicked:
     69       digitalWrite(LED_PIN, LED_OFF);
     70       break;
     71   }
     72 }