mirror of
https://github.com/HASwitchPlate/openHASP.git
synced 2025-07-27 21:26:43 +00:00
Style sweep
This commit is contained in:
parent
cdcc127a26
commit
dfde8c79ba
@ -1,81 +1,73 @@
|
|||||||
/*
|
/*
|
||||||
Button - a small library for Arduino to handle button debouncing
|
Button - a small library for Arduino to handle button debouncing
|
||||||
|
|
||||||
MIT licensed.
|
|
||||||
|
MIT licensed.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "Button.h"
|
#include "Button.h"
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
Button::Button(uint8_t pin)
|
Button::Button(uint8_t pin) : _pin(pin), _delay(100), _state(HIGH), _ignore_until(0), _has_changed(false)
|
||||||
: _pin(pin)
|
{}
|
||||||
, _delay(500)
|
|
||||||
, _state(HIGH)
|
|
||||||
, _ignore_until(0)
|
|
||||||
, _has_changed(false)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void Button::begin()
|
void Button::begin()
|
||||||
{
|
{
|
||||||
pinMode(_pin, INPUT_PULLUP);
|
pinMode(_pin, INPUT_PULLUP);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// public methods
|
// public methods
|
||||||
//
|
//
|
||||||
|
|
||||||
bool Button::read()
|
bool Button::read()
|
||||||
{
|
{
|
||||||
// ignore pin changes until after this delay time
|
// ignore pin changes until after this delay time
|
||||||
if (_ignore_until > millis())
|
if(_ignore_until > millis()) {
|
||||||
{
|
// ignore any changes during this period
|
||||||
// ignore any changes during this period
|
}
|
||||||
}
|
|
||||||
|
// pin has changed
|
||||||
// pin has changed
|
else if(digitalRead(_pin) != _state) {
|
||||||
else if (digitalRead(_pin) != _state)
|
_ignore_until = millis() + _delay;
|
||||||
{
|
_state = !_state;
|
||||||
_ignore_until = millis() + _delay;
|
_has_changed = true;
|
||||||
_state = !_state;
|
}
|
||||||
_has_changed = true;
|
|
||||||
}
|
return _state;
|
||||||
|
|
||||||
return _state;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// has the button been toggled from on -> off, or vice versa
|
// has the button been toggled from on -> off, or vice versa
|
||||||
bool Button::toggled()
|
bool Button::toggled()
|
||||||
{
|
{
|
||||||
read();
|
read();
|
||||||
return has_changed();
|
return has_changed();
|
||||||
}
|
}
|
||||||
|
|
||||||
// mostly internal, tells you if a button has changed after calling the read() function
|
// mostly internal, tells you if a button has changed after calling the read() function
|
||||||
bool Button::has_changed()
|
bool Button::has_changed()
|
||||||
{
|
{
|
||||||
if (_has_changed == true)
|
if(_has_changed == true) {
|
||||||
{
|
_has_changed = false;
|
||||||
_has_changed = false;
|
return true;
|
||||||
return true;
|
}
|
||||||
}
|
return false;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// has the button gone from off -> on
|
// has the button gone from off -> on
|
||||||
bool Button::pressed()
|
bool Button::pressed()
|
||||||
{
|
{
|
||||||
if (read() == PRESSED && has_changed() == true)
|
if(read() == PRESSED && has_changed() == true)
|
||||||
return true;
|
return true;
|
||||||
else
|
else
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// has the button gone from on -> off
|
// has the button gone from on -> off
|
||||||
bool Button::released()
|
bool Button::released()
|
||||||
{
|
{
|
||||||
if (read() == RELEASED && has_changed() == true)
|
if(read() == RELEASED && has_changed() == true)
|
||||||
return true;
|
return true;
|
||||||
else
|
else
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user