Class to drive a single push button.
Public declarations:
class ButtonOwner{ public: virtual void buttonPressed(byte btID){} virtual void buttonReleased(byte btID){} }; class PushButton { public: byte btID=0; PushButton(); PushButton(byte btID, ButtonOwner *owner); void begin(byte buttonPin, boolean pullUp=true); boolean run(); boolean isPressed(); boolean read(); protected: virtual void onButtonPressed(){} virtual void onButtonReleased(){} };
Call begin() before any method informing button's pin. By default pin is initialized with pull-up, but you can disable this resourse.
Call read() to read button's status.
If you prefer work with events, derive PushButton class and rewrite the events onButtonPressed() and onButtonReleased(). In this case you must call run() at least once per loop.
isPressed() method only works if you use run() method.
ButtonOwner
Is a abstract class to receive PushButton's events. This is usefull when the class that respond the events, can not be descending of PushButton class, or you have many buttons to control. To use this way, derive ButtonOwner class, rewrite the events and use the constructor with owner to create button's instances.
Constructor with button's owner. If you use this constructor, events will be send to owner and not to PushButton class.
btID: button's ID
owner: ButtonOwner instance. Events are send to owner.
Call before any othes method.
buttonPin: button's pin
pullUp: Activate arduino's pull up resistor to button's pin.
Read and return the button's status
return: true if button is pressed, false if not.
Reads button status and triggers events if needed. If you use events, this method must be called at least once per loop.
return: true if the button status is changed, false if not.
Call to check if button is pressed or not. Only works if run() method is used.
return: true if the button is pressed , false if not.
Event triggered when button is pressed. To capture this event, derive PushButton class and rewrite it. Events are triggered by run() method, so to work with events you must call run() at least once per loop.
This event is not triggered if the constructor with owner is used.
Event triggered when button is released. To capture this event, derive PushButton class and rewrite it. Events are triggered by run() method, so to work with events you must call run() at least once per loop.
This event is not triggered if the constructor with owner is used.
Wiki: Home
Wiki: JoyStick
Wiki: JoyStickShield
Wiki: JsRadioMaster
Wiki: JsRadioSlave