Menu

PushButton

Perrotti

Class PushButton

Class to drive a single push button.

  • File: pPushButton.h
  • Dependences: pGlobals.h, pOwners.h
  • Em português: Classe PushButton

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(){}
};
How to use

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.


PushButton(byte btID, ButtonOwner *owner)

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.


void begin(byte buttonPin, boolean pullUp=true)

Call before any othes method.

buttonPin: button's pin
pullUp: Activate arduino's pull up resistor to button's pin.


boolean read()

Read and return the button's status

return: true if button is pressed, false if not.


boolean run()

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.


boolean isPressed()

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.


virtual void onButtonPressed()

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.


virtual void onButtonReleased()

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.


Related

Wiki: Home
Wiki: JoyStick
Wiki: JoyStickShield
Wiki: JsRadioMaster
Wiki: JsRadioSlave

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.