Documentation updates and formatting.

Arduino 1.0 compatibility.
This commit is contained in:
Jack Christensen
2012-03-21 20:44:33 -04:00
parent baff7a8076
commit a10684701b
3 changed files with 100 additions and 75 deletions

View File

@@ -1,11 +1,11 @@
/*----------------------------------------------------------------------* /*----------------------------------------------------------------------*
* Button.cpp -- Library for reading momentary contact switches like * * Arduino Button Library v1.0 *
* tactile button switches. Intended for use in state machine * * Jack Christensen Mar 2012 *
* constructs. Use the read() function to read all buttons in the *
* main loop, which should execute as fast as possible, or at least *
* very frequently. *
* * * *
* Jack Christensen 11May2011 * * Library for reading momentary contact switches like tactile button *
* switches. Intended for use in state machine constructs. *
* Use the read() function to read all buttons in the main loop, *
* which should execute as fast as possible. *
* * * *
* 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,23 +13,30 @@
* 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 "button.h"
//the constructor button(pin, puEnabled, invert, dbTime) instantiates a button object. #include "Button.h"
//pin is the Arduino pin the button is connected to.
//puEnabled enables the AVR internal pullup resistor if != 0 (can use true or false). /*----------------------------------------------------------------------*
//invert = 0 interprets a high state as pressed, low as released. * Button(pin, puEnable, invert, dbTime) instantiates a button object. *
//invert != 0 interprets a high state as released, low as pressed (can use true or false). * pin Is the Arduino pin the button is connected to. *
//dbTime is the debounce time in milliseconds. * puEnable Enables the AVR internal pullup resistor if != 0 (can also *
//note that invert cannot be implied from puEnabled since an external pullup could be used. * use true or false). *
button::button(uint8_t pin, uint8_t puEnabled, uint8_t invert, uint32_t dbTime) * invert If invert == 0, interprets a high state as pressed, low as *
* released. If invert != 0, interprets a high state as *
* released, low as pressed (can also use true or false). *
* dbTime Is the debounce time in milliseconds. *
* *
* (Note that invert cannot be implied from puEnable since an external *
* pullup could be used.) *
*----------------------------------------------------------------------*/
Button::Button(uint8_t pin, uint8_t puEnable, uint8_t invert, uint32_t dbTime)
{ {
_pin = pin; _pin = pin;
_puEnabled = puEnabled; _puEnable = puEnable;
_invert = invert; _invert = invert;
_dbTime = dbTime; _dbTime = dbTime;
pinMode(_pin, INPUT); pinMode(_pin, INPUT);
if (_puEnabled != 0) if (_puEnable != 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;
@@ -40,9 +47,11 @@ button::button(uint8_t pin, uint8_t puEnabled, uint8_t invert, uint32_t dbTime)
_lastChange = _time; _lastChange = _time;
} }
//read() returns the state of the button 1 (pressed), or 0 (released), /*----------------------------------------------------------------------*
//does debouncing, and also captures and maintains times, previous states, etc. * read() returns the state of the button, 1==pressed, 0==released, *
uint8_t button::read(void) * does debouncing, captures and maintains times, previous states, etc. *
*----------------------------------------------------------------------*/
uint8_t Button::read(void)
{ {
static uint32_t ms; static uint32_t ms;
static uint8_t pinVal; static uint8_t pinVal;
@@ -72,47 +81,58 @@ uint8_t button::read(void)
} }
} }
//isPressed() and isReleased() check the button state when it was last read, /*----------------------------------------------------------------------*
//and return false (0) or true (!=0) accordingly. * isPressed() and isReleased() check the button state when it was last *
//These functions do not cause the button to be read. * read, and return false (0) or true (!=0) accordingly. *
uint8_t button::isPressed(void) * These functions do not cause the button to be read. *
*----------------------------------------------------------------------*/
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 /*----------------------------------------------------------------------*
//between the last two reads and return false (0) or true (!=0) accordingly. * wasPressed() and wasReleased() check the button state to see if it *
//These functions do not cause the button to be read. * changed between the last two reads and return false (0) or *
uint8_t button::wasPressed(void) * true (!=0) accordingly. *
* These functions do not cause the button to be read. *
*----------------------------------------------------------------------*/
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), /*----------------------------------------------------------------------*
//and has been in that state for the specified time. Returns false (0) or true (1) accordingly. * pressedFor(ms) and releasedFor(ms) check to see if the button is *
//These functions do not cause the button to be read. * pressed (or released), and has been in that state for the specified *
uint8_t button::pressedFor(uint32_t ms) * time in milliseconds. Returns false (0) or true (1) accordingly. *
* These functions do not cause the button to be read. *
*----------------------------------------------------------------------*/
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. /*----------------------------------------------------------------------*
uint32_t button::lastChange(void) * lastChange() returns the time the button last changed state, *
* in milliseconds. *
*----------------------------------------------------------------------*/
uint32_t Button::lastChange(void)
{ {
return _lastChange; return _lastChange;
} }

View File

@@ -10,13 +10,17 @@
* 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. *
*----------------------------------------------------------------------*/ *----------------------------------------------------------------------*/
#ifndef button_h #ifndef Button_h
#define button_h #define Button_h
#include "WProgram.h" #if ARDUINO >= 100
class button #include <Arduino.h>
#else
#include <WProgram.h>
#endif
class Button
{ {
public: public:
button(uint8_t pin, uint8_t puEnabled, uint8_t invert, uint32_t dbTime); Button(uint8_t pin, uint8_t puEnable, uint8_t invert, uint32_t dbTime);
uint8_t read(); uint8_t read();
uint8_t isPressed(); uint8_t isPressed();
uint8_t isReleased(); uint8_t isReleased();
@@ -25,9 +29,10 @@ class button
uint8_t pressedFor(uint32_t ms); uint8_t pressedFor(uint32_t ms);
uint8_t releasedFor(uint32_t ms); uint8_t releasedFor(uint32_t ms);
uint32_t lastChange(); uint32_t lastChange();
private: private:
uint8_t _pin; //arduino pin number uint8_t _pin; //arduino pin number
uint8_t _puEnabled; //internal pullup resistor enabled uint8_t _puEnable; //internal pullup resistor enabled
uint8_t _invert; //if 0, interpret high state as pressed, else interpret low state as pressed uint8_t _invert; //if 0, interpret high state as pressed, else interpret low state as pressed
uint8_t _state; //current button state uint8_t _state; //current button state
uint8_t _lastState; //previous button state uint8_t _lastState; //previous button state

View File

@@ -1,4 +1,4 @@
button KEYWORD1 Button KEYWORD1
read KEYWORD2 read KEYWORD2
isPressed KEYWORD2 isPressed KEYWORD2
isReleased KEYWORD2 isReleased KEYWORD2