esp32c3 使用 platformio 开发墨水屏简介

发布时间 2023-04-05 22:48:48作者: LiuChengloong

这个教程是提供给购买了我 esp32c3 墨水屏开发板的朋友看的,也适用于 esp32c3 驱动墨水屏的项目。

安装 platformio 开发环境

这里就不赘述了,安装过程中如果下载很慢,看我的博客中有解决的方案。

新建项目

这里注意要选择 Espressif ESP32-C3-DevKitM-1 这个 Board,这个适用于合宙 esp32c3

配置开发库

编辑 platformio.ini 修改为以下配置

[env:esp32-c3-devkitm-1]
platform = espressif32
board = esp32-c3-devkitm-1
framework = arduino
lib_ldf_mode = deep+
board_build.flash_mode = dio
lib_deps = zinggjm/GxEPD2@^1.5.0

亮屏程序

这里注意第六行代码:
GxEPD2_290_T5D 这个是屏幕的驱动程序,不同屏幕对应的驱动是不一样的,不要选择错误。
屏幕的驱动到屏幕的生产厂商处可以找到,屏幕驱动对应代码关系到库的 github 上找,或者看源码。
https://github.com/ZinggJM/GxEPD2

#include <Arduino.h>

#include <GxEPD2_BW.h>
#include <Fonts/FreeMonoBold9pt7b.h>

GxEPD2_BW<GxEPD2_290_T5D, GxEPD2_290_T5D::HEIGHT> display(GxEPD2_290_T5D(/*CS=D8*/ SS, /*DC=D3*/ 10, /*RST=D4*/ 3, /*BUSY=D2*/ 2));

const char HelloWorld[] = "Hello World!";

void helloWorld()
{
  // Serial.println("helloWorld");
  display.setRotation(1);
  display.setFont(&FreeMonoBold9pt7b);
  display.setTextColor(GxEPD_BLACK);
  int16_t tbx, tby;
  uint16_t tbw, tbh;
  display.getTextBounds(HelloWorld, 0, 0, &tbx, &tby, &tbw, &tbh);
  // center bounding box by transposition of origin:
  uint16_t x = ((display.width() - tbw) / 2) - tbx;
  uint16_t y = ((display.height() - tbh) / 2) - tby;
  // full window mode is the initial mode, set it anyway
  display.setFullWindow();
  display.fillScreen(GxEPD_WHITE);
  display.setCursor(x, y);
  display.print(HelloWorld);
  display.display(false); // full update
  Serial.println("helloWorld done");
}

void setup() {
  display.init(115200);
  helloWorld();
  display.hibernate();
}

void loop() {
  // put your main code here, to run repeatedly:
}