wobblyhead hg > arduino > Button
changeset 0:968a3e3eb8ef
initial - v1.0
| author | ark |
|---|---|
| date | 2017-01-22 23:39 +0100 |
| parents | |
| children | 284066a427c8 |
| files | Button.cpp Button.h library.properties |
| diffstat | 3 files changed, 133 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Button.cpp Sun Jan 22 23:39:37 2017 +0100 @@ -0,0 +1,55 @@ +#if defined(ARDUINO) && ARDUINO >= 100 +#include "Arduino.h" +#else +#include "WProgram.h" +#endif + +#include "Button.h" + +/** + see Button.h for description. +*/ + +Button::Button() {} + +void Button::setup(byte pin) { + this->setup(pin, true); +} + +void Button::setup(byte pin, bool pu) { + this->pin = pin; + this->isPullup = pu; + pinMode(this->pin, this->isPullup?INPUT_PULLUP:INPUT); + this->isPressed = this->readState(); + this->lastChangeMs = 0; +} + +void Button::update() { + this->update(millis()); +} + + +void Button::update(unsigned long ms) { + bool s = this->readState(); + // process state change + if(this->isPressed != s) { + // if last state change is older than threshold, accept new state + // change is not a bounce. + if(ms - this->lastChangeMs >= Button::BOUNCE_THRESHOLD) { + this->isPressed = s; + this->lastChangeMs = ms; + } + else { + Serial.print("ignoring bounce at "); + Serial.println(ms); + } + } +} + + +bool Button::readState() { + bool s = digitalRead(this->pin); + if(this->isPullup) s = !s; + return s; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Button.h Sun Jan 22 23:39:37 2017 +0100 @@ -0,0 +1,70 @@ +#ifndef Button_h +#define Button_h + +/** + utility class for debouncing a button based on pin state and time. + + notes time of state change. ignores further state changes for + Button::BOUNCE_THRESHOLD miliseconds. + + (c) 2017 arkadiusz@wobblyhead.net +*/ +class Button { + public: + // stabilised button state + bool isPressed; + // millis() time of last state change + unsigned long lastChangeMs; + + /** + create an uninitialised Button. + setup() must be called after this to make it usable. + */ + Button(); + + /** + setup button instance. + assumes internal pullup. + @param {byte} pin button input pin + */ + void setup(byte pin); + + /** + setup button instance. + @param {byte} pin button input pin + @param {bool} pu if true, button uses internal pullup + */ + void setup(byte pin, bool pu); + + /** + check pin and update button state. + calls millis() before calling update(ulong). if a recent millis() + value is known, call update(ulong) directly to avoid the additional + millis() call. + */ + void update(); + + /** + check pin and update button state. + @param {unsigned long} ms current millis() value + */ + void update(unsigned long ms); + + /** bounce threshold in miliseconds. state changes are ignored for that + * long after a change. + */ + const unsigned long BOUNCE_THRESHOLD = 100; + + private: + byte pin; + bool isPullup; + + /** + read pin state, translate to pressed state. + @return {bool} true if buton is pressed + */ + bool readState(); + +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/library.properties Sun Jan 22 23:39:37 2017 +0100 @@ -0,0 +1,8 @@ +name=Button +version=1.0 +author=Arkadiusz Juszczyk <arkadiusz@wobblyhead.net> +maintainer=Arkadiusz Juszczyk <arkadiusz@wobblyhead.net> +sentence=Debounced button for Arduino +category=Signal Input/Output +url=https://hg.wobblyhead.net/arduino/Button +architectures=*
