Coder Social home page Coder Social logo

code-shift / chatterbutton Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 5 KB

Contacts debounce assistant for stable operation of buttons and other hardware triggers

License: MIT License

C++ 100.00%
arduino arduino-library button buttons debounce debounce-button debounce-input embedded embedded-systems esp32

chatterbutton's Introduction

ChatterButton

Contacts debounce assistant for stable operation of buttons and other hardware triggers.

  • works with buttons on GND or Vdd connect
  • time tracking of button's states
  • can check button's state directly or with callback function
  • can button update from main loop or with hardware interrupt
  • compatible with Arduino and some many other platforms

Usage (see examples folder)

  1. Create instance like: ChatterButton button(CButtonWire::GND);
  2. Set handler if needed: button.setHandler(buttonState);

handler:

void buttonState(bool active) {
	digitalWrite(LED_BUILTIN, active);
    Serial.println("Previous state duration: " + String(button.getPreviousStateDuration()) +" ms");
    Serial.println("<method A> button " + String(active ? "pressed" : "released"));
}
  1. Organize update button state in runloop:
void loop() {
    button.update(digitalRead(BUTTON_PIN));
}
  1. Bingo!

You can check button state in hadler function or direct button poll:

if (button.didChange()) {
	Serial.println("String(button ? "pressed" : "released")); 
}

Some features

  • Select button's connection closed on GND or Vdd (positive) also it use to correct connect inverted (normal close) button in constructor ChatterButton(CButtonWire) // CButtonWire has ::GND and ::Vdd options

  • Request current button state from instance; true - active; once call will reset 'didChange()' flag if (button) { /*do something*/ }

  • Once check button state change; will reset after call 'operator bool()' if (button.didChange()) {...}

  • Force set button state; didChange() doesn't triggered; (use for forced init etc) button.set(bool state);

  • Request duration of current button state; milliseconds button.getStateDuration();

  • Request duration of previous button state; milliseconds button.getPreviousStateDuration();

The MIT License (MIT) Copyright (c) 2022 0xdeb [email protected]

chatterbutton's People

Stargazers

 avatar  avatar

Watchers

 avatar

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.