Button class with auto repeat function.
Button.h
- Committer:
- vargham
- Date:
- 2017-02-14
- Revision:
- 1:3385a1ea4a7d
- Parent:
- 0:a39041955963
- Child:
- 2:97ef12e06605
File content as of revision 1:3385a1ea4a7d:
#ifndef BUTTON_H_INCLUDED #define BUTTON_H_INCLUDED #include "mbed.h" enum ButtonEvent { BUTTON_DOWN = 0, BUTTON_UP, BUTTON_PRESS, BUTTON_LONG_PRESS }; class Button { public: Button(PinName pinName, PinMode pinMode, int activeState, void(*onButtonEvent)(ButtonEvent event, PinName pinName) = NULL, uint16_t autoRepeatTime = 0, uint16_t longPressTime = 600, uint16_t shortPressTime = 30) : _pinName(pinName) , _in(pinName) , _shortPressTime(shortPressTime) , _longPressTime(longPressTime) , _autoRepeatTime(autoRepeatTime) , _activeState(activeState) , _prevState(0) , _fireUpDown(false) , _onButtonEvent(onButtonEvent) { _in.mode(pinMode); _in.rise(Callback<void()>(this, &Button::isrRise)); _in.fall(Callback<void()>(this, &Button::isrFall)); }; inline void setAutoRepeatTime_ms(uint16_t autoRepeatTime) { _autoRepeatTime = autoRepeatTime; }; inline uint16_t getAutoRepeatTime_ms() const { return _autoRepeatTime; } inline int read() { return _in.read() == _activeState; }; operator int() { return read(); }; void setFireUpDown(bool fire) { _fireUpDown = fire; }; inline bool isFireUpDown() const { return _fireUpDown; }; void setShortPressTime_ms(uint16_t time_ms) { _shortPressTime = time_ms; }; inline uint16_t getShortPressTime_ms() const { return _shortPressTime; }; void setLongPressTime_ms(uint16_t time_ms) { _longPressTime = time_ms; }; inline uint16_t getLongPressTime_ms() const { return _longPressTime; }; void attach(void(*onButtonEvent)(ButtonEvent event, PinName pinName)) { _onButtonEvent = onButtonEvent; }; void detach() { _onButtonEvent = NULL; }; private: PinName _pinName; InterruptIn _in; Timer _timer; Ticker _tickerAutoRepeat; Timeout _autoRepeatStarter; uint16_t _shortPressTime; uint16_t _longPressTime; uint16_t _autoRepeatTime; int _activeState; int _prevState; bool _fireUpDown; void(*_onButtonEvent)(ButtonEvent event, PinName pinName); void isrFall(void); void isrRise(void); void onPress(void); void onRelease(void); void autoRepeat(void); void startAutoRepeat(void); }; #endif /* BUTTON_H_INCLUDED */