Skip to content

An easy to use but comprehensive Event Library for Buttons, Encoders, Encoder Buttons, Analog Inputs, Joysticks and Switches with built in debouncing.

License

Notifications You must be signed in to change notification settings

Stutchbury/InputEvents

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

96 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The InputEvents Library

Picture of button, encoder, potentiometer, joystick and switch

InputEvents takes the hassle out of handling buttons, switches, encoders, joysticks, and more — whether you’re just starting out or building advanced projects.

Instead of writing your own debouncing, timing, or multi-click logic, InputEvents turns all those complex, messy details into simple, reliable events like clicked, long press, double-clicked, or encoder turned.

You get clean, maintainable code, fewer bugs, and the freedom to focus on your actual project - just four lines of code and a function or class method gives you easy access to all events for an input. It works across Arduino, ESP32, ESP8266, Teensy or any microcontroller that is supported by the Arduino framework.

Although this library is primarily for physical inputs, all it really cares about is the pin(s). So if you have a digital input, use an EventSwitch or EventButton and for an analog input, use an EventAnalog. The EventEncoder could be connected to any encoder but I have not (yet) attached it to a motor...

Since v1.6.0 (Release Notes), InputEvents includes the EncoderAdapter so there are no longer any explicit dependencies, however, if you are using EventEncoder or EventEncoderButton directly attached to GPIO pins, by default, you will need to install Paul Stoffregen's Encoder library. Other encoder libraries can be used via a simple encoder adapter.

Encoders can now be connected to a GPIO expander but this should only be used if you really have to!

v1.5.2 (Release Notes) added adapters for Adafruit's and Rob Tillaart's GPIO Expander libraries (MCP23017, PCF8574 & PCF8575). Thank you to @HubertJH for helping with the testing - much appreciated.

Since v1.5.0 (Release Notes), buttons and switches can be attached to GPIO Expanders to be used with EventButton and EventSwitch. The button of the EventEncoderButton can also be connected via the GPIO Expander but the encoder itself will continue to rely on the underlying encoder library pin connections. Many thanks to @leon-gh for his contributon to this - much appreciated.

Please see GPIO Expander Adapter docs for more details.

Since v1.4.0 InputEvents uses a default (but optional) built in debouncer. If you really want to, this can be substituted with a Debouncer of your choice via a DebounceAdapter. Continued thanks to Thomas Fredericks and his Bounce2 library which inspired the original EventButton library!

Paul Stoffregen's Encoder library is the default for EventEncoder and EventButtonEncoder classes, so many, many thanks for releasing the code.

Please see Encoder Adapter docs on using encoder libraries and additional notes on using PJRC's Encoder library with InputEvents.

Also a huge thanks to @kfoltman for genty guiding me towards 'better' solutions to issues I was having and the 'interesting' approaches I was trying.

I am standing on the shoulders of giants.

Not heard of the term 'event' programming? Here's a quick primer: Event Programming 101. It is by far the easiest way to get your project working with external controls.

Other InputEvents Libraries

There are two other libraries in the InputEvents family:

  1. InputEventsTouchUI - allows a touch screen to fire button events plus DRAG and DRAGGED and also includes a UI abstraction layer for GUI elements like regions, widgets and icons.
  2. SerialMessageEvents - takes character (char) input from any serial source via a serial adapter, creating a message from the stream, pass it to your message consumer class or method and optionally pass decoded key/value pairs to your key/value consumer.

SUPPORT

Feedback and bug reports are welcome or if you have any questions, drop by for a chat on Discord Discord Logo

If you're in a hurry, please see the full Doxygen generated API documentation.

INSTALLATION

Arduino IDE

Install the InputEvents and Encoder (if you are using EventEncoder or EventEncoderButton) libraries via the Arduino IDE Libary Manager.

PlatformIO

Add the following to your platformio.ini file:

lib_deps = 
	paulstoffregen/Encoder@^1.4.4
	stutchbury/InputEvents@^1.5.2

Paul Stoffregen's Encoder library is the default for EventEncoder and EventEncoderButton but is not an explicit dependency so needs to be installed separately.

BASIC USAGE

#include <EventButton.h>
// Create a button 
EventButton myButton(<YOUR_BUTTON_PIN>);
// Create a callback function
void onButtonCallback(InputEventType et, EventButton& eb) {
  Serial.println("A button event was fired!");
}
void setup() {
    myButton.begin();
    // Link the button's callback to function defined above
    myButton.setCallback(onButtonCallback);
}
void loop() {
    // Call the button's update() method
    myButton.update();
}

That's it! A function and four lines of code and you have a button connected and ready to process events.

Note: By default, buttons and switches are wired between the GPIO pin & GND using the MCU's internal INPUT_PULLUP resistors. Please see EventButton/EventSwitch/PinAdapter docs if you need to use pull down resistors.

Input Classes

There is one class for each type of physical input. All classes have some common methods for handling enable/disable and idle timeout (detailed below).

An overview of the events each input type fires is in the InputEventType section below with details provided in each the documentation page for each input type.

The EventButton class is used with monentary switches or inputs, usually refered to as 'buttons'.

The EventEncoder class is used for plain encoders such as an MPG or motor encoder. If you use this with an encoder button, no button events will be fired!

The EventAnalog class is generally used for potentiometers but anything that normally connects to an analog pin will suffice. The analog input value is redefined as a limited number of increments or 'slices' so events are fired at specified thresholds rather than 1024 times over the range of the input.

The EventSwitch class is for plain on/off switches or inputs.

The EventEncoderButton class contains an EventEncoder and an EventButton. Certain button events are changed if the encoder is turned while pressed. See InputEventType section below for an overview.

The EventJoystick class contains two EventAnalog(s), enabling very easy use of joysticks with 'interesting' resistance values across their range. The joystick will automatically adjust the extent of the analog range, adjusting slices accordingly. Both X and Y axis can be accessed and configured directly if required.

The InputEventType is an enum class using a uint8_t to identify the type of event. Some are common to all inputs, others are specific to a type of input.

Full details about all event types are here.


Please see the documentation pages or the full Doxygen generated API docs for more detailed information or chat on Discord if you have any questions.

About

An easy to use but comprehensive Event Library for Buttons, Encoders, Encoder Buttons, Analog Inputs, Joysticks and Switches with built in debouncing.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages