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

SingleButton.ino (3004B)

      1 /*
      2  * A demo of a simple AceButton used to handle the events from one button.
      3  * Similar to HelloButton with some additions;
      4  * - more comments
      5  * - prints out the button events to the Serial monitor
      6  * - enables the all button events, including LongPress and RepeatPress
      7  * - suppresses lower-level events when higher-level events are detected
      8  *   (e.g. Clicked suppressed Released, DoubleClicked suppresses the
      9  *   second Clicked, LongPressed suppressed the Released, etc.)
     10  * - setup() determines if a button was pressed during reboot
     11  */
     12 
     13 #include <AceButton.h>
     14 using namespace ace_button;
     15 
     16 // The pin number attached to the button.
     17 const int BUTTON_PIN = 2;
     18 
     19 #ifdef ESP32
     20   // Different ESP32 boards use different pins
     21   const int LED_PIN = 2;
     22 #else
     23   const int LED_PIN = LED_BUILTIN;
     24 #endif
     25 
     26 // LED states. Some microcontrollers wire their built-in LED the reverse.
     27 const int LED_ON = HIGH;
     28 const int LED_OFF = LOW;
     29 
     30 // One button wired to the pin at BUTTON_PIN. Automatically uses the default
     31 // ButtonConfig. The alternative is to call the AceButton::init() method in
     32 // setup() below.
     33 AceButton button(BUTTON_PIN);
     34 
     35 void handleEvent(AceButton*, uint8_t, uint8_t);
     36 
     37 void setup() {
     38   delay(1000); // some microcontrollers reboot twice
     39   Serial.begin(115200);
     40   while (! Serial); // Wait until Serial is ready - Leonardo/Micro
     41   Serial.println(F("setup(): begin"));
     42 
     43   // initialize built-in LED as an output
     44   pinMode(LED_PIN, OUTPUT);
     45 
     46   // Button uses the built-in pull up register.
     47   pinMode(BUTTON_PIN, INPUT_PULLUP);
     48 
     49   // Configure the ButtonConfig with the event handler, and enable all higher
     50   // level events.
     51   ButtonConfig* buttonConfig = button.getButtonConfig();
     52   buttonConfig->setEventHandler(handleEvent);
     53   buttonConfig->setFeature(ButtonConfig::kFeatureClick);
     54   buttonConfig->setFeature(ButtonConfig::kFeatureDoubleClick);
     55   buttonConfig->setFeature(ButtonConfig::kFeatureLongPress);
     56   buttonConfig->setFeature(ButtonConfig::kFeatureRepeatPress);
     57 
     58   // Check if the button was pressed while booting
     59   if (button.isPressedRaw()) {
     60     Serial.println(F("setup(): button was pressed while booting"));
     61   }
     62 
     63   Serial.println(F("setup(): ready"));
     64 }
     65 
     66 void loop() {
     67   // Should be called every 4-5ms or faster, for the default debouncing time
     68   // of ~20ms.
     69   button.check();
     70 }
     71 
     72 // The event handler for the button.
     73 void handleEvent(AceButton* /* button */, uint8_t eventType,
     74     uint8_t buttonState) {
     75 
     76   // Print out a message for all events.
     77   Serial.print(F("handleEvent(): eventType: "));
     78   Serial.print(eventType);
     79   Serial.print(F("; buttonState: "));
     80   Serial.println(buttonState);
     81 
     82   // Control the LED only for the Pressed and Released events.
     83   // Notice that if the MCU is rebooted while the button is pressed down, no
     84   // event is triggered and the LED remains off.
     85   switch (eventType) {
     86     case AceButton::kEventPressed:
     87       digitalWrite(LED_PIN, LED_ON);
     88       break;
     89     case AceButton::kEventReleased:
     90       digitalWrite(LED_PIN, LED_OFF);
     91       break;
     92   }
     93 }