关于 LGFX_Button

这个库的设计兼容 Adafruit GFX Library .虚拟的按键可以被放置在屏幕,以便于控制.

用法

LGFX_Button button();

定义并使用LGFX_Button.

使用示例:

#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");
    button.drawButton();
}

void loop(void) {
    M5.update();
    if (M5.BtnA.isPressed()) {
        button.press(true);
        button.drawButton(true);
        M5.Lcd.drawString("Button Pressed ", 0, 0, &fonts::lgfxJapanGothic_16);
        Serial.println("Pressed");
    } else {
        button.press(false);
        button.drawButton(false, "Test");
        M5.Lcd.drawString("Button Released", 0, 0, &fonts::lgfxJapanGothic_16);
        Serial.println("Released");
    }
    delay(50);
}

API

contains()

确定指定的坐标是否包含在按钮位置中,用于确定按钮是否在触摸面板上被按下。

函数原型:

bool contains(int16_t x, int16_t y)

参数 类型 描述
x int16_t point x
y int16_t point y

drawButton()

绘制一个按键,如果倒置参数被设置为 "true",则背景色被反转。

函数原型:

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

参数 类型 描述
inverted bool 是否反转按钮的文字和背景颜色.
long_name const char* 将标签改为字符串.

initButton

初始化按键.

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

参数 类型 描述
gfx M5GFX* Panel or M5Canvas
x int16_t x点
y int16_t y点
w int16_t 宽度
h int16_t 高度
outline const T& 轮廓线颜色(*1)
fill const T& 背景色颜色(*1)
textcolor const T& 字体颜色(*1)
label const char* 标签字体
textsize_x float 文本x的比例
textsize_y float 文本y的比例

*1. 关于字体代码

isPressed()

返回由 press() 设置的值.

justPressed()

判断一个按钮是否被按下

函数原型:

bool justPressed()

justReleased()

判断一个按钮是否被释放.

函数原型:

bool justReleased()

press()

设置按键状态.

函数原型:

void press(bool p)

参数 类型 描述
p bool 按键状态

setLabelDatum()

设置标签基准点.

函数原型:

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

参数 类型 説明
x_delta int16_t Delta x
y_delta int16_t Delta y
datum textdatum_t Datum(*1)

*1. 关于 TextDatum

On This Page