回到首页 返回首页
回到顶部 回到顶部
返回上一页 返回上一页

ESP32 墨水屏4.7 测试 简单

头像 zoologist 2026.03.15 11 0

1.因为支持库的原因,只能使用 ESP32 2.0.3版本,更高的版本可能出现编译不过问题

2. 因为使用了字库,因此需要使用下面的配置才能正常编译

epd47.png

代码




#include <Arduino.h>
#include "epd_driver.h"
#include "hzk\hz3500_36.h"
#include "memo_historyManager.h"

memo_historyManager* objmemo_historyManager;
uint8_t *framebuffer;

//------------------字符显示------------------//
int GetCharwidth(String ch)
{
 //修正,空格计算的的宽度为0, 强制36 字体不一样可能需要修改!
 if (ch == " ") return 28;

 char buf[50];
 int x1 = 0, y1 = 0, w = 0, h = 0;
 int tmp_cur_x = 0;
 int tmp_cur_y = 0;
 FontProperties properties;
 get_text_bounds((GFXfont *)&msyh36, (char *) ch.c_str(), &tmp_cur_x, &tmp_cur_y, &x1, &y1, &w, &h, &properties);
 //sprintf(buf, "x1=%d,y1=%d,w=%d,h=%d", x1, y1, w, h);
 //Serial.println("ch="+ ch + ","+ buf);

 //负数说明没找到这个字,会不显示出来
 if (w <= 0)
   w = 0;
 return (w);
}

//------------------文字显示------------------//
void Show_hz(String rec_text, bool loadbutton)
{
 //最长限制160字节,40汉字
 //6个字串,最长约在 960字节,小于1024, json字串最大不超过1024
 rec_text = rec_text.substring(0, 160);
 Serial.println("begin Showhz:" + rec_text);

 //更正汉字符号显示的bug
 rec_text.replace(",", ",");
 rec_text.replace("。", ".");
 rec_text.replace("?", "?");

 epd_poweron();
 //uint32_t t1 = millis();
 //全局刷
 epd_clear();
 
 //局刷,一样闪屏
 //epd_clear_area(screen_area);
 //epd_full_screen()

 //此句不要缺少,否则显示会乱码
 memset(framebuffer, 0xFF, EPD_WIDTH * EPD_HEIGHT / 2);
 //uint32_t t2 = millis();
 //printf("EPD clear took %dms.\n", t2 - t1);
 int cursor_x = 10;
 int cursor_y = 80;

 //多行文本换行显示算法。
 if (!loadbutton)
   objmemo_historyManager->multi_append_txt_list(rec_text);
   
 String now_string = "";
 int i;
 //write_string 能根据手工加的 "\n"换行,但不能自由控制行距,此处我自行控制了.
 for ( i = 0; i < objmemo_historyManager->memolist.size() ; i++)
 {
   now_string = objmemo_historyManager->memolist.get(i);
   //Serial.println("Show_hz line:" + String((now_index + i) % TXT_LIST_NUM) + " " + now_string);

   if (now_string.length() > 0)
   {
     //加">"字符,规避epd47的bug,当所有字库不在字库时,esp32会异常重启
     // “Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled."
     now_string = ">" + now_string;
     //墨水屏writeln不支持自动换行
     //delay(200);
     //一定要用framebuffer参数,否则当最后一行数据过长时,会导致代码在此句阻塞,无法休眠,原因不明!

     writeln((GFXfont *)&msyh36, (char *)now_string.c_str(), &cursor_x, &cursor_y, framebuffer);

     //writeln调用后,cursor_x会改变,需要重新赋值
     cursor_x = 10;
     cursor_y = cursor_y + 85;
   }
 }

 //前面不要用writeln,有一定机率阻塞,无法休眠
 epd_draw_grayscale_image(epd_full_screen(), framebuffer);

 //delay(500);
 epd_poweroff();

 Serial.println("end Showhz:" + rec_text );
}

void setup() {
 Serial.begin(115200);
 Serial.println("Start");
 // put your setup code here, to run once:
 //水墨屏硬件初始化
 epd_init();

 framebuffer = (uint8_t *)ps_calloc(sizeof(uint8_t), EPD_WIDTH * EPD_HEIGHT / 2);
 if (!framebuffer)
 {
   Serial.println("alloc memory failed !!!");
   delay(1000);
   while (1);
 }
 
 objmemo_historyManager = new memo_historyManager();
 objmemo_historyManager->GetCharwidth = GetCharwidth;
 
 memset(framebuffer, 0xFF, EPD_WIDTH * EPD_HEIGHT / 2);

}

void loop() {
 Show_hz("测试", false);
 delay(3000);
 Show_hz("循环显示", false);  
 delay(3000);
 Show_hz("测试20000000", false);
 delay(3000);
 Show_hz("月亮渐渐落下,乌鸦啼叫,到处弥漫着寒霜,江边的枫树黑糊糊的一片,渔船上的灯火不停摇动,我与愁思相伴难以入眠", false);
 delay(3000);  
}

附件

评论

user-avatar