M5STACK上手

一、已封装M5函数

1.system

begin()

功能:

初始化 LCD; 初始化 SD 卡; 清串口缓冲区,设置串口波特率为 115200; 初始化 I2C; 设置 MBus模式; 初始化 Axp电源管理芯片; 初始化触屏功能; 初始化 RTC

函数原型:

void begin(bool LCDEnable = true, bool SDEnable = true, bool SerialEnable = true, bool I2CEnable = false, mbus_mode_t mode = kMBusModeOutput)

使用示例:

#include <M5Core2.h>

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

update()

功能:

读取按键的状态

函数原型:

void update()

使用示例:

#include <M5Core2.h>

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

void loop() {
M5.update();
}

shutdown()

函数重载1:关闭电源,再次启动需要通过PWR按键唤醒

void shutdown()

函数重载2:关闭电源,根据传入的延时秒数,在延时结束后通过RTC唤醒设备。

int shutdown( int seconds )

函数重载3:关闭电源,传入指定了某个时间点的RTC时间结构体,当符合该时间的,,的时候通过RTC唤醒设备。

int shutdown( const RTC_TimeTypeDef &RTC_TimeStruct)

函数重载4:关闭电源,传入指定了某个时间点的RTC时间结构体,当同时符合该时间点的周数,天数,时间的时通过RTC唤醒设备。

int shutdown( const RTC_DateTypeDef &RTC_DateStruct, const RTC_TimeTypeDef &RTC_TimeStruct)

使用示例:

#include <M5Core2.h>

RTC_TimeTypeDef RTCtime;
RTC_TimeTypeDef RTCtime_Now;

char timeStrbuff[64];

void setup(){
M5.begin(true,true,true,true);

RTCtime.Hours = 10;
RTCtime.Minutes = 30;
RTCtime.Seconds = 45;

M5.Lcd.setCursor(0,80);
M5.Lcd.println("");
M5.Lcd.println("BtnA: shutdown, use power button to turn back on");
M5.Lcd.println("");
M5.Lcd.println("BtnB: shutdown, wake up after 5 seconds");
M5.Lcd.println("");
M5.Lcd.println("BtnC: shutdown, wake up at RTC Time 10:30:45");
}

void loop(){
M5.update();
if(M5.BtnA.wasPressed()){
M5.shutdown();
}else if(M5.BtnB.wasPressed()){
M5.shutdown(5);
}else if(M5.BtnC.wasPressed()){
M5.shutdown(RTCtime);
}

M5.Lcd.setCursor(0,140);
M5.Rtc.GetTime(&RTCtime_Now);
sprintf(timeStrbuff,"RTC Time Now is %02d:%02d:%02d",
RTCtime_Now.Hours,RTCtime_Now.Minutes,RTCtime_Now.Seconds);
M5.Lcd.println(timeStrbuff);
}

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

颜色代码:

定义 16进制值 R G B
BLACK 0x0000 0 0 0
NAVY 0x000F 0 0 128
DARKGREEN 0x03E0 0 128 0
MAROON 0x7800 128 0 0
PURPLE 0x780F 128 0 128
OLIVE 0x7BE0 128 128 0
LIGHTGREY 0xC618 192 192 192
DARKGREY 0x7BEF 128 128 128
BLUE 0x001F 0 0 255
GREENYELLOW 0xB7E0 180 255 0
GREEN 0x07E0 0 255 0
YELLOW 0xFFE0 255 255 0
ORANGE 0xFDA0 255 180 0
PINK 0xFC9F 255 255 16
CYAN 0x07FF 0 255 255
DARKCYAN 0x03EF 0 128 128
RED 0xF800 255 0 0
MAGENTA 0xF81F 255 0 255
WHITE 0xFFFF 255 255 255

2.$AXP192$

AXP192 是一款高度集成的电源系统管理芯片, 在M5Core2库中封装了一系列电源芯片对周边外设电源控制的API

SetLcdVoltage()

功能:

设置屏幕电压,调整亮度,参数有效范围 2500-3300

函数原型:

void SetLcdVoltage(uint16_t voltage)

使用示例:

#include <M5Core2.h>

void setup() {
M5.begin(); //By default, "M5.begin()" will initialize AXP192 chip
M5.Lcd.fillScreen(RED);
}
void loop() {
M5.update();
for(int i=2500; i<3300;i++){
M5.Axp.SetLcdVoltage(i);
delay(10);
}
for(int i=3300; i>2500;i--){
M5.Axp.SetLcdVoltage(i);
delay(10);
}
}

PowerOff()

功能:

切断所有供电(除RTC外)

函数原型:

void PowerOff()

使用示例:

#include <M5Core2.h>

void setup() {
M5.begin();
M5.Lcd.fillScreen(RED);
delay(1000);
}
void loop() {
M5.Axp.PowerOff();
}

deepSleep()

功能:

深度睡眠(恢复后程序将从头开始执行)

函数原型:

void deepSleep(uint64_t time_in_us)
参数 类型 描述
time_in_us uint64_t 睡眠时间

使用示例:

#include <M5Core2.h>

void setup() {
M5.begin();
M5.Lcd.println("Going to deep sleep for 5 seconds.");
delay(2000);
M5.Axp.DeepSleep(SLEEP_SEC(5));
}
void loop() {
}

lightSleep()

功能:

浅度睡眠(恢复后程序将从下一行继续执行)

函数原型:

void lightSleep(uint64_t time_in_us)
参数 类型 描述
time_in_us uint64_t 睡眠时间

使用示例:

#include <M5Core2.h>

void setup() {
M5.begin();
M5.Lcd.println("Going to light sleep for 5 seconds.");
delay(2000);
M5.Axp.lightSleep(SLEEP_SEC(5));
}
void loop() {
}

SetLed()

功能:

设置内置LED灯: state = 1 为点亮; state = 0 为熄灭

函数原型:

void SetLed(uint8_t state)

使用示例:

#include <M5Core2.h>

void setup() {
M5.begin();
}
void loop() {
M5.Axp.SetLed(1);
delay(1000);
M5.Axp.SetLed(0);
delay(1000);
}

SetBusPowerMode()

功能:

设置BUS电源模式,设置0为USB/BAT供电,设置1为外部输入供电

函数原型:

void SetBusPowerMode( uint8_t state )

使用示例:

#include <M5Core2.h>

void setup() {
M5.begin();
M5.Axp.SetBusPowerMode(0); //Set the Bus power mode for USB/BAT power supply. 设置Bus电源模式为USB/BAT供电
}
void loop() {
}

SetSpkEnable()

功能:

设置扬声器电源启用

函数原型:

void SetSpkEnable(uint8_t state)

使用示例:

#include <M5Core2.h>

void setup() {
M5.begin();
M5.Axp.SetSpkEnable(1); //Power on the speaker. 开启扬声器电源
}
void loop() {
}

SetCHGCurrent()

功能:

设置电池充电电流

函数原型:

void SetCHGCurrent(uint8_t state)

GetBatVoltage()

功能:

读取电池电压

函数原型:

float GetBatVoltage()

使用示例:

#include <M5Core2.h>

void setup() {
M5.begin();
}
void loop() {
Serial.printf("Bat Voltage:%f\n", M5.Axp.GetBatVoltage());
delay(500);
}

GetBatCurrent()

函数原型:

功能:

读取电池电流

float GetBatCurrent()

使用示例:

#include <M5Core2.h>

void setup() {
M5.begin();
}
void loop() {
Serial.printf("Bat Current:%f\n", M5.Axp.GetBatCurrent());
delay(500);
}

GetVBusVoltage()

功能:

读取VBUS电压

函数原型:

float GetVBusVoltage()

使用示例:

#include <M5Core2.h>

void setup() {
M5.begin();
}
void loop() {
Serial.printf("VBus Voltage:%f\n", M5.Axp.GetVBusVoltage());
delay(500);
}

GetVBusCurrent()

功能:

读取VBUS电流

函数原型:

float GetVBusCurrent()

使用示例:

#include <M5Core2.h>

void setup() {
M5.begin();
}
void loop() {
Serial.printf("VBus Current:%f\n", M5.Axp.GetVBusCurrent());
delay(500);
}

GetTempInAXP192()

功能:

读取AXP192芯片温度

函数原型:

float GetTempInAXP192()

使用示例:

#include <M5Core2.h>

void setup() {
M5.begin();
}
void loop() {
Serial.printf("AXP192 Temp:%f\n", M5.Axp.GetTempInAXP192());
delay(500);
}

GetBatPower()

功能:

读取电池当前消耗功率

函数原型:

float GetBatPower()

使用示例:

#include <M5Core2.h>

void setup() {
M5.begin();
}
void loop() {
Serial.printf("AXP192 Temp:%f\n", M5.Axp.GetTempInAXP192());
delay(500);
}

GetBatChargeCurrent()

功能:

读取电池充电电流

函数原型:

float GetBatChargeCurrent()

使用示例:

#include <M5Core2.h>

void setup() {
M5.begin();
}
void loop() {
Serial.printf("Bat Charge Current:%f\n", M5.Axp.GetBatChargeCurrent());
delay(500);
}

isCharging()

功能:

检查是否处于充电状态

函数原型:

bool isCharging()

使用示例:

#include <M5Core2.h>

void setup() {
M5.begin();
}
void loop() {
Serial.printf("Charging state:%d\n", M5.Axp.isCharging());
delay(500);
}
#include <M5Core2.h>
TFT_eSprite img = TFT_eSprite(&M5.Lcd);

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

void loop() {}

3.LCD 屏幕

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

颜色代码:

定义 16进制值 R G B
BLACK 0x0000 0 0 0
NAVY 0x000F 0 0 128
DARKGREEN 0x03E0 0 128 0
MAROON 0x7800 128 0 0
PURPLE 0x780F 128 0 128
OLIVE 0x7BE0 128 128 0
LIGHTGREY 0xC618 192 192 192
DARKGREY 0x7BEF 128 128 128
BLUE 0x001F 0 0 255
GREENYELLOW 0xB7E0 180 255 0
GREEN 0x07E0 0 255 0
YELLOW 0xFFE0 255 255 0
ORANGE 0xFDA0 255 180 0
PINK 0xFC9F 255 255 16
CYAN 0x07FF 0 255 255
DARKCYAN 0x03EF 0 128 128
RED 0xF800 255 0 0
MAGENTA 0xF81F 255 0 255
WHITE 0xFFFF 255 255 255

类名: Lcd

begin()

功能:

初始化以供使用

函数原型:

void begin()

注意:
如果您不想使用M5.begin() 初始化LCD,请在使用显示器之前调用此函数

使用示例:

#include <M5Core2.h>

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

void loop() {
}

sleep()

功能:

将显示切换到节能模式

函数原型:

void sleep()

使用示例:

#include <M5Core2.h>

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

void loop() {
}

clear()

功能:

清空显示屏所显示的内容

函数原型:

void clear()

使用示例:

#include <M5Core2.h>

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

void loop() {
}

wakeup()

功能:

从节能模式恢复显示

函数原型:

void wakeup()

使用示例:

#include <M5Core2.h>

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

void loop() {
}

hight()

功能:

返回屏幕高度

函数原型:

void hight()

使用示例:

#include <M5Core2.h>

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

void loop() {
}

width()

功能:

返回屏幕宽度

函数原型:

void width()

使用示例:

#include <M5Core2.h>

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

void loop() {
}

getCursorX()

功能:

获取字符末尾处的x坐标

函数原型:

int16_t getCursorX()

注意:
不适用于drawNumber()

使用示例:

#include <M5Core2.h>

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

void loop(){
}

getCursorY()

功能:

获取字符末尾处的y坐标

函数原型:

int16_t getCursorY()

注意:
不适用于drawNumber()

使用示例:

#include <M5Core2.h>

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

void loop(){
}

getRotation()

功能:

返回屏幕旋转方向

函数原型:

uint8_t getRotation()

使用示例:

#include <M5Core2.h>

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

void loop(){
}

getTextDatum()

功能:

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

函数原型:

textdatum_t getRotation()

使用示例:

#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(){
}

setCursor()

功能:

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

函数原型:

void setCursor(int16_t x, int16_t y)
参数 类型 类型
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() {}

setRotation()

功能:

旋转屏幕

函数原型:

void setRotation(uint8_t m)
参数 类型 描述
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*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() {}

SetLcdVoltage()

功能:

设置屏幕亮度

函数原型:

void SetLcdVoltage(uint16_t voltage)
参数 类型 描述
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);
}
}

alphaBlend()

功能:

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

函数原型:

uint16_t alphaBlend(uint8_t alpha, uint16_t fgc, uint16_t bgc)
参数 描述 类型
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() {
}

setFreeFont()

功能:

设置要使用的GFX字体

函数原型:

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

使用示例:

loadFont()

功能:

从VLW文件加载字体

函数原型:

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

使用示例:

#include <M5Core2.h>

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

void loop() {
}

unloadFont()

功能:

卸载字体

函数原型:

void unloadFont()

使用示例:

#include <M5Core2.h>

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

void loop() {
}

fontsLoaded()

功能:

返回是否加载自己的字体

函数原型:

uint16_t fontsLoaded(void)

返回值:

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

使用示例:

#include <M5Core2.h>

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

void loop() {
}

fillScreen()

功能:

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

函数原型:

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

使用示例:

#include <M5Core2.h>

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

void loop(){
}

invertDisplay()

功能:

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

函数原型:

void invertDisplay(boolean i)
参数 类型 说明
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); //关闭反转
}

color565()

功能:

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

函数原型:

color565(uint8_t red, uint8_t green, uint8_t blue)
参数 类型 描述
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() {}

4.Text

print()

功能:

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

函数原型:

size_t print()

使用示例:

#include <M5Core2.h>

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

void loop() {
}

textWidth()

功能:

返回文本所占像素宽度

函数原型:

int16_t textWidth(const String& string)
参数 类型 描述
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() {}

setTextSize()

功能:

设置显示文字的大小

函数原型:

void setTextSize(uint8_t s)
参数 类型 描述
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() {
}

setTextColor()

功能:

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

函数原型:

void setTextColor(uint16_t color)
void setTextColor(uint16_t color, uint16_t backgroundcolor)
参数 类型 描述
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(){
}

setTextWrap()

功能:

设置自动换行功能

函数原型:

void setTextWrap(boolean wrapX, boolean wrapY)
参数 类型 描述
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() {}

setTextPadding()

功能:

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

函数原型:

void setTextPadding(uint16_t x_width)
参数 类型 描述
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);
}

setTextDatum()

功能:

设置文本对齐方式

函数原型:

void setTextDatum(uint8_t datum)
参数 类型 描述
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(){
}

5.Draw

drawFastHLine()

功能:

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

函数原型:

void drawFastHLine(int32_t x, int32_t y, int32_t w, uint32_t color)
参数 类型 功能
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() {
}

drawFastVLine()

功能:

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

函数原型:

void drawFastVLine(int32_t x, int32_t y, int32_t w, uint32_t color)
参数 类型 功能
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(){
}

drawString()

功能:

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

函数原型:

int16_t drawString(const char *string, int32_t poX, int32_t poY, uint8_t font)
参数 类型 描述
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(){
}

drawNumber()

功能:

在(x,y)处显示整数

函数原型:

void drawNumber(long long_num, int32_t poX, int32_t poY)
参数 类型 描述
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(){
}

drawChar()

功能:

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

函数原型:

int16_t drawChar(int16_t uniCode, int32_t x, uint16_t y, uint8_t font)
参数 类型 描述
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(){
}

drawFloat()

功能:

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

函数原型:

int16_t drawFloat(float floatNumber, uint8_t dp, int32_t poX, int32_t poY)
参数 类型 描述
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() {}

drawPixel()

功能:

在(X,Y)处画点

函数原型:

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

参数:

参数 类型 描述
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() {}

drawLine()

功能:

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

函数原型:

void drawLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, uint32_t color)
参数 类型 描述
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(){
}

drawRect()

功能:

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

函数原型:

void drawRect(int32_t x, int32_t y, int32_t w, int32_t h, uint32_t color)
参数 类型 描述
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(){
}

fillRect()

功能:

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

函数原型:

void fillRect(int32_t x, int32_t y, int32_t w, int32_t h, uint32_t color)
参数 类型 描述
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(){
}

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)
参数 类型 描述
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() {}

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)
参数 类型 描述
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() {}

drawCircle()

功能:

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

函数原型:

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

参数:

参数 类型 描述
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() {}

fillCircle()

功能:

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

函数原型:

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

参数:

参数 类型 描述
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() {}

drawEllipse()

功能:

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

函数原型:

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

参数:

参数 类型 描述
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() {}

fillEllipse()

功能:

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

函数原型:

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

参数:

参数 类型 描述
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() {}

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)
参数 类型 描述
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() {}

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)
参数 类型 描述
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() {}

drawXBitmap()

功能:

绘制位图

函数原型:

void drawXBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color)
参数 类型 描述
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);
}

我们提供一个可以用来转换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) {
参数 类型 描述
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.根据大小,位数和格式(渐进等),可能无法扩展

  1. 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(){
}

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() {
}

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() {
}

6.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() {}

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

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() {}

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() {}

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() {}

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() {}

deleteSprite()

功能:

从内存删除画布

函数原型:

void deleteSprite(void)

使用示例:

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

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

void loop() {}

7.按键

类名: BtnA / BtnB / BtnC

read()

功能:

读取按键状态: 0,松开; 1,按下

函数原型:

uint8_t read()

使用示例:

#include <M5Core2.h>

void setup() {
M5.begin();
M5.Lcd.println("Please pressed Button A.");
}

void loop() {
M5.Lcd.setCursor(0, 0);
M5.Lcd.printf("Button A Status: %d ",M5.BtnA.read()); //打印按键A按下的状态
}

lastChange()

功能:

返回最后一次状态发生变化的时间

函数原型:

uint32_t lastChange()

注意:
1.返回的时间是从M5Core初始化的那一刻开始计时,单位为毫秒

使用示例:

#include <M5Core2.h>

void setup() {
M5.begin();
M5.Lcd.println("Please pressed Button A.");
}

void loop() {
M5.update();
M5.Lcd.setCursor(0, 0);
M5.Lcd.printf("The last change at %d ms /n",M5.BtnA.lastChange()); //打印按键A最后一次状态变化的时间
}

8.Press

isPressed()

功能:

返回按键按下状态: 如果按键按下,返回true; 否则返回false

函数原型:

uint8_t isPressed()

使用示例:

#include <M5Core2.h>

void setup() {
M5.begin();
M5.Lcd.println("Please pressed Button A.");
}

void loop() {
M5.update(); //需添加M5.update()才能读取到按键的状态,细节请见System
M5.Lcd.setCursor(0, 0);
if (M5.BtnA.isPressed()) { //如果按键按下
M5.Lcd.println("Button is Pressed.");
}else{
M5.Lcd.println("Button is Released.");
}
delay(20);
}

pressedFor()

功能:

返回按键按下状态: 如果按键按下超过指定时间后,返回true; 否则返回false

函数原型:

uint8_t pressedFor(uint32_t ms)
参数 类型 描述
ms uint32_t 按键按下时间 (毫秒)

使用示例:

#include <M5Core2.h>

void setup() {
M5.begin();
M5.Lcd.println("Please pressed Button A.");
}

void loop() {
M5.update();
if (M5.BtnA.pressedFor(2000)) { //如果按键按下超过2秒
M5.Lcd.println("Button A was pressed for more than 2 seconds.");
delay(1000);
}
}

wasPressed()

功能:

返回按键按下状态: 如果按键按下,只会返回一次true,否则返回false

函数原型:

uint8_t wasPressed()

使用示例:

#include <M5Core2.h>

void setup() {
M5.begin();
M5.Lcd.println("Please pressed Button A.");
}

void loop() {
M5.update();
if (M5.BtnA.wasPressed()) { //如果按键按下
M5.Lcd.println("Button is pressed.");
}
delay(20);
}

9.Released

isReleased()

功能:

返回按键释放状态: 如果按键释放,返回true; 否则返回false

函数原型:

uint8_t isPressed()

使用示例:

#include <M5Core2.h>

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

void loop() {
M5.update(); //需添加M5.update()才能读取到按键的状态,细节请见System
if (M5.BtnA.isReleased()) { //如果按键释放
M5.Lcd.println("Button is released.");
}else{
M5.Lcd.println("Button is Pressed .");
}
delay(20);
}

releasedFor()

功能:

返回按键释放状态: 如果按键释放超过指定时间后,返回true; 否则返回false

函数原型:

uint8_t pressedFor(uint32_t ms)
参数 类型 描述
ms uint32_t 按键释放时间 (毫秒)

使用示例:

#include <M5Core2.h>

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

void loop() {
M5.update();
M5.Lcd.setCursor(0, 0);
if (M5.BtnA.releasedFor(2000)) { //如果按键释放超过2秒
M5.Lcd.println("Button A was released for more than 2 seconds.");
delay(1000);
}else{
M5.Lcd.println("Button A is pressed ");
}
}

wasReleased()

功能:

返回按键释放状态: 如果按键释放,只会返回一次true,否则返回false

函数原型:

uint8_t wasReleased()

使用示例:

#include <M5Core2.h>

void setup() {
M5.begin();
M5.Lcd.println("Please pressed Button A.");
}

void loop() {
M5.update();
if(M5.BtnA.wasReleased()) { //如果按键释放
M5.Lcd.println("Button is Released.");
}
delay(20);
}

wasReleasefor()

功能:

返回按键释放状态: 如果按键按下,在超过指定时间后释放,只会返回一次true,否则返回false

函数原型:

uint8_t wasReleasefor(uint32_t ms)
参数 类型 描述
ms uint32_t 按键按下时间 (毫秒)

使用示例:

#include <M5Core2.h>

void setup() {
M5.begin();
M5.Lcd.println("Please pressed Button A.");
}

void loop() {
M5.update();
if (M5.BtnA.wasReleasefor(3000)) { //如果按键A按下3s之后释放
M5.Lcd.println("OK");
}
}

10.TOUCH

这是M5Stack Core2触摸屏库,你可以用该库中的API获取触摸交互信息。以及监听一些触控,手势事件并指定相应的处理程序.触摸屏幕的实际尺寸为320x280, 除去覆盖屏幕部分,剩余的40px高度覆盖到了面板上印有红色圆圈的位置,用户可以通过程序控制来模拟实体按键。

TouchButton类

TouchButton继承至TouchZone类, 使用前通过创建实例按键实例,调用实例中包含的方法,进行使用

构造函数:

TouchButton(uint16_t x_, uint16_t y_, uint16_t w_, uint16_t h_, const char* name_ = "")

功能:创建矩形按键区域实例

获取状态:

  • 设置按键状态
  • bool setState(bool)
  • 按键是否按下
  • bool isPressed()
  • 按键是否释放
  • bool isReleased()
  • 按键按下触发单次
  • bool wasPressed()
  • 按键释放触发单次
  • bool wasReleased()
  • 按键长按-指定时间
  • bool pressedFor(uint32_t ms)
  • 按键长按-指定时间
  • bool pressedFor(uint32_t ms, uint32_t continuous_time)
  • 按键释放-指定时间
  • bool releasedFor(uint32_t ms)
  • 按键按下-指定时间
  • bool wasReleasefor(uint32_t ms)

获取属性:

  • int32_t lastChange()
  • uint8_t finger;
  • bool changed;
  • char name[16];

事件监听:

  • 添加按键触摸处理事件,设置触发事件的类型
  • void addHandler(void (*fn)(TouchEvent&), uint16_t eventMask = TE_ALL)
  • 事件类型
#define NUM_EVENTS    8
#define TE_TOUCH 0x0001
#define TE_RELEASE 0x0002
#define TE_MOVE 0x0004
#define TE_GESTURE 0x0008
#define TE_TAP 0x0010
#define TE_DBLTAP 0x0020
#define TE_DRAGGED 0x0040
#define TE_PRESSED 0x0080
#define TE_ALL 0x0FFF
#define TE_BTNONLY 0x1000

使用示例:

  #include <M5Core2.h>

Button lt(0, 0, 160, 120, "left-top");
Button lb(0, 120, 160, 120, "left-bottom");
Button rt(160, 0, 160, 120, "right-top");
Button rb(160, 120, 160, 120, "right-bottom");

void colorButtons(Event& e) {
Button& b = *e.button;
M5.Lcd.fillRect(b.x, b.y, b.w, b.h, b.isPressed() ? WHITE : BLACK);
}

void dblTapped(Event& e) {
Serial.println("--- TOP RIGHT BUTTON WAS DOUBLETAPPED ---");
}

void setup() {
M5.begin();
M5.Buttons.addHandler(colorButtons, E_TOUCH + E_RELEASE);
rt.addHandler(dblTapped, E_DBLTAP);
}

void loop() {
M5.update();
}

Gesture类

Gesture类支持传入两个TouchButton区域,创建触摸手势对象与手势名称。通过addHandler创建手势触发后的处理函数,当由区域1移动至区域2且符合手势配置时将触发处理函数。

构造函数:

Gesture(TouchZone fromZone_, TouchZone toZone_, const char* name_ = "", uint16_t maxTime_ = GESTURE_MAXTIME, uint16_t minDistance_ = GESTURE_MINDIST)

功能:创建矩形按键区域实例

使用示例:

#include <M5Core2.h>

TouchZone topHalf(0,0,320,120);
TouchZone bottomHalf(0,120,320,160);
Gesture swipeDown(topHalf, bottomHalf, "Swipe Down");

void yayWeSwiped(TouchEvent& e) {
Serial.println("--- SWIPE DOWN DETECTED ---");
}


void setup() {
M5.begin();
swipeDown.addHandler(yayWeSwiped);
}

void loop() {
M5.update();
}

update

所有触摸状态的更新,都依赖于M5.update()

TouchEvent结构体

当触摸事件被触发时候,将会自动调用该区域使用addHandler所绑定的处理函数,同时将TouchEvent结构体作为参数进行传入。

struct TouchEvent {
uint8_t finger; //触摸点序号,最大支持两点
uint16_t type; //事件类型
TouchPoint from; //事件初始触摸点坐标-->x,y
TouchPoint to; //事件结束触摸点坐标-->x,y
uint16_t duration; //事件持续时间
TouchButton* button; //事件触发对象
Gesture* gesture; //事件触发手势
};

使用示例:

#include <M5Core2.h>

TouchButton lt = TouchButton(0, 0, 160, 120, "left-top");
TouchButton lb = TouchButton(0, 120, 160, 120, "left-bottom");
TouchButton rt = TouchButton(160, 0, 160, 120, "right-top");
TouchButton rb = TouchButton(160, 120, 160, 120, "right-bottom");


void eventDisplay(TouchEvent& e) {
Serial.printf("%-12s finger%d %-18s (%3d, %3d)", M5.Touch.eventTypeName(e), e.finger, M5.Touch.eventObjName(e), e.from.x, e.from.y);
if (e.type != TE_TOUCH && e.type != TE_TAP && e.type != TE_DBLTAP) {
Serial.printf("--> (%3d, %3d) %5d ms", e.to.x, e.to.y, e.duration);
}
Serial.println();
}

void setup() {
M5.begin();
M5.Touch.addHandler(eventDisplay);
}

void loop() {
M5.update();
}

Touch对象

随着M5.begin()初始化,将会生成一个Touch实例,该实例中可以获取到当前屏幕的一些触摸操作信息,如坐标,状态等。

getPressPoint()

功能

获取触摸坐标

函数原型:

TouchPoint_t getPressPoint()
#include <M5Core2.h>

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

void loop() {
TouchPoint_t coordinate;
coordinate = M5.Touch.getPressPoint();
Serial.printf("x:%d, y:%d \r\n", coordinate.x, coordinate.y);
}

ispressed()

功能

检查屏幕是否按压

函数原型:

bool ispressed()

Example

#include <M5Core2.h>

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

void loop() {
if(M5.Touch.ispressed()) {
Serial.println("Pressed");
}
}

HotZone_t* createHotZone()

功能

创建一个触摸热区

函数原型:

HotZone_t* creatHotZone(uint16_t x0, uint16_t y0, uint16_t w, uint16_t h, void (*fun)() = nullptr )

Example

#include <M5Core2.h>

void doFunc(void){
Serial.println("executed doFunc()");
}

HotZone Btn(0, 0, 320, 240, &doFunc));



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

}

inHotZone()

功能

判断是否在热区内

函数原型:

bool inHotZone(TouchPoint_t point)

Example

#include<M5Core2.h>

HotZone Btn(140, 100, 200, 160);

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

void loop() {
TouchPoint_t pos = M5.Touch.getPressPoint();
if(Btn.inHotZone(pos)) {
Serial.printf("%d, %d\r\n", pos.x, pos.y);
}

}

11.RTC

类名: Rtc

begin()

功能:

初始化RTC时钟

函数原型:

void begin(void)

注意:
1.如果您不想使用M5.begin() 初始化RTC时钟,请在使用RTC之前调用此功能

使用示例:

#include <M5Core2.h>

void setup() {
M5.Rtc.begin(); //初始化 RTC时钟
}

void loop() {
}

SetTime()

功能:

设置实时时钟时间

函数原型:

void SetTime(RTC_TimeTypeDef* RTC_TimeStruct)

使用示例:

#include <M5Core2.h>

RTC_TimeTypeDef TimeStruct;
void setup() {
M5.begin();

TimeStruct.Hours = 18; //设置实时时钟结构体的具体时刻
TimeStruct.Minutes = 56;
TimeStruct.Seconds = 10;
M5.Rtc.SetTime(&TimeStruct); //将设置的时间写入实时时钟
}
void loop(){}

GetTime()

功能:

获取实时时钟时间

函数原型:

void GetTime(RTC_TimeTypeDef* RTC_TimeStruct)

使用示例:

#include <M5Core2.h>

RTC_TimeTypeDef TimeStruct;
void setup() {
M5.begin();
M5.Lcd.println("RTC Time TEST");
TimeStruct.Hours = 18;
TimeStruct.Minutes = 56;
TimeStruct.Seconds = 10;
M5.Rtc.SetTime(&TimeStruct);
}

void loop() {
M5.Rtc.GetTime(&TimeStruct);
M5.Lcd.setCursor(0, 15);
M5.Lcd.printf("Time: %02d : %02d : %02d/n",TimeStruct.Hours, TimeStruct.Minutes, TimeStruct.Seconds);
delay(500);
}

SetData()

功能:

设置实时时钟日期

函数原型:

void SetData(RTC_TimeTypeDef* RTC_DateStruct)

使用示例:

#include <M5Core2.h>

RTC_DateTypeDef DateStruct;
void setup() {
M5.begin();
DateStruct.WeekDay = 3;
DateStruct.Month = 3;
DateStruct.Date = 22;
DateStruct.Year = 2019;
M5.Rtc.SetData(&DateStruct);
}
void loop(){}

GetData()

功能:

获取实时时钟日期

函数原型:

void GetData(RTC_TimeTypeDef* RTC_DateStruct)

使用示例:

#include <M5Core2.h>

RTC_DateTypeDef DateStruct;
void setup() {
M5.begin();
M5.Lcd.println("RTC Date TEST");
DateStruct.WeekDay = 5;
DateStruct.Month = 7;
DateStruct.Date = 23;
DateStruct.Year = 2021;
M5.Rtc.SetDate(&DateStruct);
}

void loop() {
M5.Rtc.GetDate(&DateStruct); //获取实时时钟的日期
M5.Lcd.setCursor(0, 15);
M5.Lcd.printf("Data: %04d-%02d-%02d/n",DateStruct.Year, DateStruct.Month,DateStruct.Date); //将此刻实时时钟的日期在屏幕输出
M5.Lcd.printf("Week: %d/n",DateStruct.WeekDay);
delay(500);
}

clearIRQ()

功能:

清除中断请求

函数原型:

void clearIRQ()

disableIRQ()

功能:

关闭中断请求

函数原型:

void disableIRQ()

SetAlarmIRQ()

功能:

设置中断时钟

函数原型:

int SetAlarmIRQ(int afterSeconds)
int SetAlarmIRQ( const RTC_TimeTypeDef &RTC_TimeStruct)
int SetAlarmIRQ( const RTC_DateTypeDef &RTC_DateStruct, const RTC_TimeTypeDef &RTC_TimeStruct)

使用示例:

#include <M5Core2.h>

RTC_TimeTypeDef TimeStruct;
void setup() {
M5.begin();
M5.Lcd.println("RTC SetAlarmIQR");
TimeStruct.Hours = 18;
TimeStruct.Minutes = 56;
TimeStruct.Seconds = 10;
M5.Rtc.SetTime(&TimeStruct);
}

void loop() {
M5.update();
M5.Rtc.GetTime(&TimeStruct);
M5.Lcd.setCursor(0, 15);
M5.Lcd.printf("Time: %02d : %02d : %02d/n",TimeStruct.Hours, TimeStruct.Minutes, TimeStruct.Seconds);
if(M5.BtnA.wasPressed()){
M5.Lcd.println("M5Core2 Will Close, Restore After 5 seconds ");
delay(2000);
M5.Rtc.clearIRQ();
M5.Rtc.SetAlarmIRQ(5);
delay(10);
M5.Axp.PowerOff();
}
}

二、ESP32函数

1.GPIO与中断

GPIO6 ~ 11一般不使用,因为它们接了存储程序的Flash。
1.引脚工作方式设置
mode可选:
INPUT、OUTPUT、INPUT_PULLUP、INPUT_PULLDOWN
输入、输出、上拉输入、下拉输入

pinMode(pin, mode)

2.设置某引脚高低电平

digitalWrite(pin, value)

3.读取某引脚电平值

digitalRead(pin)

外部中断:
打开中断,使用

attachInterrupt(uint8_t pin, void (*)(void), int mode)

(引脚号、中断服务函数、服务函数的输入参数、外部中断触发模式)
mode可选:
RISING、FALLING、CHANGE……
上升沿、下降沿、改变时、低电平、高电平…

attachInterruptArg(uint8_t pin, void (*)(void*), void * arg, int mode)

3.关闭中断,使用

detachInterrupt(uint8_t pin)
使用示例:
void callBack(void)
{
int level = digitalRead(13); //读取GPIO_13上的电平
Serial.printf("触发了中断,当前电平是: %d\n", level);
}

void setup()
{
Serial.begin(115200);
Serial.println();

pinMode(13,OUTPUT); //GPIO_13,输出模式
attachInterrupt(13, callBack, CHANGE); //当电平发生变化时,触发中断

for (int i = 0; i < 5; i++)
{
delay(1000);
digitalWrite(13, 1 ^ digitalRead(13)); //翻转 GPIO_13 电平
}

detachInterrupt(13); //关闭中断
}

void loop()
{
}

2.PWM(LEDC)

ESP32有一个LEDC,原本设计用来控制LED,可以作简单的PWM输出
LEDC总共有16个路通道(0 ~ 15),分为高低速两组,高速通道(0 ~ 7)由80MHz时钟驱动,低速通道(8 ~ 15)由1MHz时钟驱动。

double ledcSetup(uint8_t channel, double freq, uint8_t resolution_bits)

channel为通道号,取值0 ~ 15;freq,设置频率;resolution_bits计数位数,取值0 ~ 20(该值决定后面ledcWrite方法中占空比的最大值,如该值写10,则占空比最大可写2^10-1=1023 ;
通道最终频率 = 时钟频率 / ( 分频系数 * ( 2^计数位数 ) );(分频系数最大为1024)
该方法返回值:最终频率;

void ledcWrite(uint8_t channel, uint32_t duty)

(通道,占空比);指定通道输出一定占空比的波形;

double ledcWriteTone(uint8_t channel, double freq)

(通道,频率)

uint32_t ledcRead(uint8_t channel)

(通道);返回指定通道占空比的值;

double ledcReadFreq(uint8_t channel)

(通道);返回指定通道当前频率(如果当前占空比为0 则该方法返回0);

void ledcAttachPin(uint8_t pin, uint8_t channel)

(引脚,通道);绑定:将LEDC通道投射到指定IO口上;

void ledcDetachPin(uint8_t pin)

(引脚);解除绑定:解除IO口的LEDC功能;

使用示例:

// GPIO_14 输出PWM
// GPIO_12 读取IO14输出的信号

void setup()
{
Serial.begin(115200);
Serial.println();

ledcSetup(8, 1, 10); //设置LEDC通道8频率为1,分辨率为10位,即占空比可选0~1023
ledcAttachPin(14, 8); //设置LEDC通道8在IO14上输出

pinMode(12, INPUT_PULLDOWN); //引脚12,下拉输入

for (int i = 0; i < 5; i++)
{
ledcWrite(8, 250 * i); //设置输出PWM
for (int j = 0; j < 100; j++)
{
delay(10);
Serial.println(digitalRead(12)); //读取引脚12的电平并输出
}
}
}

void loop()
{
}

3.FreeRtos

  • 创建一个任务
xTaskCreate(

TaskCode, /* 任务将要调用的功能函数 */

TaskName, /* 任务名字 */

StackDepth, /* Stack size in bytes. */

Parameter, /* Parameter passed as input of the task */

priority, /* 任务优先级 */

TaskHandle); /* 句柄 */

  • TaskCode:指向任务函数的指针

  • TaskName:任务的名称

  • StackDepth:任务堆栈大小,以字节数表示。

  • Parameter:指针,指向任务函数所接收的参数

  • Priority:任务的优先级,数字越大优先级越高,默认最大32级(不定,可以自行去库文件修改),不同任务可以有相同优先级,够用的情况下,最大优先级越小越好

  • TaskHandle:返回一个句柄,用于以后进行函数调用(比如要删除某个任务或者修改其优先级)时对任务的引用

  • 该函数会返回pdPass(成功时)或错误代码

  • 任务函数

任务函数就是我们自定义的功能函数了,注意,任务函数不能有任何返回值,即应该定义成void使用TaskDelte(句柄)函数可以删除任务

void setup() {
Serial.begin(112500);
delay(1000);
xTaskCreate(
taskOne, /* Task function. */
"TaskOne", /* String with name of task. */
10000, /* Stack size in bytes. */
NULL, /* Parameter passed as input of the task */
1, /* Priority of the task. */
NULL); /* Task handle. */

xTaskCreate(
, /* Task function. */
"TaskTwo", /* String with name of task. */
10000, /* Stack size in bytes. */
NULL, /* Parameter passed as input of the task */
1, /* Priority of the task. */
NULL); /* Task handle. */
}

void loop() {
delay(1000);
}

void taskOne( void * parameter )

{
for( int i = 0;i<10;i++ ){
Serial.println("Hello from task 1");
​ delay(1000);
}
Serial.println("Ending task 1");
vTaskDelete( NULL );
}

void taskTwo( void * parameter)

{
for( int i = 0;i<10;i++ ){
Serial.println("Hello from task 2");
​ delay(1000);
}
Serial.println("Ending task 2");
vTaskDelete( NULL );
}