Giter VIP home page Giter VIP logo

acebutton's Introduction

AceButton

AUnit Tests

An adjustable, compact, event-driven button library for Arduino platforms.

This library provides classes which accept inputs from a mechanical button connected to a digital input pin on the Arduino. The library should be able to handle momentary buttons, maintained buttons, and switches, but it was designed primarily for momentary (aka push) buttons.

The library is named "AceButton" because:

  • many configurations of the button are adjustable, either at compile-time or run-time
  • the library is optimized to create compact objects which take up a minimal amount of static memory
  • the library detects changes in the button state and sends events to a user-defined EventHandler callback function

Most of the features of the library can be accessed through 2 classes, using either a callback function or an interface:

  • AceButton (class)
  • ButtonConfig (class)
  • EventHandler (typedef for callback function)
  • IEventHandler (interface)

The AceButton class contains the logic for debouncing and determining if a particular event has occurred.

The ButtonConfig class holds various timing parameters, the event handler, code for reading the button, and code for getting the internal clock.

The EventHandler is a user-defined callback function with a specific signature which is registered with the ButtonConfig object. When the library detects interesting events, the callback function is called by the library, allowing the client code to handle the event.

The IEventHandler is an interface (pure abstract class) that provides an alternative to the EventHandler. Instead of using a callback function, an object of type IEventHandler can be used to handle the button events.

The supported events are:

  • AceButton::kEventPressed
  • AceButton::kEventReleased
  • AceButton::kEventClicked
  • AceButton::kEventDoubleClicked
  • AceButton::kEventLongPressed
  • AceButton::kEventRepeatPressed
  • AceButton::kEventLongReleased (v1.8)
  • AceButton::kEventHeartBeat (v1.10)

The basic ButtonConfig class assumes that each button is connected to a single digital input pin. In some situations, the number of buttons that we want is greater than the number of input pins available. This library provides 2 subclasses of ButtonConfig which may be useful:

  • EncodedButtonConfig
    • Supports binary encoded buttons, to read 2^N - 1 buttons using N pins (e.g. 7 buttons using 3 digital pins).
  • LadderButtonConfig
    • Supports 1-8 buttons (maybe more) on a single analog pin through a resistor ladder. The analogRead() method is used to read the different voltage levels corresponding to each button.

Both EncodedButtonConfig and LadderButtonConfig support all events listed above (e.g. kEventClicked and kEventDoubleClicked).

Version: 1.10.1 (2023-05-25)

Changelog: CHANGELOG.md

Table of Contents

Features

Here are the high-level features of the AceButton library:

  • debounces the mechanical contact
  • supports both pull-up and pull-down wiring
  • event-driven through a user-defined EventHandler callback function
  • event-driven through an object-based IEventHandler (>= v1.6)
  • supports the following event types:
    • kEventPressed
    • kEventReleased
    • kEventClicked
    • kEventDoubleClicked
    • kEventLongPressed
    • kEventRepeatPressed
    • kEventLongReleased
    • kEventHeartBeat
  • adjustable configurations at runtime or compile-time
    • timing parameters
    • digitalRead() button read function can be overridden
    • millis() clock function can be overridden
  • small memory footprint
    • each AceButton consumes 17 bytes (8-bit) or 20 bytes (32-bit)
    • each ButtonConfig consumes 20 bytes (8-bit) or 24 bytes (32-bit)
    • one System ButtonConfig instance created automatically by the library
    • 970-2180 bytes of flash memory for the simple case of 1 AceButton and 1 ButtonConfig, depending on 8-bit or 32-bit processors
  • supports multiple buttons on shared pins using various circuits
  • only 13-15 microseconds (on 16MHz ATmega328P) per polling call to AceButton::check()
  • extensive testing
    • thoroughly unit tested using AUnit
    • Tier 1 support includes: Arduino AVR (UNO, Nano, Micro etc), SAMD21 (Seeed XIAO M0), STM32 (Blue Pill), SAMD51 (Adafruit ItsyBitsy M4), ESP8266, and ESP32

Compared to other Arduino button libraries, I think the unique or exceptional features of the AceButton library are:

  • many supported event types (e.g. LongPressed and RepeatPressed)
  • able to distinguish between Clicked and DoubleClicked
  • small memory usage
  • thorough unit testing
  • support for multiple buttons using Binary Encoding or a Resistor Ladder

HelloButton

Here is a simple program (see examples/HelloButton) which controls the builtin LED on the Arduino board using a momentary button connected to PIN 2.

#include <AceButton.h>
using namespace ace_button;

const int BUTTON_PIN = 2;
const int LED_ON = HIGH;
const int LED_OFF = LOW;

AceButton button(BUTTON_PIN);

void handleEvent(AceButton*, uint8_t, uint8_t);

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  button.setEventHandler(handleEvent);
}

void loop() {
  button.check();
}

void handleEvent(AceButton* /*button*/, uint8_t eventType,
    uint8_t /*buttonState*/) {
  switch (eventType) {
    case AceButton::kEventPressed:
      digitalWrite(LED_BUILTIN, LED_ON);
      break;
    case AceButton::kEventReleased:
      digitalWrite(LED_BUILTIN, LED_OFF);
      break;
  }
}

(The button and buttonState parameters are commented out to avoid an unused parameter warning from the compiler. We can't remove the parameters completely because the method signature is defined by the EventHandler typedef.)

Installation

The latest stable release is available in the Arduino IDE Library Manager. Search for "AceButton". Click install.

The development version can be installed by cloning the GitHub repository (https://github.com/bxparks/AceButton), checking out the develop branch, then manually copying over the contents to the ./libraries directory used by the Arduino IDE. (The result is a directory named ./libraries/AceButton.)

The master branch contains the tagged stable releases.

External Dependencies

The core of the library is self-contained and has no external dependencies.

The some programs in examples/ may depend on:

The unit tests under tests depend on:

Source Code

The source files are organized as follows:

  • src/AceButton.h - main header file
  • src/ace_button/ - all implementation files
  • src/ace_button/testing/ - internal testing files
  • tests/ - unit tests which require AUnit
  • examples/ - example sketches

Documentation

Examples

The following example sketches are provided:

  • Basic Single Button
    • HelloButton
      • minimal program that reads a switch and control the built-in LED
    • SingleButton
      • single button wired with an internal pull-up resistor
    • SingleButtonPullDown
      • same as SingleButton but with an external pull-down resistor
    • SingleButtonUsingIEventHandler
      • same as SingleButton using an object-based IEventHandler
    • Stopwatch
      • measures the speed of AceButton:check() with a start/stop/reset
      • button uses kFeatureLongPress
  • Multiple Buttons
    • TwoButtonsUsingOneButtonConfig
      • two buttons using one ButtonConfig
    • TwoButtonsUsingTwoButtonConfigs
      • two buttons using two ButtonConfigs
    • ThreeButtonsUsingOneButtonConfig
      • three buttons using one ButtonConfig
      • used as a reference for ThreeButtonsUsingOneButtonConfigFast (below)
    • TunerButtons
      • implements 5 radio buttons (tune-up, tune-down, and 3 presets)
      • shows multiple ButtonConfig and EventHandler instances
      • shows an example of how to use getId()
      • uses kFeatureLongPress, kFeatureRepeatPress, kFeatureSuppressAfterLongPress, and kFeatureSuppressAfterRepeatPress
    • ArrayButtons
      • shows how to define an array of AceButton and initialize them using the init() method in a loop
    • SimultaneousButtons
      • detecting simultaneous Pressed and Released of 2 buttons using a custom IEventHandler
  • Distinguishing Click versus Double-Click
  • Distinguishing Pressed and LongPressed
  • CapacitiveButton
  • HeartBeat
    • demo of activating the new (v1.10) kEventHeartBeat feature, and using it to generate 2 custom events: kCustomEventLongPressed (similar to kEventLongPressed) and kCustomEventLongReleased (no built-in equivalent)
  • Binary Encoded Buttons
    • Encoded4To2Buttons
      • demo of Encoded4To2ButtonConfig class to decode M=3 buttons with N=2 pins
    • Encoded8To3Buttons
      • demo of Encoded8To3ButtonConfig class to decode M=7 buttons with N=3 pins
    • Encoded16To4Buttons
      • demo of general M-to-N EncodedButtonConfig class to handle M=15 buttons with N=4 pins
  • Resistor Ladder Buttons
    • LadderButtonCalibrator
      • print out the value returned by analogRead() for various buttons
      • useful to compare the expected values of the resistor ladder versus the actual values returned by the function
    • LadderButtons
      • demo of 4 buttons on a single analog pin using analogRead()
    • LadderButtonsTiny
      • 2 buttons on the RESET/A0 pin of an ATtiny85 microcontroller
      • avoids wasting the RESET pin, saving the other pins for other purposes
  • digitalWriteFast
  • Benchmarks
    • These are internal benchmark programs. They were not written as examples of how to use the library.
    • AutoBenchmark
      • generates the timing stats (min/average/max) for the AceButton::check() method for various types of events (idle, press/release, click, double-click, and long-press)
    • MemoryBenchmark
      • determines the amount of flash memory consumed by various objects and features of the library

Usage

There are 2 classes and one typedef that a user will normally interact with:

  • AceButton (class)
  • ButtonConfig (class)
  • EventHandler (typedef)

Advanced usage is supported by:

  • EncodedButtonConfig - binary encoded buttons supporting 2^N-1 buttons on N digital pins
  • LadderButtonConfig - resistor ladder buttons using analog pins
  • IEventHandler - use a callback object instead of a callback function

We explain how to use these below.

Include Header and Use Namespace

Only a single header file AceButton.h is required to use this library. To prevent name clashes with other libraries that the calling code may use, all classes are defined in the ace_button namespace. To use the code without prepending the ace_button:: prefix, use the using directive:

#include <AceButton.h>
using namespace ace_button;

If you are dependent on just AceButton, the following might be sufficient:

#include <AceButton.h>
using ace_button::AceButton;

Pin Wiring and Initialization

The ButtonConfig class supports the simplest wiring. Each button is connected to a single digital input pin, as shown below. In the example below, 3 buttons labeled S0, S1 and S2 are connected to digital input pins D2, D3, and D4:

Direct Digital

An Arduino microcontroller pin can be in an OUTPUT mode, an INPUT mode, or an INPUT_PULLUP mode. This mode is controlled by the pinMode() method.

By default upon boot, the pin is set to the INPUT mode. However, this INPUT mode puts the pin into a high impedance state, which means that if there is no wire connected to the pin, the voltage on the pin is indeterminate. When the input pin is read (using digitalRead()), the boolean value will be a random value. If you are using the pin in INPUT mode, you must connect an external pull-up resistor (connected to Vcc) or pull-down resistor (connected to ground) so that the voltage level of the pin is defined when there is nothing connected to the pin (i.e. when the button is not pressed).

The INPUT_PULLUP mode is a special INPUT mode which tells the microcontroller to connect an internal pull-up resistor to the pin. It is activated by calling pinMode(pin, INPUT_PULLUP) on the given pin. This mode is very convenient because it eliminates the external resistor, making the wiring simpler.

The 3 resistors Rc1, Rc2 and Rc3 are optional current limiting resistors. They help protect the microcontroller in the case of misconfiguration. If the pins are accidentally set to OUTPUT mode, then pressing one of the buttons would connect the output pin directly to ground, causing a large amount of current to flow that could permanently damage the microcontroller. The resistance value of 220 ohms (or maybe 330 ohms) is high enough to keep the current within safety limits, but low enough compared to the internal pullup resistor that it is able to pull the digital pin to a logical 0 level. These current limiting resistors are good safety measures, but I admit that I often get lazy and don't use them when doing quick experiments.

The AceButton library itself does not call the pinMode() function. The calling application is responsible for calling pinMode(). Normally, this happens in the global setup() method but the call can happen somewhere else if the application requires it. The reason for decoupling the hardware configuration from the AceButton library is mostly because the library does not actually care about the specific hardware wiring of the button. It does not care whether an external resistor is used, or the internal resistor is used. It only cares about whether the resistor is a pull-up or a pull-down.

See https://www.arduino.cc/en/Tutorial/DigitalPins for additional information about the I/O pins on an Arduino.

AceButton Class

The AceButton class looks like this (not all public methods are shown):

namespace ace_button {

class AceButton {
  public:
    static const uint8_t kEventPressed = 0;
    static const uint8_t kEventReleased = 1;
    static const uint8_t kEventClicked = 2;
    static const uint8_t kEventDoubleClicked = 3;
    static const uint8_t kEventLongPressed = 4;
    static const uint8_t kEventRepeatPressed = 5;
    static const uint8_t kEventLongReleased = 6;
    static const uint8_t kEventHeartBeat = 7;

    static const uint8_t kButtonStateUnknown = 127;

    static __FlashStringHelper eventName(uint8_t e);

    explicit AceButton(uint8_t pin = 0, uint8_t defaultReleasedState = HIGH,
        uint8_t id = 0);
    explicit AceButton(ButtonConfig* buttonConfig, uint8_t pin = 0,
        uint8_t defaultReleasedState = HIGH, uint8_t id = 0);
    void init(uint8_t pin = 0, uint8_t defaultReleasedState = HIGH,
        uint8_t id = 0);
    void init(ButtonConfig* buttonConfig, uint8_t pin = 0,
        uint8_t defaultReleasedState = HIGH, uint8_t id = 0);

    ButtonConfig* getButtonConfig();
    void setButtonConfig(ButtonConfig* buttonConfig);
    void setEventHandler(ButtonConfig::EventHandler eventHandler);

    uint8_t getPin();
    uint8_t getDefaultReleasedState();
    uint8_t getId();

    void check();
};

}

Each physical button will be handled by an instance of AceButton. At a minimum, the instance needs to be told the pin number of the button. This can be done through the constructor:

const uint8_t BUTTON_PIN = 2;

AceButton button(BUTTON_PIN);

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  ...
}

Or we can use the init() method in the setup():

AceButton button;

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  button.init(BUTTON_PIN);
  ...
}

Both the constructor and the init() function take 3 optional parameters as shown above:

  • pin: the I/O pin number assigned to the button
  • defaultReleasedState: the logical value of the button when it is in its default "released" state (HIGH using a pull-up resistor, LOW for a pull-down resistor)
  • id: an optional, user-defined identifier for the button, for example, an index into an array with additional information

The pin must be defined either through the constructor or the init() method. But the other two parameters may be optional in many cases.

Sampling Rate

To read the state of the button, the AceButton::check() method should be called from the loop() method periodically. Roughly speaking, this should be about 4 times faster than the value of getDebounceDelay() so that the various event detection logic can work properly. For example, for the default debounce delay is 20 ms, AceButton::check() should be called every 5 ms. I have successfully experimented with using a sampling delay as large as 10 ms, but I recommend about 5 ms in most cases.

You could call the AceButton::check() method directly in the global loop() function like this:

void loop() {
  ...
  button.check();
  ...
}

This would sample the button as fast as possible on your particular microprocessor, perhaps as fast as 10,000 or 100,000 times a second, depending on the other code that is in the loop() function.

Most of the time, a high sampling rate is not a problem except for 2 things:

  • Calling the AceButton::check() has a small overhead and your processor could be doing other things during that time.
  • If you use Resistor Ladder Buttons described below, on an ESP8266, you will trigger a bug that causes the WiFi to disconnect if you sample the analogRead() function more than a 1000 times/second.

If you want to limit the sampling rate, see the example code in Rate Limit CheckButtons. The code relies on using a static variable to implement a non-blocking delay, like this:

AceButton button;
...

void checkButtons() {
  static uint16_t prev = millis();

  // DO NOT USE delay(5) to do this.
  // The (uint16_t) cast is required on 32-bit processors, harmless on 8-bit.
  uint16_t now = millis();
  if ((uint16_t) (now - prev) >= 5) {
    button.check();
    prev = now;
  }
}

void loop() {
  checkButtons();
  ...
}

Compiler Error On Pin 0

If you attempt to use Pin 0 in the AceButton() constructor:

AceButton button(0);

you may encounter a compile-time error such as this:

error: call of overloaded 'AceButton(int)' is ambiguous

The solution is to explicitly cast the 0 to a uint8_t type, or to assign it explicitly to a uint8_t const, like this:

// Explicit cast
AceButton button((uint8_t) 0);

// Or assign to a const first.
static const uint8_t PIN = 0;
AceButton button(PIN);

See Issue #40 for details.

ButtonConfig Class

The core concept of the AceButton library is the separation of the button (AceButton) from its configuration (ButtonConfig).

  • The AceButton class has the logic for debouncing and detecting the various events (Pressed, Released, etc), and the various bookkeeping variables needed to implement the logic. These variables are associated with the specific instance of that AceButton.
  • The ButtonConfig class has the various timing parameters which control how much time is needed to detect certain events. This class also has the ability to override the default methods for reading the pin (readButton()) and the clock (getClock()). This ability allows unit tests to be written.

The class looks like this (not all public methods are shown):

namespace ace_button {

class ButtonConfig {
  public:
    static const uint16_t kDebounceDelay = 20;
    static const uint16_t kClickDelay = 200;
    static const uint16_t kDoubleClickDelay = 400;
    static const uint16_t kLongPressDelay = 1000;
    static const uint16_t kRepeatPressDelay = 1000;
    static const uint16_t kRepeatPressInterval = 200;
    static const uint16_t kHeartBeatInterval = 5000;

    typedef uint16_t FeatureFlagType;
    static const FeatureFlagType kFeatureClick = 0x01;
    static const FeatureFlagType kFeatureDoubleClick = 0x02;
    static const FeatureFlagType kFeatureLongPress = 0x04;
    static const FeatureFlagType kFeatureRepeatPress = 0x08;
    static const FeatureFlagType kFeatureSuppressAfterClick = 0x10;
    static const FeatureFlagType kFeatureSuppressAfterDoubleClick = 0x20;
    static const FeatureFlagType kFeatureSuppressAfterLongPress = 0x40;
    static const FeatureFlagType kFeatureSuppressAfterRepeatPress = 0x80;
    static const FeatureFlagType kFeatureSuppressClickBeforeDoubleClick = 0x100;
    static const FeatureFlagType kFeatureHeartBeat = 0x200;
    static const FeatureFlagType kFeatureSuppressAll = (
        kFeatureSuppressAfterClick
        | kFeatureSuppressAfterDoubleClick
        | kFeatureSuppressAfterLongPress
        | kFeatureSuppressAfterRepeatPress
        | kFeatureSuppressClickBeforeDoubleClick);

    typedef void (*EventHandler)(AceButton* button, uint8_t eventType,
        uint8_t buttonState);

    ButtonConfig() = default;

    uint16_t getDebounceDelay() const;
    uint16_t getClickDelay() const;
    uint16_t getDoubleClickDelay() const;
    uint16_t getLongPressDelay() const;
    uint16_t getRepeatPressDelay() const;
    uint16_t getRepeatPressInterval() const;
    uint16_t getHeartBeatInterval() const;

    void setDebounceDelay(uint16_t debounceDelay);
    void setClickDelay(uint16_t clickDelay);
    void setDoubleClickDelay(uint16_t doubleClickDelay);
    void setLongPressDelay(uint16_t longPressDelay);
    void setRepeatPressDelay(uint16_t repeatPressDelay);
    void setRepeatPressInterval(uint16_t repeatPressInterval);
    void setHeartBeatInterval(uint16_t heartBeatInterval);

    virtual unsigned long getClock();
    virtual int readButton(uint8_t pin);

    bool isFeature(FeatureFlagType features) const;
    void setFeature(FeatureFlagType features);
    void clearFeature(FeatureFlagType features);
    void resetFeatures();

    void setEventHandler(EventHandler eventHandler);
    void setIEventHandler(IEventHandler* eventHandler);

    static ButtonConfig* getSystemButtonConfig();
};

}

The ButtonConfig (or a customized subclass) can be created and assigned to one or more AceButton instances using dependency injection through the AceButton(ButtonConfig*) constructor. This constructor also accepts the same (pin, defaultReleasedState, id) parameters as init(pin, defaultReleasedState, id) method. Sometimes it's easier to set all the parameters in one place using the constructor. Other times, the parameters are not known until the AceButton::init() method can be called from the global setup() method.

const uint8_t PIN1 = 2;
const uint8_t PIN2 = 4;

ButtonConfig buttonConfig;
AceButton button1(&buttonConfig, PIN1);
AceButton button2(&buttonConfig, PIN2);

void setup() {
  pinMode(PIN1, INPUT_PULLUP);
  pinMode(PIN2, INPUT_PULLUP);
  ...
}

Another way to inject the ButtonConfig dependency is to use the AceButton::setButtonConfig() method but it is recommended that you use the constructor instead because the dependency is easier to follow.

System ButtonConfig

A single instance of ButtonConfig called the "System ButtonConfig" is automatically created by the library at startup. By default, all instances of AceButton are automatically assigned to this singleton instance. We explain in the Single Button Simplifications section below how this simplifies the code needed to handle a single button.

Configuring the EventHandler

The ButtonConfig class provides a number of methods which are mostly used internally by the AceButton class. The one method which is expected to be used by the calling client code is setEventHandler() which assigns the user-defined EventHandler callback function to the ButtonConfig instance. This is explained in more detail below in the EventHandler section below.

Timing Parameters

Here are the methods to retrieve the timing parameters:

  • uint16_t getDebounceDelay(); (default: 20 ms)
  • uint16_t getClickDelay(); (default: 200 ms)
  • uint16_t getDoubleClickDelay(); (default: 400 ms)
  • uint16_t getLongPressDelay(); (default: 1000 ms)
  • uint16_t getRepeatPressDelay(); (default: 1000 ms)
  • uint16_t getRepeatPressInterval(); (default: 200 ms)

The default values of each timing parameter can be changed at run-time using the following methods:

  • void setDebounceDelay(uint16_t debounceDelay);
  • void setClickDelay(uint16_t clickDelay);
  • void setDoubleClickDelay(uint16_t doubleClickDelay);
  • void setLongPressDelay(uint16_t longPressDelay);
  • void setRepeatPressDelay(uint16_t repeatPressDelay);
  • void setRepeatPressInterval(uint16_t repeatPressInterval);

Hardware Dependencies

The ButtonConfig class has 2 methods which provide hooks to its external hardware dependencies:

  • virtual unsigned long getClock();
  • virtual int readButton(uint8_t pin);

By default these are mapped to the underlying Arduino system functions respectively:

  • millis()
  • digitalRead()

Unit tests are possible because these methods are virtual and the hardware dependencies can be swapped out with fake ones.

Multiple ButtonConfig Instances

We have assumed that there is a 1-to-many relationship between a ButtonConfig and the AceButton. In other words, multiple buttons will normally be associated with a single configuration. Each AceButton has a pointer to an instance of ButtonConfig. So the cost of separating the ButtonConfig from AceButton is 2 bytes in each instance of AceButton. Note that this is equivalent to adding virtual methods to AceButton (which would add 2 bytes), so in terms of static RAM size, this is a wash.

The library is designed to handle multiple buttons, and it assumes that the buttons are normally grouped together into a handful of types. For example, consider the buttons of a car radio. It has several types of buttons:

  • the tuner buttons (2, up and down)
  • the preset buttons (6)
  • the AM/FM band button (1)

In this example, there are 9 buttons, but only 3 instances of ButtonConfig would be needed.

EventHandler Typedef

The event handler is a callback function that gets called when the AceButton class determines that an interesting event happened on the button. The advantage of this mechanism is that all the complicated logic of determining the various events happens inside the AceButton class, and the user will normally not need to worry about the details.

EventHandler Signature

The event handler is defined in the ButtonConfig class and has the following signature:

class ButtonConfig {
  public:
    typedef void (*EventHandler)(AceButton* button, uint8_t eventType,
        uint8_t buttonState);
    ...
};

The event handler is registered with the ButtonConfig object, not with the AceButton object, although the convenience method AceButton::setEventHandler() is provided as a pass-through to the underlying ButtonConfig (see the Single Button Simplifications section below):

ButtonConfig buttonConfig;

void handleEvent(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  ...
}

void setup() {
  ...
  buttonConfig.setEventHandler(handleEvent);
  ...
}

The motivation for this design is to save static memory. If multiple buttons are associated with a single ButtonConfig, then it is not necessary for every button of that type to hold the same pointer to the EventHandler function. It is only necessary to save that information once, in the ButtonConfig object.

Pro Tip 1: Comment out the unused parameter(s) in the handleEvent() method to avoid the unused parameter compiler warning:

void handleEvent(AceButton* /*button*/, uint8_t eventType,
    uint8_t /*buttonState*/) {
  ...
}

The Arduino sketch compiler can get confused with the parameters commented out, so you may need to add a forward declaration for the handleEvent() method before the setup() method:

void handleEvent(AceButton*, uint8_t, uint8_t);

Pro Tips 2: The event handler can be an object instead of just a function pointer. An object-based event handler can be useful in more complex applications with numerous buttons. See the section on Object-based Event Handler in the Advanced Topics below.

EventHandler Parameters

The EventHandler function receives 3 parameters from the AceButton:

  • button
    • pointer to the AceButton instance that generated this event
    • can be used to retrieve the getPin() or the getId()
  • eventType
    • the type of this event given by the various AceButton::kEventXxx constants
  • buttonState
    • the HIGH or LOW button state that generated this event

The button pointer should be used only to extract information about the button that triggered the event. It should not be used to modify the button's internal variables in any way within the eventHandler. The logic in AceButton::check() assumes that those internal variable are held constant, and if they are changed by the eventHandler, unpredictable results may occur. (I should have made the button be a const AceButton* but by the time I realized this, there were too many users of the library already, and I did not want to make a breaking change to the API.)

If you are using only a single button, then you should need to check only the eventType.

It is not expected that buttonState will be needed very often. It should be sufficient to examine just the eventType to determine the action that needs to be performed. Part of the difficulty with this parameter is that it has the value of LOW or HIGH, but the physical interpretation of those values depends on whether the button was wired with a pull-up or pull-down resistor. Use the helper function button->isReleased(buttonState) to translate the raw buttonState into a more meaningful determination if you need it.

One EventHandler Per ButtonConfig

Only a single EventHandler per ButtonConfig is supported. An alternative would have been to register a separate event handler for each of the 8 kEventXxx events. But each callback function requires 2 bytes of memory (on 8-bit processors, or 4 bytes on 32-bit processors) and it was assumed that in most cases, the calling client code would be interested in only a few of these event types, so it seemed wasteful to allocate 16 or 32 bytes when most of these would be unused. If the client code really wants separate event handlers, it can be easily emulated by invoking them through the main event handler:

void handleEvent(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  switch (eventType) {
    case AceButton::kEventPressed:
      handleEventPressed(button, eventType, buttonState);
      break;
    case AceButton::kEventReleased:
      handleEventReleased(button, eventType, buttonState);
      break;
    ...
  }
}

EventHandler Tips

The Arduino runtime environment is single-threaded, so the EventHandler is called in the middle of the AceButton::check() method, in the same thread as the check() method. It is therefore important to write the EventHandler code to run somewhat quickly, so that the delay doesn't negatively impact the logic of the AceButton::check() algorithm. Since AceButton::check() should run approximately every 5 ms, the user-provided EventHandler should run somewhat faster than 5 ms. Given a choice, it is probably better to use the EventHandler to set some flags or variables and return quickly, then do additional processing from the loop() method.

Sometimes it is too convenient or unavoidable to perform a long-running operation inside the event handler (e.g. making an HTTP). This is fine, I have done this occasionally. Just be aware that the button scanning operation will not work during that long-running operation.

Speaking of threads, the API of the AceButton Library was designed to work in a multi-threaded environment, if that situation were to occur in the Arduino world.

Event Types

The supported events are defined by a list of integer (uint8_t) constants in AceButton.h:

  • AceButton::kEventPressed (always enabled, cannot be suppressed)
  • AceButton::kEventReleased (default: enabled)
  • AceButton::kEventClicked (default: disabled)
  • AceButton::kEventDoubleClicked (default: disabled)
  • AceButton::kEventLongPressed (default: disabled)
  • AceButton::kEventRepeatPressed (default: disabled)
  • AceButton::kEventLongReleased (default: disabled, autoenabled by kFeatureSuppressAfterLongPress, new for v1.8)

These values are sent to the EventHandler in the eventType parameter.

Two of the events are enabled by default, four are disabled by default but can be enabled by using a Feature flag described below.

During development and debugging, it is useful to print a human-readable version of these integer constants. The AceButton::eventName(e) is a static function which returns a string for each event constant, and can be used like this:

void handleEvent(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  ...
  Serial.print(AceButton::eventName(eventType));
  ...
}

ButtonConfig Feature Flags

There are 9 flags defined in ButtonConfig which can control the behavior of AceButton event handling:

  • ButtonConfig::kFeatureClick
  • ButtonConfig::kFeatureDoubleClick
  • ButtonConfig::kFeatureLongPress
  • ButtonConfig::kFeatureRepeatPress
  • ButtonConfig::kFeatureSuppressAfterClick
  • ButtonConfig::kFeatureSuppressAfterDoubleClick
  • ButtonConfig::kFeatureSuppressAfterLongPress
  • ButtonConfig::kFeatureSuppressAfterRepeatPress
  • ButtonConfig::kFeatureSuppressClickBeforeDoubleClick
  • ButtonConfig::kFeatureSuppressAll

These constants are used to set or clear the given flag:

// Get the current config.
ButtonConfig* config = button.getButtonConfig();

// Set a specific feature
config->setFeature(ButtonConfig::kFeatureLongPress);

// Clear a specific feature
config->clearFeature(ButtonConfig::kFeatureLongPress);

// Test for a specific feature
if (config->isFeature(ButtonConfig::kFeatureLongPress)) {
  ...
}

// Clear all features
config->resetFeatures()

The meaning of these flags are described below.

Event Activation

Of the various event types, the following are disabled by default:

  • AceButton::kEventClicked
  • AceButton::kEventDoubleClicked
  • AceButton::kEventLongPressed
  • AceButton::kEventRepeatPressed
  • AceButton::kEventLongReleased
  • AceButton::kEventHeartBeat

To receive these events, call ButtonConfig::setFeature() with the following corresponding flags:

  • ButtonConfig::kFeatureClick
  • ButtonConfig::kFeatureDoubleClick
  • ButtonConfig::kFeatureLongPress
  • ButtonConfig::kFeatureRepeatPress
  • ButtonConfig::kFeatureSuppressAfterLongPress
    • suppresses kEventReleased after a LongPress, but turns on kEventLongReleased as a side effect
  • ButtonConfig::kFeatureHeartBeat

like this:

ButtonConfig *config = button.getButtonConfig();
config->setFeature(ButtonConfig::kFeatureClick);

To disable these events, call ButtonConfig::clearFeature() with one of these flags, like this:

ButtonConfig *config = button.getButtonConfig();
config->clearFeature(ButtonConfig::kFeatureLongPress);

Enabling kFeatureDoubleClick automatically enables kFeatureClick, because we need to have a Clicked event before a DoubleClicked event can be detected.

It seems unlikely that both LongPress and RepeatPress events would be useful at the same time, but both event types can be activated if you need it.

Event Suppression

Event types can be considered to be built up in layers, starting with the lowest level primitive events: Pressed and Released. Higher level events are built on top of the lower level events through various timing delays. When a higher level event is detected, it is sometimes useful to suppress the lower level event that was used to detect the higher level event.

For example, a Clicked event requires a Pressed event followed by a Released event within a ButtonConfig::getClickDelay() milliseconds (200 ms by default). The Pressed event is always generated. If a Clicked event is detected, we could choose to generate both a Released event and a Clicked event, and this is the default behavior.

However, many times, it is useful to suppress the Released event if the Clicked event is detected. The ButtonConfig can be configured to suppress these lower level events. Call the setFeature(feature) method passing the various kFeatureSuppressXxx constants:

  • ButtonConfig::kFeatureSuppressAfterClick
    • suppresses the kEventReleased event after a Clicked event is detected
    • also suppresses the Released event from the first Clicked of a DoubleClicked, since kFeatureDoubleClick automatically enables kFeatureClick
  • ButtonConfig::kFeatureSuppressAfterDoubleClick
    • suppresses the kEventReleased event and the second Clicked event if a DoubleClicked event is detected
  • ButtonConfig::kFeatureSuppressAfterLongPress
    • suppresses the kEventReleased event if a LongPressed event is detected
    • (v1.8) automatically enables kEventLongReleased event as a substitute for the suppressed kEventReleased, see Distinguishing Pressed and Long Pressed subsection below for more details.
  • ButtonConfig::kFeatureSuppressAfterRepeatPress
    • suppresses the kEventReleased event after the last RepeatPressed event
  • ButtonConfig::kFeatureSuppressClickBeforeDoubleClick
    • The first kEventClicked event is postponed by getDoubleClickDelay() millis until the code can determine if a DoubleClick has occurred. If so, then the postponed kEventClicked message to the EventHandler is suppressed.
    • See Distinguishing Clicked and DoubleClicked subsection below for more info.
  • ButtonConfig::kFeatureSuppressAll
    • a convenience parameter that is the equivalent of suppressing all of the previous events

By default, no suppression is performed.

As an example, to suppress the kEventReleased after a kEventLongPressed (this is actually often the case), you would do this:

ButtonConfig* config = button.getButtonConfig();
config->setFeature(ButtonConfig::kFeatureSuppressAfterLongPress);

The special convenient constant kFeatureSuppressAll is equivalent of using all suppression constants:

ButtonConfig* config = button.getButtonConfig();
config->setFeature(ButtonConfig::kFeatureSuppressAll);

All suppressions can be cleared by using:

ButtonConfig* config = button.getButtonConfig();
config->clearFeature(ButtonConfig::kFeatureSuppressAll);

Note, however, that the isFeature(ButtonConfig::kFeatureSuppressAll) currently means "isAnyFeature() implemented?" not "areAllFeatures() implemented?" I don't expect isFeature() to be used often (or at all) for kFeatureSuppressAll.

You can clear all feature at once using:

ButtonConfig* config = button.getButtonConfig();
config->resetFeatures();

This is useful if you want to reuse a ButtonConfig instance and you want to reset its feature flags to its initial state.

Single Button Simplifications

Although the AceButton library is designed to shine for multiple buttons, you may want to use it to handle just one button. The library provides some features to make this simple case easy.

  1. The library automatically creates one instance of ButtonConfig called a "System ButtonConfig". This System ButtonConfig can be retrieved using the class static method ButtonConfig::getSystemButtonConfig().
  2. Every instance of AceButton is assigned an instance of the System ButtonConfig by default (which can be overridden manually).
  3. A convenience method allows the EventHandler for the System ButtonConfig to be set easily through AceButton itself, instead of having to get the System ButtonConfig first, then set the event handler. In other words, button.setEventHandler(handleEvent) is a synonym for button.getButtonConfig()->setEventHandler(handleEvent).

These simplifying features allow a single button to be configured and used like this:

AceButton button(BUTTON_PIN);

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  button.setEventHandler(handleEvent);
  ...
}

void loop() {
  button.check();
}

void handleEvent(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  ...
}

To configure the System ButtonConfig, you may need to add something like this to the setup() section:

  button.getButtonConfig()->setFeature(ButtonConfig::kFeatureLongPress);

Multiple Buttons

When transitioning from a single button to multiple buttons, it's important to remember what's happening underneath the convenience methods. The single AceButton button is assigned to the System ButtonConfig that was created automatically. When an EventHandler is assigned to the button, it is actually assigned to the System ButtonConfig. All subsequent instances of AceButton will also be associated with this event handler, unless another ButtonConfig is explicitly assigned.

There are at least 2 ways you can configure multiple buttons.

Option 1: Multiple ButtonConfigs

#include <AceButton.h>
using namespace ace_button;

ButtonConfig config1;
AceButton button1(&config1);
ButtonConfig config2;
AceButton button2(&config2);

void button1Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  Serial.println("button1");
}

void button2Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  Serial.println("button2");
}

void setup() {
  Serial.begin(115200);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  config1.setEventHandler(button1Handler);
  config2.setEventHandler(button2Handler);
  button1.init(6);
  button2.init(7);
}

void loop() {
  button1.check();
  button2.check();
}

See the example sketch TwoButtonsUsingTwoButtonConfigs which uses 2 ButtonConfig instances to configure 2 AceButton instances.

Option 2: Multiple Button Discriminators

Another technique keeps the single system ButtonConfig and the single EventHandler, but use the AceButton::getPin() to discriminate between the multiple buttons:

#include <AceButton.h>
using namespace ace_button;

AceButton button1(6);
AceButton button2(7);

void button1Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  Serial.println("button1");
}

void button2Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  Serial.println("button2");
}

void buttonHandler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  switch (button->getPin()) {
    case 6:
      button1Handler(button, eventType, buttonState);
      break;
    case 7:
      button2Handler(button, eventType, buttonState);
      break;
  }
}

void setup() {
  Serial.begin(115200);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  ButtonConfig* config = ButtonConfig::getSystemButtonConfig();
  config->setEventHandler(buttonHandler);
}

void loop() {
  button1.check();
  button2.check();
}

See the example code TwoButtonsUsingOneButtonConfig. which uses a single ButtonConfig instance to handle 2 AceButton instances.

Sometimes, it is more convenient to use the AceButton::getId() method to identify the button instead of the AceButton::getPin(). See ArrayButtons for an example.

Advanced Topics

Object-based Event Handler

The EventHandler is a typedef that is defined to be a function pointer. This is a simple, low-overhead design that produces the smallest memory footprint, and allows the event handler to be written with the smallest amount of boilerplate code. The user does not have to override a class.

In more complex applications involving larger number of AceButton and ButtonConfig objects, it is often useful for the EventHandler to be an object instead of a simple function pointer. This is especially true if the application uses Object Oriented Programming (OOP) techniques for modularity and encapsulation. Using an object as the event handler allows additional context information to be injected into the event handler.

To support OOP techniques, AceButton v1.6 adds:

  • IEventHandler interface class
    • contains a single pure virtual function handleEvent()
  • ButtonConfig::setIEventHandler() method
    • accepts a pointer to an instance of the IEventHandler interface.

The IEventHandler interface is simply this:

class IEventHandler {
  public:
    virtual void handleEvent(AceButton* button, uint8_t eventType,
        uint8_t buttonState) = 0;
};

At least one of ButtonConfig::setEventHandler() or ButtonConfig::setIEventHandler() must be called before events are actually dispatched. If both are called, the last one takes precedence.

See examples/SingleButtonUsingIEventHandler for an example.

Distinguishing Clicked and DoubleClicked

On a project using only a small number of buttons (due to physical limits or the limited availability of pins), it may be desirable to distinguish between a single Clicked event and a DoubleClicked event from a single button. This is a challenging problem to solve because fundamentally, a DoubleClicked event must always generate a Clicked event, because a Clicked event must happen before it can become a DoubleClicked event.

Notice that on a desktop computer (running Windows, MacOS or Linux), a double-click on a mouse always generates both a Clicked and a DoubleClicked. The first Click selects the given desktop object (e.g. an icon or a window), and the DoubleClick performs some action on the selected object (e.g. open the icon, or resize the window).

The AceButton Library provides 3 solutions which may work for some projects:

Method 1: The kFeatureSuppressClickBeforeDoubleClick flag causes the first Clicked event to be detected, but the posting of the event message (i.e. the call to the EventHandler) is postponed until the state of the DoubleClicked can be determined. If the DoubleClicked happens, then the first Clicked event message is suppressed. If DoubleClicked does not occur, the long delayed Clicked message is sent via the EventHandler.

There are two noticeable disadvantages of this method. First, the response time of all Clicked events is delayed by about 600 ms (kClickDelay + kDoubleClickDelay) whether or not the DoubleClicked event happens. Second, the user may not be able to accurately produce a Clicked event (due to the physical characteristics of the button, or the user's dexterity).

It may also be worth noting that only the Clicked event is postponed. The accompanying Released event of the Clicked event is not postponed. So a single click action (without a DoubleClick) produces the following sequence of events to the EventHandler:

  1. kEventPressed - at time 0ms
  2. kEventReleased - at time 200ms
  3. kEventClicked - at time 600ms (200ms + 400ms)

The ButtonConfig configuration looks like this:

ButtonConfig* buttonConfig = button.getButtonConfig();
buttonConfig->setFeature(ButtonConfig::kFeatureDoubleClick);
buttonConfig->setFeature(
    ButtonConfig::kFeatureSuppressClickBeforeDoubleClick);

See the example code at ClickVersusDoubleClickUsingSuppression.

Method 2: A viable alternative is to use the Released event instead of the Clicked event to distinguish it from the DoubleClicked. For this method to work, we need to suppress the Released event after both Clicked and DoubleClicked.

The advantage of using this method is that there is no response time lag in the handling of the Released event. To the user, there is almost no difference between triggering on the Released event, versus triggering on the Clicked event.

The disadvantage of this method is that the Clicked event must be be ignored (because of the spurious Clicked event generated by the DoubleClicked). If the user accidentally presses and releases the button too quickly, it generates a Clicked event, which will cause the program to do nothing.

The ButtonConfig configuration looks like this:

ButtonConfig* buttonConfig = button.getButtonConfig();
buttonConfig->setEventHandler(handleEvent);
buttonConfig->setFeature(ButtonConfig::kFeatureDoubleClick);
buttonConfig->setFeature(ButtonConfig::kFeatureSuppressAfterClick);
buttonConfig->setFeature(ButtonConfig::kFeatureSuppressAfterDoubleClick);

See the example code at ClickVersusDoubleClickUsingReleased.

Method 3: We could actually combine both Methods 1 and 2 so that either Released or a delayed Click is considered to be a "Click". This may be the best of both worlds.

The ButtonConfig configuration looks like this:

ButtonConfig* buttonConfig = button.getButtonConfig();
buttonConfig->setEventHandler(handleEvent);
buttonConfig->setFeature(ButtonConfig::kFeatureDoubleClick);
buttonConfig->setFeature(
    ButtonConfig::kFeatureSuppressClickBeforeDoubleClick);
buttonConfig->setFeature(ButtonConfig::kFeatureSuppressAfterClick);
buttonConfig->setFeature(ButtonConfig::kFeatureSuppressAfterDoubleClick);

See the example code at examples/ClickVersusDoubleClickUsingBoth/.

Distinguishing Pressed and LongPressed

Sometimes it is useful to capture both a Pressed event and a LongPressed event from a single button. Since every button press always triggers a kEventPressed event, the only reasonable way to distinguish between Pressed and LongPressed is to use the kEventReleased as a substitute for the simple Pressed event. When we activate kFeatureLongPress, we then must activate the kFeatureSuppressAfterLongPress feature to suppress the kEventReleased event after the kEventLongPressed to avoid yet another overlap of events.

ButtonConfig* config = button.getButtonConfig();
config->setFeature(ButtonConfig::kFeatureLongPress);
config->setFeature(ButtonConfig::kFeatureSuppressAfterLongPress);

This works most of the time, but I encountered an edge case. Occasionally we want to capture the Released event after the LongPressed event, even if kEventReleased must be suppressed as described above. To solve this edge case, in v1.8, I added a new event type kEventLongReleased which is triggered as a substitute for kEventReleased, only if kFeatureSuppressAfterLongPress is used to suppress kEventReleased.

See the example code at examples/PressVersusLongPress to see how all these come together.

Events After Reboot

A number of edge cases occur when the microcontroller is rebooted:

  • if the button is held down, should the Pressed event be triggered?
  • if the button is in its natural Released state, should the Released event happen?
  • if the button is Pressed down, and ButtonConfig is configured to support RepeatPress events, should the kEventRepeatPressed events be triggered initially?

I think most users would expect that in all these cases, the answer is no, the microcontroller should not trigger an event until the button undergoes a human-initiated change in state. The AceButton library implements this logic. (It might be useful to make this configurable using a ButtonConfig feature flag but that is not implemented.)

On the other hand, it is sometimes useful to perform some special action if a button is pressed while the device is rebooted. To support this use-case, call the AceButton::isPressedRaw() in the global setup() method (after the button is configured). It will directly call the digitalRead() method associated with the button pin and return true if the button is in the Pressed state.

Orphaned Clicks

When a Clicked event is generated, the AceButton class looks for a second Clicked event within a certain time delay (default 400 ms) to determine if the second Clicked event is actually a DoubleClicked event.

All internal timestamps in AceButton are stored as uint16_t (i.e. an unsigned integer of 16 bits) in millisecond units. A 16-bit unsigned counter rolls over after 65536 iterations. Therefore, if the second Clicked event happens between (65.636 seconds, 66.036 seconds) after the first Clicked event, a naive-logic would erroneously consider the (long-delayed) second click as a double-click.

The AceButton contains code that prevents this from happening.

Note that even if the AceButton class uses an unsigned long type (a 32-bit integer on the Arduino), the overflow problem would still occur after 2^32 milliseconds (i.e. 49.7 days). To be strictly correct, the AceButton class would still need logic to take care of orphaned Clicked events.

Binary Encoded Buttons

Instead of allocating one pin for each button, we can use Binary Encoding to support large number of buttons with only a few pins. The circuit can be implemented using a 74LS148 chip, or simple diodes like this:

8 To 3 Encoding

Three subclasses of ButtonConfig are provided to handle binary encoded buttons:

  • Encoded4To2ButtonConfig: 3 buttons with 2 pins
  • Encoded8To3ButtonConfig: 7 buttons with 3 pins
  • EncodedButtonConfig: M=2^N-1 buttons with N pins

See docs/binary_encoding/README.md for information on how to use these classes.

Resistor Ladder Buttons

It is possible to attach 1-8 (maybe more) buttons on a single analog pin through a resistor ladder, and use the analogRead() to read the different voltages generated by each button. An example circuit looks like this:

Parallel Resistor Ladder

The LadderButtonConfig class handles this configuration.

See docs/resistor_ladder/README.md for information on how to use this class.

Dynamic Allocation on the Heap

All classes in this library were originally designed to be created statically at startup time and never deleted during the lifetime of the application. Since they were never meant to be deleted through the pointer, I did not include the virtual destructor for polymorphic classes (i.e. ButtonConfig and its subclasses). The AceButton class is not polymorphic and does not need a virtual destructor.

Most 8-bit processors have limited flash and static memory (for example, 32 kB flash and 2 KB static for the Nano or UNO). Adding a virtual destructor causes 600 additional bytes of flash memory to be consumed. I suspect this is due to the virtual destructor pulling the malloc() and free() functions which are needed to implement the new and delete operators. For a library that consumes only about 1200 bytes on an 8-bit processor, this increase in flash memory size did not seem acceptable.

For 32-bit processors (e.g. ESP8266, ESP32) which have far more flash memory (e.g. 1 MB) and static memory (e.g. 80 kB), it seems reasonable to allow AceButton and ButtonConfig to be created and deleted from the heap. (See Issue #46 for the motivation.) Testing shows that the virtual destructor adds only about 60-120 bytes of flash memory for these microcontrollers, probably because the malloc() and free() functions are already pulled in by something else. The 60-120 bytes of additional consumption seems trivial compared to the range of ~256 kB to ~4 MB flash memory available on these 32-bit processors.

Therefore, I added a virtual destructor for the ButtonConfig class (v1.5) and enabled it for all architectures other than ARDUINO_ARCH_AVR (v1.6.1). This prevents 8-bit processors with limited memory from suffering the overhead of an extra 600 bytes of flash memory usage.

Even for 32-bit processors, I still recommend avoiding the creation and deletion of objects from the heap, to avoid the risk of heap fragmentation. If a variable number of buttons is needed, it might be possible to design the application so that all buttons which will ever be needed are predefined in a global pool. Even if some of the AceButton and ButtonConfig instances are unused, the overhead is probably smaller than the overhead of wasted space due to heap fragmentation.

Digital Write Fast

The digitalWriteFast libraries provide smaller and faster alternative versions the digitalWrite(), digitalRead(), and pinMode() functions. I have used 2 of the following libraries, but there probably others:

These libraries provide the following functions: digitalWriteFast(), digitalReadFast(), and pinModeFast() which are usually valid only AVR processors. These alternative functions depend on the pin number and value to be compile-time constants, bypassing the pin number lookup tables used by the standard versions. These fast versions can be 20-50X faster. More importantly in many situations, they can save 100-500 bytes of flash memory by not pulling in the pin number lookup tables.

I created 3 alternative versions of ButtonConfig which use the digitalWriteFast libraries:

(If ButtonConfigFast4.h is needed, it is easy to copy ButtonConfigFast3.h and create a 4-pin version.)

These classes use C++ templates on the pin numbers, so that they can be passed to the digitalReadFast() functions as compile-time constants. Because they depend on an external digitalWriteFast library, they are not included in the <AceButton.h> header file. They must be included explicitly, as shown below:

#include <Arduino.h>
#include <AceButton.h>
#include <digitalWriteFast.h>
#include <ace_button/fast/ButtonConfigFast2.h>

using namespace ace_button;

// Physical pin numbers attached to the buttons.
const uint8_t BUTTON1_PHYSICAL_PIN = 2;
const uint8_t BUTTON2_PHYSICAL_PIN = 3;

// Virtual pin numbers attached to the buttons.
const uint8_t BUTTON1_PIN = 0;
const uint8_t BUTTON2_PIN = 1;

ButtonConfigFast2<BUTTON1_PHYSICAL_PIN, BUTTON2_PHYSICAL_PIN> buttonConfig;
AceButton button1(&buttonConfig, BUTTON1_PIN);
AceButton button2(&buttonConfig, BUTTON2_PIN);

void handleEvent(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  ...
}

void setup() {
  ...
}

void loop() {
  // Should be called every 4-5ms or faster, for the default debouncing time
  // of ~20ms.
  button1.check();
  button2.check();
}

Each physical pin number given as template arguments to the ButtonConfigFast2<> class corresponds to a virtual pin number (starting with 0) assigned to the AceButton object. Within the event handler, everything is referenced by the virtual pin number, just like the EncodedButtonConfig and LadderButtonConfig classes.

Here are the example programs for each ButtonConfigFast{N} class:

The LadderButtonConfig class uses analogRead() which does not seem to directly benefit from digitalWriteFast libraries. However, if you use pinModeFast() instead of pinMode() in your global setup() function, you can save about 50 bytes of flash (I think).

The Encoded4To2ButtonConfig and Encoded8To3ButtonConfig classes would probably benefit from digitalWriteFast libraries, but I have not created the "fast" versions of these (i.e. Encoded4To2ButtonConfigFast and Encoded8To3ButtonConfigFast) because I have not needed them personally.

The general EncodedButtonConfig class is more difficult to convert into a "fast" version, because its constructor takes a pointer argument to an array of physical pins. These are not compile-time constants so we would not be able to use the digitalWriteFast libraries directly. I think the best we could do is create special Encoded16To4ButtonConfigFast and Encoded32To5ButtonConfigFast classes.

Heart Beat Event

Version 1.10 added the kEventHeartBeat event. By default it is disabled. It can be enabled using the kFeatureHeartBeat flag:

ButtonConfig* config = button.getButtonConfig();
config->setFeature(ButtonConfig::kFeatureHeartBeat);

When enabled, the AceButton object sends a kEventHeartBeat event at a periodic interval, with the number of milliseconds managed by the following methods on the ButtonConfig object:

  • void setHeartBeatInterval(uint16_t interval)
  • uint16_t getHeartBeatInterval() const

The default is 5000 milliseconds.

The primary purpose of the HeartBeat event is to allow the user-provided event handler (IEventHandler will likely be easiest for this purpose) to generate custom event types which are not provided by AceButton itself by default. When the button does not undergo any change in state explicitly initiated by the user (e.g. Released for a long time), the AceButton object will not trigger any events normally. By activating the kFeatureHeartBeat, the event handler can generate custom events such as "Pressed for 5 minutes", or "Released for 5 Minutes". See examples/HeartBeat for an example of an IEventHandler that implements this.

The kEventHeartBeat is triggered only by the progression of time, and is not affected by any internal state of the AceButton, such as the debouncing state, or the various logic for detecting Clicked, DoubleClicked, and so on. The HeartBeatInterval is intended to be relatively large, with the default set to 5000 milliseconds, to avoid the overhead of calling the event handler too often. Using a smaller interval may affect the detection logic of various other button events if the HeartBeat handler consumes too much CPU time.

The buttonState is passed to the event handler by the HeartBeat dispatcher, through the callback function or IEventHandler interface, for example:

typedef void (*EventHandler)(AceButton* button, uint8_t eventType,
    uint8_t buttonState);

This button state will be the last known, debounced and validated state. It will not be the current button state. This is because the HeartBeat detector operates independently of the debouncing logic, and it did not seem appropriate for the unvalidated buttonState to be passed to the event handler just because the timer for the HeartBeat triggered in the middle of the debouncing logic.

Resource Consumption

SizeOf Classes

Here are the sizes of the various classes on the 8-bit AVR microcontrollers (Arduino Uno, Nano, etc):

sizeof(AceButton): 17
sizeof(ButtonConfig): 20
sizeof(ButtonConfigFast1<>): 20
sizeof(ButtonConfigFast2<>): 20
sizeof(ButtonConfigFast3<>): 20
sizeof(Encoded4To2ButtonConfig): 23
sizeof(Encoded8To3ButtonConfig): 24
sizeof(EncodedButtonConfig): 27
sizeof(LadderButtonConfig): 28

For 32-bit microcontrollers:

sizeof(AceButton): 20
sizeof(ButtonConfig): 24
sizeof(Encoded4To2ButtonConfig): 28
sizeof(Encoded8To3ButtonConfig): 28
sizeof(EncodedButtonConfig): 36
sizeof(LadderButtonConfig): 36

(An early version of AceButton, with only half of the functionality, consumed 40 bytes. It got down to 11 bytes before additional functionality increased it to its current 17.)

Flash And Static Memory

MemoryBenchmark was used to determine the size of the library for various microcontrollers (Arduino Nano to ESP32). Here are 2 samples:

Arduino Nano

+--------------------------------------------------------------+
| functionality                   |  flash/  ram |       delta |
|---------------------------------+--------------+-------------|
| Baseline                        |    462/   11 |     0/    0 |
| Baseline+pinMode+digitalRead    |    766/   11 |   304/    0 |
|---------------------------------+--------------+-------------|
| ButtonConfig                    |   1970/   56 |  1508/   45 |
| ButtonConfigFast1               |   1686/   56 |  1224/   45 |
| ButtonConfigFast2               |   1586/   73 |  1124/   62 |
| ButtonConfigFast3               |   1628/   90 |  1166/   79 |
|---------------------------------+--------------+-------------|
| Encoded4To2ButtonConfig         |   2098/   93 |  1636/   82 |
| Encoded8To3ButtonConfig         |   2318/  162 |  1856/  151 |
| EncodedButtonConfig             |   2362/  185 |  1900/  174 |
|---------------------------------+--------------+-------------|
| LadderButtonConfig              |   2360/  198 |  1898/  187 |
+--------------------------------------------------------------+

ESP8266:

+--------------------------------------------------------------+
| functionality                   |  flash/  ram |       delta |
|---------------------------------+--------------+-------------|
| Baseline                        | 260105/27892 |     0/    0 |
| Baseline+pinMode+digitalRead    | 260201/27892 |    96/    0 |
|---------------------------------+--------------+-------------|
| ButtonConfig                    | 261589/27944 |  1484/   52 |
|---------------------------------+--------------+-------------|
| Encoded4To2ButtonConfig         | 261741/27984 |  1636/   92 |
| Encoded8To3ButtonConfig         | 261885/28064 |  1780/  172 |
| EncodedButtonConfig             | 262013/28104 |  1908/  212 |
|---------------------------------+--------------+-------------|
| LadderButtonConfig              | 262057/28116 |  1952/  224 |
+--------------------------------------------------------------+

CPU Cycles

The profiling numbers for AceButton::check(), EncodedButtonConfig::checkButtons(), and LadderButtonConfig::checkButtons() can be found in examples/AutoBenchmark. Here are 2 samples, in units of microseconds.

Arduino Nano:

+---------------------------+-------------+---------+
| Button Event              | min/avg/max | samples |
|---------------------------+-------------+---------|
| idle                      |  12/ 16/ 24 |    1929 |
| press/release             |  12/ 17/ 28 |    1924 |
| click                     |  12/ 16/ 28 |    1925 |
| double_click              |  12/ 16/ 32 |    1922 |
| long_press/repeat_press   |  12/ 18/ 28 |    1923 |
|---------------------------+-------------+---------|
| ButtonConfigFast1         |  12/ 16/ 24 |    1932 |
| ButtonConfigFast2         |  20/ 30/ 40 |    1905 |
| ButtonConfigFast3         |  32/ 44/ 52 |    1880 |
|---------------------------+-------------+---------|
| Encoded4To2ButtonConfig   |  60/ 73/ 80 |    1831 |
| Encoded8To3ButtonConfig   | 168/196/204 |    1645 |
| EncodedButtonConfig       |  84/110/116 |    1769 |
| LadderButtonConfig        | 184/211/288 |    1625 |
+---------------------------+-------------+---------+

ESP8266:

+---------------------------+-------------+---------+
| Button Event              | min/avg/max | samples |
|---------------------------+-------------+---------|
| idle                      |   6/  8/ 62 |    1920 |
| press/release             |   6/  8/ 45 |    1921 |
| click                     |   6/  7/ 18 |    1921 |
| double_click              |   6/  7/ 12 |    1922 |
| long_press/repeat_press   |   6/  8/ 12 |    1920 |
|---------------------------+-------------+---------|
| Encoded4To2ButtonConfig   |  22/ 27/ 46 |    1879 |
| Encoded8To3ButtonConfig   |  56/ 67/ 76 |    1810 |
| EncodedButtonConfig       |  43/ 54/ 70 |    1841 |
| LadderButtonConfig        |  81/ 93/212 |    1772 |
+---------------------------+-------------+---------+

System Requirements

Hardware

Tier 1: Fully Supported

These boards are tested on each release:

  • Arduino Nano (16 MHz ATmega328P)
  • SparkFun Pro Micro (16 MHz ATmega32U4)
  • Seeeduino XIAO M0 (SAMD21, 48 MHz ARM Cortex-M0+)
  • STM32 Blue Pill (STM32F103C8, 72 MHz ARM Cortex-M3)
  • Adafruit ItsyBitsy M4 (SAMD51, 120 MHz ARM Cortext-M4)
  • NodeMCU 1.0 (ESP-12E module, 80MHz ESP8266)
  • WeMos D1 Mini (ESP-12E module, 80 MHz ESP8266)
  • ESP32 Dev Module (ESP-WROOM-32 module, 240MHz dual core Tensilica LX6)

Tier 2: Should work

These boards should work but I don't test them as often:

  • ATtiny85 (8 MHz ATtiny85)
  • Arduino Pro Mini (16 MHz ATmega328P)
  • Mini Mega 2560 (Arduino Mega 2560 compatible, 16 MHz ATmega2560)
  • Teensy LC (48 MHz ARM Cortex-M0+)
  • Teensy 3.2 (96 MHz ARM Cortex-M4)

Tier 3: May work, but not supported

  • Any platform using the ArduinoCore-API (https://github.com/arduino/ArduinoCore-api).
    • For example, Nano Every, MKRZero, and Raspberry Pi Pico RP2040.
    • Most of my libraries do not work on platforms using the ArduinoCore-API.
    • However AceButton is simple enough that it may still work on these boards.

Tool Chain

This library was developed and tested using:

It should work with PlatformIO but I have not tested it.

The library works on Linux or MacOS (using both g++ and clang++ compilers) using the EpoxyDuino emulation layer.

Operating System

I use Ubuntu Linux 22.04 or its variants (i.e. Linux Mint) for most of my development.

Background Motivation

There are numerous "button" libraries out there for the Arduino. Why write another one? I wanted to add a button to an addressable strip LED controller, which was being refreshed at 120 Hz. I had a number of requirements:

  • the button needed to support a LongPress event, in addition to the simple Press and Release events
  • the button code must not interfere with the LED refresh code which was updating the LEDs at 120 Hz
  • well-tested, I didn't want to be hunting down random and obscure bugs

Since the LED refresh code needed to run while the button code was waiting for a "LongPress" delay, it seemed that the cleanest API for a button library would use an event handler callback mechanism. This reduced the number of candidate libraries to a handful. Of these, only a few of them supported a LongPress event. I did not find the remaining ones flexible enough for my button needs in the future. Finally, I knew that it was tricky to write correct code for debouncing and detecting various events (e.g. DoubleClick, LongPress, RepeatPress). I looked for a library that contained unit tests, and I found none.

I decided to write my own and use the opportunity to learn how to create and publish an Arduino library.

Non-goals

An Arduino UNO or Nano has 16 times more flash memory (32KB) than static memory (2KB), so the library is optimized to minimize the static memory usage. The AceButton library is not optimized to create a small program size (i.e. flash memory), or for small CPU cycles (i.e. high execution speed). I assumed that if you are seriously optimizing for program size or CPU cycles, you will probably want to write everything yourself from scratch.

That said, examples/MemoryBenchmark shows that the library consumes between 970-2180 bytes of flash memory, and AutoBenchmark shows that AceButton::check() takes between ~15 microseconds on a 16MHz ATmega328P chip and 2-3 microseconds on an ESP32. Hopefully that is small enough and fast enough for the vast majority of people.

With v1.9, I started using AceButton on an ATtiny85 which has only 8kB of flash and 0.5 kB of static RAM. Flash memory consumption became more important and I created the ButtonConfigFast1, ButtonConfigFast2, and ButtonConfigFast3 classes to decrease the flash memory consumption by using one of the <digitalWriteFast.h> 3rd party libraries. See Digital Write Fast for more info.

Bugs and Limitations

This is the first Arduino library that I ever created. It has grown organically over time, while maintaining backwards compatibility as much as possible. There are some early design decisions that could have been better with infinite insight. Here are some limitations and bugs.

  • The ButtonConfig class should have been named ButtonGroup.
    • This was not obvious until I had implemented the EncodedButtonConfig and the LadderButtonConfig classes.
    • But I cannot change the names without breaking backwards compatibility.
  • The Single Button Simplifications was probably a mistake.
    • It makes the API more complex and cluttered than it could be.
    • It causes an automatic creation of the SystemButtonConfig instance of ButtonConfig even when it is not needed by the application, unless special (non-obvious) precautions are taken.
    • On the other hand, it makes the simplest HelloButton program very simple.
  • Embedding multiple AceButton objects in an array or a struct is difficult if not impossible.
    • See Discussion#106 for some details.
    • The C++ rules for object initialization are so complicated, and have changed with different versions of C++ (C++11, C++14, C++17, C++20, C++23??), that I cannot understand them anymore.
    • Probably will never be fixed because I'm tired of the complexity of the C++ language.
  • AceButton does not provide built-in support for simultaneous buttons.
    • See Discussion#83, Discussion#94, and Discussion#96.
    • The examples/SimultaneousButtons program shows how it could be detected using a custom IEventHandler. However, it has not been extensively tested. I don't even remember writing it 2 years ago.
    • This remains an open problem because I don't use simultaneous buttons in my applications, and I have not spent much time thinking about how to handle all the combinations of events and their timing interactions that are possible with 2 buttons.
  • The EventHandler and IEventHandler send an AceButton* pointer into the arguments, instead of a const AceButton* pointer.
    • Too late to change that without breaking backwards compatibility.
  • All internal timing variables are uint16_t instead of uint32_t.
    • This means that various timing parameters (ClickDelay, LongPressDelay, etc) can be maximum of 65535 milliseconds.
    • Using 16-bit integers saves RAM, at least 8 bytes for each instance of AceButton, and 14 bytes for ButtonConfig and its subclasses. And probably saves flash memory on 8-bit processors because fewer machine instruction are needed to operate on 16-bit variables compared to 32-bit variables.
    • Client applications were assumed to use as many as 10-20 buttons. That's a savings of 80-160 bytes which makes a difference on 8-bit AVR processors with only 2 kB of RAM.
  • The library does not support the mirror-image version of kEventLongPressed for the Released state.
    • In other words, an event that is triggered when a button has been released for a long time (several seconds).
    • The current kEventLongReleased is a different type of event.
    • See Discussion#118 for info.
    • An alternative solution might be to use the kEventHeartBeat and a custom IEventHandler to generate a custom event. See examples/HeartBeat for a example.
  • The Event Supression features are complicated, hard to understand and remember.
    • I wrote the code and I cannot remember how they all work. I have to refer to this README.md each time I want to use them.
    • These features grew organically. Maybe there is a better, more consistent way of implementing them, but it was not obvious during the development of these features.

License

I changed to the MIT License starting with version 1.1 because the MIT License is so simple to understand. I could not be sure that I understood what the Apache License 2.0 meant.

Feedback and Support

If you have any questions, comments, or feature requests for this library, please use the GitHub Discussions for this project. If you have a bug report, please file a ticket in GitHub Issues. Feature requests should go into Discussions first because they often have alternative solutions which are useful to remain visible, instead of disappearing from the default view of the Issue tracker after the ticket is closed.

Please refrain from emailing me directly unless the content is sensitive. The problem with email is that I cannot reference the email conversation when other people ask similar questions later.

Author

Created by Brian T. Park ([email protected]).

acebutton's People

Contributors

bxparks avatar salvagione avatar thijstriemstra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

acebutton's Issues

Remote inputs

Hi, In my sketch I want to be able to receive input information from remote inputs

I still don't know how to achieve the communication, if RS485 or other method, but let's consider it as a Port expander, is this library able to work?

Multiple Buttons

Hi there, just a quick question. Do you have a good example using AceButton for multiple independent buttons that are floating that go low when pressed. All are connected to individual pins on a teensy.

I've tried a number of the examples but they dont seem to work as described.

Thanks

[Question] How to pass object method as an event handler?

Hi there!

Many thanks for this library! Feature-rich, great OOP design, and docs are awesome!

I'm trying to isolate all buttons' logic inside another "Facade" class (FrontPanel in my case). All buttons and their configs are instantiated as class properties. I have several private methods for handling buttons' events, with proper signature (as recommended for plain global functions here).

The question is: how to pass this private method as an event handler?

#include <AceButton.h>

using namespace ace_button;

class FrontPanel
{
  public:
    void init();

  private:
    ButtonConfig shiftButtonConfig;
    AceButton shiftButton;

    void handleShiftButtonEvent(AceButton* /* button */, uint8_t /* eventType */, uint8_t /* buttonState */);

    void initButtons();
    void onShiftButtonPressed();
    void onShiftButtonReleased();
};


// Event handler for ace_button::ButtonConfig
void FrontPanel::handleShiftButtonEvent(AceButton* /* button */, uint8_t eventType, uint8_t /* buttonState */) {
    switch (eventType) {
      case AceButton::kEventPressed:
        onShiftButtonPressed();
        break;

      case AceButton::kEventReleased:
        onShiftButtonReleased();
        break;
    }
}

void FrontPanel::initButtons() {
  // How to pass object method as an event handler here???
  shiftButtonConfig.setEventHandler(handleShiftButtonEvent);
}

GPIO pin 0 cannot be used?

Trying to use AceButton on pin 0 gives me this error:

exit status 1
call of overloaded 'AceButton(int)' is ambiguous

All other pins are okay, except pin 0. What can be done about this?

[OT] featuring AceButton

hi Brian

I have no other means to get in touch with you (no social media or contacts are in your profile), so here is the only place I can post a message to you.
I wanted to let you know I have used AceButton in our content update video on Arduino CLI 0.14.0
https://www.youtube.com/watch?v=vmTYh9jDoQ8

you can remove this issue now :)
regards,

ubi [ Arduino Tooling Team ]

Fetching button-state after reboot

Hey,
first I'd like to thank you for this wonderful piece of code.

In some places (doc, sourcecode) you mention the intention of not trigger on events that already occured before the button is created. What would be your proposal for getting these events, e.g. for behaviour like
"if you press the Start button and switch the device on, you will get into a special configuration menu"

Currently I do it the following way:

void setup() {
...
if (!once_after_reboot)
{
    if (button.getLastButtonState()) 
    { 
       showSpecialSetup();
    }
    once_after_reboot = true;
}
...
}

ESP32 Support?

I'm wanting to use this lib with an ESP32, but I'm not using the Arduino IDE or Arduino library. Will it still work?

Interrupts

How can I use interrupts with this libruary, like in EasyButton?

Possible conflict with TC3 library on SAMD21 architecture ?

Hi,
I'm trying to work with the DAC and your library on seeduino XIAO controller.
Events are not handled when uncommenting these lines, but work when commented.

    TIMER.initialize(TIMER_TIME);
    TIMER.attachInterrupt(GenerateAudioStream);
    pinMode(DAC_PIN, OUTPUT);
    analogReadResolution(5);
    analogWriteResolution(8);

Full code

#include <Arduino.h>
#include <AceButton.h>
using namespace ace_button;

#include <TimerTC3.h>
#define USE_TIMER_TC3  // use TimerTc3
#define TIMER_TIME 125 // 1/8000 Hz = 125 micro seconds
#define TIMER TimerTc3
#define DAC_PIN 0 // pin of the XIAO DAC

#define PIN_ANALOG_1 A1

// Create 4 AceButton objects, with their virtual pin number 0 to 3. The number
// of buttons does not need to be identical to the number of analog levels. You
// can choose to define only a subset of buttons here.
//
// Use the 4-parameter `AceButton()` constructor with the `buttonConfig`
// parameter explicitly to `nullptr` to prevent the automatic creation of the
// default SystemButtonConfig, saving about 30 bytes of flash and 26 bytes of
// RAM on an AVR processor.
static const uint8_t NUM_BUTTONS = 7;
static AceButton b0(nullptr, 0);
static AceButton b1(nullptr, 1);
static AceButton b2(nullptr, 2);
static AceButton b3(nullptr, 3);
static AceButton b4(nullptr, 4);
static AceButton b5(nullptr, 5);
static AceButton b6(nullptr, 6);
static AceButton *const BUTTONS[NUM_BUTTONS] = {
    &b0, &b1, &b2, &b3, &b4, &b5, &b6};

// Define the ADC voltage levels for each button.
// For 4 buttons, we need 5 levels.
static const uint8_t NUM_LEVELS = NUM_BUTTONS + 1;
static const uint16_t LEVELS[NUM_LEVELS] = {
    0,
    132,
    264,
    363,
    429,
    528,
    594,
    1023};

// The LadderButtonConfig constructor binds the AceButton to the
// LadderButtonConfig.
static LadderButtonConfig buttonConfig(
    PIN_ANALOG_1, NUM_LEVELS, LEVELS, NUM_BUTTONS, BUTTONS);

// The event handler for the button.
void handleEvent(AceButton *button, uint8_t eventType, uint8_t buttonState)
{
    // Print out a message for all events.
    Serial.print(F("handleEvent(): "));
    Serial.print(F("virtualPin: "));
    Serial.print(button->getPin());
    Serial.print(F("; eventType: "));
    Serial.print(eventType);
    Serial.print(F("; buttonState: "));
    Serial.println(buttonState);
}

// The buttonConfig.checkButtons() should be called every 4-5ms or faster, if
// the debouncing time is ~20ms. On ESP8266, analogRead() must be called *no*
// faster than 4-5ms to avoid a bug which disconnects the WiFi connection.
void checkButtons()
{
    static unsigned long prev = millis();

    // DO NOT USE delay(5) to do this.
    unsigned long now = millis();
    if (now - prev > 5)
    {
        buttonConfig.checkButtons();
        prev = now;
    }
}

void GenerateAudioStream()
{
    static int t = 0;
    t++;

    analogWrite(DAC_PIN, (uint32_t)t / 16);
}

void compareTinyExprAndNativeEvaluation();

void setup()
{
    Serial.begin(115200);
    while (!Serial)
        ;

    pinMode(PIN_ANALOG_1, INPUT);
    interrupts();
    // Configure the ButtonConfig with the event handler, and enable all higher
    // level events.
    buttonConfig.setEventHandler(handleEvent);
    buttonConfig.setFeature(ButtonConfig::kFeatureClick);
    buttonConfig.setFeature(ButtonConfig::kFeatureDoubleClick);
    buttonConfig.setFeature(ButtonConfig::kFeatureLongPress);
    buttonConfig.setFeature(ButtonConfig::kFeatureRepeatPress);

    TIMER.initialize(TIMER_TIME);
    TIMER.attachInterrupt(GenerateAudioStream);
    pinMode(DAC_PIN, OUTPUT);
    analogReadResolution(5);
    analogWriteResolution(8);
}

void loop()
{
    checkButtons();
}

More over, when trying to do analogRead without the library and the DAC, the analogRead works

Analog Pins without GPIO

Hi,

I had some trouble with one of the multiple buttons of my project not working. It took me a while until I found out why. I am using almost all PINs on my Arduino Nano and ended up using A6, which does not have the GPIO functionality. It would be nice if this issue and possibly the fix were mentioned somewhere in the README.md.
I fixed it with adding a pull-up resistor (10k) and replacing following code in ButtonConfig.h:

    virtual int readButton(uint8_t pin) {
      if (pin == A6 || pin == A7)
        return (analogRead(pin) >= 512 ? HIGH : LOW);
      return digitalRead(pin);
    }

It works reliably for me, even with the standard setup pinMode(A6, INPUT_PULLUP);. So with an external pull-up, the above code is the only change needed and those pins behave like any other. Are there any issues with this solution? Except of course you have to match the pins to your hardware if you do not use a Nano.

Regards,
Ben

How to make AceButton work with multiplexed wiring scheme

Hi
I have 3 GPIO pins available but want to use 5 buttons using diode multiplexing. Pressing button 1-5 will pull pin 1-3 to ground following this order:
Button 1 - pin 1
Button 2 - pin 2
Button 3 - pin 3
Button 4 - pin 1 + 2
Button 5 - pin 2 + 3
Will this work with AceButton ? If yes can you provide some example code if possible ?
Thanks

call of overloaded 'init(int)' is ambiguous

This did work OK in v1.3.5, fails after upgrading to v1.8.0 with call of overloaded 'init(int)' is ambiguous compiler error:

#define PIN 0
#include <AceButton.h>
using namespace ace_button;
void handleButton(AceButton* /*button*/, uint8_t eventType, uint8_t /*buttonState*/); // forward declaration
ButtonConfig buttonConfig;
AceButton button(&buttonConfig);

void setup()
{
    buttonConfig.setEventHandler(handleButton);
    buttonConfig.setFeature(ButtonConfig::kFeatureClick);
    tlacitkoConfig.setFeature(ButtonConfig::kFeatureDoubleClick);
    tlacitkoConfig.setFeature(ButtonConfig::kFeatureLongPress);
    button.init(PIN);

kEventClicked comes always with kEventDoubleClicked

Hi, I see an issue here when testing the library. I registered all events (just following/modifying your examples) as I'm looking for a library that can easily distinguish between single and double clicks, which is not that easy to find. I can easily send out a click on the button and I get the kEventClicked as expected. But when I do a double click I always get first the kEventClicked which is then immeditely followed by the kEventDoubleClicked. This is a nogo for me as this is not presize enough.
In other words a kEventClicked is only allowed to be fired, when there is no kEventDoubleClicked following. I do not know, whether this is already today possible, but I did not get it managed to behave like that.
Simple test is, turn your LED on with one click and turn it off with double click. You will recognize, when it's off and you double click again, it will briefly flash, right after the first click. As I want to switch a relais, this is not good enough for me. Any suggestions?

Button presses not recognized at all or random

AceButton version: 1.3.2
Arduino IDE version: 1.8.5
Arduino: Arduino Micro (A clone, not the original one. The IDE recognizes the Arduino as an "Arduino Leonardo". )

The click and long press events aren't triggered at all no matter if I press the button for only a few milliseconds or for a long time. Sometimes instead of not reacting at all it just thinks it is constantly receiving presses. It works just fine and as expected when I simply use digitalRead instead.

I minimized the code to a minimum and even took the button and anything else connected to the Arduino out of the equation, I only used a wire to "short" GND to a digital pin (I tried many different pins including 2, 3, 4 and 9). Just to make sure I opened up another brand new Arduino Micro clone, but I had the exact same issues. I also tried instantiating with AceButton leftPedal(LEFT_PEDAL, HIGH); and AceButton leftPedal(LEFT_PEDAL, LOW);. But still it didn't behave correctly. (I'm not using an external pull-down resistor btw, just a wire.)

This works perfectly fine:

const int buttonPin = 2;
int buttonState = 0;

void setup() {
  delay(1000);
  Serial.begin(115200);
  while (! Serial); // Wait until Serial is ready - Leonardo/Micro
  Serial.println(F("setup(): begin"));
  digitalWrite(buttonPin, HIGH);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    Serial.println(F("HIGH"));
  } else {
    Serial.println(F("LOW"));
  }
}

This code misbehaves as described above:

#include <AceButton.h>
using namespace ace_button;

//uint16_t clickDelay = 100;
//uint16_t debounceDelay = 50;
//uint16_t repeatPressDelay = 500;
//uint16_t repeatPressInterval = 500;

const int LEFT_PEDAL = 2;

AceButton leftPedal(LEFT_PEDAL);

void scrollDownEvent(AceButton*, uint8_t, uint8_t);

void setup() {
  delay(1000); // some microcontrollers reboot twice
  Serial.begin(115200);
  while (! Serial); // Wait until Serial is ready - Leonardo/Micro
  Serial.println(F("setup(): begin"));

  ButtonConfig* leftPedalConfig = leftPedal.getButtonConfig();
  leftPedalConfig->setEventHandler(scrollDownEvent);
  leftPedalConfig->setFeature(ButtonConfig::kFeatureClick);
  //leftPedalConfig->setFeature(ButtonConfig::kFeatureDoubleClick);
  //leftPedalConfig->setFeature(ButtonConfig::kFeatureLongPress);
  leftPedalConfig->setFeature(ButtonConfig::kFeatureRepeatPress);
  //leftPedalConfig->setDebounceDelay(clickDelay);
  //leftPedalConfig->setDebounceDelay(debounceDelay);
  //leftPedalConfig->setRepeatPressDelay(repeatPressDelay);
  //leftPedalConfig->setRepeatPressInterval(repeatPressInterval);
  
  Serial.println(F("setup(): ready"));
}

void loop() {
  leftPedal.check();
}

void scrollDownEvent(AceButton* /* button */, uint8_t eventType, uint8_t buttonState) {
  switch (eventType) {
    case AceButton::kEventPressed:
      Serial.println(F("kEventPressed: SCROLLDOWN"));
      break;
    case AceButton::kEventRepeatPressed:
      Serial.println(F("kEventRepeatPressed: SCROLLDOWN"));
      break;
  }
}

I spent the last 4 hours desperately trying to get this to work. I would really appreciate it if you could check this out and tell me what the problem is.

Example Request

Hi There,

I love your library and it's interface.

I haven't written C++ in years, and I would love an analog button read example.

Roughly following the capacitive example, I think it has something to do with overriding ace_button::ButtonConfig::readButton. I have attempted it but I'm doing something wrong.

I think I'm doing all of the subclassing properly, but as I said I haven't written C++ in a loooong time.

Definitely feel free to close as won't-fix. Just thought I'd throw it out as something that might be useful to users.

Thanks!

Compiling on Nano Every gives errors

On trying to compile, for example, HelloButton for one of the new Nano Every modules, the Arduino IDE spits out a load of errors (see below). These seem to be related to the testing code in your library. I am using Version 1.5.0 of your AceButton library and Arduino IDE 1.8.13. Every other board type I've tried seems to compile without any issues.

Arduino: 1.8.13 (Windows 10), Board: "Arduino Nano Every, ATMEGA328"

In file included from F:\Arduino\libraries\AceButton\src\ace_button\testing\EventTracker.cpp:2:0:

F:\Arduino\libraries\AceButton\src\ace_button\testing\EventTracker.h:62:18: error: reference to 'Print' is ambiguous

 void printTo(Print& printer) const;

              ^~~~~

F:\Arduino\libraries\AceButton\src\ace_button\testing\EventTracker.h:30:7: note: candidates are: class Print

class Print;

   ^~~~~

In file included from C:\Users\Philip\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.6\cores\arduino/api/deprecated/Print.h:23:0,

             from F:\Arduino\libraries\AceButton\src\ace_button\testing\EventTracker.cpp:1:

c:\users\philip\appdata\local\arduino15\packages\arduino\hardware\megaavr\1.8.6\cores\arduino\api\print.h:34:7: note: class arduino::Print

class Print

   ^~~~~

In file included from F:\Arduino\libraries\AceButton\src\ace_button\testing\EventTracker.cpp:2:0:

F:\Arduino\libraries\AceButton\src\ace_button\testing\EventTracker.h:62:18: error: 'Print' has not been declared

 void printTo(Print& printer) const;

              ^~~~~

F:\Arduino\libraries\AceButton\src\ace_button\testing\EventTracker.h:98:18: error: reference to 'Print' is ambiguous

 void printTo(Print& printer) const;

              ^~~~~

F:\Arduino\libraries\AceButton\src\ace_button\testing\EventTracker.h:30:7: note: candidates are: class Print

class Print;

   ^~~~~

In file included from C:\Users\Philip\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.6\cores\arduino/api/deprecated/Print.h:23:0,

             from F:\Arduino\libraries\AceButton\src\ace_button\testing\EventTracker.cpp:1:

c:\users\philip\appdata\local\arduino15\packages\arduino\hardware\megaavr\1.8.6\cores\arduino\api\print.h:34:7: note: class arduino::Print

class Print

   ^~~~~

In file included from F:\Arduino\libraries\AceButton\src\ace_button\testing\EventTracker.cpp:2:0:

F:\Arduino\libraries\AceButton\src\ace_button\testing\EventTracker.h:98:18: error: 'Print' has not been declared

 void printTo(Print& printer) const;

              ^~~~~

F:\Arduino\libraries\AceButton\src\ace_button\testing\EventTracker.cpp:7:27: error: variable or field 'printTo' declared void

void EventRecord::printTo(Print& printer) const {

                       ^~~~~

F:\Arduino\libraries\AceButton\src\ace_button\testing\EventTracker.cpp:7:27: error: reference to 'Print' is ambiguous

In file included from F:\Arduino\libraries\AceButton\src\ace_button\testing\EventTracker.cpp:2:0:

F:\Arduino\libraries\AceButton\src\ace_button\testing\EventTracker.h:30:7: note: candidates are: class Print

class Print;

   ^~~~~

In file included from C:\Users\Philip\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.6\cores\arduino/api/deprecated/Print.h:23:0,

             from F:\Arduino\libraries\AceButton\src\ace_button\testing\EventTracker.cpp:1:

c:\users\philip\appdata\local\arduino15\packages\arduino\hardware\megaavr\1.8.6\cores\arduino\api\print.h:34:7: note: class arduino::Print

class Print

   ^~~~~

F:\Arduino\libraries\AceButton\src\ace_button\testing\EventTracker.cpp:7:34: error: 'printer' was not declared in this scope

void EventRecord::printTo(Print& printer) const {

                              ^~~~~~~

F:\Arduino\libraries\AceButton\src\ace_button\testing\EventTracker.cpp:7:34: note: suggested alternative: 'printf'

void EventRecord::printTo(Print& printer) const {

                              ^~~~~~~

                              printf

F:\Arduino\libraries\AceButton\src\ace_button\testing\EventTracker.cpp:17:28: error: variable or field 'printTo' declared void

void EventTracker::printTo(Print& printer) const {

                        ^~~~~

F:\Arduino\libraries\AceButton\src\ace_button\testing\EventTracker.cpp:17:28: error: reference to 'Print' is ambiguous

In file included from F:\Arduino\libraries\AceButton\src\ace_button\testing\EventTracker.cpp:2:0:

F:\Arduino\libraries\AceButton\src\ace_button\testing\EventTracker.h:30:7: note: candidates are: class Print

class Print;

   ^~~~~

In file included from C:\Users\Philip\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.6\cores\arduino/api/deprecated/Print.h:23:0,

             from F:\Arduino\libraries\AceButton\src\ace_button\testing\EventTracker.cpp:1:

c:\users\philip\appdata\local\arduino15\packages\arduino\hardware\megaavr\1.8.6\cores\arduino\api\print.h:34:7: note: class arduino::Print

class Print

   ^~~~~

F:\Arduino\libraries\AceButton\src\ace_button\testing\EventTracker.cpp:17:35: error: 'printer' was not declared in this scope

void EventTracker::printTo(Print& printer) const {

                               ^~~~~~~

F:\Arduino\libraries\AceButton\src\ace_button\testing\EventTracker.cpp:17:35: note: suggested alternative: 'printf'

void EventTracker::printTo(Print& printer) const {

                               ^~~~~~~

                               printf

exit status 1

Error compiling for board Arduino Nano Every.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Singleton system button config can be an issue

Why is the default config implemented as a Singleton? If you have more than one button and they both point to the same config, then they also all point to the same handler. So you get several buttons that provide a single behavior. I'm just failing to see a use case that would benefit from this.

Wouldn't a factory method be more appropriate? ...or perhaps just making the handler a member of the button class? For now, I can simply assign my own instantiations. So, this is more of a comment than an issue (although, it could be an issue for unsuspecting users).

There's some nice code here. Thanks for sharing. It's refreshing to see some OOP in this world of microcontrollers.

Multiple buttons not working

I tested a bit around with this and figured out that I can't use more buttons than one?
The following code will print "button2" if I press button1 or button2. It doesn't matter.

#include <AceButton.h>
using namespace ace_button;

AceButton button1(6);
AceButton button2(7);

void button1Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  Serial.println("button1");
}

void button2Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  Serial.println("button2");
}

void setup() {
  Serial.begin(9600);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  button1.setEventHandler(button1Handler);
  button2.setEventHandler(button2Handler);
}

void loop() {
  button1.check();
  button2.check();
}

Public method to supress long kEventLongPressed

Wonderful Library! I'm using AceButton to detect button presses on a rotary encoder that also has press functionality, because of this I consider rotations of the encoder to cancel the fact the button is counting up to being 'long pressed' exposing a public:

void AceButton::cancelLongPress() {
  clearPressed();
}

Would seem to be a reasonable modification unless you can see some horrible pitfall or better way of expressing it?

add internal stats variable to track amount of CPU time taken for certain code paths

The StopWatch.ino example code gives the CPU time taken by AceButton.check() when nothing is happening. It'd be nice to get information about how much CPU time it takes to handle certain events,
like Click and DoubleClick. I think the only way to get that information is to insert stats variables into the AceButton class itself. I'd want something like average, min, max and the ability to reset the stats periodically, say every 5 seconds. I think one way to reduce the runtime cost of the stats gathering, and reduce static memory is to inject the "stats" object into AceButton and only do the calculation if the stats object is not null. So 2 more extra bytes on an 8-bit AVR.

Run tests on travis

Would be nice to run the tests on travis-ci.com, using platformio, every time a new commit is added.

Tri-state button example

First of all, thanks for making and sharing this library ๐Ÿ‘

I have a use case (car cruise control) where a single momentary push button serves three functions.

  1. when pressed and released (click) it bumps the current target speed by 1
  2. when pressed and held it commands an acceleration event until released
  3. when released after a long press it sets the current speed as the target speed

Detecting short press / long press on release is no issue.
Just wondering if there's a minimal way to handle the "long press while held" event.

Multiple buttons, issue with none release button....

Hello,

1st of all, thank you for Acebutton! - I've been using it for a while on ESP32s and mostly loving it. I'm having a bit of a problem (conceptually) with code for a held button (stuck button event) that I'd like to cater for.

Essentially I'd like to be able to use kEventPressed and kEventReleased in combination with another button to take a different action when one is held down.

I've attached separate handlers for each button, and use Pressed and Released but get no other buttons when the button is held down.

Doubt this is an issue with the library but would appreciate your advice.

Cheers!

How to correctly create array of buttons from existing buttons?

I can create an array of buttons by doing this:

AceButtons buttons[4];

but what if I want to create individual buttons and then add those to an array? The reason I want to do this is so that I can add specifiy configs to each button in an array. My best guess is this:

AceButton button1;
AceButton button2;
AceButton button3;
AceButton arrayOfButtons[] = {button1, button2, button3};

but I get errors that butto1, button2 etc. has no type but if I type them {AceButton button1 etc.) I get a redefinition error.

What's the correct way to do this?

Thanks and thanks for this amazing button library - I've tried quite a few and this is by far the best and most confgurable (and incredibly documented!)

Neil

Suggestion for implementing 'setLongReleasedDelay'

Hello, @bxparks

Thank you so much for this GREAT library!

I started using the Acebutton library in my projects and I'm really enjoying it.

I used the EasyButton library in my projects.
With EasyButton it was possible to use the function to identify how long a button was released [ex: button.releasedFor(releasedForTime) ]; .
However, I haven't found a way to do this with Acebutton.
See here: https://easybtn.earias.me/docs/released-for-api/

This function is especially important when we want to identify the state of a water float . The problem is that water waves/ripples can change the float state from HIGHT to LOW quickly (see image below).

I'm trying the following approach:

ButtonConfig* config_buoy = buoy.getButtonConfig();
config_buoy ->setEventHandler(handleEvent_boia_superior);
config_buoy ->setDebounceDelay(50);
config_buoy ->setFeature(ButtonConfig::kFeatureLongPress);
config_buoy ->setLongPressDelay(5000);
config_buoy ->setFeature(ButtonConfig::kFeatureSuppressAfterLongPress);

The problem is that kEventLongReleased is quickly triggered when water waves/ripples change the float state from HIGHT to LOW.

Therefore, it would be important to assign a delay to kEventLongReleased, something like a setLongReleasedDelay method:

ButtonConfig* config_buoy = buoy.getButtonConfig();
config_buoy ->setEventHandler(handleEvent_boia_superior);
config_buoy ->setDebounceDelay(50);
config_buoy ->setFeature(ButtonConfig::kFeatureLongPress);
config_buoy ->setLongPressDelay(5000);
config_buoy ->setFeature(ButtonConfig::kFeatureSuppressAfterLongPress);
config_buoy ->setLongReleasedDelay(10000);

With setLongReleasedDelay(10000); the kEventLongReleased event would not be triggered on the ripples of the water, but only when it stabilizes for a period of 10 seconds (in the example above).

Would this implementation in Acebutton be viable? This was the only EasyButton functionality that I didn't find in Acebutton.

image
image

Build AceButton with AUniter

Hi!

I'm experimenting with AceButton, and trying to build it using the AUniter cli for AUnit... I'm sure this is a Path problems, but don't know how to solve it:

`~/development/AceButton % auniter verify esp32 $(find . -name '*.ino') [0]
======== Processing environment 'esp32'
-------- Processing file './tests/EncodedButtonConfigTest/EncodedButtonConfigTest.ino'
$ /Applications/Arduino.app/Contents/MacOS/Arduino --verify --board esp32:esp32:esp32:PartitionScheme=default,FlashMode=qio,FlashFreq=80,FlashSize=4M,UploadSpeed=921600,DebugLevel=none --pref 'compiler.cpp.extra_flags=-DAUNITER -DAUNITER_ESP32 -DAUNITER_SSID="wonderland2G_EXT" -DAUNITER_PASSWORD="a1b2c3d4e5"' ./tests/EncodedButtonConfigTest/EncodedButtonConfigTest.ino
2020-08-20 22:36:33.970 Arduino[57775:1921892] Loading Application 'Arduino'
2020-08-20 22:36:33.971 Arduino[57775:1921892] JVMRuntime=jre8u252-b09.jre
2020-08-20 22:36:33.971 Arduino[57775:1921892] CFBundleName=Arduino
2020-08-20 22:36:33.971 Arduino[57775:1921892] WorkingDirectory=(null)
2020-08-20 22:36:33.971 Arduino[57775:1921892] JVMMainClassName=processing.app.Base
2020-08-20 22:36:33.971 Arduino[57775:1921892] JVMOptions=(
"-Dapple.awt.application.name=Arduino",
"-Dcom.apple.macos.use-file-dialog-packages=true",
"-Dcom.apple.smallTabs=true",
"-DAPP_DIR=$APP_ROOT/Contents/Java",
"-Djava.ext.dirs=$JVM_RUNTIME/Contents/Home/lib/ext/:$JVM_RUNTIME/Contents/Home/jre/lib/ext/",
"-Djava.net.preferIPv4Stack=true",
"-Xdock:name=Arduino",
"-Dcom.apple.mrj.application.apple.menu.about.name=Arduino",
"-Dfile.encoding=UTF-8",
"-Xms128M",
"-Xmx512M",
"-splash:$APP_ROOT/Contents/Java/lib/splash.png"
)
2020-08-20 22:36:33.972 Arduino[57775:1921892] JVMArguments=(
)
2020-08-20 22:36:33.972 Arduino[57775:1921892] JVMClasspath=(null)
2020-08-20 22:36:33.972 Arduino[57775:1921892] JVMDefaultOptions={
}
2020-08-20 22:36:33.972 Arduino[57775:1921892] -> Bundle path: /Applications/Arduino.app
2020-08-20 22:36:33.972 Arduino[57775:1921892] -> Working Directory: '/Users/victorhg/development/AceButton'
2020-08-20 22:36:33.972 Arduino[57775:1921892] -> JVM Runtime path: /Applications/Arduino.app/Contents/PlugIns/jre8u252-b09.jre
2020-08-20 22:36:33.972 Arduino[57775:1921892] Searching for a Java 8 virtual machine
2020-08-20 22:36:33.972 Arduino[57775:1921892] Search for java VM in '/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home'
2020-08-20 22:36:33.972 Arduino[57775:1921892] KO - error: 'launch path not accessible'
2020-08-20 22:36:34.041 Arduino[57775:1921892] No matching JDK found.
2020-08-20 22:36:34.042 Arduino[57775:1921892] -> Java Runtime Dylib Path: '/Applications/Arduino.app/Contents/PlugIns/jre8u252-b09.jre/Contents/Home/lib/jli/libjli.dylib'
2020-08-20 22:36:34.056 Arduino[57775:1921892] Command line passed to application argc=27:
2020-08-20 22:36:34.057 Arduino[57775:1921892] Arg 0: '/Applications/Arduino.app/Contents/MacOS/Arduino'
2020-08-20 22:36:34.057 Arduino[57775:1921892] Arg 1: '-Djava.class.path=/Applications/Arduino.app/Contents/Java/log4j-core-2.12.0.jar:/Applications/Arduino.app/Contents/Java/rsyntaxtextarea-3.0.3-SNAPSHOT.jar:/Applications/Arduino.app/Contents/Java/batik-ext-1.8.jar:/Applications/Arduino.app/Contents/Java/slf4j-api-1.7.22.jar:/Applications/Arduino.app/Contents/Java/arduino-core.jar:/Applications/Arduino.app/Contents/Java/batik-xml-1.8.jar:/Applications/Arduino.app/Contents/Java/slf4j-simple-1.7.22.jar:/Applications/Arduino.app/Contents/Java/batik-dom-1.8.jar:/Applications/Arduino.app/Contents/Java/commons-compress-1.8.jar:/Applications/Arduino.app/Contents/Java/commons-httpclient-3.1.jar:/Applications/Arduino.app/Contents/Java/bcprov-jdk15on-152.jar:/Applications/Arduino.app/Contents/Java/commons-logging-1.0.4.jar:/Applications/Arduino.app/Contents/Java/log4j-api-2.12.0.jar:/Applications/Arduino.app/Contents/Java/batik-script-1.8.jar:/Applications/Arduino.app/Contents/Java/jackson-annotations-2.9.5.jar:/Applications/Arduino.app/Contents/Java/batik-parser-1.8.jar:/Applications/Arduino.app/Contents/Java/batik-squiggle-1.8.jar:/Applications/Arduino.app/Contents/Java/batik-awt-util-1.8.jar:/Applications/Arduino.app/Contents/Java/batik-gvt-1.8.jar:/Applications/Arduino.app/Contents/Java/bcpg-jdk15on-152.jar:/Applications/Arduino.app/Contents/Java/xml-apis-1.3.04.jar:/Applications/Arduino.app/Contents/Java/batik-anim-1.8.jar:/Applications/Arduino.app/Contents/Java/jtouchbar-1.0.0.jar:/Applications/Arduino.app/Contents/Java/batik-bridge-1.8.jar:/Applications/Arduino.app/Contents/Java/jssc-2.8.0-arduino4.jar:/Applications/Arduino.app/Contents/Java/batik-1.8.jar:/Applications/Arduino.app/Contents/Java/batik-transcoder-1.8.jar:/Applications/Arduino.app/Contents/Java/batik-svg-dom-1.8.jar:/Applications/Arduino.app/Contents/Java/batik-rasterizer-1.8.jar:/Applications/Arduino.app/Contents/Java/batik-codec-1.8.jar:/Applications/Arduino.app/Contents/Java/commons-io-2.6.jar:/Applications/Arduino.app/Contents/Java/commons-codec-1.7.jar:/Applications/Arduino.app/Contents/Java/xmlgraphics-commons-2.0.jar:/Applications/Arduino.app/Contents/Java/apple.jar:/Applications/Arduino.app/Contents/Java/commons-exec-1.1.jar:/Applications/Arduino.app/Contents/Java/batik-css-1.8.jar:/Applications/Arduino.app/Contents/Java/batik-util-1.8.jar:/Applications/Arduino.app/Contents/Java/jna-platform-4.2.2.jar:/Applications/Arduino.app/Contents/Java/commons-net-3.3.jar:/Applications/Arduino.app/Contents/Java/batik-svgpp-1.8.jar:/Applications/Arduino.app/Contents/Java/xml-apis-ext-1.3.04.jar:/Applications/Arduino.app/Contents/Java/jmdns-3.5.5.jar:/Applications/Arduino.app/Contents/Java/java-semver-0.8.0.jar:/Applications/Arduino.app/Contents/Java/pde.jar:/Applications/Arduino.app/Contents/Java/jna-4.2.2.jar:/Applications/Arduino.app/Contents/Java/commons-lang3-3.8.1.jar:/Applications/Arduino.app/Contents/Java/jackson-core-2.9.5.jar:/Applications/Arduino.app/Contents/Java/jsch-0.1.50.jar:/Applications/Arduino.app/Contents/Java/jackson-databind-2.9.5.jar'
2020-08-20 22:36:34.057 Arduino[57775:1921892] Arg 2: '-Djava.library.path=/Applications/Arduino.app/Contents/MacOS'
2020-08-20 22:36:34.057 Arduino[57775:1921892] Arg 3: '-DLibraryDirectory=/Users/victorhg/Library'
2020-08-20 22:36:34.057 Arduino[57775:1921892] Arg 4: '-DDocumentsDirectory=/Users/victorhg/Documents'
2020-08-20 22:36:34.057 Arduino[57775:1921892] Arg 5: '-DApplicationSupportDirectory=/Users/victorhg/Library/Application Support'
2020-08-20 22:36:34.057 Arduino[57775:1921892] Arg 6: '-DCachesDirectory=/Users/victorhg/Library/Caches'
2020-08-20 22:36:34.058 Arduino[57775:1921892] Arg 7: '-DSandboxEnabled=true'
2020-08-20 22:36:34.058 Arduino[57775:1921892] Arg 8: '-Dapple.awt.application.name=Arduino'
2020-08-20 22:36:34.058 Arduino[57775:1921892] Arg 9: '-Dcom.apple.macos.use-file-dialog-packages=true'
2020-08-20 22:36:34.058 Arduino[57775:1921892] Arg 10: '-Dcom.apple.smallTabs=true'
2020-08-20 22:36:34.058 Arduino[57775:1921892] Arg 11: '-DAPP_DIR=/Applications/Arduino.app/Contents/Java'
2020-08-20 22:36:34.058 Arduino[57775:1921892] Arg 12: '-Djava.ext.dirs=/Applications/Arduino.app/Contents/PlugIns/jre8u252-b09.jre/Contents/Home/lib/ext/:/Applications/Arduino.app/Contents/PlugIns/jre8u252-b09.jre/Contents/Home/jre/lib/ext/'
2020-08-20 22:36:34.058 Arduino[57775:1921892] Arg 13: '-Djava.net.preferIPv4Stack=true'
2020-08-20 22:36:34.058 Arduino[57775:1921892] Arg 14: '-Xdock:name=Arduino'
2020-08-20 22:36:34.058 Arduino[57775:1921892] Arg 15: '-Dcom.apple.mrj.application.apple.menu.about.name=Arduino'
2020-08-20 22:36:34.059 Arduino[57775:1921892] Arg 16: '-Dfile.encoding=UTF-8'
2020-08-20 22:36:34.059 Arduino[57775:1921892] Arg 17: '-Xms128M'
2020-08-20 22:36:34.059 Arduino[57775:1921892] Arg 18: '-Xmx512M'
2020-08-20 22:36:34.059 Arduino[57775:1921892] Arg 19: '-splash:/Applications/Arduino.app/Contents/Java/lib/splash.png'
2020-08-20 22:36:34.059 Arduino[57775:1921892] Arg 20: 'processing.app.Base'
2020-08-20 22:36:34.059 Arduino[57775:1921892] Arg 21: '--verify'
2020-08-20 22:36:34.059 Arduino[57775:1921892] Arg 22: '--board'
2020-08-20 22:36:34.059 Arduino[57775:1921892] Arg 23: 'esp32:esp32:esp32:PartitionScheme=default,FlashMode=qio,FlashFreq=80,FlashSize=4M,UploadSpeed=921600,DebugLevel=none'
2020-08-20 22:36:34.059 Arduino[57775:1921892] Arg 24: '--pref'
2020-08-20 22:36:34.059 Arduino[57775:1921892] Arg 25: 'compiler.cpp.extra_flags=-DAUNITER -DAUNITER_ESP32 -DAUNITER_SSID="wonderland2G_EXT" -DAUNITER_PASSWORD="a1b2c3d4e5"'
2020-08-20 22:36:34.059 Arduino[57775:1921892] Arg 26: './tests/EncodedButtonConfigTest/EncodedButtonConfigTest.ino'
2020-08-20 22:36:34.061 Arduino[57775:1921902] Loading Application 'Arduino'
2020-08-20 22:36:34.061 Arduino[57775:1921902] JVMRuntime=jre8u252-b09.jre
2020-08-20 22:36:34.061 Arduino[57775:1921902] CFBundleName=Arduino
2020-08-20 22:36:34.061 Arduino[57775:1921902] WorkingDirectory=(null)
2020-08-20 22:36:34.061 Arduino[57775:1921902] JVMMainClassName=processing.app.Base
2020-08-20 22:36:34.061 Arduino[57775:1921902] JVMOptions=(
"-Dapple.awt.application.name=Arduino",
"-Dcom.apple.macos.use-file-dialog-packages=true",
"-Dcom.apple.smallTabs=true",
"-DAPP_DIR=$APP_ROOT/Contents/Java",
"-Djava.ext.dirs=$JVM_RUNTIME/Contents/Home/lib/ext/:$JVM_RUNTIME/Contents/Home/jre/lib/ext/",
"-Djava.net.preferIPv4Stack=true",
"-Xdock:name=Arduino",
"-Dcom.apple.mrj.application.apple.menu.about.name=Arduino",
"-Dfile.encoding=UTF-8",
"-Xms128M",
"-Xmx512M",
"-splash:$APP_ROOT/Contents/Java/lib/splash.png"
)
2020-08-20 22:36:34.061 Arduino[57775:1921902] JVMArguments=(
)
2020-08-20 22:36:34.062 Arduino[57775:1921902] JVMClasspath=(null)
2020-08-20 22:36:34.062 Arduino[57775:1921902] JVMDefaultOptions={
}
2020-08-20 22:36:34.062 Arduino[57775:1921902] -> Bundle path: /Applications/Arduino.app
2020-08-20 22:36:34.062 Arduino[57775:1921902] -> Working Directory: '/Users/victorhg/development/AceButton'
2020-08-20 22:36:34.062 Arduino[57775:1921902] -> JVM Runtime path: /Applications/Arduino.app/Contents/PlugIns/jre8u252-b09.jre
2020-08-20 22:36:34.062 Arduino[57775:1921902] Searching for a Java 8 virtual machine
2020-08-20 22:36:34.062 Arduino[57775:1921902] Search for java VM in '/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home'
2020-08-20 22:36:34.062 Arduino[57775:1921902] KO - error: 'launch path not accessible'
2020-08-20 22:36:34.079 Arduino[57775:1921902] No matching JDK found.
2020-08-20 22:36:34.079 Arduino[57775:1921902] -> Java Runtime Dylib Path: '/Applications/Arduino.app/Contents/PlugIns/jre8u252-b09.jre/Contents/Home/lib/jli/libjli.dylib'
2020-08-20 22:36:34.080 Arduino[57775:1921902] Command line passed to application argc=27:
2020-08-20 22:36:34.080 Arduino[57775:1921902] Arg 0: '/Applications/Arduino.app/Contents/MacOS/Arduino'
2020-08-20 22:36:34.080 Arduino[57775:1921902] Arg 1: '-Djava.class.path=/Applications/Arduino.app/Contents/Java/log4j-core-2.12.0.jar:/Applications/Arduino.app/Contents/Java/rsyntaxtextarea-3.0.3-SNAPSHOT.jar:/Applications/Arduino.app/Contents/Java/batik-ext-1.8.jar:/Applications/Arduino.app/Contents/Java/slf4j-api-1.7.22.jar:/Applications/Arduino.app/Contents/Java/arduino-core.jar:/Applications/Arduino.app/Contents/Java/batik-xml-1.8.jar:/Applications/Arduino.app/Contents/Java/slf4j-simple-1.7.22.jar:/Applications/Arduino.app/Contents/Java/batik-dom-1.8.jar:/Applications/Arduino.app/Contents/Java/commons-compress-1.8.jar:/Applications/Arduino.app/Contents/Java/commons-httpclient-3.1.jar:/Applications/Arduino.app/Contents/Java/bcprov-jdk15on-152.jar:/Applications/Arduino.app/Contents/Java/commons-logging-1.0.4.jar:/Applications/Arduino.app/Contents/Java/log4j-api-2.12.0.jar:/Applications/Arduino.app/Contents/Java/batik-script-1.8.jar:/Applications/Arduino.app/Contents/Java/jackson-annotations-2.9.5.jar:/Applications/Arduino.app/Contents/Java/batik-parser-1.8.jar:/Applications/Arduino.app/Contents/Java/batik-squiggle-1.8.jar:/Applications/Arduino.app/Contents/Java/batik-awt-util-1.8.jar:/Applications/Arduino.app/Contents/Java/batik-gvt-1.8.jar:/Applications/Arduino.app/Contents/Java/bcpg-jdk15on-152.jar:/Applications/Arduino.app/Contents/Java/xml-apis-1.3.04.jar:/Applications/Arduino.app/Contents/Java/batik-anim-1.8.jar:/Applications/Arduino.app/Contents/Java/jtouchbar-1.0.0.jar:/Applications/Arduino.app/Contents/Java/batik-bridge-1.8.jar:/Applications/Arduino.app/Contents/Java/jssc-2.8.0-arduino4.jar:/Applications/Arduino.app/Contents/Java/batik-1.8.jar:/Applications/Arduino.app/Contents/Java/batik-transcoder-1.8.jar:/Applications/Arduino.app/Contents/Java/batik-svg-dom-1.8.jar:/Applications/Arduino.app/Contents/Java/batik-rasterizer-1.8.jar:/Applications/Arduino.app/Contents/Java/batik-codec-1.8.jar:/Applications/Arduino.app/Contents/Java/commons-io-2.6.jar:/Applications/Arduino.app/Contents/Java/commons-codec-1.7.jar:/Applications/Arduino.app/Contents/Java/xmlgraphics-commons-2.0.jar:/Applications/Arduino.app/Contents/Java/apple.jar:/Applications/Arduino.app/Contents/Java/commons-exec-1.1.jar:/Applications/Arduino.app/Contents/Java/batik-css-1.8.jar:/Applications/Arduino.app/Contents/Java/batik-util-1.8.jar:/Applications/Arduino.app/Contents/Java/jna-platform-4.2.2.jar:/Applications/Arduino.app/Contents/Java/commons-net-3.3.jar:/Applications/Arduino.app/Contents/Java/batik-svgpp-1.8.jar:/Applications/Arduino.app/Contents/Java/xml-apis-ext-1.3.04.jar:/Applications/Arduino.app/Contents/Java/jmdns-3.5.5.jar:/Applications/Arduino.app/Contents/Java/java-semver-0.8.0.jar:/Applications/Arduino.app/Contents/Java/pde.jar:/Applications/Arduino.app/Contents/Java/jna-4.2.2.jar:/Applications/Arduino.app/Contents/Java/commons-lang3-3.8.1.jar:/Applications/Arduino.app/Contents/Java/jackson-core-2.9.5.jar:/Applications/Arduino.app/Contents/Java/jsch-0.1.50.jar:/Applications/Arduino.app/Contents/Java/jackson-databind-2.9.5.jar'

2020-08-20 22:36:34.080 Arduino[57775:1921902] Arg 2: '-Djava.library.path=/Applications/Arduino.app/Contents/MacOS'
2020-08-20 22:36:34.080 Arduino[57775:1921902] Arg 3: '-DLibraryDirectory=/Users/victorhg/Library'
2020-08-20 22:36:34.080 Arduino[57775:1921902] Arg 4: '-DDocumentsDirectory=/Users/victorhg/Documents'
2020-08-20 22:36:34.080 Arduino[57775:1921902] Arg 5: '-DApplicationSupportDirectory=/Users/victorhg/Library/Application Support'
2020-08-20 22:36:34.080 Arduino[57775:1921902] Arg 6: '-DCachesDirectory=/Users/victorhg/Library/Caches'
2020-08-20 22:36:34.080 Arduino[57775:1921902] Arg 7: '-DSandboxEnabled=true'
2020-08-20 22:36:34.081 Arduino[57775:1921902] Arg 8: '-Dapple.awt.application.name=Arduino'
2020-08-20 22:36:34.081 Arduino[57775:1921902] Arg 9: '-Dcom.apple.macos.use-file-dialog-packages=true'
2020-08-20 22:36:34.081 Arduino[57775:1921902] Arg 10: '-Dcom.apple.smallTabs=true'
2020-08-20 22:36:34.081 Arduino[57775:1921902] Arg 11: '-DAPP_DIR=/Applications/Arduino.app/Contents/Java'
2020-08-20 22:36:34.081 Arduino[57775:1921902] Arg 12: '-Djava.ext.dirs=/Applications/Arduino.app/Contents/PlugIns/jre8u252-b09.jre/Contents/Home/lib/ext/:/Applications/Arduino.app/Contents/PlugIns/jre8u252-b09.jre/Contents/Home/jre/lib/ext/'
2020-08-20 22:36:34.081 Arduino[57775:1921902] Arg 13: '-Djava.net.preferIPv4Stack=true'
2020-08-20 22:36:34.081 Arduino[57775:1921902] Arg 14: '-Xdock:name=Arduino'
2020-08-20 22:36:34.081 Arduino[57775:1921902] Arg 15: '-Dcom.apple.mrj.application.apple.menu.about.name=Arduino'
2020-08-20 22:36:34.081 Arduino[57775:1921902] Arg 16: '-Dfile.encoding=UTF-8'
2020-08-20 22:36:34.081 Arduino[57775:1921902] Arg 17: '-Xms128M'
2020-08-20 22:36:34.081 Arduino[57775:1921902] Arg 18: '-Xmx512M'
2020-08-20 22:36:34.081 Arduino[57775:1921902] Arg 19: '-splash:/Applications/Arduino.app/Contents/Java/lib/splash.png'
2020-08-20 22:36:34.081 Arduino[57775:1921902] Arg 20: 'processing.app.Base'
2020-08-20 22:36:34.081 Arduino[57775:1921902] Arg 21: '--verify'
2020-08-20 22:36:34.081 Arduino[57775:1921902] Arg 22: '--board'
2020-08-20 22:36:34.081 Arduino[57775:1921902] Arg 23:

'esp32:esp32:esp32:PartitionScheme=default,FlashMode=qio,FlashFreq=80,FlashSize=4M,UploadSpeed=921600,DebugLevel=none'

2020-08-20 22:36:34.081 Arduino[57775:1921902] Arg 24: '--pref'
2020-08-20 22:36:34.081 Arduino[57775:1921902] Arg 25: 'compiler.cpp.extra_flags=-DAUNITER -DAUNITER_ESP32 -DAUNITER_SSID="wonderland2G_EXT" -DAUNITER_PASSWORD="a1b2c3d4e5"'
2020-08-20 22:36:34.081 Arduino[57775:1921902] Arg 26: './tests/EncodedButtonConfigTest/EncodedButtonConfigTest.ino'

Loading configuration...
Initializing packages...
Preparing boards...
Verifying...

EncodedButtonConfigTest:4:23: fatal error: AceButton.h: No such file or directory
compilation terminated.
exit status 1

FAILED verify: esp32 ./tests/EncodedButtonConfigTest/EncodedButtonConfigTest.ino`

Should be able to work out of the box, right? But it seems not to find the headers contained on src folders.

Thanks

Could we make `isLongPressed()` public?

Hi,
I'm overhauling my indicator system in my scooter. It has two buttons for indication.

I cycle through modes by tapping both indicators at the same time. However, I need to process combination taps but ignore it if either is long pressed. Rather than keep my own state tracker, why not make isLongPressed() public?

      if (eventType == AceButton::kEventPressed) {
        if (left.isPressedRaw() && right.isPressedRaw()) { // If both bottons are pressed
          if (!left.isLongPressed() && !right.isLongPressed()) { // Skip if either button is long pressed, taps only
            Serial.println("Both");
          }
        }
      }

Button as switch

Is it possible to use the same button once to turn on an output and once to turn off the output? like a switch.. with the same funtion (keventpressed or clicked)

Does not build on Arduino Nano Every

Hi,

building AceButton on the brand new Arduino Nano Every (megaAVR architecture) fails with this error message:

/libraries/AceButton/src/ace_button/AceButton.cpp:33:4: error: #error HIGH must be defined to be 1
   #error HIGH must be defined to be 1
    ^~~~~
exit status 1

It's obviously this piece of code:

// Check that the Arduino constants HIGH and LOW are defined to be 1 and 0,
// respectively. Otherwise, this library won't work.
#if HIGH != 1
  #error HIGH must be defined to be 1
#endif
#if LOW != 0
  #error LOW must be defined to be 0
#endif

I tracked down where HIGH and LOW are defined (hardware/arduino/avr/cores/arduino/Arduino.h):

#define HIGH 0x1
#define LOW  0x0

This looks fine to me. I removed the check, which clears the error message and the builds but the library does not work. So as advertised in the comment above the check haha. Any idea how to resolve this?

If you need more information, let me know.

Two button both pressed (sample LongPress)

Hi, congratulations on a very nice project.

What I want to ask is, is it possible to have a feature that I think would be very nice if added?

I need to use the long-press method of two keys to turn a device off and on safely.

I am able to check and catch the isPressedRaw() property on both keys in the kEventLongPressed event. But the event throwing occurs on the second depressed key.

I wonder if it's possible to do two-key presses with the existing library?

code;

#include <AceButton.h>
using namespace ace_button;

#define PIN_BUTTON1 6
#define PIN_BUTTON2 7

AceButton button1(PIN_BUTTON1);
AceButton button2(PIN_BUTTON2);

void handleEvent(AceButton *, uint8_t, uint8_t);

void setup()
{
  pinMode(PIN_BUTTON1, INPUT_PULLUP);
  pinMode(PIN_BUTTON2, INPUT_PULLUP);

  ButtonConfig *buttonConfig = ButtonConfig::getSystemButtonConfig();
  buttonConfig->setEventHandler(handleEvent);
  buttonConfig->setFeature(ButtonConfig::kFeatureLongPress);
  buttonConfig->setLongPressDelay(3000);
}

void handleEvent(AceButton *button, uint8_t eventType, uint8_t buttonState)
{
  switch (eventType)
  {
  case AceButton::kEventLongPressed:
    if (button1.isPressedRaw() && button2.isPressedRaw())
    {
      Serial.println("LongPressed: Both");
    }
    break;
  }
}

terminal;

LongPressed: Both
LongPressed: Both

No longer responding to single click

I have been using AceButton on an ESP32's 'Boot' button (GPIO 0) and it worked really well with Click, Doubleclick, and Longpress. But suddenly I get no longer a 'Clicked' response, but I still get Double and Long.

I was using version 1.6, and after detecting the problem, I upgraded to 1.8.3, but no change to my problem.

When pressing a button I see a kEventPressed. Pressing again after some 10+ seconds, I see another kEventPressed followed by a kEventDoubleClicked. Doing a doubleclick as fast as I can also gives a correct kEventDoubleClicked.

Longpress still coming correctly after 2 sec (programmed setLongPressDelay(2000);).

Setting 'setClickDelay()' down to 100 or up to 1000 does not make a difference.

How do I get my click back?

Examples depend on "using namespace ace_button" in the global namespace

I found that https://github.com/bxparks/AceButton#IncludeHeader explained how to import the ace_button namespace into global, but the examples seem to all demonstrate dependency on the ace_button:: members all being in the global namespace as being best practice.

Shouldn't the recommended uses be more encapsulated? Like:

/*
 * A demo of a simplest AceButton that has a visible effect. One button is
 * connected to the digital pin BUTTON_PIN. It uses the internal pull-up
 * resistor (INPUT_PULLUP). Pressing the button turns on the built-in LED.
 * Releasing the button turns off the LED.
 */

#include <AceButton.h>

// Some ESP32 boards have multiple builtin LEDs so don't define LED_BUILTIN.
#if defined(ESP32)
  const int LED_PIN = 2;
#else
  const int LED_PIN = LED_BUILTIN;
#endif

const int BUTTON_PIN = 2;
const int LED_ON = HIGH;
const int LED_OFF = LOW;

ace_button::AceButton button(BUTTON_PIN);

// Forward reference to prevent Arduino compiler becoming confused.
void handleEvent(ace_button::AceButton*, uint8_t, uint8_t);

void setup() {
  delay(2000);

#if defined(ARDUINO_AVR_LEONARDO)
  RXLED0; // LED off
  TXLED0; // LED off
#endif

  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  button.setEventHandler(handleEvent);
}

void loop() {
  button.check();
}

void handleEvent(ace_button::AceButton* /* button */, uint8_t eventType,
    uint8_t /* buttonState */) {
  using namespace ace_button;
  switch (eventType) {
    case AceButton::kEventPressed:
      digitalWrite(LED_PIN, LED_ON);
      break;
    case AceButton::kEventReleased:
      digitalWrite(LED_PIN, LED_OFF);
      break;
  }
}

... or with a specific name import instead of the wildcard:

/*
 * A demo of a simplest AceButton that has a visible effect. One button is
 * connected to the digital pin BUTTON_PIN. It uses the internal pull-up
 * resistor (INPUT_PULLUP). Pressing the button turns on the built-in LED.
 * Releasing the button turns off the LED.
 */

#include <AceButton.h>
using ace_button::AceButton;

// Some ESP32 boards have multiple builtin LEDs so don't define LED_BUILTIN.
#if defined(ESP32)
  const int LED_PIN = 2;
#else
  const int LED_PIN = LED_BUILTIN;
#endif

const int BUTTON_PIN = 2;
const int LED_ON = HIGH;
const int LED_OFF = LOW;

AceButton button(BUTTON_PIN);

// Forward reference to prevent Arduino compiler becoming confused.
void handleEvent(AceButton*, uint8_t, uint8_t);

void setup() {
  delay(2000);

#if defined(ARDUINO_AVR_LEONARDO)
  RXLED0; // LED off
  TXLED0; // LED off
#endif

  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  button.setEventHandler(handleEvent);
}

void loop() {
  button.check();
}

void handleEvent(AceButton* /* button */, uint8_t eventType,
    uint8_t /* buttonState */) {
      using namespace ace_button;

  switch (eventType) {
    case AceButton::kEventPressed:
      digitalWrite(LED_PIN, LED_ON);
      break;
    case AceButton::kEventReleased:
      digitalWrite(LED_PIN, LED_OFF);
      break;
  }
}

Initial button state

Hi,

first: thanks for your AceButton lib,it provides a lot of possibilities and makes button handling quite easy. However, I have an issue with the initial state of the buttons: the initial state is always 'pressed'. I tried it with and without external and internal pull-down, even with no wiring to the button-pins at all. Finally, I tried the following: pressed the button on restart and voila, this time it was not recognized as being pressed at startup. Could it be the case, that the initial state is negated?
Thanks for investigating and kind regards, Tom

reduce default debounce delay from 50 ms to 20 ms

I'm going to change the default debouncing delay kDebounceDelay from 50 ms to 20 ms.

I have noticed that at 50 milliseconds, a button can feel sluggish if I push it up and down fast enough. With the current delay of 50 ms, a Pressed event is delayed by 50 ms, then a Released event is delayed by 50 ms, for a total delay of at least 100 ms, and it is actually possible to push a button up and down fast enough for the library to miss real button events. At 20 ms, a button feels far more responsive.

This extensive article (http://www.ganssle.com/debouncing-pt2.htm) says that most switches will settle down within 10 ms, and recommends a debounce time of 20-50 ms. I'm going with the lower end of that.

This change means that the AceButton::check() must be called a little more frequently than before. So instead of calling it every 10-20 ms, it should be called every 5 ms or lower.

End-users are, of course, always free to override the default values of any of the timing parameters.

proposed deprecation of AdjustableButtonConfig into ButtonConfig

Hello everyone,

Thanks for using the AceButton library. I am thinking of making a change to the ButtonConfig class of the library, and was wondering if anyone has any strong opinions.

Background

When I first created the AceButton library, I was aiming to have a low RAM requirement for the library. Coming from the cloud computing world (where I have access to GiB and TiB of RAM), it was difficult to wrap my head around a programming environment where I have only 2kB of RAM (ATmega328P processor on an UNO or Nano). I made ButtonConfig mostly immutable, containing compile-time constants that seemed reasonable for various timing parameters, and used virtual functions to dispatch down to the AdjustableButtonConfig subclass to allow people to override those parameters if they wanted to.

Proposal

For the next revision of the AceButton library, I am proposing to merge the AdjustableButtonConfig class into the ButtonConfig class, and remove most of those virtual methods that retrieve those timing parameters. The AdjustableButtonConfig class will be kept for backwards compatibility, but it will become just a empty shell subclass of ButtonConfig with no additional features. I can remove that class in later versions.

Reasons

  1. In theory, making ButtonConfig adjustable should increase the RAM usage by 12 bytes (because there are 6 uint16_t timing parameters in AdjustableButtonConfig which are not present in ButtonConfig). But in practice, I have found that the compiler optimizes the code in strange ways, and the overal RAM usage does not actually increase in many cases.

  2. Removing the virtual method on the various getters allows the compiler to inline those methods, instead of using a virtual dispatch, and makes the AceButton.check() method run faster. Of course, we are talking about a difference between, say, 17 microseconds versus 14 microseconds, so maybe this is micro-optimizing, but I think a simple button library should have as little overhead as possible.

  3. Removing AdjustableButtonConfig makes the code simpler, easier to maintain, easier to document, and hopefully easier to use.

Benchmarks

Here are some benchmarks. I used Arduino IDE version 1.8.6, using the arduino:avr:nano:cpu=atmega328old fully-qualified board identifier. Other processors have so much more RAM than the ATMega328P, (e.g. 64kB for Teensy 3.2, 80kB for ESP8266, ~300kB for ESP32), that we will only worry about RAM size for the ATMega328P family of boards.

  • before means the current code,
  • after means the new code in the adjustable branch where I have merged AdjustableButtonConfig into ButtonConfig.

CPU Time

AutoBenchmark

before:

------------------------+-------------+---------+
button event ย ย ย ย ย ย ย ย ย ย ย | min/avg/max | samples |
------------------------+-------------+---------+
idle ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย |  16/ 16/ 24 | 1925    |
press/release ย ย ย ย ย ย ย ย ย ย |   8/ 18/ 28 | 1920    |
click ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย |   8/ 17/ 28 | 1922    |
double click ย ย ย ย ย ย ย ย ย ย ย |   8/ 16/ 32 | 1921    |
long press/repeat press | ย ย 8/ 19/ 28 | 1919    |
------------------------+-------------+---------+

after:

------------------------+-------------+---------+
button event ย ย ย ย ย ย ย ย ย ย ย | min/avg/max | samples |
------------------------+-------------+---------+
idle ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย |  12/ 13/ 20 | 1934    |
press/release ย ย ย ย ย ย ย ย ย ย |   8/ 14/ 20 | 1925    |
click ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย |   8/ 14/ 24 | 1925    |
double click ย ย ย ย ย ย ย ย ย ย ย |   8/ 13/ 24 | 1925    |
long press/repeat press | ย ย 8/ 15/ 24 | 1927    |
------------------------+-------------+---------+

Program Size and RAM Usage

HelloWorld

  • before (flash/ram): 2406/55
  • after (flash/ram): 2282/55

SingleButton

  • before (flash/ram): 3812/234
  • after (flash/ram): 3692/234

StopWatch

  • before (flash/ram): 5962/268
  • after (flash/ram): 5686/256

TunerButtons

  • before (flash/ram): 4348/306
  • after (flash/ram): 4364/318

ClickVersusDoubleClickUsingBoth

  • before: 2342/55
  • after: 2218/55

CapacitiveButton

  • before: 5488/266
  • after: 5498/298

Summary

For the most part, the new code consumes less flash memory, but surprisingly, it often uses the same amount of static RAM.

invalid use of non-static member function 'void Interface::handleEvent(ace_button::AceButton*, uint8_t, uint8_t)'

Hello,

I try to instantiate a ButtonConfig in a member class function but I get the following error.

It is possible to do this pattern ?

class Test {
    private:
        AceButton button1 = AceButton(1);
        void configure();
        void handleEvent(AceButton *, uint8_t, uint8_t);
};
#include <Test.h>

void Test::configure(){
     pinMode(1, INPUT_PULLUP);
     ButtonConfig *buttonConfig = ButtonConfig::getSystemButtonConfig();
     buttonConfig->setEventHandler(handleEvent);
     buttonConfig->setFeature(ButtonConfig::kFeatureClick);
     buttonConfig->setFeature(ButtonConfig::kFeatureLongPress);
     buttonConfig->setFeature(ButtonConfig::kFeatureDoubleClick);
     buttonConfig->setFeature(ButtonConfig::kFeatureRepeatPress);
}

void Test::handleEvent(AceButton *button, uint8_t eventType, uint8_t buttonState){
     switch (button->getPin())
    {
    case 1:
        Serial.println("Hourrray");
        break;
    }
}

Thank you

Typo in documentation

Hi Brian,

while reading through the new documentation parts of v1.4 I found below typo in this README.md:

When each button is connected to a single button

One button should be pin here Iโ€™d assume.

Nice new feature though! Really appreciate the addition and the way this evolved from a GitHub issue into a feature!

Cheers
Stephan

warning: "DEPRECATED" redefined

I observe the following:

"/home/dns/.arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/2.5.0-4-b40a506/bin/xtensa-lx106-elf-g++" -D__ets__ -DICACHE_FLASH -U__STRICT_ANSI__ "-I/home/dns/.arduino15/packages/esp8266/hardware/esp8266/2.7.4/tools/sdk/include" "-I/home/dns/.arduino15/packages/esp8266/hardware/esp8266/2.7.4/tools/sdk/lwip2/include" "-I/home/dns/.arduino15/packages/esp8266/hardware/esp8266/2.7.4/tools/sdk/libc/xtensa-lx106-elf/include" "-I/tmp/arduino_build_36749/core" -c -Wall -Wextra -Os -g -mlongcalls -mtext-section-literals -fno-rtti -falign-functions=4 -std=gnu++11 -MMD -ffunction-sections -fdata-sections -fno-exceptions  -DNONOSDK22x_190703=1 -DF_CPU=80000000L -DLWIP_OPEN_SRC -DTCP_MSS=536 -DLWIP_FEATURES=1 -DLWIP_IPV6=0   -DARDUINO=10813 -DARDUINO_ESP8266_GENERIC -DARDUINO_ARCH_ESP8266 -DARDUINO_BOARD="ESP8266_GENERIC" -DLED_BUILTIN=2 -DFLASHMODE_DOUT  -DESP8266 "-I/home/dns/.arduino15/packages/esp8266/hardware/esp8266/2.7.4/cores/esp8266" "-I/home/dns/.arduino15/packages/esp8266/hardware/esp8266/2.7.4/variants/generic" "-I/home/dns/Arduino/libraries/JLed/src" "-I/home/dns/.arduino15/packages/esp8266/hardware/esp8266/2.7.4/libraries/ESP8266WebServer/src" "-I/home/dns/.arduino15/packages/esp8266/hardware/esp8266/2.7.4/libraries/ESP8266WiFi/src" "-I/home/dns/Arduino/libraries/AceButton/src" "-I/home/dns/.arduino15/packages/esp8266/hardware/esp8266/2.7.4/libraries/Ticker/src" "-I/home/dns/Arduino/libraries/LinkedList" "-I/home/dns/Arduino/libraries/UniversalTelegramBot/src" "-I/home/dns/Arduino/libraries/ArduinoJson/src" "-I/home/dns/.arduino15/packages/esp8266/hardware/esp8266/2.7.4/libraries/ESP8266HTTPClient/src" "-I/home/dns/Arduino/libraries/Uptime_Library/src" "-I/home/dns/.arduino15/packages/esp8266/hardware/esp8266/2.7.4/libraries/LittleFS/src" "-I/home/dns/Arduino/libraries/WiFiManager" "-I/home/dns/.arduino15/packages/esp8266/hardware/esp8266/2.7.4/libraries/DNSServer/src" "-I/home/dns/.arduino15/packages/esp8266/hardware/esp8266/2.7.4/libraries/ESP8266mDNS/src" "/tmp/arduino_build_36749/sketch/MailBox.cpp" -o "/tmp/arduino_build_36749/sketch/MailBox.cpp.o"
In file included from /home/dns/Arduino/libraries/ArduinoJson/src/ArduinoJson/Deserialization/../JsonVariantCasts.hpp:8:0,
                 from /home/dns/Arduino/libraries/ArduinoJson/src/ArduinoJson/Deserialization/../JsonVariantBase.hpp:7,
                 from /home/dns/Arduino/libraries/ArduinoJson/src/ArduinoJson/Deserialization/../JsonVariant.hpp:13,
                 from /home/dns/Arduino/libraries/ArduinoJson/src/ArduinoJson/Deserialization/../JsonBuffer.hpp:12,
                 from /home/dns/Arduino/libraries/ArduinoJson/src/ArduinoJson/Deserialization/JsonParser.hpp:7,
                 from /home/dns/Arduino/libraries/ArduinoJson/src/ArduinoJson/JsonBufferBase.hpp:7,
                 from /home/dns/Arduino/libraries/ArduinoJson/src/ArduinoJson/DynamicJsonBuffer.hpp:7,
                 from /home/dns/Arduino/libraries/ArduinoJson/src/ArduinoJson.hpp:9,
                 from /home/dns/Arduino/libraries/ArduinoJson/src/ArduinoJson.h:9,
                 from /home/dns/Arduino/libraries/UniversalTelegramBot/src/UniversalTelegramBot.h:26,
                 from /tmp/arduino_build_36749/sketch/Telegram.h:12,
                 from /tmp/arduino_build_36749/sketch/MailBox.cpp:10:
/home/dns/Arduino/libraries/ArduinoJson/src/ArduinoJson/Deserialization/../Polyfills/attributes.hpp:18:0: warning: "DEPRECATED" redefined [enabled by default]
 #define DEPRECATED(msg) __attribute__((deprecated(msg)))
 ^
In file included from /home/dns/Arduino/libraries/AceButton/src/AceButton.h:36:0,
                 from /tmp/arduino_build_36749/sketch/System.h:75,
                 from /tmp/arduino_build_36749/sketch/MySystem.h:42,
                 from /tmp/arduino_build_36749/sketch/MailBox.cpp:9:
/home/dns/Arduino/libraries/AceButton/src/ace_button/ButtonConfig.h:33:0: note: this is the location of the previous definition
   #define DEPRECATED __attribute__((deprecated))
 ^

ArduinoJson 5.13.5
AceButton 1.6.1

Even though in this case it is ArduinoJson who's guilty for DEPRECATED redefinition, the AceButton code does not seem to be protected from this either.

I want to set setDefaultReleasedState to LOW

I need to set setDefaultReleasedState to LOW so how do i do it ?
as well as why are using pull down resistor than pull up resistor?
ie why are initiating or expecting all the pins to HIGH by default ? doesnt it cause more current ?

Capacitive button returns release while button is pressed

I am using the capacitive button as a switch to control a smart light. The problem is that the released occurs while not expected. I don't understand why and whether this is AceButton related or CapacitiveSensor related.

Hardware

  • Wemos D1 mini
  • D2 (GPIO 4) connected to wire and metal strip
  • D1 (GPIO 5) connected with resistor (680k) to D2

Problem
When touching and holding the metal strip I expect that the buttonevent is pressed. To see what event occurs, I addds a Serial.Println with millis() and the eventType. The monitor shows:

368761 Pressed
368792 Clicked
368792 Released
368821 Pressed
368852 DoubleClicked
368852 Released
368881 Pressed
368912 Clicked
368913 Released
368941 Pressed
368972 DoubleClicked
368972 Released
369001 Pressed
369032 Clicked
369032 Released
369062 Pressed
369094 DoubleClicked
369094 Released

So while I expected to only see a pressed, I also see a released, and because of that also clicked and doubleClicked.

I am not sure why this occurs:

  • bad hardware setup
  • wrong capacitiveSensor config
  • something in the code

Code
The code is used to function as a smart (led) light, which is controlled by diyHue https://diyhue.github.io/. This light has a touch button to switch the light on or off.

The full code is below (based upon https://github.com/diyhue/Lights/tree/master/Arduino/Generic_SK6812_Strip, by Marius Motea).

// diyHue switch 
// AceButton with capacitiveSensor
// ESP 8266 (Wemos D1 min)
// WS2812 RGBW led on D4

// diyHue must be installed (on a RPi for example)
// after first start, ESP functions as a WIFI access point (AP). Connect to the access point (for example with tablet or phone) and use your browser to go to 192.168.4.1 
// to set wifi credentials


#include <FS.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ESP8266HTTPUpdateServer.h>
#include <ESP8266WebServer.h>
#include <NeoPixelBus.h>
#include <WiFiManager.h>
#include <ArduinoJson.h>

#include <CapacitiveSensor.h>
#include <AceButton.h>
using namespace ace_button;

/**
 * A subclass of ButtonConfig that allows a CapacitiveSensor to emulate a
 * mechanical switch connected to a pull-up resistor on the input pin. A "touch"
 * sends a LOW signal, just like a mechnical switch.
 */
class CapacitiveConfig: public ButtonConfig {
  public:
    CapacitiveConfig(CapacitiveSensor& sensor):
      mSensor(sensor) {}

  protected:
    // Number of iterations to sample the capacitive switch. Higher number
    // provides better smoothing but increases the time taken for a single read.
    static const uint8_t kSamples = 30;

    // The threshold value which is considered to be a "touch" on the switch.
    static const long kTouchThreshold = 100;

    int readButton(uint8_t /*pin*/) override {
      long total =  mSensor.capacitiveSensor(kSamples);
      return (total > kTouchThreshold) ? LOW : HIGH;
    }

  private:
    CapacitiveSensor& mSensor;
};

// Timeout for a single read of the capacitive switch.
static const unsigned long TIMEOUT_MILLIS = 10;

// I used a 1M resistor between pins 4 (send) & metal plate, and a 1K resistor
// between the plate and pin 5 (receive). Try adjusting the
// CapacitiveConfig::kTouchThreshold value for other resistor values.
CapacitiveSensor capSensor(5, 4);

CapacitiveConfig buttonConfig(capSensor);
AceButton button(&buttonConfig);


// All variables must be changed from light web interface. Change them here only if you need different defaults

IPAddress address ( 192,  168,   0,  95); // choose an unique IP Adress
IPAddress gateway ( 192,  168,   0,   1); // Router IP
IPAddress submask(255, 255, 255,   0);

struct state {
  uint8_t colors[4], bri = 100, sat = 254, colorMode = 2;
  bool lightState;
  int ct = 200, hue;
  float stepLevel[4], currentColors[4], x, y;
};

//core

#define entertainmentTimeout 1500 // millis

state lights[10];
bool inTransition, entertainmentRun, useDhcp = true;
byte mac[6], packetBuffer[46];
unsigned long lastEPMillis;

//settings
char *lightName = "Hue SK6812 strip";
uint8_t scene, startup, onPin = 4, offPin = 5;
bool hwSwitch = false;

uint8_t lightsCount = 3;
uint16_t pixelCount = 60, lightLedsCount;
uint8_t transitionLeds = 6; // must be even number



ESP8266WebServer server(80);
WiFiUDP Udp;
ESP8266HTTPUpdateServer httpUpdateServer;

RgbwColor red = RgbwColor(255, 0, 0, 0);
RgbwColor green = RgbwColor(0, 255, 0, 0);
RgbwColor white = RgbwColor(255);
RgbwColor black = RgbwColor(0);

NeoPixelBus<NeoGrbwFeature, NeoEsp8266Uart1Ws2812xMethod>* strip = NULL;

void handleEvent(AceButton* /* button */, uint8_t eventType,
    uint8_t /* buttonState */) {
  switch (eventType) {
    case AceButton::kEventPressed:
      Serial.print(millis());
      Serial.println(F(" Pressed"));
      break;
    case AceButton::kEventReleased:
      Serial.print(millis());
      Serial.println(F(" Released"));
      break;
    case AceButton::kEventClicked:
      Serial.print(millis());
      Serial.println(F(" Clicked"));
      for (int light = 0; light < lightsCount; light++) {
        lights[light].lightState = !lights[light].lightState; //change light from ON to OFF or from OFF to ON
      }
      break;
    case AceButton::kEventDoubleClicked:
      Serial.print(millis());
      Serial.println(F(" DoubleClicked"));
      //apply_scene(1);
      break;
  }
}

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.println("START");
  delay(1000);

  //Serial.println("mounting FS...");

  if (!SPIFFS.begin()) {
    //Serial.println("Failed to mount file system");
    return;
  }

  if (!loadConfig()) {
    //Serial.println("Failed to load config");
  } else {
    ////Serial.println("Config loaded");
  }

  lightLedsCount = pixelCount / lightsCount;
  ChangeNeoPixels(pixelCount);


  if (startup == 1) {
    for (uint8_t i = 0; i < lightsCount; i++) {
      lights[i].lightState = true;
    }
  }
  if (startup == 0) {
    restoreState();
  } else {
    apply_scene(scene);
  }
  for (uint8_t i = 0; i < lightsCount; i++) {
    processLightdata(i, 4);
  }
  if (lights[0].lightState) {
    for (uint8_t i = 0; i < 200; i++) {
      lightEngine();
    }
  }
  
  WiFi.mode(WIFI_STA);
  WiFiManager wifiManager;

  if (!useDhcp) {
    wifiManager.setSTAStaticIPConfig(address, gateway, submask);
  }

  if (!wifiManager.autoConnect(lightName)) {
    delay(3000);
    ESP.reset();
    delay(5000);
  }

  if (useDhcp) {
    address = WiFi.localIP();
    gateway = WiFi.gatewayIP();
    submask = WiFi.subnetMask();
  }


  if (! lights[0].lightState) {
    infoLight(white);
    while (WiFi.status() != WL_CONNECTED) {
      infoLight(red);
    }
    // Show that we are connected
    infoLight(green);

  }

  WiFi.macAddress(mac);

  httpUpdateServer.setup(&server);

  Udp.begin(2100);

  if (hwSwitch == true) {
    pinMode(onPin, INPUT);
    pinMode(offPin, INPUT);
  }

  server.on("/state", HTTP_PUT, []() {
    bool stateSave = false;
    DynamicJsonDocument root(1024);
    DeserializationError error = deserializeJson(root, server.arg("plain"));
    if (error) {
      server.send(404, "text/plain", "FAIL. " + server.arg("plain"));
    } else {
      for (JsonPair state : root.as<JsonObject>()) {
        const char* key = state.key().c_str();
        int light = atoi(key) - 1;
        JsonObject values = state.value();
        int transitiontime = 4;

        if (values.containsKey("xy")) {
          lights[light].x = values["xy"][0];
          lights[light].y = values["xy"][1];
          lights[light].colorMode = 1;
        } else if (values.containsKey("ct")) {
          lights[light].ct = values["ct"];
          lights[light].colorMode = 2;
        } else {
          if (values.containsKey("hue")) {
            lights[light].hue = values["hue"];
            lights[light].colorMode = 3;
          }
          if (values.containsKey("sat")) {
            lights[light].sat = values["sat"];
            lights[light].colorMode = 3;
          }
        }

        if (values.containsKey("on")) {
          if (values["on"]) {
            lights[light].lightState = true;
          } else {
            lights[light].lightState = false;
          }
          if (startup == 0) {
            stateSave = true;
          }
        }

        if (values.containsKey("bri")) {
          lights[light].bri = values["bri"];
        }

        if (values.containsKey("bri_inc")) {
          lights[light].bri += (int) values["bri_inc"];
          if (lights[light].bri > 255) lights[light].bri = 255;
          else if (lights[light].bri < 1) lights[light].bri = 1;
        }

        if (values.containsKey("transitiontime")) {
          transitiontime = values["transitiontime"];
        }

        if (values.containsKey("alert") && values["alert"] == "select") {
          if (lights[light].lightState) {
            lights[light].currentColors[0] = 0; lights[light].currentColors[1] = 0; lights[light].currentColors[2] = 0; lights[light].currentColors[3] = 0;
          } else {
            lights[light].currentColors[2] = 126; lights[light].currentColors[3] = 126;
          }
        }
        processLightdata(light, transitiontime);
      }
      String output;
      serializeJson(root, output);
      server.send(200, "text/plain", output);
      if (stateSave) {
        saveState();
      }
    }
  });

  server.on("/state", HTTP_GET, []() {
    uint8_t light = server.arg("light").toInt() - 1;
    DynamicJsonDocument root(1024);
    root["on"] = lights[light].lightState;
    root["bri"] = lights[light].bri;
    JsonArray xy = root.createNestedArray("xy");
    xy.add(lights[light].x);
    xy.add(lights[light].y);
    root["ct"] = lights[light].ct;
    root["hue"] = lights[light].hue;
    root["sat"] = lights[light].sat;
    if (lights[light].colorMode == 1)
      root["colormode"] = "xy";
    else if (lights[light].colorMode == 2)
      root["colormode"] = "ct";
    else if (lights[light].colorMode == 3)
      root["colormode"] = "hs";
    String output;
    serializeJson(root, output);
    server.send(200, "text/plain", output);
  });

  server.on("/detect", []() {
    char macString[32] = {0};
    sprintf(macString, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
    DynamicJsonDocument root(1024);
    root["name"] = lightName;
    root["lights"] = lightsCount;
    root["protocol"] = "native_multi";
    root["modelid"] = "LST002";
    root["type"] = "sk6812_strip";
    root["mac"] = String(macString);
    root["version"] = 2.0;
    String output;
    serializeJson(root, output);
    server.send(200, "text/plain", output);
  });

  server.on("/config", []() {
    DynamicJsonDocument root(1024);
    root["name"] = lightName;
    root["scene"] = scene;
    root["startup"] = startup;
    root["hw"] = hwSwitch;
    root["on"] = onPin;
    root["off"] = offPin;
    root["hwswitch"] = (int)hwSwitch;
    root["lightscount"] = lightsCount;
    root["pixelcount"] = pixelCount;
    root["transitionleds"] = transitionLeds;
    root["dhcp"] = (int)useDhcp;
    root["addr"] = (String)address[0] + "." + (String)address[1] + "." + (String)address[2] + "." + (String)address[3];
    root["gw"] = (String)gateway[0] + "." + (String)gateway[1] + "." + (String)gateway[2] + "." + (String)gateway[3];
    root["sm"] = (String)submask[0] + "." + (String)submask[1] + "." + (String)submask[2] + "." + (String)submask[3];
    String output;
    serializeJson(root, output);
    server.send(200, "text/plain", output);
  });

  server.on("/", []() {
    if (server.hasArg("scene")) {
      server.arg("name").toCharArray(lightName, server.arg("name").length() + 1);
      startup = server.arg("startup").toInt();
      scene = server.arg("scene").toInt();
      lightsCount = server.arg("lightscount").toInt();
      pixelCount = server.arg("pixelcount").toInt();
      transitionLeds = server.arg("transitionleds").toInt();
      hwSwitch = server.arg("hwswitch").toInt();
      onPin = server.arg("on").toInt();
      offPin = server.arg("off").toInt();
      saveConfig();
    } else if (server.hasArg("dhcp")) {
      useDhcp = server.arg("dhcp").toInt();
      address.fromString(server.arg("addr"));
      gateway.fromString(server.arg("gw"));
      submask.fromString(server.arg("sm"));
      saveConfig();
    }

      String htmlContent = "<!DOCTYPE html> <html> <head> <meta charset=\"UTF-8\"> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"> <title>" + String(lightName) + " - DiyHue</title> <link rel=\"icon\" type=\"image/png\" href=\"https://diyhue.org/wp-content/uploads/2019/11/cropped-Zeichenfl%C3%A4che-4-1-32x32.png\" sizes=\"32x32\"> <link href=\"https://fonts.googleapis.com/icon?family=Material+Icons\" rel=\"stylesheet\"> <link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css\"> <link rel=\"stylesheet\" href=\"https://diyhue.org/cdn/nouislider.css\" /> </head> <body> <div class=\"wrapper\"> <nav class=\"nav-extended row\" style=\"background-color: #26a69a !important;\"> <div class=\"nav-wrapper col s12\"> <a href=\"#\" class=\"brand-logo\">DiyHue</a> <ul id=\"nav-mobile\" class=\"right hide-on-med-and-down\" style=\"position: relative;z-index: 10;\"> <li><a target=\"_blank\" href=\"https://github.com/diyhue\"><i class=\"material-icons left\">language</i>GitHub</a></li> <li><a target=\"_blank\" href=\"https://diyhue.readthedocs.io/en/latest/\"><i class=\"material-icons left\">description</i>Documentation</a></li> <li><a target=\"_blank\" href=\"https://diyhue.slack.com/\"><i class=\"material-icons left\">question_answer</i>Slack channel</a></li> </ul> </div> <div class=\"nav-content\"> <ul class=\"tabs tabs-transparent\"> <li class=\"tab\" title=\"#home\"><a class=\"active\" href=\"#home\">Home</a></li> <li class=\"tab\" title=\"#preferences\"><a href=\"#preferences\">Preferences</a></li> <li class=\"tab\" title=\"#network\"><a href=\"#network\">Network settings</a></li> <li class=\"tab\" title=\"/update\"><a href=\"/update\">Updater</a></li> </ul> </div> </nav> <ul class=\"sidenav\" id=\"mobile-demo\"> <li><a target=\"_blank\" href=\"https://github.com/diyhue\">GitHub</a></li> <li><a target=\"_blank\" href=\"https://diyhue.readthedocs.io/en/latest/\">Documentation</a></li> <li><a target=\"_blank\" href=\"https://diyhue.slack.com/\">Slack channel</a></li> </ul> <div class=\"container\"> <div class=\"section\"> <div id=\"home\" class=\"col s12\"> <form> <input type=\"hidden\" name=\"section\" value=\"1\"> <div class=\"row\"> <div class=\"col s10\"> <label for=\"power\">Power</label> <div id=\"power\" class=\"switch section\"> <label> Off <input type=\"checkbox\" name=\"pow\" id=\"pow\" value=\"1\"> <span class=\"lever\"></span> On </label> </div> </div> </div> <div class=\"row\"> <div class=\"col s12 m10\"> <label for=\"bri\">Brightness</label> <input type=\"text\" id=\"bri\" class=\"js-range-slider\" name=\"bri\" value=\"\" /> </div> </div> <div class=\"row\"> <div class=\"col s12\"> <label for=\"hue\">Color</label> <div> <canvas id=\"hue\" width=\"320px\" height=\"320px\" style=\"border:1px solid #d3d3d3;\"></canvas> </div> </div> </div> <div class=\"row\"> <div class=\"col s12\"> <label for=\"ct\">Color Temp</label> <div> <canvas id=\"ct\" width=\"320px\" height=\"50px\" style=\"border:1px solid #d3d3d3;\"></canvas> </div> </div> </div> </form> </div> <div id=\"preferences\" class=\"col s12\"> <form method=\"POST\" action=\"/\"> <input type=\"hidden\" name=\"section\" value=\"1\"> <div class=\"row\"> <div class=\"col s12\"> <label for=\"name\">Light Name</label> <input type=\"text\" id=\"name\" name=\"name\"> </div> </div> <div class=\"row\"> <div class=\"col s12 m6\"> <label for=\"startup\">Default Power:</label> <select name=\"startup\" id=\"startup\"> <option value=\"0\">Last State</option> <option value=\"1\">On</option> <option value=\"2\">Off</option> </select> </div> </div> <div class=\"row\"> <div class=\"col s12 m6\"> <label for=\"scene\">Default Scene:</label> <select name=\"scene\" id=\"scene\"> <option value=\"0\">Relax</option> <option value=\"1\">Read</option> <option value=\"2\">Concentrate</option> <option value=\"3\">Energize</option> <option value=\"4\">Bright</option> <option value=\"5\">Dimmed</option> <option value=\"6\">Nightlight</option> <option value=\"7\">Savanna sunset</option> <option value=\"8\">Tropical twilight</option> <option value=\"9\">Arctic aurora</option> <option value=\"10\">Spring blossom</option> </select> </div> </div> <div class=\"row\"> <div class=\"col s4 m3\"> <label for=\"pixelcount\" class=\"col-form-label\">Pixel count</label> <input type=\"number\" id=\"pixelcount\" name=\"pixelcount\"> </div> </div> <div class=\"row\"> <div class=\"col s4 m3\"> <label for=\"lightscount\" class=\"col-form-label\">Lights count</label> <input type=\"number\" id=\"lightscount\" name=\"lightscount\"> </div> </div> <label class=\"form-label\">Light division</label> </br> <label>Available Pixels:</label> <label class=\"availablepixels\"><b>null</b></label> <div class=\"row dividedLights\"> </div> <div class=\"row\"> <div class=\"col s4 m3\"> <label for=\"transitionleds\">Transition leds:</label> <select name=\"transitionleds\" id=\"transitionleds\"> <option value=\"0\">0</option> <option value=\"2\">2</option> <option value=\"4\">4</option> <option value=\"6\">6</option> <option value=\"8\">8</option> <option value=\"10\">10</option> </select> </div> </div> <div class=\"row\"> <div class=\"col s4 m3\"> <label for=\"rpct\" class=\"form-label\">Red multiplier</label> <input type=\"number\" id=\"rpct\" class=\"js-range-slider\" data-skin=\"round\" name=\"rpct\" value=\"\" /> </div> <div class=\"col s4 m3\"> <label for=\"gpct\" class=\"form-label\">Green multiplier</label> <input type=\"number\" id=\"gpct\" class=\"js-range-slider\" data-skin=\"round\" name=\"gpct\" value=\"\" /> </div> <div class=\"col s4 m3\"> <label for=\"bpct\" class=\"form-label\">Blue multiplier</label> <input type=\"number\" id=\"bpct\" class=\"js-range-slider\" data-skin=\"round\" name=\"bpct\" value=\"\" /> </div> </div> <div class=\"row\"> <label class=\"control-label col s10\">HW buttons:</label> <div class=\"col s10\"> <div class=\"switch section\"> <label> Disable <input type=\"checkbox\" name=\"hwswitch\" id=\"hwswitch\" value=\"1\"> <span class=\"lever\"></span> Enable </label> </div> </div> </div> <div class=\"switchable\"> <div class=\"row\"> <div class=\"col s4 m3\"> <label for=\"on\">On Pin</label> <input type=\"number\" id=\"on\" name=\"on\"> </div> <div class=\"col s4 m3\"> <label for=\"off\">Off Pin</label> <input type=\"number\" id=\"off\" name=\"off\"> </div> </div> </div> <div class=\"row\"> <div class=\"col s10\"> <button type=\"submit\" class=\"waves-effect waves-light btn teal\">Save</button> <!--<button type=\"submit\" name=\"reboot\" class=\"waves-effect waves-light btn grey lighten-1\">Reboot</button>--> </div> </div> </form> </div> <div id=\"network\" class=\"col s12\"> <form method=\"POST\" action=\"/\"> <input type=\"hidden\" name=\"section\" value=\"2\"> <div class=\"row\"> <div class=\"col s12\"> <label class=\"control-label\">Manual IP assignment:</label> <div class=\"switch section\"> <label> Disable <input type=\"checkbox\" name=\"disdhcp\" id=\"disdhcp\" value=\"0\"> <span class=\"lever\"></span> Enable </label> </div> </div> </div> <div class=\"switchable\"> <div class=\"row\"> <div class=\"col s12 m3\"> <label for=\"addr\">Ip</label> <input type=\"text\" id=\"addr\" name=\"addr\"> </div> <div class=\"col s12 m3\"> <label for=\"sm\">Submask</label> <input type=\"text\" id=\"sm\" name=\"sm\"> </div> <div class=\"col s12 m3\"> <label for=\"gw\">Gateway</label> <input type=\"text\" id=\"gw\" name=\"gw\"> </div> </div> </div> <div class=\"row\"> <div class=\"col s10\"> <button type=\"submit\" class=\"waves-effect waves-light btn teal\">Save</button> <!--<button type=\"submit\" name=\"reboot\" class=\"waves-effect waves-light btn grey lighten-1\">Reboot</button>--> <!--<button type=\"submit\" name=\"reboot\" class=\"waves-effect waves-light btn grey lighten-1\">Reboot</button>--> </div> </div> </form> </div> </div> </div> </div> <script src=\"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js\"></script> <script src=\"https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js\"></script> <script src=\"https://diyhue.org/cdn/nouislider.js\"></script> <script src=\"https://diyhue.org/cdn/diyhue.js\"></script> </body> </html>";
      server.send(200, "text/html", htmlContent);
    if (server.args()) {
      delay(100);
      ESP.reset();
    }

  });

  server.on("/reset", []() {
    server.send(200, "text/html", "reset");
    delay(100);
    ESP.reset();
  });

  server.onNotFound(handleNotFound);

  server.begin();

  // Set the timeout to 10 millisecond so that AceButton::check()
  // can have about 4-5 iterations during the 50 millisecond debouncing time.
  capSensor.set_CS_Timeout_Millis(TIMEOUT_MILLIS);

  // Configure the button using CapacitiveConfig.
  buttonConfig.setFeature(ButtonConfig::kFeatureClick);
  buttonConfig.setFeature(ButtonConfig::kFeatureDoubleClick);
  buttonConfig.setEventHandler(handleEvent);

}



void loop() {
  server.handleClient();
  button.check();
  if (!entertainmentRun) {
    lightEngine();
  } else {
    if ((millis() - lastEPMillis) >= entertainmentTimeout) {
      entertainmentRun = false;
      for (uint8_t i = 0; i < lightsCount; i++) {
        processLightdata(i, 4);
      }
    }
  }
  entertainment();

}

void convertHue(uint8_t light)
{
  lights[light].colors[3] = 0;

  double      hh, p, q, t, ff, s, v;
  long        i;

  s = lights[light].sat / 255.0;
  v = lights[light].bri / 255.0;

  if (s <= 0.0) {      // < is bogus, just shuts up warnings
    lights[light].colors[0] = v;
    lights[light].colors[1] = v;
    lights[light].colors[2] = v;
    return;
  }
  hh = lights[light].hue;
  if (hh >= 65535.0) hh = 0.0;
  hh /= 11850, 0;
  i = (long)hh;
  ff = hh - i;
  p = v * (1.0 - s);
  q = v * (1.0 - (s * ff));
  t = v * (1.0 - (s * (1.0 - ff)));

  switch (i) {
    case 0:
      lights[light].colors[0] = v * 255.0;
      lights[light].colors[1] = t * 255.0;
      lights[light].colors[2] = p * 255.0;
      break;
    case 1:
      lights[light].colors[0] = q * 255.0;
      lights[light].colors[1] = v * 255.0;
      lights[light].colors[2] = p * 255.0;
      break;
    case 2:
      lights[light].colors[0] = p * 255.0;
      lights[light].colors[1] = v * 255.0;
      lights[light].colors[2] = t * 255.0;
      break;

    case 3:
      lights[light].colors[0] = p * 255.0;
      lights[light].colors[1] = q * 255.0;
      lights[light].colors[2] = v * 255.0;
      break;
    case 4:
      lights[light].colors[0] = t * 255.0;
      lights[light].colors[1] = p * 255.0;
      lights[light].colors[2] = v * 255.0;
      break;
    case 5:
    default:
      lights[light].colors[0] = v * 255.0;
      lights[light].colors[1] = p * 255.0;
      lights[light].colors[2] = q * 255.0;
      break;
  }

}

void convertXy(uint8_t light)
{
  lights[light].colors[3] = 0;

  int optimal_bri = lights[light].bri;
  if (optimal_bri < 5) {
    optimal_bri = 5;
  }
  float Y = lights[light].y;
  float X = lights[light].x;
  float Z = 1.0f - lights[light].x - lights[light].y;

  // sRGB D65 conversion
  float r =  X * 3.2406f - Y * 1.5372f - Z * 0.4986f;
  float g = -X * 0.9689f + Y * 1.8758f + Z * 0.0415f;
  float b =  X * 0.0557f - Y * 0.2040f + Z * 1.0570f;


  // Apply gamma correction
  r = r <= 0.04045f ? r / 12.92f : pow((r + 0.055f) / (1.0f + 0.055f), 2.4f);
  g = g <= 0.04045f ? g / 12.92f : pow((g + 0.055f) / (1.0f + 0.055f), 2.4f);
  b = b <= 0.04045f ? b / 12.92f : pow((b + 0.055f) / (1.0f + 0.055f), 2.4f);

  float maxv = 0;// calc the maximum value of r g and b
  if (r > maxv) maxv = r;
  if (g > maxv) maxv = g;
  if (b > maxv) maxv = b;

  if (maxv > 0) {// only if maximum value is greater than zero, otherwise there would be division by zero
    r /= maxv;   // scale to maximum so the brightest light is always 1.0
    g /= maxv;
    b /= maxv;
  }

  r = r < 0 ? 0 : r;
  g = g < 0 ? 0 : g;
  b = b < 0 ? 0 : b;

  lights[light].colors[0] = (int) (r * optimal_bri); lights[light].colors[1] = (int) (g * optimal_bri); lights[light].colors[2] = (int) (b * optimal_bri);
}

void convertCt(uint8_t light) {
  lights[light].colors[3] = lights[light].bri;
  int hectemp = 10000 / lights[light].ct;
  int r, g, b;
  if (hectemp <= 66) {
    r = 255;
    g = 99.4708025861 * log(hectemp) - 161.1195681661;
    b = hectemp <= 19 ? 0 : (138.5177312231 * log(hectemp - 10) - 305.0447927307);
  } else {
    r = 329.698727446 * pow(hectemp - 60, -0.1332047592);
    g = 288.1221695283 * pow(hectemp - 60, -0.0755148492);
    b = 255;
  }
  r = r > 255 ? 255 : r;
  g = g > 255 ? 255 : g;
  b = b > 255 ? 255 : b;
  lights[light].colors[0] = r * (lights[light].bri / 255.0f); lights[light].colors[1] = g * (lights[light].bri / 255.0f); lights[light].colors[2] = b * (lights[light].bri / 255.0f);
}

void handleNotFound() {
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
}

void infoLight(RgbwColor color) {
  // Flash the strip in the selected color. White = booted, green = WLAN connected, red = WLAN could not connect
  for (int i = 0; i < pixelCount; i++)
  {
    strip->SetPixelColor(i, color);
    strip->Show();
    delay(10);
    strip->SetPixelColor(i, black);
    strip->Show();
  }
}


void apply_scene(uint8_t new_scene) {
  for (uint8_t light = 0; light < lightsCount; light++) {
    if ( new_scene == 1) {
      lights[light].bri = 254; lights[light].ct = 346; lights[light].colorMode = 2; convertCt(light);
    } else if ( new_scene == 2) {
      lights[light].bri = 254; lights[light].ct = 233; lights[light].colorMode = 2; convertCt(light);
    }  else if ( new_scene == 3) {
      lights[light].bri = 254; lights[light].ct = 156; lights[light].colorMode = 2; convertCt(light);
    }  else if ( new_scene == 4) {
      lights[light].bri = 77; lights[light].ct = 367; lights[light].colorMode = 2; convertCt(light);
    }  else if ( new_scene == 5) {
      lights[light].bri = 254; lights[light].ct = 447; lights[light].colorMode = 2; convertCt(light);
    }  else if ( new_scene == 6) {
      lights[light].bri = 1; lights[light].x = 0.561; lights[light].y = 0.4042; lights[light].colorMode = 1; convertXy(light);
    }  else if ( new_scene == 7) {
      lights[light].bri = 203; lights[light].x = 0.380328; lights[light].y = 0.39986; lights[light].colorMode = 1; convertXy(light);
    }  else if ( new_scene == 8) {
      lights[light].bri = 112; lights[light].x = 0.359168; lights[light].y = 0.28807; lights[light].colorMode = 1; convertXy(light);
    }  else if ( new_scene == 9) {
      lights[light].bri = 142; lights[light].x = 0.267102; lights[light].y = 0.23755; lights[light].colorMode = 1; convertXy(light);
    }  else if ( new_scene == 10) {
      lights[light].bri = 216; lights[light].x = 0.393209; lights[light].y = 0.29961; lights[light].colorMode = 1; convertXy(light);
    } else {
      lights[light].bri = 144; lights[light].ct = 447; lights[light].colorMode = 2; convertCt(light);
    }
  }
}

void processLightdata(uint8_t light, float transitiontime) {
  transitiontime *= 17 - (pixelCount / 40); //every extra led add a small delay that need to be counted
  if (lights[light].colorMode == 1 && lights[light].lightState == true) {
    convertXy(light);
  } else if (lights[light].colorMode == 2 && lights[light].lightState == true) {
    convertCt(light);
  } else if (lights[light].colorMode == 3 && lights[light].lightState == true) {
    convertHue(light);
  }
  for (uint8_t i = 0; i < 4; i++) {
    if (lights[light].lightState) {
      lights[light].stepLevel[i] = ((float)lights[light].colors[i] - lights[light].currentColors[i]) / transitiontime;
    } else {
      lights[light].stepLevel[i] = lights[light].currentColors[i] / transitiontime;
    }
  }
}

RgbwColor blending(float left[4], float right[4], uint8_t pixel) {
  uint8_t result[4];
  for (uint8_t i = 0; i < 4; i++) {
    float percent = (float) pixel / (float) (transitionLeds + 1);
    result[i] = (left[i] * (1.0f - percent) + right[i] * percent) / 2;
  }
  return RgbwColor((uint8_t)result[0], (uint8_t)result[1], (uint8_t)result[2], (uint8_t)result[3]);
}

RgbwColor convInt(float color[4]) {
  return RgbwColor((uint8_t)color[0], (uint8_t)color[1], (uint8_t)color[2], (uint8_t)color[3]);
}

RgbwColor convFloat(float color[4]) {
  return RgbwColor((uint8_t)color[0], (uint8_t)color[1], (uint8_t)color[2], (uint8_t)color[3]);
}

void lightEngine() {
  for (int light = 0; light < lightsCount; light++) {
    if (lights[light].lightState) {
      if (lights[light].colors[0] != lights[light].currentColors[0] || lights[light].colors[1] != lights[light].currentColors[1] || lights[light].colors[2] != lights[light].currentColors[2]  || lights[light].colors[3] != lights[light].currentColors[3]) {
        inTransition = true;
        for (uint8_t k = 0; k < 4; k++) {
          if (lights[light].colors[k] != lights[light].currentColors[k]) lights[light].currentColors[k] += lights[light].stepLevel[k];
          if ((lights[light].stepLevel[k] > 0.0 && lights[light].currentColors[k] > lights[light].colors[k]) || (lights[light].stepLevel[k] < 0.0 && lights[light].currentColors[k] < lights[light].colors[k])) lights[light].currentColors[k] = lights[light].colors[k];
        }
        if (lightsCount > 1) {
          if (light == 0) {
            for (uint8_t pixel = 0; pixel < lightLedsCount + transitionLeds / 2; pixel++) {
              if (pixel < lightLedsCount - transitionLeds / 2) {
                strip->SetPixelColor(pixel, convFloat(lights[light].currentColors));
              } else {
                strip->SetPixelColor(pixel, blending(lights[0].currentColors, lights[1].currentColors, pixel + 1 - (lightLedsCount - transitionLeds / 2 )));
              }
            }
          } else if (light == lightsCount - 1) {
            for (uint8_t pixel = 0; pixel < lightLedsCount + transitionLeds / 2 ; pixel++) {
              if (pixel < transitionLeds) {
                strip->SetPixelColor(pixel - transitionLeds / 2 + lightLedsCount * light, blending( lights[light - 1].currentColors, lights[light].currentColors, pixel + 1));
              } else {
                strip->SetPixelColor(pixel - transitionLeds / 2 + lightLedsCount * light, convFloat(lights[light].currentColors));
              }
            }
          } else {
            for (uint8_t pixel = 0; pixel < lightLedsCount + transitionLeds; pixel++) {
              if (pixel < transitionLeds) {
                strip->SetPixelColor(pixel - transitionLeds / 2 + lightLedsCount * light,  blending( lights[light - 1].currentColors, lights[light].currentColors, pixel + 1));
              } else if (pixel > lightLedsCount - 1) {
                strip->SetPixelColor(pixel - transitionLeds / 2 + lightLedsCount * light,  blending( lights[light].currentColors, lights[light + 1].currentColors, pixel + 1 - lightLedsCount));
              } else  {
                strip->SetPixelColor(pixel - transitionLeds / 2 + lightLedsCount * light, convFloat(lights[light].currentColors));
              }
            }
          }
        } else {
          strip->ClearTo(convFloat(lights[light].currentColors), 0, pixelCount - 1);
        }
        strip->Show();
      }
    } else {
      if (lights[light].currentColors[0] != 0 || lights[light].currentColors[1] != 0 || lights[light].currentColors[2] != 0  || lights[light].currentColors[3] != 0) {
        inTransition = true;
        for (uint8_t k = 0; k < 4; k++) {
          if (lights[light].currentColors[k] != 0) lights[light].currentColors[k] -= lights[light].stepLevel[k];
          if (lights[light].currentColors[k] < 0) lights[light].currentColors[k] = 0;
        }
        if (lightsCount > 1) {
          if (light == 0) {
            for (uint8_t pixel = 0; pixel < lightLedsCount + transitionLeds / 2; pixel++) {
              if (pixel < lightLedsCount - transitionLeds / 2) {
                strip->SetPixelColor(pixel, convFloat(lights[light].currentColors));
              } else {
                strip->SetPixelColor(pixel,  blending( lights[light].currentColors, lights[light + 1].currentColors, pixel + 1 - (lightLedsCount - transitionLeds / 2 )));
              }
            }
          } else if (light == lightsCount - 1) {
            for (uint8_t pixel = 0; pixel < lightLedsCount + transitionLeds / 2 ; pixel++) {
              if (pixel < transitionLeds) {
                strip->SetPixelColor(pixel - transitionLeds / 2 + lightLedsCount * light,  blending( lights[light - 1].currentColors, lights[light].currentColors, pixel + 1));
              } else {
                strip->SetPixelColor(pixel - transitionLeds / 2 + lightLedsCount * light, convFloat(lights[light].currentColors));
              }
            }
          } else {
            for (uint8_t pixel = 0; pixel < lightLedsCount + transitionLeds; pixel++) {
              if (pixel < transitionLeds) {
                strip->SetPixelColor(pixel - transitionLeds / 2 + lightLedsCount * light,  blending( lights[light - 1].currentColors, lights[light].currentColors, pixel + 1));
              } else if (pixel > lightLedsCount - 1) {
                strip->SetPixelColor(pixel - transitionLeds / 2 + lightLedsCount * light,  blending( lights[light].currentColors, lights[light + 1].currentColors, pixel + 1 - lightLedsCount));
              } else  {
                strip->SetPixelColor(pixel - transitionLeds / 2 + lightLedsCount * light, convFloat(lights[light].currentColors));
              }
            }
          }
        } else {
          strip->ClearTo(convFloat(lights[light].currentColors), 0, pixelCount - 1);
        }
        strip->Show();
      }
    }
  }
  if (inTransition) {
    delay(6);
    inTransition = false;
  } 
    //read switch "ON"
    else if (hwSwitch == true) { 
    if (digitalRead(onPin) == HIGH) {
      int i = 0;
      while (digitalRead(onPin) == HIGH && i < 30) {
        delay(20); //20*30=600ms
        i++;
      }
    //end of read switch "ON"
    //i<30 means short press
    //i=30 means long press
      for (int light = 0; light < lightsCount; light++) {
        if (i < 30) { 
          // there was a short press
          lights[light].lightState = true;
        }
        else {
          // there was a long press
          lights[light].bri += 56;
          if (lights[light].bri > 255) {
            // don't increase the brightness more then maximum value
            lights[light].bri = 255;
          }
        }
      }
    } 
    //read switch "OFF"
    else if (digitalRead(offPin) == HIGH) {
      int i = 0;
      while (digitalRead(offPin) == HIGH && i < 30) {
        delay(20);
        i++;
      }
    //end of read switch "OFF"
    //i<30 means short press
    //i=30 means long press
        for (int light = 0; light < lightsCount; light++) {
        if (i < 30) {
          // there was a short press
          lights[light].lightState = false;
        }
        else {
          // there was a long press
          lights[light].bri -= 56;
          if (lights[light].bri < 1) {
            // don't decrease the brightness less than minimum value.
            lights[light].bri = 1;
          }
        }
      }
    }
  }
}

void saveState() {
  DynamicJsonDocument json(1024);
  for (uint8_t i = 0; i < lightsCount; i++) {
    JsonObject light = json.createNestedObject((String)i);
    light["on"] = lights[i].lightState;
    light["bri"] = lights[i].bri;
    if (lights[i].colorMode == 1) {
      light["x"] = lights[i].x;
      light["y"] = lights[i].y;
    } else if (lights[i].colorMode == 2) {
      light["ct"] = lights[i].ct;
    } else if (lights[i].colorMode == 3) {
      light["hue"] = lights[i].hue;
      light["sat"] = lights[i].sat;
    }
  }
  File stateFile = SPIFFS.open("/state.json", "w");
  serializeJson(json, stateFile);

}


void restoreState() {
  File stateFile = SPIFFS.open("/state.json", "r");
  if (!stateFile) {
    saveState();
    return;
  }

  DynamicJsonDocument json(1024);
  DeserializationError error = deserializeJson(json, stateFile.readString());
  if (error) {
    //Serial.println("Failed to parse config file");
    return;
  }
  for (JsonPair state : json.as<JsonObject>()) {
    const char* key = state.key().c_str();
    int lightId = atoi(key);
    JsonObject values = state.value();
    lights[lightId].lightState = values["on"];
    lights[lightId].bri = (uint8_t)values["bri"];
    if (values.containsKey("x")) {
      lights[lightId].x = values["x"];
      lights[lightId].y = values["y"];
      lights[lightId].colorMode = 1;
    } else if (values.containsKey("ct")) {
      lights[lightId].ct = values["ct"];
      lights[lightId].colorMode = 2;
    } else {
      if (values.containsKey("hue")) {
        lights[lightId].hue = values["hue"];
        lights[lightId].colorMode = 3;
      }
      if (values.containsKey("sat")) {
        lights[lightId].sat = (uint8_t) values["sat"];
        lights[lightId].colorMode = 3;
      }
    }
  }
}


bool saveConfig() {
  DynamicJsonDocument json(1024);
  json["name"] = lightName;
  json["startup"] = startup;
  json["scene"] = scene;
  json["on"] = onPin;
  json["off"] = offPin;
  json["hw"] = hwSwitch;
  json["dhcp"] = useDhcp;
  json["lightsCount"] = lightsCount;
  json["pixelCount"] = pixelCount;
  json["transLeds"] = transitionLeds;
  JsonArray addr = json.createNestedArray("addr");
  addr.add(address[0]);
  addr.add(address[1]);
  addr.add(address[2]);
  addr.add(address[3]);
  JsonArray gw = json.createNestedArray("gw");
  gw.add(gateway[0]);
  gw.add(gateway[1]);
  gw.add(gateway[2]);
  gw.add(gateway[3]);
  JsonArray mask = json.createNestedArray("mask");
  mask.add(submask[0]);
  mask.add(submask[1]);
  mask.add(submask[2]);
  mask.add(submask[3]);
  File configFile = SPIFFS.open("/config.json", "w");
  if (!configFile) {
    //Serial.println("Failed to open config file for writing");
    return false;
  }

  serializeJson(json, configFile);
  return true;
}

bool loadConfig() {
  File configFile = SPIFFS.open("/config.json", "r");
  if (!configFile) {
    //Serial.println("Create new file with default values");
    return saveConfig();
  }

  if (configFile.size() > 1024) {
    Serial.println("Config file size is too large");
    return false;
  }

  DynamicJsonDocument json(1024);
  DeserializationError error = deserializeJson(json, configFile.readString());
  if (error) {
    //Serial.println("Failed to parse config file");
    return false;
  }

  strcpy(lightName, json["name"]);
  startup = (uint8_t) json["startup"];
  scene  = (uint8_t) json["scene"];
  onPin = (uint8_t) json["on"];
  offPin = (uint8_t) json["off"];
  hwSwitch = json["hw"];
  lightsCount = (uint16_t) json["lightsCount"];
  pixelCount = (uint16_t) json["pixelCount"];
  transitionLeds = (uint8_t) json["transLeds"];
  useDhcp = json["dhcp"];
  address = {json["addr"][0], json["addr"][1], json["addr"][2], json["addr"][3]};
  submask = {json["mask"][0], json["mask"][1], json["mask"][2], json["mask"][3]};
  gateway = {json["gw"][0], json["gw"][1], json["gw"][2], json["gw"][3]};
  return true;
}

void ChangeNeoPixels(uint16_t newCount)
{
  if (strip != NULL) {
    delete strip; // delete the previous dynamically created strip
  }
  strip = new NeoPixelBus<NeoGrbwFeature, NeoEsp8266Uart1Ws2812xMethod>(newCount); // and recreate with new count
  strip->Begin();
}
RgbwColor blendingEntert(float left[4], float right[4], float pixel) {
  uint8_t result[4];
  for (uint8_t i = 0; i < 3; i++) {
    float percent = (float) pixel / (float) (transitionLeds + 1);
    result[i] = (left[i] * (1.0f - percent) + right[i] * percent) / 2;
  }
  return RgbwColor((uint8_t)result[0], (uint8_t)result[1], (uint8_t)result[2], 0);
}

void entertainment() {
  uint8_t packetSize = Udp.parsePacket();
  if (packetSize) {
    if (!entertainmentRun) {
      entertainmentRun = true;
    }
    lastEPMillis = millis();
    Udp.read(packetBuffer, packetSize);
    for (uint8_t i = 0; i < packetSize / 4; i++) {
      lights[packetBuffer[i * 4]].currentColors[0] = packetBuffer[i * 4 + 1];
      lights[packetBuffer[i * 4]].currentColors[1] = packetBuffer[i * 4 + 2];
      lights[packetBuffer[i * 4]].currentColors[2] = packetBuffer[i * 4 + 3];
    }
    for (uint8_t light = 0; light < lightsCount; light++) {
      if (lightsCount > 1) {
        if (light == 0) {
          for (uint8_t pixel = 0; pixel < lightLedsCount + transitionLeds / 2; pixel++) {
            if (pixel < lightLedsCount - transitionLeds / 2) {
              strip->SetPixelColor(pixel, convInt(lights[light].currentColors));
            } else {
              strip->SetPixelColor(pixel, blendingEntert(lights[0].currentColors, lights[1].currentColors, pixel + 1 - (lightLedsCount - transitionLeds / 2 )));
            }
          }
        } else if (light == lightsCount - 1) {
          for (uint8_t pixel = 0; pixel < lightLedsCount - transitionLeds / 2 ; pixel++) {
            strip->SetPixelColor(pixel + transitionLeds / 2 + lightLedsCount * light, convInt(lights[light].currentColors));
          }
        } else {
          for (uint8_t pixel = 0; pixel < lightLedsCount; pixel++) {
            if (pixel < lightLedsCount - transitionLeds) {
              strip->SetPixelColor(pixel + transitionLeds / 2 + lightLedsCount * light, convInt(lights[light].currentColors));
            } else {
              strip->SetPixelColor(pixel + transitionLeds / 2 + lightLedsCount * light, blendingEntert(lights[light].currentColors, lights[light + 1].currentColors, pixel - (lightLedsCount - transitionLeds ) + 1));
            }
          }
        }
      } else {
        strip->ClearTo(RgbwColor(lights[0].colors[0], lights[0].colors[1], lights[0].colors[2], 0), 0, lightLedsCount - 1);
      }
    }
    strip->Show();
  }
}

use of __FlashStringHelper is ambiguous

Hello,

Love your AceButton library!

One minor issue with the 1.10.0 release: In AceButton.h you define class __FlashStringHelper in the global namespace, but in AceButton.cpp you define it in the ace_button namespace.

Depending on how restrictive your compiler is, this may result in a compiler error. In my case I'm seeing this:
.pio/libdeps/debug/AceButton/src/ace_button/AceButton.h:127:12: error: reference to '__FlashStringHelper' is ambiguous

The issue can be fixed by moving __FlashStringHelper into the ace_button namespace in AceButton.h

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.