Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added public functions for button label and button enable/disable. #438

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
31 changes: 31 additions & 0 deletions Adafruit_GFX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1726,6 +1726,37 @@ bool Adafruit_GFX_Button::justPressed() { return (currstate && !laststate); }
/**************************************************************************/
bool Adafruit_GFX_Button::justReleased() { return (!currstate && laststate); }

/**************************************************************************/
/*!
@brief Set label to button object.
@param label char pointer to new button label
*/
void Adafruit_GFX_Button::setLabel(char *label) {
strncpy(_label, label, 9);
_label[9] = 0;
drawButton();
}
/**************************************************************************/
/*!
@brief Get button label
@return button label char pointer
*/
char *Adafruit_GFX_Button::getLabel() { return _label; }

/**********************************************************************/
/*!
@brief Set the button to enable/disable
@param enable boolean true to enable, false to disable
@param fillColor uint16_t set button fillColor
*/
/**********************************************************************/
void Adafruit_GFX_Button::setEnabled(bool enable, uint16_t fillColor) {
_enabled = enable;
_fillcolor = fillColor;
currstate = (enable ? currstate : false);
drawButton();
}

// -------------------------------------------------------------------------

// GFXcanvas1, GFXcanvas8 and GFXcanvas16 (currently a WIP, don't get too
Expand Down
16 changes: 14 additions & 2 deletions Adafruit_GFX.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ class Adafruit_GFX_Button {
uint8_t textsize_y);
void drawButton(bool inverted = false);
bool contains(int16_t x, int16_t y);
char *getLabel();
void setLabel(char *label);

/**********************************************************************/
/*!
Expand All @@ -280,7 +282,7 @@ class Adafruit_GFX_Button {
/**********************************************************************/
void press(bool p) {
laststate = currstate;
currstate = p;
currstate = p & _enabled;
}

bool justPressed();
Expand All @@ -294,6 +296,16 @@ class Adafruit_GFX_Button {
/**********************************************************************/
bool isPressed(void) { return currstate; };

/**********************************************************************/
/*!
@brief Check whether the button is enabled
@returns True if enabled
*/
/**********************************************************************/
bool isEnabled(void) { return _enabled; }

void setEnabled(bool enable, uint16_t fillColor);

private:
Adafruit_GFX *_gfx;
int16_t _x1, _y1; // Coordinates of top-left corner
Expand All @@ -303,7 +315,7 @@ class Adafruit_GFX_Button {
uint16_t _outlinecolor, _fillcolor, _textcolor;
char _label[10];

bool currstate, laststate;
bool currstate, laststate, _enabled = true;
};

/// A GFX 1-bit canvas context for graphics
Expand Down