Skip to content

Commit 1d35c77

Browse files
committed
Started working on documentation.
1 parent e04af10 commit 1d35c77

File tree

5 files changed

+164
-8
lines changed

5 files changed

+164
-8
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/docs

Doxyfile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Doxygen configuration file for generating documentation.
2+
PROJECT_NAME = "Pushbutton library"
3+
OUTPUT_DIRECTORY = docs
4+
INLINE_INHERITED_MEMB = YES
5+
INPUT = .
6+
USE_MDFILE_AS_MAINPAGE = README.md
7+
RECURSIVE = YES
8+
SOURCE_BROWSER = YES
9+
USE_MATHJAX = YES
10+
GENERATE_LATEX = NO

LICENSE.txt

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Copyright (c) 2014 Pololu Corporation. For more information, see
2+
3+
http://www.pololu.com/
4+
http://forum.pololu.com/
5+
6+
Permission is hereby granted, free of charge, to any person
7+
obtaining a copy of this software and associated documentation
8+
files (the "Software"), to deal in the Software without
9+
restriction, including without limitation the rights to use,
10+
copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the
12+
Software is furnished to do so, subject to the following
13+
conditions:
14+
15+
The above copyright notice and this permission notice shall be
16+
included in all copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25+
OTHER DEALINGS IN THE SOFTWARE.

Pushbutton/Pushbutton.h

+23-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
// Copyright (C) Pololu Corporation. See LICENSE.txt for details.
2+
13
/*! \file Pushbutton.h
24
*
3-
* See the Pushbutton class reference for more information about this library.
5+
* This is the main header file for the Pushbutton library.
46
*
5-
* \class Pushbutton Pushbutton.h
6-
* \brief Read button presses and releases with debouncing
7+
* For an overview of the library's features, see
8+
* https://github.com/pololu/pushbutton-arduino. That is the main repository
9+
* for the library, though copies of the library may exist in other
10+
* repositories.
711
*
812
*/
913

@@ -12,17 +16,19 @@
1216

1317
#include <Arduino.h>
1418

15-
// This ZUMO_BUTTON definition is for the Zumo Shield. It doesn't
16-
// really belong here in this general Pushbutton library and will
17-
// probably be removed in the future.
18-
#define ZUMO_BUTTON 12
19-
2019
#define PULL_UP_DISABLED 0
2120
#define PULL_UP_ENABLED 1
2221

2322
#define DEFAULT_STATE_LOW 0
2423
#define DEFAULT_STATE_HIGH 1
2524

25+
/*! \brief The pin used for the button on the
26+
* [Zumo Shield for Arduino](http://www.pololu.com/product/2504).
27+
*
28+
* This does not really belong here in this general pushbutton library and will
29+
* probably be removed in the future. */
30+
#define ZUMO_BUTTON 12
31+
2632
class PushbuttonStateMachine
2733
{
2834
public:
@@ -36,6 +42,7 @@ class PushbuttonStateMachine
3642
class PushbuttonBase
3743
{
3844
public:
45+
3946
PushbuttonBase();
4047

4148
// wait for button to be pressed, released, or pressed and released
@@ -66,6 +73,7 @@ class PushbuttonBase
6673
PushbuttonStateMachine releaseState;
6774
bool initialized;
6875

76+
// TODO: move all this init stuff down into Pushbutton
6977
inline void init()
7078
{
7179
if (!initialized)
@@ -76,9 +84,14 @@ class PushbuttonBase
7684
}
7785
};
7886

87+
/* \class Pushbutton Pushbutton.h
88+
* \brief Interface with pushbuttons on dedicated pins.
89+
*
90+
*/
7991
class Pushbutton : public PushbuttonBase
8092
{
8193
public:
94+
8295
// constructor; takes arguments specifying whether to enable internal pull-up
8396
// and the default state of the pin that the button is connected to
8497
Pushbutton(uint8_t pin, uint8_t pullUp = PULL_UP_ENABLED,
@@ -87,9 +100,11 @@ class Pushbutton : public PushbuttonBase
87100
virtual void init2();
88101

89102
protected:
103+
90104
virtual bool _isPressed();
91105

92106
private:
107+
93108
uint8_t _pin;
94109
bool _pullUp;
95110
bool _defaultState;

README.md

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Pushbutton library for Arduino
2+
3+
Version: 1.1.0<br/>
4+
Release date: 2014-12-01<br/>
5+
[www.pololu.com](http://www.pololu.com/)
6+
7+
## Summary
8+
9+
This is a library for the Arduino IDE that helps interface with pushbuttons by either reading the state of the button or monitoring it for press/release events.
10+
11+
## Supported platforms
12+
13+
This library is designed to work with the Arduino IDE versions 1.0.x and 1.5.x, and will probably not work with older versions.
14+
15+
This library supports any Arduino-compatible board.
16+
17+
## Getting started
18+
19+
### Hardware
20+
21+
This library supports many different ways of connecting a button to your board. The simplest way is to select a pin that is not being used for anything else and to connect a [normally-open momentary pushbutton](http://www.pololu.com/product/1400) from that pin to ground.
22+
23+
### Software
24+
25+
Download the [pushbutton-arduino library from github](http://pololu.github.io/), decompress it, and drag the "Pushbutton" folder into the "libraries" subdirectory inside your Arduino sketchbook directory. You can view your sketchbook location by selecting File->Preferences in the Arduino environment; if there is not already a "libraries" folder in that location, you should create it yourself. After installing the library, restart the Arduino environment. Example code for using this library can be found in the File->Examples menu.
26+
27+
## Creating a Pushbutton object
28+
29+
To create a Pushbutton object with default settings, which enables the internal pull-up on the pin and interprets a high pin value as the default (unpressed) state of the button, put this line near the top of your sketch:
30+
31+
~~~{.cpp}
32+
Pushbutton button(BUTTON_PIN);
33+
~~~
34+
35+
Optional arguments can be passed to the constructor to specify other button types and connection methods; see the documentation links below for details.
36+
37+
## Reading the state of the button
38+
39+
The `isPressed()` function can be used to directly read the state of the button. This function simply calls `digitalRead` to get the current state of the button and does not take care of any debouncing.
40+
41+
~~~{.cpp}
42+
if (button.isPressed())
43+
{
44+
// The button is currently pressed.
45+
}
46+
~~~
47+
48+
## Waiting for an event
49+
50+
The Pushbutton class has several functions that wait for an event on the button. These functions take care of debouncing for you.
51+
52+
The `waitForPress()` function waits until the button is pressed. The `waitForRelease()` function waits until the button is released.
53+
54+
Note that if the button is already pressed when `waitForPress()` is called, it will return quickly (in 10 ms). If you want to perform an action when the user presses the button but want to avoid performing the action multiple times while the user holds the button down, you will need to call `waitForRelease()`:
55+
56+
~~~{.cpp}
57+
void loop() {
58+
button.waitForPress();
59+
// Perform action here.
60+
button.waitForRelease();
61+
}
62+
~~~
63+
64+
The `waitForButton()` function waits until the button is pressed, and then waits until the button is released. This function could be used like this:
65+
66+
~~~{.cpp}
67+
void loop() {
68+
button.waitForButton();
69+
// Perform action here.
70+
}
71+
~~~
72+
73+
These functions wait for an event to happen before returning, so your board cannot do much else while it is waiting. They are appropriate for simple programs where a button press is the only event you need to respond to in the main loop.
74+
75+
## Monitoring for an event
76+
77+
The Pushbutton class provides two non-blocking that allow you to monitor the button for transitions. These functions are powerful and can be used in almost any situation.
78+
79+
The `getSingleDebouncedButtonPress()` function will return true once for each time it detects the button changing from the released state to the pressed state. The `getSingleDebouncedButtonRelease()` function will return true once for each time it detects the button changing from the pressed state to the released state.
80+
81+
These functions are non-blocking and are meant to be called repeatedly in a loop:
82+
83+
~~~{.cpp}
84+
void loop() {
85+
if (button.getSingleDebouncedButtonPress())
86+
{
87+
// The button was pressed, so perform some action.
88+
}
89+
// Perform other tasks
90+
}
91+
~~~
92+
93+
## Documentation
94+
95+
For complete documentation of this library, including many features that were not mentioned here, see [the Pushbutton.h file documentation](https://pololu.github.io/pushbutton-arduino/_pushbutton_8h.html) from https://pololu.github.io/fastgpio-arduino.
96+
97+
## Version history
98+
99+
* 1.1.0 (2014 Dec 01):
100+
** Added the PushbuttonBase class, which allows custom pushbutton classes with their own unique ways of reading the state of the button.
101+
** Fixed a problem in the logic for getSingleDebouncedPress().
102+
** Added the PushbuttonStateMachine class to reduce code duplication.
103+
** Moved the library to its own repository at https://github.com/pololu/pushbutton-arduino.
104+
* 1.0.1 (2014 Jan 22): Fixed it to work properly with multiple instances. Improved the debouncing logic and reduced the width of PrevTimeMillis variables. This release was from commit pololu/zumo-shield@8c4e8f5.
105+
* 1.0.0 (2012 Nov 09): Original release. At this time, the Pushbutton library lived in https://github.com/pololu/zumo-shield and this release was from commit pololu/zumo-shield@a02cf00.

0 commit comments

Comments
 (0)