40 lines
1.7 KiB
C++
40 lines
1.7 KiB
C++
/*----------------------------------------------------------------------*
|
|
* button.h -- Library for reading momentary contact switches like *
|
|
* tactile button switches. *
|
|
* *
|
|
* Jack Christensen 11 May 2011 *
|
|
* *
|
|
* This work is licensed under the Creative Commons Attribution- *
|
|
* ShareAlike 3.0 Unported License. To view a copy of this license, *
|
|
* visit http://creativecommons.org/licenses/by-sa/3.0/ or send a *
|
|
* letter to Creative Commons, 171 Second Street, Suite 300, *
|
|
* San Francisco, California, 94105, USA. *
|
|
*----------------------------------------------------------------------*/
|
|
#ifndef button_h
|
|
#define button_h
|
|
#include "WProgram.h"
|
|
class button
|
|
{
|
|
public:
|
|
button(uint8_t pin, uint8_t puEnabled, uint8_t invert, uint32_t dbTime);
|
|
uint8_t read();
|
|
uint8_t isPressed();
|
|
uint8_t isReleased();
|
|
uint8_t wasPressed();
|
|
uint8_t wasReleased();
|
|
uint8_t pressedFor(uint32_t ms);
|
|
uint8_t releasedFor(uint32_t ms);
|
|
uint32_t lastChange();
|
|
private:
|
|
uint8_t _pin; //arduino pin number
|
|
uint8_t _puEnabled; //internal pullup resistor enabled
|
|
uint8_t _invert; //if 0, interpret high state as pressed, else interpret low state as pressed
|
|
uint8_t _state; //current button state
|
|
uint8_t _lastState; //previous button state
|
|
uint8_t _changed; //state changed since last read
|
|
uint32_t _time; //time of current state (all times are in ms)
|
|
uint32_t _lastTime; //time of previous state
|
|
uint32_t _lastChange; //time of last state change
|
|
uint32_t _dbTime; //debounce time
|
|
};
|
|
#endif |