System

begin()

功能:

初始化触摸屏; 初始化TF Card; 清串口缓冲区,设置串口波特率为 115200; 电池 ADC读取; 初始化 I2C

函数原型:

void begin(bool touchEnable = true, bool SDEnable = true, bool SerialEnable = true, bool BatteryADCEnable = true, bool I2CEnable = false)

函数实现:

void M5EPD::begin(bool touchEnable, bool SDEnable, bool SerialEnable,
                  bool BatteryADCEnable, bool I2CEnable) {
    if (_isInited) {
        return;
    }
    _isInited = true;

    pinMode(M5EPD_MAIN_PWR_PIN, OUTPUT);
    enableMainPower();

    if (SerialEnable == true) {
        Serial.begin(115200);
        Serial.flush();
        delay(50);
        Serial.print("M5EPD initializing...");
    }

    pinMode(M5EPD_EXT_PWR_EN_PIN, OUTPUT);
    pinMode(M5EPD_EPD_PWR_EN_PIN, OUTPUT);
    pinMode(M5EPD_KEY_RIGHT_PIN, INPUT);
    pinMode(M5EPD_KEY_PUSH_PIN, INPUT);
    pinMode(M5EPD_KEY_LEFT_PIN, INPUT);
    delay(100);

    enableEXTPower();
    enableEPDPower();
    delay(1000);

    EPD.begin(M5EPD_SCK_PIN, M5EPD_MOSI_PIN, M5EPD_MISO_PIN, M5EPD_CS_PIN,
              M5EPD_BUSY_PIN);

    if (SDEnable == true) {
        SPI.begin(14, 13, 12, 4);
        SD.begin(4, SPI, 20000000);
    }

    if (touchEnable == true) {
        if (TP.begin(21, 22, 36) != ESP_OK) {
            log_e("Touch pad initialization failed.");
        }
    } else if (I2CEnable == true) {
        Wire.begin(21, 22, (uint32_t)400000U);
    }

    if (BatteryADCEnable == true) {
        BatteryADCBegin();
    }

    if (SerialEnable == true) {
        Serial.println("OK");
    }
}

使用示例:

#include <M5EPD.h>

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

update()

功能:

检测按键状态变化

函数原型:

void update()

使用示例:

#include <M5EPD.h>
void setup() {
  M5.begin(false,false,true,false,false);
}

void loop() {
  M5.update();
  if( M5.BtnL.wasPressed()) {
    Serial.println("Left button pressed");
  }
  delay(100);
}

Button

相关引脚定义

定义 引脚 描述
M5EPD_KEY_LEFT_PIN 37 拨轮向上滚动
M5EPD_KEY_PUSH_PIN 38 拨轮按下
M5EPD_KEY_RIGHT_PIN 39 拨轮向下滚动
M5EPD_PORTA_W_PIN 32 PORT-A
M5EPD_PORTA_Y_PIN 25 PORT-A
M5EPD_PORTA_W_PIN 32 PORT-A
M5EPD_PORTA_Y_PIN 25 PORT-A
M5EPD_PORTB_W_PIN 33 PORT-B
M5EPD_PORTB_Y_PIN 26 PORT-B
M5EPD_PORTC_W_PIN 19 PORT-C
M5EPD_PORTC_Y_PIN 18 PORT-C
M5EPD_PORTC_Y_PIN 18 PORT-C
M5EPD_MISO_PIN 13 IT8951E/TF-card MISO
M5EPD_MOSI_PIN 12 IT8951E/TF-card MOSI
M5EPD_SCK_PIN 14 IT8951E/TF-card SCK
M5EPD_CS_PIN 15 IT8951E CS
M5EPD_MAIN_PWR_PIN 2 PWR
M5EPD_SCK_PIN 14 IT8951E/TF-card SCK
M5EPD_SCK_PIN 14 IT8951E/TF-card SCK
M5EPD_BUSY_PIN 27 /
M5EPD_EXT_PWR_EN_PIN 5 /
M5EPD_EPD_PWR_EN_PIN 23 /
M5EPD_BAT_VOL_PIN 35 /

lastChange()

功能:

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

函数原型:

uint32_t lastChange()

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

使用示例:

#include <M5EPD.h>

void setup() {
  M5.begin();
  Serial.println("Please pressed Button L.");
}

void loop() {
  M5.update();
  Serial.printf("The last change at %d ms /n",M5.BtnL.lastChange());    //Print the time of the last state change of key L. 打印按键L最后一次状态变化的时间
}

isPressed()

功能:

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

函数原型:

uint8_t isPressed()

使用示例:

#include <M5EPD.h>

void setup() {
    M5.begin();
    Serial.println("Please pressed Button L.");
}

void loop() {
    M5.update(); // Need to add M5.update() to read the state of the button
     //需添加M5.update()才能读取到按键的状态.
    if (M5.BtnL.isPressed()) {  //If the key is pressed. 如果按键按下
        Serial.println("Button is Pressed.");
    } else {
        Serial.println("Button is Released.");
    }
    delay(20);
}

pressedFor()

功能:

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

函数原型:

uint8_t pressedFor(uint32_t ms)

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

使用示例:

#include <M5EPD.h>

void setup() {
    M5.begin();
    Serial.println("Please pressed Button L.");
}

void loop() {
    M5.update();
    if (M5.BtnL.pressedFor(
            2000)) {  // If the button is pushed up for more than 2 seconds.
                      //如果按键被推上超过2秒.
        Serial.println("Button L was pressed for more than 2 seconds.");
        delay(1000);
    }
}

wasPressed()

功能:

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

函数原型:

uint8_t wasPressed()

使用示例:

#include <M5EPD.h>

void setup() {
  M5.begin();
  Serial.println("Please push on Button L.");
}

void loop() {
  M5.update();
  if (M5.BtnL.wasPressed()) {    //If the button is pushed up. 如果按键被推上
    Serial.println("Button is pressed.");
  }
  delay(20);
}

Released

isReleased()

功能:

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

函数原型:

uint8_t isPressed()

使用示例:

#include <M5EPD.h>

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

void loop() {
    M5.update();  // Need to add M5.update() to read the state of the button
                  //需添加M5.update()才能读取到按键的状态.
    if (M5.BtnL.isReleased()) {  //If the key is released. 如果按键释放.
        Serial.println("Button is released.");
    } else {
        Serial.println("Button is Pressed .");
    }
    delay(20);
}

releasedFor()

功能:

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

函数原型:

uint8_t pressedFor(uint32_t ms)

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

使用示例:

#include <M5EPD.h>

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

void loop() {
    M5.update();  // Need to add M5.update() to read the state of the button
                  //需添加M5.update()才能读取到按键的状态.
    if (M5.BtnL.isReleased()) {  //If the key is released. 如果按键释放
        Serial.println("Button is released.");
    } else {
        Serial.println("Button is pressed.");
    }
    delay(20);
}

wasReleased()

功能:

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

函数原型:

uint8_t wasReleased()

使用示例:

#include <M5EPD.h>

void setup() {
    M5.begin();
    Serial.println("Please pressed Button L.");
}

void loop() {
    M5.update();
    if (M5.BtnL.wasReleased()) {  //If the key is released. 如果按键释放
        Serial.println("Button is Released.");
    }
    delay(20);
}

wasReleasefor()

功能:

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

函数原型:

uint8_t wasReleasefor(uint32_t ms)

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

使用示例:

#include <M5EPD.h>

void setup() {
    M5.begin();
    Serial.println("Please pressed Button L.");
}

void loop() {
    M5.update();
    if (M5.BtnL.wasReleasefor(
            3000)) {  // If button A is pressed for 3s and then released
                      //如果按键A按下3s之后释放.
        Serial.println("OK");
    }
}

Power

enableEXTPower()

功能:

启用拓展端口电源

函数原型:

void enableEXTPower()

使用示例:

#include <M5EPD.h>

void setup() {
    M5.begin();
    M5.enableEXTPower();  //enable external power. 启用拓展端口电源
    Serial.println("Expansion port power is on");
}

void loop() {}

disableEXTPower()

功能:

关闭拓展端口电源

函数原型:

void disableEXTPower()

使用示例:

#include <M5EPD.h>

void setup() {
    M5.begin();
    M5.disableEXTPower();  //disable external power. 关闭拓展端口电源
    Serial.println("Expansion port power is off");
}

void loop() {}

enableEPDPower()

功能:

启用墨水屏电源

函数原型:

void enableEPDPower()

使用示例:

#include <M5EPD.h>

void setup() {
    M5.begin();
    M5.enableEPDPower();  //enable EPD power. 启用墨水屏电源
    Serial.println("Ink screen power is activated");
}

void loop() {}

disableEPDPower()

功能:

关闭墨水屏电源

函数原型:

void disableEPDPower()

使用示例:

#include <M5EPD.h>

void setup() {
    M5.begin();
    M5.disableEPDPower();  //disable EPD power. 关闭墨水屏电源
    Serial.println("Ink screen power off");
}

void loop() {}

enableMainPower()

功能:

启用主电源

函数原型:

void enableMainPower()

使用示例:

#include <M5EPD.h>

void setup() {
    M5.begin();
    M5.enableMainPower();  //Start main power. 启动主电源
    Serial.println("Main power is on");
}

void loop() {}

disableMainPower()

功能:

关闭主电源

函数原型:

void disableMainPower()

使用示例:

#include <M5EPD.h>

void setup() {
    M5.begin();
    M5.disableMainPower();  //Turn off the main power. 关闭主电源
    Serial.println("Main power is off");
}

void loop() {}

BatteryADCBegin()

功能:

初始化电池ADC检测

函数原型:

void BatteryADCBegin()

使用示例:

#include <M5EPD.h>

void setup() {
    M5.begin();
    M5.BatteryADCBegin();  // Initialize battery ADC detection.
                           // 初始化电池ADC检测
}

void loop() {}

getBatteryRaw()

功能:

读取电池电压原生ADC值

函数原型:

uint32_t getBatteryRaw()

使用示例:

#include <M5EPD.h>

void setup() {
    M5.begin();
    Serial.printf("ADC RAW:%d",
                  M5.getBatteryRaw());  // Read battery voltage native ADC values
                                        // 读取电池电压原生ADC值
}

void loop() {}

getBatteryVoltage()

功能:

读取电池电压

函数原型:

uint32_t getBatteryVoltage()

使用示例:

#include <M5EPD.h>

void setup() {
    M5.begin();
    Serial.printf("Battery Voltage:%d",
                  M5.getBatteryVoltage());  // Read battery voltage
                                            // 读取电池电压
}

void loop() {}

shutdown()

函数重载1:

关闭电源,再次启动需要通过PWR按键唤醒

void shutdown()

函数重载2:

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

int shutdown( int seconds )

函数重载3:

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

int shutdown( const rtc_time_t &RTC_TimeStruct)

函数重载4:

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

int shutdown( const rtc_date_t &RTC_DateStruct, const rtc_time_t &RTC_TimeStruct)

使用示例:

#include <M5EPD.h>

M5EPD_Canvas canvas(&M5.EPD);

void setup(){
  M5.begin();
  M5.EPD.SetRotation(90);
  M5.TP.SetRotation(90);
  M5.EPD.Clear(true);
  M5.RTC.begin();
  canvas.createCanvas(540, 960);
  canvas.setTextSize(3);
  canvas.drawString("Press PWR Btn for sleep!", 45, 350);
  canvas.drawString("after 5 sec wakeup!", 70, 450);
  canvas.pushCanvas(0,0,UPDATE_MODE_DU4);
}

void loop(){
  if(M5.BtnP.wasPressed()){
    canvas.drawString("I'm going to sleep.zzzZZZ~", 45, 550);
    canvas.pushCanvas(0,0,UPDATE_MODE_DU4);
    delay(1000);
    M5.shutdown(5);
  }
  M5.update();
  delay(100);
}
On This Page