Menu

PushButtonArray

Perrotti

Class PushButtonArray

Class to drive a array of push buttons.

Public declarations:

class ButtonOwner{
public:
    virtual void buttonPressed(byte btID){}
    virtual void buttonReleased(byte btID){}
};

class PushButtonArray{
public:
    byte btCount=0;
    PushButton** but;

    PushButtonArray();
    PushButtonArray(ButtonOwner *owner);

    boolean begin(byte maxButtons);
    int addButton(byte buttonPin, boolean pullUp=true);
    void run();
    boolean isPressed(byte btID);

protected:
    virtual void onButtonPressed(byte btID){}
    virtual void onButtonReleased(byte btID){}
};

How to use

Call begin() before any method informing how many buttons you want in array. Dont forget to check begin() return to make sure that the array was successfully allocated.
Add buttons to array calling addButton() informing pin and input mode.
Call run() at least once per loop to class trigger events.
Call isPressed() to check if one button is pressed.

ButtonOwner
Is a abstract class to receive button's events. This is usefull when the class that respond the events, is not descending of PushButtonArray class. To use this way, derive ButtonOwner class, rewrite the events and use the constructor with owner to create PushButtonArray's instance.


PushButtonArray(ButtonOwner *owner)

Constructor with button's owner. If you use this constructor, events will be send to owner and not to PushButtonArray class.

owner: ButtonOwner instance. Events are sent to owner.


boolean begin(byte maxButtons)

Call before any other method. This method alloc the array, check the return to make sure that the array was successfully allocated.

  • maxButtons: How many buttons in the array.
  • return: true if array was successfully allocated, false if not.

int addButton(byte buttonPin, boolean pullUp=true)

Add button on array.

  • buttonPin: button pin.
  • return: The button's ID or negative value if any error occurred.

void run()

Call once per loop. This method trigger the events.


boolean isPressed(byte btID)

Call to check if one button is pressed or not.

btID: ID of button
return: true if the button is pressed , false if not.


virtual void onButtonPressed(byte btID)

Event triggered when one button is pressed. To capture this event, derive PushButtonArray class and rewrite it.
This event is not triggered if the constructor with owner is used.

btID: ID of button


virtual void onButtonReleased(byte btID)

Event triggered when button is released. To capture this event, derive PushButtonArray class and rewrite it.
This event is not triggered if the constructor with owner is used.

btID: ID of button


Related

Wiki: Home
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.