tabs and comments
This commit is contained in:
105
button.cpp
105
button.cpp
@@ -1,11 +1,11 @@
|
|||||||
/*----------------------------------------------------------------------*
|
/*----------------------------------------------------------------------*
|
||||||
* button.cpp -- Library for reading momentary contact switches like *
|
* Button.cpp -- Library for reading momentary contact switches like *
|
||||||
* tactile button switches. Intended for use in state machine *
|
* tactile button switches. Intended for use in state machine *
|
||||||
* constructs. Use the read() function to read all buttons in the *
|
* constructs. Use the read() function to read all buttons in the *
|
||||||
* main loop, which should execute as fast as possible, or at least *
|
* main loop, which should execute as fast as possible, or at least *
|
||||||
* very frequently. *
|
* very frequently. *
|
||||||
* *
|
* *
|
||||||
* Jack Christensen 11May2011 *
|
* Jack Christensen 11May2011 *
|
||||||
* *
|
* *
|
||||||
* This work is licensed under the Creative Commons Attribution- *
|
* This work is licensed under the Creative Commons Attribution- *
|
||||||
* ShareAlike 3.0 Unported License. To view a copy of this license, *
|
* ShareAlike 3.0 Unported License. To view a copy of this license, *
|
||||||
@@ -13,7 +13,6 @@
|
|||||||
* letter to Creative Commons, 171 Second Street, Suite 300, *
|
* letter to Creative Commons, 171 Second Street, Suite 300, *
|
||||||
* San Francisco, California, 94105, USA. *
|
* San Francisco, California, 94105, USA. *
|
||||||
*----------------------------------------------------------------------*/
|
*----------------------------------------------------------------------*/
|
||||||
#include "WProgram.h"
|
|
||||||
#include "button.h"
|
#include "button.h"
|
||||||
|
|
||||||
//the constructor button(pin, puEnabled, invert, dbTime) instantiates a button object.
|
//the constructor button(pin, puEnabled, invert, dbTime) instantiates a button object.
|
||||||
@@ -25,52 +24,52 @@
|
|||||||
//note that invert cannot be implied from puEnabled since an external pullup could be used.
|
//note that invert cannot be implied from puEnabled since an external pullup could be used.
|
||||||
button::button(uint8_t pin, uint8_t puEnabled, uint8_t invert, uint32_t dbTime)
|
button::button(uint8_t pin, uint8_t puEnabled, uint8_t invert, uint32_t dbTime)
|
||||||
{
|
{
|
||||||
_pin = pin;
|
_pin = pin;
|
||||||
_puEnabled = puEnabled;
|
_puEnabled = puEnabled;
|
||||||
_invert = invert;
|
_invert = invert;
|
||||||
_dbTime = dbTime;
|
_dbTime = dbTime;
|
||||||
pinMode(_pin, INPUT);
|
pinMode(_pin, INPUT);
|
||||||
if (_puEnabled != 0)
|
if (_puEnabled != 0)
|
||||||
digitalWrite(_pin, HIGH); //enable pullup resistor
|
digitalWrite(_pin, HIGH); //enable pullup resistor
|
||||||
_state = digitalRead(_pin);
|
_state = digitalRead(_pin);
|
||||||
if (_invert != 0) _state = !_state;
|
if (_invert != 0) _state = !_state;
|
||||||
_time = millis();
|
_time = millis();
|
||||||
_lastState = _state;
|
_lastState = _state;
|
||||||
_changed = 0;
|
_changed = 0;
|
||||||
_lastTime = _time;
|
_lastTime = _time;
|
||||||
_lastChange = _time;
|
_lastChange = _time;
|
||||||
}
|
}
|
||||||
|
|
||||||
//read() returns the state of the button 1 (pressed), or 0 (released),
|
//read() returns the state of the button 1 (pressed), or 0 (released),
|
||||||
//does debouncing, and also captures and maintains times, previous states, etc.
|
//does debouncing, and also captures and maintains times, previous states, etc.
|
||||||
uint8_t button::read(void)
|
uint8_t button::read(void)
|
||||||
{
|
{
|
||||||
static uint32_t ms;
|
static uint32_t ms;
|
||||||
static uint8_t pinVal;
|
static uint8_t pinVal;
|
||||||
|
|
||||||
ms = millis();
|
ms = millis();
|
||||||
pinVal = digitalRead(_pin);
|
pinVal = digitalRead(_pin);
|
||||||
if (_invert != 0) pinVal = !pinVal;
|
if (_invert != 0) pinVal = !pinVal;
|
||||||
if (ms < _lastChange + _dbTime) {
|
if (ms < _lastChange + _dbTime) {
|
||||||
_lastTime = _time;
|
_lastTime = _time;
|
||||||
_time = ms;
|
_time = ms;
|
||||||
_changed = 0;
|
_changed = 0;
|
||||||
return _state;
|
return _state;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
_lastTime = _time;
|
_lastTime = _time;
|
||||||
_lastState = _state;
|
_lastState = _state;
|
||||||
_state = pinVal;
|
_state = pinVal;
|
||||||
_time = ms;
|
_time = ms;
|
||||||
if (_state != _lastState) {
|
if (_state != _lastState) {
|
||||||
_lastChange = ms;
|
_lastChange = ms;
|
||||||
_changed = 1;
|
_changed = 1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
_changed = 0;
|
_changed = 0;
|
||||||
}
|
}
|
||||||
return _state;
|
return _state;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//isPressed() and isReleased() check the button state when it was last read,
|
//isPressed() and isReleased() check the button state when it was last read,
|
||||||
@@ -78,12 +77,12 @@ uint8_t button::read(void)
|
|||||||
//These functions do not cause the button to be read.
|
//These functions do not cause the button to be read.
|
||||||
uint8_t button::isPressed(void)
|
uint8_t button::isPressed(void)
|
||||||
{
|
{
|
||||||
return _state == 0 ? 0 : 1;
|
return _state == 0 ? 0 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t button::isReleased(void)
|
uint8_t button::isReleased(void)
|
||||||
{
|
{
|
||||||
return _state == 0 ? 1 : 0;
|
return _state == 0 ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//wasPressed() and wasReleased() check the button state to see if it changed
|
//wasPressed() and wasReleased() check the button state to see if it changed
|
||||||
@@ -91,12 +90,12 @@ uint8_t button::isReleased(void)
|
|||||||
//These functions do not cause the button to be read.
|
//These functions do not cause the button to be read.
|
||||||
uint8_t button::wasPressed(void)
|
uint8_t button::wasPressed(void)
|
||||||
{
|
{
|
||||||
return _state && _changed;
|
return _state && _changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t button::wasReleased(void)
|
uint8_t button::wasReleased(void)
|
||||||
{
|
{
|
||||||
return !_state && _changed;
|
return !_state && _changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
//pressedFor(ms) and releasedFor(ms) check to see if the button is pressed (or released),
|
//pressedFor(ms) and releasedFor(ms) check to see if the button is pressed (or released),
|
||||||
@@ -104,16 +103,16 @@ uint8_t button::wasReleased(void)
|
|||||||
//These functions do not cause the button to be read.
|
//These functions do not cause the button to be read.
|
||||||
uint8_t button::pressedFor(uint32_t ms)
|
uint8_t button::pressedFor(uint32_t ms)
|
||||||
{
|
{
|
||||||
return (_state == 1 && _time - _lastChange >= ms) ? 1 : 0;
|
return (_state == 1 && _time - _lastChange >= ms) ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t button::releasedFor(uint32_t ms)
|
uint8_t button::releasedFor(uint32_t ms)
|
||||||
{
|
{
|
||||||
return (_state == 0 && _time - _lastChange >= ms) ? 1 : 0;
|
return (_state == 0 && _time - _lastChange >= ms) ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//lastChange() returns the time of the last state change in milliseconds.
|
//lastChange() returns the time of the last state change in milliseconds.
|
||||||
uint32_t button::lastChange(void)
|
uint32_t button::lastChange(void)
|
||||||
{
|
{
|
||||||
return _lastChange;
|
return _lastChange;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user