pdf-icon

Arduino 上手教程

LCD

Core2 屏幕像素为 320x240, 以屏幕左上角为原点 (0,0)

begin()

功能说明:

初始化以供使用

函数原型:

void begin();
Copy

案例程序:

#include <M5Core2.h>

void setup() {
  M5.Lcd.begin();  //初始化 M5Stack
}

void loop() {
}
Copy

sleep()

功能说明:

将显示切换到节能模式

函数原型:

void sleep();
Copy

案例程序:

#include <M5Core2.h>

void setup() {
  M5.Lcd.begin();  //初始化 M5Core2
  M5.Lcd.sleep();    //切换至休眠模式
}

void loop() {
}
Copy

clear()

功能说明:

清空显示屏所显示的内容

函数原型:

void clear();
Copy

案例程序:

#include <M5Core2.h>

void setup() {
  M5.Lcd.begin();  //初始化 M5Core2
  M5.Lcd.fillScreen(RED);
  delay(1000);
  M5.Lcd.clear();    //清空显示屏所显示的内容
}

void loop() {
}
Copy

wakeup()

功能说明:

从节能模式恢复显示

函数原型:

void wakeup();
Copy

案例程序:

#include <M5Core2.h>

void setup() {
  M5.Lcd.begin();  //初始化 M5Core2
  M5.Lcd.wakeup();    //从节能模式恢复显示
}

void loop() {
}
Copy

hight()

功能说明:

返回屏幕高度

函数原型:

void hight();
Copy

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();   //初始化 M5Core2
  M5.Lcd.print(M5.Lcd.height());    //在屏幕上显示屏幕的高度
}

void loop() {
}
Copy

width()

功能说明:

返回屏幕宽度

函数原型:

void width();
Copy

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();   //初始化 M5Core2
  M5.Lcd.print(M5.Lcd.width());    //在屏幕上显示屏幕的宽度
}

void loop() {
}
Copy

getCursorX()

功能说明:

获取字符末尾处的x坐标

函数原型:

int16_t getCursorX();
Copy
注意:
不适用于drawNumber()

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();  //初始化 M5Core2
  M5.Lcd.print("Hello");
  int X = M5.Lcd.getCursorX();
  M5.Lcd.print(X);
}

void loop(){
}
Copy

getCursorY()

功能说明:

获取字符末尾处的y坐标

函数原型:

int16_t getCursorY();
Copy
注意:
不适用于drawNumber()

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();  //初始化 M5Core2
  M5.Lcd.print("Hello");
  int X = M5.Lcd.getCursorY();
  M5.Lcd.print(Y);
}

void loop(){
}
Copy

getRotation()

功能说明:

返回屏幕旋转方向

函数原型:

uint8_t getRotation();
Copy

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();  //初始化 M5Core2
  M5.Lcd.print(M5.Lcd.getRotation());    //在屏幕上输出屏幕的旋转方向
}

void loop(){
}
Copy

getTextDatum()

功能说明:

返回文字对齐方式( 为上方列表中对齐方式的编号 )

函数原型:

textdatum_t getRotation();
Copy

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();  //初始化 M5Core2
  M5.Lcd.setTextDatum(MC_DATUM);    //设置文字的对齐方式
  M5.Lcd.drawString("hello", 160, 120, 2);    //在(160,120)处以2号字体打印字符串hello
  M5.Lcd.print(M5.Lcd.getTextDatum());    //屏幕打印获取到的文字对齐方式
}

void loop(){
}
Copy

setCursor()

功能说明:

设置文本光标在(x,y)处

函数原型:

void setCursor(int16_t x, int16_t y);
Copy
参数 类型 类型
x int16_t x坐标(像素)
y int16_t y坐标(像素)

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();             //初始化 M5Core2
  M5.Lcd.setCursor(0, 30);
  M5.Lcd.printf("Hello M5");
}

void loop() {}
Copy

setRotation()

功能说明:

旋转屏幕

函数原型:

void setRotation(uint8_t m);
Copy
参数 类型 描述
m uint8_t 旋转角度 ( * 90°)
注意:
1. 旋转角度为90°的倍数
2. 0-3为顺时针旋转,4-7逆时针旋转(默认为1)
3. 需要在显示前设置

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();             //初始化 M5Core2
  M5.Lcd.setRotation(2);  //将屏幕顺时针旋转180度(2 x 90)
  M5.Lcd.fillEllipse(160, 100, 60, 100, YELLOW);    //在(160,100)处创建一个长轴,短轴分别为60,100黄色椭圆
  delay(1000);
  M5.Lcd.setRotation(1);  //将屏幕恢复为默认显示状态
  M5.Lcd.fillEllipse(160, 100, 60, 100, GREEN);
}

void loop() {}
Copy

SetLcdVoltage()

功能说明:

设置屏幕亮度

函数原型:

void SetLcdVoltage(uint16_t voltage);
Copy
参数 类型 描述
voltage uint16_t 电压值
注意:
电压值取值范围(2500~3300)

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();
  M5.Lcd.fillScreen(RED);
}
void loop() {
  M5.update();
  for(int i=2500; i<3300;i++){
    M5.Axp.SetLcdVoltage(i);  //每隔10ms设置一次电压值
    delay(10);
  }
  for(int i=3300; i>2500;i--){
    M5.Axp.SetLcdVoltage(i);
    delay(10);
  }
}
Copy

alphaBlend()

功能说明:

设置透明度,混合前景和背景色.

函数原型:

uint16_t alphaBlend(uint8_t alpha, uint16_t fgc, uint16_t bgc);
Copy
参数 描述 类型
alpha uint8_t 透明度
fgc uint16_t 前景色
bgc uint16_t 背景色

案例程序:

#include <M5Core2.h>

void setup() {
  M5.Lcd.begin();  //初始化 M5Core2
  M5.Lcd.fillScreen(M5.Lcd.alphaBlend(128, 0X00FF00, 0XFF0000));
  //设置前景、背景色分别为0X00FF00,0XFF0000 透明度为128,并填充至整个屏幕
}

void loop() {
}
Copy

setFreeFont()

功能说明:

设置要使用的GFX字体

函数原型:

void setFreeFont(const GFXfont *f);
Copy
参数 类型 说明
GFXfont *f const 字体名称

案例程序:

loadFont()

功能说明:

从VLW文件加载字体

函数原型:

void loadFont(String fontName, bool flash);
Copy
参数 类型 说明
fontName String 字体名称
flash bool 文件来源

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();  //初始化 M5Core2
  M5.Lcd.loadFont("filename", SD);
}

void loop() {
}
Copy

unloadFont()

功能说明:

卸载字体

函数原型:

void unloadFont();
Copy

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();  //初始化 M5Core2
  M5.Lcd.unloadFont();
}

void loop() {
}
Copy

fontsLoaded()

功能说明:

返回是否加载自己的字体

函数原型:

uint16_t fontsLoaded(void);
Copy

返回值:

返回显示字体的编码的16进制值

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();  //初始化 M5Core2
  M5.Lcd.print(M5.Lcd.fontsLoaded());
}

void loop() {
}
Copy

fillScreen()

功能说明:

以指定的颜色填充整个屏幕

函数原型:

void fillScreen(uint32_t color);
Copy
参数 类型 描述
color uint32_t 颜色值

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();  //初始化 M5Core2
  M5.Lcd.fillScreen(RED);    //在屏幕上填充红色
}

void loop(){
}
Copy

invertDisplay()

功能说明:

以负/正方式反转屏幕颜色

函数原型:

void invertDisplay(boolean i);
Copy
参数 类型 说明
i boolean 反转时为 true

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();  //初始化 M5Core2
  M5.Lcd.fillScreen(RED);    //在屏幕上填充红色
}

void loop() {
  M5.Lcd.invertDisplay(1);    //开启反转
  delay(1000);
  M5.Lcd.invertDisplay(0);    //关闭反转
}
Copy

color565()

功能说明:

更改为函数中使用的颜色代码(rgb 565)

函数原型:

color565(uint8_t red, uint8_t green, uint8_t blue);
Copy
参数 类型 描述
red uint8_t
green uint8_t 绿
blue uint8_t

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();  //初始化 M5Core2
  uint16_t colorvalue = 0;
  colorvalue = color565(255, 255, 255);
  M5.Lcd.fillEllipse(160, 100, 60, 100, colorvalue);
}

void loop() {}
Copy

Text

print()

功能说明:

在屏幕当前位置打印字符串

函数原型:

size_t print();
Copy

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();  //初始化 M5Core2
  M5.Lcd.print("this is a print text function");
}

void loop() {
}
Copy

textWidth()

功能说明:

返回文本所占像素宽度

函数原型:

int16_t textWidth(const String& string);
Copy
参数 类型 描述
string const String& 字符串

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();   //初始化 M5Core2
  String text = "hello  ";
  M5.Lcd.print(text);
  M5.Lcd.print(M5.Lcd.textWidth(text));    //在屏幕上打印字符串数组text所占像素宽度
}

void loop() {}
Copy

setTextSize()

功能说明:

设置显示文字的大小

函数原型:

void setTextSize(uint8_t s);
Copy
参数 类型 描述
s uint8_t 文字的大小 (1~7)

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();  //初始化 M5Core2
  M5.Lcd.setTextSize(4);    //设置字体大小为4
  M5.Lcd.print("Hello M5Core2");
}

void loop() {
}
Copy

setTextColor()

功能说明:

设置显示文本的前景颜色 / 设置显示文本的前景颜色和背景颜色

函数原型:

void setTextColor(uint16_t color);
Copy
void setTextColor(uint16_t color, uint16_t backgroundcolor);
Copy
参数 类型 描述
color uint16_t 文本的前景颜色
backgroundcolor uint16_t 文本的背景颜色
注意:
1.如果函数的 backgroundcolor 值没给出,则使用当前的背景颜色
2.若不设置文字的颜色,默认为白色

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin(); //初始化 M5Core2
  M5.Lcd.setTextColor(RED,BLACK);    //设置文本的前、背景色分别为红色和黑色
  //M5.Lcd.setTextColor(RED);
}

void loop(){
}
Copy

setTextWrap()

功能说明:

设置自动换行功能

函数原型:

void setTextWrap(boolean wrapX, boolean wrapY);
Copy
参数 类型 描述
wrapX boolean X 方向(默认开启)
wrapY boolean Y 方向

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();   //初始化 M5Core2
  M5.Lcd.setTextWrap(true, true);    //开启x、y轴自动换行
    M5.Lcd.print("hello M5Core2 hello M5Core2 hello M5Core2 hello M5Core2 hello M5Core2 hello M5Core2 hello M5Core2 hello M5Core2");
}

void loop() {}
Copy

setTextPadding()

功能说明:

填充指定空白宽度(可帮助擦除旧的文本和数字)

函数原型:

void setTextPadding(uint16_t x_width);
Copy
参数 类型 描述
x_width uint16_t 空白区域宽度

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();
}

void loop() {
  M5.Lcd.drawString("Orbitron 32", 160, 60, 2);
  delay(2000);
  M5.Lcd.setTextPadding(M5.Lcd.width() - 20);
  M5.Lcd.drawString("Orbitron 32 with padding", 160, 60, 2);
  delay(2000);
}
Copy

setTextDatum()

功能说明:

设置文本对齐方式

函数原型:

void setTextDatum(uint8_t datum);
Copy
参数 类型 描述
TL_DATUM (0) uint8_t 左上角对齐(默认)
TC_DATUM (1) uint8_t 居中向上对齐
TR_DATUM (2) uint8_t 右上角对齐
ML_DATUM (3) uint8_t 中部左对齐
MC_DATUM (4) uint8_t 中心对齐
MR_DATUM (5) uint8_t 中部右对齐
BL_DATUM (6) uint8_t 左下角对齐
BC_DATUM (7) uint8_t 居中底部对齐
BR_DATUM (8) uint8_t 右下角对齐
L_BASELINE (9) uint8_t 左字符基线
C_BASELINE (10) uint8_t 中字符基线
R_BASELINE (11) uint8_t 右字符基线
注意:
1.不适用于print()

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();  //初始化 M5Core2
  M5.Lcd.setTextDatum(MC_DATUM);    //设置文本对齐方式为中心对齐
  M5.Lcd.drawString("hello", 160, 120, 2);    //在(160,120)处以2号字体打印字符串hello
}

void loop(){
}
Copy

Draw

drawFastHLine()

功能说明:

在(X,Y)处划一条长度为w的color色水平线条

函数原型:

void drawFastHLine(int32_t x, int32_t y, int32_t w, uint32_t color);
Copy
参数 类型 功能
x int32_t 坐标 X
y int32_t 坐标 Y
w int32_t 宽度(像素)
color uint32_t 线条颜色

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();  //初始化 M5Core2
  M5.Lcd.drawFastHLine(3, 100, 255, GREEN);    //在(3,100)处划一条长度为255的绿色水平线条
}

void loop() {
}
Copy

drawFastVLine()

功能说明:

在(X,Y)处划一条长度为w的color色 垂直线条

函数原型:

void drawFastVLine(int32_t x, int32_t y, int32_t w, uint32_t color);
Copy
参数 类型 功能
x int32_t 坐标 X
y int32_t 坐标 Y
w int32_t 宽度(像素)
color uint32_t 线条颜色(可选)

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();  //初始化 M5Core2
  M5.Lcd.drawFastVLine(100, 0, 255, TFT_GREEN);    //在(100,0)处划一条长度为255的绿色垂直线条
}

void loop(){
}
Copy

drawString()

功能说明:

在(x,y)处显示字符串

函数原型:

int16_t drawString(const char *string, int32_t poX, int32_t poY, uint8_t font);
Copy
参数 类型 描述
string const char * 一个字符串
poX int32_t X坐标
poY int32_t Y坐标
font uint8_t 字体

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();  //初始化 M5Core2
  M5.Lcd.drawString("Hello M5", 160, 100, 2);    //在(160,100)处以2号字体显示字符串Hello M5
}

void loop(){
}
Copy

drawNumber()

功能说明:

在(x,y)处显示整数

函数原型:

void drawNumber(long long_num, int32_t poX, int32_t poY);
Copy
参数 类型 描述
long_num long 数字
poX int32_t X坐标
poY int32_t Y坐标

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();  //初始化 M5Core2
  M5.Lcd.drawNumber(99, 55, 100);    //在(99,55)处显示整数100
}

void loop(){
}
Copy

drawChar()

功能说明:

在(X,Y)处以字体font显示字符

函数原型:

int16_t drawChar(int16_t uniCode, int32_t x, uint16_t y, uint8_t font);
Copy
参数 类型 描述
uniCode int16_t 字符
x int32_t X坐标
y uint16_t Y坐标
font uint8_t 字体

案例程序:

#include <M5Core2.h>
void setup() {
  M5.begin(); //初始化 M5Core2
  M5.Lcd.drawChar('A', 160, 120, 2);    //在(160,120)处以字体2显示字符A
}
void loop(){
}
Copy

drawFloat()

功能说明:

在(X,Y)处显示小数点后dp位的浮点数floatNumber

函数原型:

int16_t drawFloat(float floatNumber, uint8_t dp, int32_t poX, int32_t poY);
Copy
参数 类型 描述
floatNumber float 所显示的小数
dp uint8_t 小数位数
poX int32_t 在x处显示
poY int32_t 在y处显示

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();   //初始化 M5Core2
  M5.Lcd.drawFloat(3.1415928,7,100,100);    //在(100,100)处显示小数点后7位的浮点数3.1415928
}

void loop() {}
Copy

drawPixel()

功能说明:

在(X,Y)处画点

函数原型:

void drawPixel(int32_t x, int32_t y, uint32_t color);
Copy

参数:

参数 类型 描述
x int32_t X坐标
y int32_t Y坐标
color uint32_t 颜色

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();  //初始化 M5Core2
  M5.Lcd.drawPixel(22,22,RED);    //在(22,22)处画一个红色的像素点
}

void loop() {}
Copy

drawLine()

功能说明:

从点(x0,y0)到点(x1,y1)以指定颜色(color)绘制直线

函数原型:

void drawLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, uint32_t color);
Copy
参数 类型 描述
x int32_t X坐标
y int32_t Y坐标
color uint32_t 颜色

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();  //初始化 M5Core2
  M5.Lcd.drawLine(200, 0, 200,2000,GREEN);    //从点(200,0)到点(200,200)以绿色绘制直线
}

void loop(){
}
Copy

drawRect()

功能说明:

在(x,y)处以指定颜色绘制宽高分别为width和height的矩形线框

函数原型:

void drawRect(int32_t x, int32_t y, int32_t w, int32_t h, uint32_t color);
Copy
参数 类型 描述
x int32_t X坐标
y int32_t Y坐标
w int32_t 矩形框的宽(单位: 像素)
h int32_t 矩形框的高(单位: 像素)
color uint32_t 颜色值

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();  //初始化 M5Core2
  M5.Lcd.drawRect(180, 12, 122, 10, BLUE);    //在(180,12)处以蓝色绘制宽高分别为122和10的矩形线框
}

void loop(){
}
Copy

fillRect()

功能说明:

在(x,y)处以指定颜色绘制宽高分别为width和height的填充矩形

函数原型:

void fillRect(int32_t x, int32_t y, int32_t w, int32_t h, uint32_t color);
Copy
参数 类型 描述
x int32_t X坐标
y int32_t Y坐标
w int32_t 矩形框的宽(单位: 像素)
h int32_t 矩形框的高(单位: 像素)
color uint32_t 颜色值

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();  //初始化 M5Core2
  M5.Lcd.fillRect(150, 120, 122, 10, BLUE);    //在(150,120)处绘制一个长122、宽为10的蓝色填充矩形
}

void loop(){
}
Copy

drawRoundRect()

功能说明:

在(x,y)处绘制宽高分别为width、height的圆角矩形线框,圆角半径为radius,颜色为color

函数原型:

void drawRoundRect(int32_t x, int32_t y, int32_t w, int32_t h, int32_t r, uint32_t color);
Copy
参数 类型 描述
x int32_t 矩形左上角的x坐标
y int32_t 矩形左上角的Y坐标
w int32_t 矩形(像素)
h int32_t 矩形的高度
r int32_t 转角半径f
color uint32_t 方线的颜色

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();   //初始化 M5Core2
  M5.Lcd.drawRoundRect(55,55,30,50,10,GREEN);    //在(55,55)处绘制宽高分别为30、50的圆角半径为10,颜色为绿色的圆角矩形线框

void loop() {}
Copy

fillRoundRect()

功能说明:

在(x,y)处绘制宽高分别为width、height的圆角矩形线框,圆角半径为radius,颜色为color

函数原型:

void fillRoundRect(int32_t x, int32_t y, int32_t w, int32_t h, int32_t r, uint32_t color);
Copy
参数 类型 描述
x int32_t 矩形左上角的x坐标
y int32_t 矩形左上角的Y坐标
w int32_t 矩形宽度(像素)
h int32_t 矩形的高度(像素)
r int32_t 转角半径f
color uint32_t 方线的颜色

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();   //初始化 M5Core2
  M5.Lcd.fillRoundRect(55, 55, 30, 50, 10, GREEN);//在(55,55)处绘制宽高分别为30、50的圆角半径为10,颜色为绿色的圆角矩形  
}

void loop() {}
Copy

drawCircle()

功能说明:

在(x,y)处绘制半径为r的color色圆线框

函数原型:

void drawCircle(int32_t x0, int32_t y0, int32_t r, uint32_t color);
Copy

参数:

参数 类型 描述
x0 int32_t 圆中心X坐标
y0 int32_t 圆中心Y坐标
r int32_t 圆的半径
color uint32_t 圆的颜色

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();   //初始化 M5Core2
  M5.Lcd.drawCircle(100, 100, 50, RED);   //在(x,y)处绘制半径为50的红色圆线圈
}

void loop() {}
Copy

fillCircle()

功能说明:

在(x,y)处绘制半径为r的color色填充圆

函数原型:

void drawCircle(int32_t x0, int32_t y0, int32_t r, uint32_t color);
Copy

参数:

参数 类型 描述
x0 int32_t 圆中心X坐标
y0 int32_t 圆中心Y坐标
r int32_t 圆的半径
color uint32_t 圆的颜色

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();   //初始化 M5Core2
  M5.Lcd.fillCircle(100, 100, 50, RED); //在(x,y)处绘制半径为50的填充红色圆
}

void loop() {}
Copy

drawEllipse()

功能说明:

在(x,y)处绘制宽度、高度分别为rx,ry的椭圆线框

函数原型:

void fillEllipse(int16_t x0, int16_t y0, int32_t rx, int32_t ry, uint16_t color);
Copy

参数:

参数 类型 描述
x0 int16_t 椭圆的中心X坐标
y0 int16_t 椭圆的中心Y坐标
rx int32_t 椭圆的宽度(像素)
ry int32_t 椭圆的高度(像素)
color uint16_t 椭圆的颜色

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();   //初始化 M5Core2
  M5.Lcd.drawEllipse(160, 100, 60, 100, YELLOW);//在(160,100)处绘制颜色为黄色的宽度、高度分别为60,100的椭圆轮廓线
}

void loop() {}
Copy

fillEllipse()

功能说明:

在(x,y)处绘制宽度、高度分别为rx,ry的填充椭圆

函数原型:

void fillEllipse(int16_t x0, int16_t y0, int32_t rx, int32_t ry, uint16_t color);
Copy

参数:

参数 类型 描述
x0 int16_t 椭圆的中心X坐标
y0 int16_t 椭圆的中心Y坐标
rx int32_t 圆的宽度(像素)
ry int32_t 圆的高度(像素)
color uint16_t 椭圆的颜色

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();  //初始化 M5Core2
  M5.Lcd.fillEllipse(160, 100, 60, 100, YELLOW);    //在(160,100)处绘制颜色为黄色的宽度、高度分别为60,100的填充黄色椭圆
}

void loop() {}
Copy

drawTriangle()

功能说明:

以(x1, y1) (x2, y2) (x3, y3)为顶点绘制三角形线框

函数原型:

void drawTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t color);
Copy
参数 类型 描述
x* int32_t 顶点X*的x坐标
y* int32_t 顶点Y*的x坐标
color uint32_t 三角形的颜色

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();   //初始化 M5Core2
  M5.Lcd.drawTriangle(30, 30, 180, 100, 80, 150, YELLOW); //以 (30,30) (180,100) (80,150)为顶点绘制黄色三角形线框
}

void loop() {}
Copy

drawTriangle()

功能说明:

以(x1, y1) (x2, y2) (x3, y3)为顶点绘制填充三角形

函数原型:

void drawTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t color);
Copy
参数 类型 描述
x* int32_t 顶点X*的x坐标
y* int32_t 顶点Y*的x坐标
color uint32_t 三角形的颜色

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();   //初始化 M5Core2
  M5.Lcd.drawTriangle(30, 30, 180, 100, 80, 150, YELLOW); //以 (30,30) (180,100) (80,150)为顶点绘制填充黄色三角形
}

void loop() {}
Copy

drawXBitmap()

功能说明:

绘制位图

函数原型:

void drawXBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color);
Copy
参数 类型 描述
x int16_t 坐标 X
y int16_t 坐标 Y
bitmap const uint8_t 所示图像
w int16_t 宽度(像素)
h int16_t 高度(像素)
color uint16_t 颜色

案例程序:

见使用示例 sketch:M5Stack->Advanced->Display->drawXBitmap

drawBitmap()

功能说明:

绘制位图

函数原型:

drawBitmap(int16_t x0, int16_t y0, int16_t w, int16_t h, const uint16_t *data)

drawBitmap(int16_t x0, int16_t y0, int16_t w, int16_t h, uint16_t *data)

drawBitmap(int16_t x0, int16_t y0, int16_t w, int16_t h, const uint16_t *data, uint16_t transparent)

drawBitmap(int16_t x0, int16_t y0, int16_t w, int16_t h, const uint8_t *data)

drawBitmap(int16_t x0, int16_t y0, int16_t w, int16_t h, uint8_t *data)

参数 类型 描述
x0 uint16_t 坐标 X
y0 uint16_t 坐标 Y
w int16_t 宽度 (像素)
h int16_t 高度 (像素)
data uint16_t* / uint8_t* 图像数量
transparent uint16_t 透明色码
注意:
颜色代码由总共16位表示:红色5位,绿色6位,顶部蓝色5位

案例程序:

见使用示例 sketch:M5Stack->games->Tetris

drawBmpFile()

功能说明:

从文件中读取位图并绘制它

函数原型:

drawBmpFile(fs::FS &fs, const char *path, uint16_t x, uint16_t y)

参数 类型 描述
fs fs::FS 文件流
path const char * 文件路径(SD 、SPIFFS)
x int16_t 坐标 X
y int16_t 坐标 Y
注意:
1.根据大小和位数可能无法扩展
2.需要提前预装 Arduino ESP32 filesystem uploader

案例程序:

#include "FS.h"
//#include "SPIFFS.h"
#include <M5Core2.h>
void setup(){
    M5.begin(true, false, false, false);
  M5.Lcd.drawBmpFile(SD, "/p2.bmp",0,0);
  //M5.Lcd.drawBmpFile(SPIFFS, "/p2.bmp", 0, 0);
}
Copy

我们提供一个可以用来转换jpg图像->.c文件的脚本, 可以使用它来转换一些图片, 并使用上面的API将图像绘制到屏幕上 bin2code.py

drawJpg()

功能说明:

从内存中读取 JPEG 格式的图片数据并绘制它

函数原型:

void drawJpg(const uint8_t *jpg_data, size_t jpg_len, uint16_t x,uint16_t y, uint16_t maxWidth, uint16_t maxHeight,uint16_t offX, uint16_t offY, jpeg_div_t scale) {;
Copy
参数 类型 描述
jpg_data uint8_t * 数据顶部
jpg_len size_t 数据长度
x uint16_t 坐标 X
y uint16_t 坐标 Y
maxWidth uint16_t 最大宽度 (像素)
maxHeight uint16_t 最大高度 (像素)
offX uint16_t 抵消 X (像素)
offY uint16_t 抵消 Y (像素)
scale jpeg_div_t 规模

规格 (jpeg_div_t):

定义 功能
JPEG_DIV_NONE
JPEG_DIV_2 1/2
JPEG_DIV_4 1/4
JPEG_DIV_8 1/8
JPEG_DIV_MAX MAX
注意:
1.根据大小,位数和格式(渐进等),可能无法扩展
2. tetris_img下载

案例程序:

#include <M5Core2.h>
extern uint8_t tetris_img[];    //引用存储图像的数组,需要提前和 xxx.ino放在同一文件夹中

void setup() {
  M5.begin();  //初始化 M5Core2
  M5.Lcd.drawJpg(tetris_img, 34215);    //从内存中读取名为tetris_img的jpeg文件
}
void loop(){
}
Copy

drawJpgFile()

功能说明:

从文件流中读取JPEG数据并绘制它

函数原型:

void drawJpgFiledrawJpgFile(fs::FS &fs, const char *path, uint16_t x,uint16_t y,uint16_t maxWidth, uint16_t maxHeight, uint16_t offX,uint16_t offY, jpeg_div_t scale)

参数 类型 描述
fs fs::FS 文件流
path const char * 文件路径
x uint16_t 坐标 X
y uint16_t 坐标 Y
maxWidth uint16_t Max Width (像素)
maxHeight uint16_t Max Height (像素)
offX uint16_t 抵消X (像素)
offY uint16_t 抵消Y (像素)
scale jpeg_div_t 规模

规模(jpeg_div_t):

定义 功能
JPEG_DIV_NONE no care.
JPEG_DIV_2 1/2
JPEG_DIV_4 1/4
JPEG_DIV_8 1/8
JPEG_DIV_MAX MAX
注意:
根据尺寸和格式(渐进等),可能无法扩展

progressBar()

功能说明:

显示显示进度的栏

函数原型:

void progressBar(int x, int y, int w, int h, uint8_t val)

参数 类型 描述
x int 坐标 X
y int 坐标 Y
w int 宽度 (像素)
h int 高度(像素)
val uint8_t 进度(0-100%)
注意:
进度条将用蓝色显示

案例程序:

#include <M5Core2.h>

void setup() {
  M5.begin();  //初始化 M5Core2
  M5.Lcd.progressBar(0, 0, 240, 20, 20);    //在(0,0)处显示宽高分别为240,20进度为20%的进度条
}

void loop() {
}
Copy

qrcode()

功能说明:

创建一个二维码

函数原型:

void qrcode(const char *string, uint16_t x, uint16_t y, uint8_t width, uint8_t version)

void qrcode(const String &string, uint16_t x, uint16_t y, uint8_t width, uint8_t version)

参数 类型 描述
val string / String& 要嵌入QR的字符串
x uint16_t 坐标 X
y uint16_t 坐标 Y
width uint8_t 宽度 (像素)
version uint8_t 二维码版本
注意:
请根据字符数量选择合适的二维码版本

案例程序:

#include <M5Core2.h>

void setup() {
  M5.Lcd.begin();   //初始化 M5Core2
  M5.Lcd.qrcode("http://www.m5stack.com", 50, 10, 220, 6);
}

void loop() {
}
Copy

Sprite

setColorDepth()

功能说明:

设置色深

函数原型:

void* TFT_eSprite::setColorDepth(int8_t b)

案例程序:

#include <M5Core2.h>
TFT_eSprite img = TFT_eSprite(&M5.Lcd);

void setup() {
    img.setColorDepth(8); // Set color depth.  设置色深
    img.setTextSize(2);
    img.createSprite(320, 240);  //Create a 320x240 canvas. 创建一块320x240的画布
}

void loop() {}
Copy

应在创建画布前设置相应的色深

createSprite()

功能说明:

创建一个指定宽高的画布

函数原型:

void createSprite(int16_t w, int16_t h, uint8_t frames)

参数 类型 描述
x int16_t X坐标
y int16_t Y坐标
frames uint8_t 色深[1~2,可选]

案例程序:

#include <M5Core2.h>
TFT_eSprite img = TFT_eSprite(&M5.Lcd);

void setup() {
  M5.begin();  //初始化 M5Core2
  img.createSprite(320, 240);    //创建一块320x240的画布
  img.fillSprite(RED);    //在画布上全部填充红色
  img.pushSprite(0, 0, WHITE);    //把画布推送到屏幕(0,0)处并设置白色为穿透色
  M5.Lcd.print(img.height());    //屏幕打印画布的高度    
}

void loop() {}
Copy

fillSprite()

功能说明:

将Sprite填充指定颜色

函数原型:

void fillSprite(uint32_t color)

参数 类型 描述
color int32_t filled color

案例程序:

#include <M5Core2.h>
TFT_eSprite img = TFT_eSprite(&M5.Lcd);

void setup() {
  M5.begin();  //初始化 M5Core2
  img.createSprite(320, 240);    //创建一块320x240的画布
  img.fillSprite(RED);    //在画布上全部填充红色
  img.pushSprite(0, 0);    //把画布推送到屏幕(0,0)处
}

void loop() {}
Copy

pushSprite()

功能说明:

推送画布到指定坐标,并设置穿透色

函数原型:

void pushSprite(int32_t x, int32_t y, uint16_t transparent)

参数 类型 描述
x int32_t X坐标
y int32_t Y坐标
transparent int16_t 穿透色(可选)

案例程序:

#include <M5Core2.h>
TFT_eSprite img = TFT_eSprite(&M5.Lcd);

void setup() {
  M5.begin();  //初始化 M5Core2
  img.createSprite(320, 240);    //创建一块320x240的画布
  img.fillSprite(RED);    //在画布上全部填充红色
  img.fillCircle(100,100,20,GREEN);
  img.pushSprite(0, 0, GREEN);    //把画布推送到屏幕(0,0)处并设置绿色为穿透色
}

void loop() {}
Copy

height()

功能说明:

返回Sprite的高度

函数原型:

int16_t height()

案例程序:

#include <M5Core2.h>
TFT_eSprite img = TFT_eSprite(&M5.Lcd);

void setup() {
  M5.begin();  //初始化 M5Core2
  img.createSprite(320, 240);    //创建一块320x240的画布
  img.fillSprite(RED);    //在画布上全部填充红色
  img.pushSprite(0, 0, WHITE);    //把画布推送到屏幕(0,0)处并设置白色为穿透色
  M5.Lcd.print(img.height());    //屏幕打印画布的高度    
}

void loop() {}
Copy

deleteSprite()

功能说明:

从内存删除画布

函数原型:

void deleteSprite(void)

案例程序:

#include <M5Core2.h>
TFT_eSprite img = TFT_eSprite(&M5.Lcd);

void setup() {
  M5.begin();  //初始化 M5Core2
  img.deleteSprite();    //从内存中删除画布
}

void loop() {}
Copy
注意:
LCD. img.均继承于此文件 In_eSPI.h, 且用法类似
On This Page
begin()
sleep()
clear()
wakeup()
hight()
width()
getCursorX()
getCursorY()
getRotation()
getTextDatum()
setCursor()
setRotation()
SetLcdVoltage()
alphaBlend()
setFreeFont()
loadFont()
unloadFont()
fontsLoaded()
fillScreen()
invertDisplay()
color565()
print()
textWidth()
setTextSize()
setTextColor()
setTextWrap()
setTextPadding()
setTextDatum()
drawFastHLine()
drawFastVLine()
drawString()
drawNumber()
drawChar()
drawFloat()
drawPixel()
drawLine()
drawRect()
fillRect()
drawRoundRect()
fillRoundRect()
drawCircle()
fillCircle()
drawEllipse()
fillEllipse()
drawTriangle()
drawTriangle()
drawXBitmap()
drawBitmap()
drawBmpFile()
drawJpg()
drawJpgFile()
progressBar()
qrcode()
setColorDepth()
createSprite()
fillSprite()
pushSprite()
height()
deleteSprite()
Q&A
Submit a question
Select question category*
Arduino
MicroPython
UIFlow1
UIFlow2
EzData
M5Burner
Software
Hardware
Other
Product name
Product version
Question description*
(Supports pasting screenshots.)
Attachments
Add Files
Email*
Submit
OK

M5Stack Support

Hi, this is M5Stack Support. How can I help you today?