#include <GxEPD2_BW.h>
#include <GxGDEH029A1/GxGDEH029A1.cpp> // 2.9" 128x296
#include <Fonts/FreeSansBold9pt7b.h>
#include <SPI.h>
// 定义引脚
#define CS_PIN 12
#define DC_PIN 14
#define RST_PIN 27
#define BUSY_PIN 26
// 实例化显示屏对象
GxEPD2_BW<GxEPD2_290, GxEPD2_290::HEIGHT> display(GxEPD2_290(CS_PIN, DC_PIN, RST_PIN, BUSY_PIN));
// 中文字符串转换函数
String GBKToUTF8(const char* str);
void setup() {
Serial.begin(115200);
Serial.println("初始化墨水屏...");
// 初始化显示屏
display.init(115200);
display.setRotation(1); // 设置显示方向
display.setFont(&FreeSansBold9pt7b); // 设置英文字体
display.setTextColor(GxEPD_BLACK);
// 清屏
display.firstPage();
do {
display.fillScreen(GxEPD_WHITE);
} while (display.nextPage());
// 显示欢迎信息
display.firstPage();
do {
display.fillScreen(GxEPD_WHITE);
display.setCursor(10, 30);
display.print("Initializing...");
} while (display.nextPage());
delay(1000);
}
void loop() {
// 显示"你好"两个字
display.firstPage();
do {
display.fillScreen(GxEPD_WHITE);
// 设置中文字体位置和大小
display.setCursor(40, 100);
// 使用UTF-8编码显示中文
display.print(GBKToUTF8("你好"));
} while (display.nextPage());
Serial.println("已显示'你好'两个字");
// 进入深度睡眠,节省电量
Serial.println("进入深度睡眠...");
esp_sleep_enable_timer_wakeup(60 * 1000000); // 1分钟后唤醒
esp_deep_sleep_start();
}
// GBK到UTF-8的转换函数(简化版)
// 注意:这个函数只能处理部分中文字符,完整的转换需要使用更复杂的库
String GBKToUTF8(const char* str) {
String result = "";
unsigned char ch1, ch2;
while (*str) {
ch1 = *str;
if (ch1 < 0x80) {
// ASCII字符直接添加
result += (char)ch1;
str++;
} else {
// GBK双字节字符
ch2 = *(str + 1);
// 这里是一个简化的GBK到UTF-8转换表,仅包含常用汉字
// 完整的转换需要使用更复杂的算法或查表
if (ch1 == 0xC4 && ch2 == 0xE3) result += "你"; // 你
else if (ch1 == 0xBA && ch2 == 0xC3) result += "好"; // 好
str += 2;
}
}
return result;
} Loading
epaper-2in9
epaper-2in9