Add ToggleButton derived class.

This commit is contained in:
JChristensen
2019-03-11 07:43:43 -04:00
parent db8bf3a213
commit b87dcd96d9
5 changed files with 171 additions and 6 deletions

View File

@@ -70,4 +70,42 @@ class Button
uint32_t m_time; // time of current state (ms from millis)
uint32_t m_lastChange; // time of last state change (ms)
};
// a derived class for a "push-on, push-off" (toggle) type button.
// initial state can be given, default is off (false).
class ToggleButton : public Button
{
public:
// constructor is similar to Button, but includes the initial state for the toggle.
ToggleButton(uint8_t pin, bool initialState=false, uint32_t dbTime=25, uint8_t puEnable=true, uint8_t invert=true)
: Button(pin, dbTime, puEnable, invert), m_toggleState(initialState) {}
// read the button and return its state.
// should be called frequently.
bool read()
{
Button::read();
if (wasPressed())
{
m_toggleState = !m_toggleState;
m_changed = true;
}
else
{
m_changed = false;
}
return m_toggleState;
}
// has the state changed?
bool changed() {return m_changed;}
// return the current state
bool toggleState() {return m_toggleState;}
private:
bool m_toggleState;
bool m_changed;
};
#endif