Giter VIP home page Giter VIP logo

button's Introduction

r89m Buttons

This library makes working with buttons easy.

Easily handle button events such as onPress, onHold, onHoldRepeat and onRelease. The same callback functions can be used with multiple buttons, helping to keep your code cleaner and more manageable.

Swap button types whenever you want - there's currently 3 supported types - PushButton, CapacitiveButton and MPR121Button but it is easy to create your own.

Examples

Here's some basic examples to show you just how easy using this library is!

#include <Button.h>
#include <ButtonEventCallback.h>
#include <BasicButton.h>

// Create an instance of BasicButton reading digital pin 5
BasicButton button = BasicButton(5);

void setup(){

    // When the button is first pressed, call the function onButtonPressed
    button.onPress(onButtonPressed);
}

void loop(){
    // Check the state of the button
    button.update();
}

void onButtonPressed(Button& btn){

    // The button was pressed - do something!
}
#include <Button.h>
#include <ButtonEventCallback.h>
#include <BasicButton.h>

// Create an instance of BasicButton reading digital pin 5
BasicButton button = BasicButton(5);

void setup(){

    // Open up the serial port so that we can write to it
    Serial.begin(9600);

    // When the button is first pressed, call the function onButtonPressed
    button.onPress(onButtonPressed);
    // Once the button has been held for 1 second (1000ms) call onButtonHeld. Call it again every 0.5s (500ms) until it is let go
    button.onHoldRepeat(1000, 500, onButtonHeld);
    // When the button is released, call onButtonReleased
    button.onRelease(onButtonReleased);
}

void loop(){
    // Check the state of the button
    button.update();
}

// btn is a reference to the button that fired the event. That means you can use the same event handler for many buttons
void onButtonPressed(Button& btn){

    Serial.println("button pressed");
}

// duration reports back how long it has been since the button was originally pressed.
// repeatCount tells us how many times this function has been called by this button.
void onButtonHeld(Button& btn, uint16_t duration, uint16_t repeatCount){

    Serial.print("button has been held for ");
    Serial.print(duration);
    Serial.print(" ms; this event has been fired ");
    Serial.print(repeatCount);
    Serial.println(" times");
}

// duration reports back the total time that the button was held down
void onButtonReleased(Button& btn, uint16_t duration){

    Serial.print("button released after ");
    Serial.print(duration);
    Serial.println(" ms");
}

Built-in Button Types

A simple push button debounced using the Bounce library

Check out the examples!

A capacitive touch button utilising the CapSense library

Check out the examples!

A capacitive touch button utilising the MPR121 touch switch IC

Check out the examples!

BasicButton (not recommended!)

A simple button using digitalRead() to determine the state of the button. Does not perform any kind of debouncing, so false positives and multiple calls are likely. Use PushButton instead for a simple button.

This is only included so that you can get an example up-and-running quickly without needing any other dependencies.

Methods

boolean update()

Update the button state - this will call any callbacks that are necessary. Returns true if the state changes.

boolean is(Button& btn)

Return whether or not the button is the same as the btn passed

boolean isPressed()

Return whether or not the button is currently pressed.

Callbacks

CallbackAttachedResponse onPress(onPressCallbackFunction)

onPressCallbackFunction is a function which will be called as soon as the button is pressed. It must be defined with the parameters shown below

void callThisFunctionOnPress(Button& btn){
  // btn is a reference to the button that was pressed.
}

CallbackAttachedResponse onRelease

There are 3 variations of onRelease:

CallbackAttachedResponse onRelease(onReleaseCallbackFunction)

onReleaseCallbackFunction is a function which is called when the button is released. It must be defined with the parameters shown below

void callThisFunctionOnRelease(Button& btn, uint_16t duration){
  // btn is a reference to the button that was pressed
  // duration is how long the button was pressed for
}

CallbackAttachedResponse onRelease(uint_16t wait, onReleaseCallbackFunction)

As above, plus:

wait if the button is held for at-least waitms onReleaseCallbackFunction will be called.

CallbackAttachedResponse onRelease(uint_16t wait, uint_16t max_wait, onReleaseCallbackFunction)

As above, plus:

max_wait if the button is held for more than max_waitms onReleaseCallbackFunction will not be called.

CallbackAttachedResponse onHold(uint_16t duration, onHoldCallbackFunction)

duration how long the button must be held before onHoldCallbackFunction is called. onHoldCallbackFunction is a function which is called when the button is held. It must be defined with the parameters shown below

void callThisFuntionOnHold(Button& btn, uint_16t duration){
  // btn is a reference to the button that was held
  // duration is how long the button was held for
}

CallbackAttachedResponse onHoldAndRepeat(uint_16t duration, uint_16t repeat_every, onHoldAndRepeatCallbackFunction)

duration how long the button must be held before onHoldAndRepeatCallbackFunction is called. repeat_every how long to wait before onHoldAndRepeatCallbackFunction is called repeatedly. onHoldAndRepeatCallbackFunction is a function which is called repeatedly when the button is held. It must be defined with the parameters shown below

void callThisFunctionOnHoldAndRepeat(Button& btn, uint16_t duration, uint8_t repeat_count){
  // btn is a reference to the button that was held
  // duration is how long the button has been held for
  // repeat_count is the number of times the callback has been called
}

Enums

CallbackAttachedResponse

attSuccessful returned when a callback has successfully been attached

attNoMoreRoom returned when a callback could not be attached because there is not enough room. Check MAX_CALLBACKS_PER_BUTTON.

Constants

MAX_CALLBACKS_PER_BUTTON (default=3)

Defines the maximum number of callbacks per button. Increasing this number will allow more callbacks but will use marginally more memory and processing power. This can be changed on a sketch by sketch basis, simply define #MAX_CALLBACKS_PER_BUTTON before your #includes.

button's People

Contributors

c0zm0z avatar ivankravets avatar njh avatar r89m avatar scls19fr avatar webguy16 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

Watchers

 avatar  avatar

button's Issues

Feature Internal button ID

It would be nice to have some sort of button id to track buttons when using the same event callback for all.

Right now I am tracking all my objects, and then use a btn.is() loop manually to ID them. which is cumbersome.

I thought about getting to the bounce pin, but it was too much effort to mod all 3 libraries or use virtuals, and since events return button classes, pushbutton derives are just as much a pain to get to, unless using templates or some pointers ( I don't know c++ all that well )

I figured just adding an ID setter getter to the button class itself would be the easiest way and _update_button_state agnostic

If there is something obvious I am not thinking of, let me know.

List this library in the Arduino IDE Library Manager

The Arduino IDE has a Library Manager making it easy to install and get updates from Libraries. It'd be great if this library and the related libraries were listed in there, it's simple to make this happen there's a quick guide here.

You may want to consider changing the name from "Button" to something easier to search for like "r89m Button".

How to manage with NC (Normally Close) button

Hi,

Sorry for posting on Issues

I'm using your librairy, works like a charm !
Now, I'm stuck on an other type of push button : a NC one
this mean when not pushed, circuit is close, and when pushed, circuit is open

I parse source files, but didn't see an obvious way to fork them
Would you recommand any way ?

Thanks !

Allow number of callbacks available to be defined per button

Use C++ templates to allow the number of callbacks available to be specified per button. This would allow one button within a sketch to have many more callbacks without wasting memory or processing time on buttons that don't need the higher number.

Fix for category warning

When using this in the Arduino IDE 1.6.8 I was getting the following warning:
WARNING: Category 'Input/Output' in library Buttons is not valid. Setting to 'Uncategorized'

I fixed this by changing Line 7 of library.properties from:
category=Input/Output
to
category=Signal Input/Output

Using the library with Interrupt Input.

Dear Concerned,
I have been using this library for the last few years and found its performance very much satisfying.
I am now in need to use interrupt along with this library but unfortunately could not find any way through.

Could you please help me achieve this. I am using ESP8266 with hardware interrupt.

Awaiting prompt response please.

Sarwar

Fix license file

LICENSE.md is currently empty, needs to have licence content put in there. Also, probably worth adding it to the README

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.