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 |
SingleButtonPullDown.ino (2785B)
1 /* 2 * A demo of a simple AceButton used to handle the events from one button. 3 * Pretty much the same as SingleButton.ino example but the button is wired with 4 * an external pull-down resistor, instead of using the built-in pull-up 5 * resistor. 6 */ 7 8 #include <AceButton.h> 9 using namespace ace_button; 10 11 // The pin number attached to the button. 12 const int BUTTON_PIN = 7; 13 14 #ifdef ESP32 15 // Different ESP32 boards use different pins 16 const int LED_PIN = 2; 17 #else 18 const int LED_PIN = LED_BUILTIN; 19 #endif 20 21 // LED states. Some microcontrollers wire their built-in LED the reverse. 22 const int LED_ON = HIGH; 23 const int LED_OFF = LOW; 24 25 // Automatically uses the default ButtonConfig. We will configure this later 26 // using AceButton::init() method in setup() below. 27 AceButton button; 28 29 void handleEvent(AceButton*, uint8_t, uint8_t); 30 31 void setup() { 32 delay(1000); // some microcontrollers reboot twice 33 Serial.begin(115200); 34 while (! Serial); // Wait until Serial is ready - Leonardo/Micro 35 Serial.println(F("setup(): begin")); 36 37 // initialize built-in LED as an output 38 pinMode(LED_PIN, OUTPUT); 39 40 // Button uses an external 10k resistor. 41 pinMode(BUTTON_PIN, INPUT); 42 43 // We use the AceButton::init() method here instead of using the constructor 44 // to show an alternative. Using init() allows the configuration of the 45 // hardware pin and the button to be placed closer to each other. 46 button.init(BUTTON_PIN, LOW); 47 48 // Configure the ButtonConfig with the event handler, and enable the LongPress 49 // and RepeatPress events which are turned off by default. 50 ButtonConfig* buttonConfig = button.getButtonConfig(); 51 buttonConfig->setEventHandler(handleEvent); 52 buttonConfig->setFeature(ButtonConfig::kFeatureClick); 53 buttonConfig->setFeature(ButtonConfig::kFeatureDoubleClick); 54 buttonConfig->setFeature(ButtonConfig::kFeatureLongPress); 55 buttonConfig->setFeature(ButtonConfig::kFeatureRepeatPress); 56 57 Serial.println(F("setup(): ready")); 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 70 // Print out a message for all events. 71 Serial.print(F("handleEvent(): eventType: ")); 72 Serial.print(eventType); 73 Serial.print(F("; buttonState: ")); 74 Serial.println(buttonState); 75 76 // Control the LED only for the Pressed and Released events. 77 // Notice that if the MCU is rebooted while the button is pressed down, no 78 // event is triggered and the LED remains off. 79 switch (eventType) { 80 case AceButton::kEventReleased: 81 digitalWrite(LED_PIN, LED_ON); 82 break; 83 case AceButton::kEventPressed: 84 digitalWrite(LED_PIN, LED_OFF); 85 break; 86 } 87 }