pdf-icon

Arduino Quick Start

2. Devices & Examples

LGFX_Button Class

initButton

Syntax:

void initButton( LovyanGFX *gfx, int16_t x, int16_t y, uint16_t w, uint16_t h, const T& outline, const T& fill, 
                 const T& textcolor, const char *label, float textsize_x = 1.0f, float textsize_y = 0.0f)

Description:

  • Initialization of the button

Parameters:

  • gfx: A pointer to the LGFX class
  • x: The x-coordinate of the button
  • y: The y-coordinate of the button
  • w: The width of the button
  • h: The height of the button
  • outline: The outline color of the button
  • fill: The fill color of the button
  • textcolor: The text color of the button
  • label: The label text of the button
  • textsize_x: The x-axis scaling factor of the button text
  • textsize_y: The y-axis scaling factor of the button text

Return:

  • null

Example:

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <Arduino.h>
#include <M5GFX.h>
#include <M5Unified.h>
static int32_t w;
static int32_t h;
LGFX_Button button;
void setup(void) {
auto cfg = M5.config();
M5.begin(cfg);
w = M5.Lcd.width();
h = M5.Lcd.height();
button.initButton(&M5.Lcd, w / 2, h / 2, 100, 50, TFT_RED, TFT_YELLOW, TFT_BLACK, "Btn", 2, 2);
button.drawButton();
}
void loop(void) {
}

initButtonUL

Syntax:

void initButtonUL( LovyanGFX *gfx, int16_t x, int16_t y, uint16_t w, uint16_t h, const T& outline, const T& fill,
                   const T& textcolor, const char *label, float textsize_x = 1.0f, float textsize_y = 0.0f)

Description:

  • Initialization of the button, this function is similar to initButton, but uses the top-left corner as the reference point.

Parameters:

  • gfx: A pointer to the LGFX class
  • x: The x-coordinate of the button
  • y: The y-coordinate of the button
  • w: The width of the button
  • h: The height of the button
  • outline: The outline color of the button
  • fill: The fill color of the button
  • textcolor: The text color of the button
  • label: The label text of the button
  • textsize_x: The x-axis scaling factor of the button text
  • textsize_y: The y-axis scaling factor of the button text

Return:

  • null

Example:

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <Arduino.h>
#include <M5GFX.h>
#include <M5Unified.h>
static int32_t w;
static int32_t h;
LGFX_Button button;
void setup(void) {
auto cfg = M5.config();
M5.begin(cfg);
w = M5.Lcd.width();
h = M5.Lcd.height();
button.initButtonUL(&M5.Lcd, w / 2, h / 2, 100, 50, TFT_RED, TFT_YELLOW, TFT_BLACK, "Btn", 2, 2);
button.drawButton();
}
void loop(void) {
}
Note:
The label parameter in the above two button initialization functions is a pointer to a string, and this string must be initialized before using, otherwise it will lead to undefined behavior and compilation failure.

setLabelText

Syntax:

void setLabelText(const char* label)

Description:

  • Set the label text of the button

Parameters:

  • label: The label text of the button

Return:

  • null

Example:

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <Arduino.h>
#include <M5GFX.h>
#include <M5Unified.h>
static int32_t w;
static int32_t h;
LGFX_Button button;
void setup(void) {
auto cfg = M5.config();
M5.begin(cfg);
w = M5.Lcd.width();
h = M5.Lcd.height();
button.initButton(&M5.Lcd, w / 2, h / 2, 100, 50, TFT_RED, TFT_YELLOW, TFT_BLACK, "null", 2, 2);
button.setLabelText("BTN");
button.drawButton();
}
void loop(void) {
}

setLabelDatum

Syntax:

void setLabelDatum(int16_t x_delta, int16_t y_delta, textdatum_t datum = middle_center)

*About TextDatum

Description:

  • Set the label datum of the button

Parameters:

  • x_delta: The x-coordinate offset of the label
  • y_delta: The y-coordinate offset of the label
  • datum: The alignment of the label

Return:

  • null

Example:

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <Arduino.h>
#include <M5GFX.h>
#include <M5Unified.h>
static int32_t w;
static int32_t h;
LGFX_Button button;
void setup(void) {
auto cfg = M5.config();
M5.begin(cfg);
w = M5.Lcd.width();
h = M5.Lcd.height();
button.initButtonUL(&M5.Lcd, 0, 0, 100, 50, TFT_RED, TFT_YELLOW, TFT_BLACK, "Btn", 2, 2);
button.setLabelDatum(0,0,middle_left);
button.drawButton();
}
void loop(void) {
}

setOutlineColor

Syntax:

void setOutlineColor(const T& clr)

Description:

  • Set the outline color of the button

Parameters:

  • clr: The outline color of the button

*About clr

Return:

  • null

setFillColor

Syntax:

void setFillColor(const T& clr)

Description:

  • Set the fill color of the button

Parameters:

  • clr: The fill color of the button

Return:

  • null

setTextColor

Syntax:

void setTextColor(const T& clr)

Description:

  • Set the text color of the button

Parameters:

  • clr: The text color of the button

Return:

  • null

drawButton

Syntax:

void drawButton(bool inverted = false, const char* long_name = nullptr)

Description:

  • Draw the button

Parameters:

  • inverted: The flag to invert the button colors
  • long_name: The long name of the button

Return:

  • null

Example:

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <Arduino.h>
#include <M5GFX.h>
#include <M5Unified.h>
static int32_t w;
static int32_t h;
LGFX_Button button;
void setup(void) {
auto cfg = M5.config();
M5.begin(cfg);
w = M5.Lcd.width();
h = M5.Lcd.height();
button.initButton(&M5.Lcd, w / 2, h / 2, 100, 50, TFT_RED, TFT_YELLOW, TFT_BLACK, "BTN", 2, 2);
button.setOutlineColor(TFT_WHITE);//set button outline color
button.setFillColor(TFT_BLUE);//set button color
button.setTextColor(TFT_DARKGRAY);//set button text color
button.drawButton();//draw button
}
void loop(void) {
}

contains

Syntax:

bool contains(int16_t x, int16_t y)

Description:

  • Determine whether the specified coordinates are within the button's range

Parameters:

  • x: The x-coordinate of the point
  • y: The y-coordinate of the point

Return:

  • bool
    • true: The coordinates are within the button's range
    • false: The coordinates are not within the button's range

Example:

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include <Arduino.h>
#include <M5GFX.h>
#include <M5Unified.h>
static int32_t w;
static int32_t h;
LGFX_Button button;
void setup(void) {
auto cfg = M5.config();
M5.begin(cfg);
w = M5.Lcd.width();
h = M5.Lcd.height();
button.initButton(&M5.Lcd, w / 2, h / 2, 100, 50, TFT_RED, TFT_YELLOW, TFT_BLACK, "BTN", 2, 2);
button.drawButton();
M5.Display.setTextDatum(middle_center);
M5.Display.setTextFont(&fonts::FreeSans12pt7b);
M5.Display.setTextSize(1);
M5.Display.drawString("Coordinate Contained ? : \n", M5.Display.width() / 2, M5.Display.height() - 50);
const char *con_str = button.contains(w / 2, h / 2) ? "Yes" : "No";// coordinate: (w / 2, h / 2)
M5.Display.drawString(con_str, M5.Display.width() / 2, M5.Display.height() - 20);
}
void loop(void) {
}

press

Syntax:

void press(bool p)

Description:

  • Set the pressed state of the button

Parameters:

  • p: The pressed state of the button

Return:

  • null

isPressed

Syntax:

bool isPressed(void)

Description:

  • Determine whether the button has been pressed

Parameters:

  • null

Return:

  • bool
    • true: The button has been pressed
    • false: The button has not been pressed

justPressed

Syntax:

bool justPressed(void)

Description:

  • Determine whether the button has just been pressed

Parameters:

  • null

Return:

  • true: The button has just been pressed
  • false: The button has not just been pressed

justReleased

Syntax:

bool justReleased(void)

Description:

  • Determine whether the button has just been released

Parameters:

  • null

Return:

  • true: The button has just been released
  • false: The button has not just been released
Note:
The above four button state functions only serve to record the state and do not detect actual button operations.

Comprehensive Example Program

Example:

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <Arduino.h>
#include <M5GFX.h>
#include <M5Unified.h>
static int32_t w;
static int32_t h;
LGFX_Button button;
void setup(void) {
auto cfg = M5.config();
M5.begin(cfg);
w = M5.Lcd.width();
h = M5.Lcd.height();
button.initButton(&M5.Lcd, w / 2, h / 2, 100, 50, TFT_RED, TFT_YELLOW,
TFT_BLACK, "null", 2, 2);
button.drawButton();
}
void loop(void) {
M5.update();
if (M5.BtnA.isPressed()) {
button.press(true);
button.drawButton(true, "BtnA");
M5.Lcd.drawString("Button A Pressed ", 0, 0, &fonts::lgfxJapanGothic_16);
}
else if (M5.BtnB.isPressed()) {
button.press(true);
button.drawButton(true, "BtnB");
M5.Lcd.drawString("Button B Pressed ", 0, 0, &fonts::lgfxJapanGothic_16);
}
else if (M5.BtnC.isPressed()) {
button.press(true);
button.drawButton(true, "BtnC");
M5.Lcd.drawString("Button C Pressed ", 0, 0, &fonts::lgfxJapanGothic_16);
}
else {
button.press(false);
button.drawButton(false, "Test");
M5.Lcd.drawString("Button Released", 0, 0, &fonts::lgfxJapanGothic_16);
}
delay(100);
}

The example program runs as follows:

On This Page