《ESP8266墨水屏阅读器开发实战》——读取SD卡内容(1)Need be mindful: ESP8266 has SPI pins: D5 (SCLK), D6 (MISO), D7 (MOSI), D8 (CS). SD card module 3.3V, level


AI free token/AI免费积分

本文实现的功能是基于GitHub上readguy开源代码的二次开发与精简,项目原始作者是friendshipender,项目地址:https://github.com/fsender/readguy


上一篇介绍了使用readguy库显示Img2LCD生成的点阵数据,现在继续分享如何读取SD卡内容,并显示到屏幕上。
先看效果:
《ESP8266墨水屏阅读器开发实战》——读取SD卡内容(1)
ESP8266 仅支持HSPI模式读取SD卡,与屏幕共享SPI总线。
《ESP8266墨水屏阅读器开发实战》——读取SD卡内容(1)
《ESP8266墨水屏阅读器开发实战》——读取SD卡内容(1)

ESP8266 GPIO  

SD卡模块引脚
说明

GPIO16

CS

片选,低电平时,选中该SD卡模块进行通信。

GPIO13

MOSI(DI)

与墨水屏共享 HSPI MOSI

GPIO12

MISO(DO)

HSPI MISO

GPIO14

CLK(SCK)

与墨水屏共享 HSPI SCK

3.3v
VCC
电源正极
GND
GND
接地
墨水屏使用引脚:
  • #define READGUY_epd_mosi 13 //  MOSI 引脚#define READGUY_epd_sclk 14 //  SCLK 引脚#define READGUY_epd_cs   15 //  CS   引脚#define READGUY_epd_dc    4 //  DC   引脚#define READGUY_epd_rst  2 //   RST  引脚#define READGUY_epd_busy 5 //   BUSY 引脚
《ESP8266墨水屏阅读器开发实战》——读取SD卡内容(1)
(点击放大)

需要ctg_u8g2_wqy12.c和ctg_u8g2_wqy12.h两个文件,在readguy库的示例文件中。【代码中使用的16号字体wqy16_gb2312a,需要从u8g2库中导出】u8g2_font_wqy16_chinese、gb2312 字符集详细介绍以及在Readguy中的使用(墨水屏)

 

readsd001.ino

#include   //arduino功能基础库. 在platformIO平台上此语句不可或缺#include "readguy.h"  //包含readguy_driver 基础驱动库#include #include //显示中文#include "ctg_u8g2_wqy12.h"                                   //中文字体库extern const uint8_t ctg_u8g2_font_wqy16_gb2312a[];           //声明中文字体文件const lgfx::U8g2font cn_font16(ctg_u8g2_font_wqy16_gb2312a);  //U8G2格式中文字体转化为LGFX格式字体
// TF卡配置(硬件SPI,CS引脚为GPIO2)#define TF_CS 16
ReadguyDriver myDisplay;  //新建一个readguy对象, 用于显示驱动.
// IO0按键定义 (ESP8266的Flash按钮)#define BUTTON_PIN 0                 // GPIO0引脚#define DEBOUNCE_DELAY 50            // 防抖延时(ms)unsigned long lastDebounceTime = 0;  // 上次抖动时间int lastButtonState = HIGH;          // 上次按键状态

void __printMemoryInfo(const char* tag) {  Serial.printf("[%s] 可用内存=%u, 可分配内存=%u, 碎片=%.1f%%\n",                tag,                ESP.getFreeHeap(),                ESP.getMaxFreeBlockSize(),                100.0 * (1 - ESP.getMaxFreeBlockSize() / (float)ESP.getFreeHeap()));}bool isTxtFile(const char *filename) {    const char *dot = strrchr(filename, '.');    if (!dot) return false;    return strcasecmp(dot + 1, "txt") == 0;  // 不区分大小写比较}void listRootFiles() {  File root = SD.open("/");  while (File entry = root.openNextFile()) {    String fileName = entry.name();    if (entry.isDirectory()){      myDisplay.print("[DIR]");    }else{      if(isTxtFile(entry.name())){        myDisplay.print("[TXT]");      }
    }    Serial.println(fileName.c_str());    myDisplay.println(fileName.c_str());    myDisplay.display();    entry.close();    delay(50);  }}
void setup() {  // put your setup code here, to run once:  Serial.begin(115200);  //初始化串口  Serial.println("");  __printMemoryInfo("setup start");
  myDisplay.init(1);  myDisplay.setEpdDriver(true, true);
  myDisplay.fillScreen(TFT_WHITE);  // 全屏填充颜色  myDisplay.setTextColor(0, 1);     //设置显示的颜色. 0代表黑色, 1代表白色
  myDisplay.setFont(&cn_font16);  myDisplay.setTextSize(1);  myDisplay.setCursor(0, 10);  myDisplay.println("Initializing SD...");  myDisplay.display();  if (SD.begin(TF_CS)) {
    myDisplay.println("TF Card OK!");    myDisplay.display();    // 显示TF卡文件列表    listRootFiles();
  } else {    myDisplay.print("TF Card Failed!");    myDisplay.display();  }
  delay(100);  myDisplay.powerOffEPD();  // 初始化IO0按键 (内部上拉,低电平有效)  pinMode(BUTTON_PIN, INPUT_PULLUP);
  __printMemoryInfo("setup end");}

void loop() {  // IO0按键检测 - 刷白屏幕  int currentButtonState = digitalRead(BUTTON_PIN);
  // 检测按键按下 (下降沿触发 + 防抖)  if (currentButtonState == LOW && lastButtonState == HIGH) {    if (millis() - lastDebounceTime > DEBOUNCE_DELAY) {      lastDebounceTime = millis();      Serial.println("IO0按下 - 刷白屏幕");      delay(50);      myDisplay.fillScreen(TFT_WHITE);  // 全屏刷白      myDisplay.display();              // 刷新显示                                        // myDisplay.display(READGUY_SLOW);  // 刷新显示
      Serial.println("屏幕已刷白");      delay(200);            // 简单消抖      myDisplay.sleepEPD();  //进入睡眠模式, 屏幕不再刷新.      // myDisplay.powerOffEPD();    }  }
  lastButtonState = currentButtonState;
  delay(10);  // 降低CPU占用}

连接屏幕和SD卡模块后,只剩GPIO0(D3)引脚,无法满足阅读器按键要求(上一页、下一页、确认、返回),后续将通过ADC引脚搭配不同电阻实现多按键输入。

This project is a secondary development and simplification of the readguy open source code on GitHub, originally created by friendshipender. The project address is: https://github.com/fsender/readguy.

The previous article introduced how to use the readguy library to display dot matrix data generated by Img2LCD. Now, this article shares how to read content from an SD card and display it on the screen.

Here is the effect:

Need be mindful: ESP8266 has SPI pins: D5 (SCLK), D6 (MISO), D7 (MOSI), D8 (CS). SD card module 3.3V, level

The ESP8266 only supports reading the SD card in HSPI mode and shares the SPI bus with the screen.

Need be mindful: ESP8266 has SPI pins: D5 (SCLK), D6 (MISO), D7 (MOSI), D8 (CS). SD card module 3.3V, level

Need be mindful: ESP8266 has SPI pins: D5 (SCLK), D6 (MISO), D7 (MOSI), D8 (CS). SD card module 3.3V, level

ESP8266 GPIO

SD card module pins

Description

GPIO16

CS

Chip select, low level signal selects the SD card module for communication.

GPIO13

MOSI(DI)

Shares the HSPI MOSI with the e-paper display.

GPIO12

MISO(DO)

HSPI MISO

GPIO14

CLK(SCK)

Shares the HSPI SCK with the e-paper display.

3.3v

VCC

Power positive pole

GND

GND

Ground

E-paper display used pins:

# define READGUY_epd_mosi 13 //  MOSI pin # define READGUY_epd_sclk 14 //  SCLK pin # define READGUY_epd_cs   15 //  CS   pin # define READGUY_epd_dc    4 //  DC   pin # define READGUY_epd_rst  2 //   RST  pin # define READGUY_epd_busy 5 //   BUSY pin

Need be mindful: ESP8266 has SPI pins: D5 (SCLK), D6 (MISO), D7 (MOSI), D8 (CS). SD card module 3.3V, level

(Click to enlarge)

Two files are required: ctg_u8g2_wqy12.c and ctg_u8g2_wqy12.h, which are available in the example files of the readguy library. The code uses the 16-point font wqy16_gb2312a, which needs to be exported from the u8g2 library. For a detailed introduction to the u8g2_font_wqy16_chinese and gb2312 character set, and how to use them in Readguy (E-paper display), please refer to the relevant documentation.

readsd001.ino

# include // Basic Arduino functionality library. This statement is essential in the platformIO environment. # include "readguy.h" // Include the readguy_driver basic driver library. # include # include // For displaying Chinese text. # include "ctg_u8g2_wqy12.h" // Chinese font library. extern const uint8_t ctg_u8g2_font_wqy16_gb2312a[]; // Declare the Chinese font file. const lgfx::U8g2font cn_font16 (ctg_u8g2_font_wqy16_gb2312a) ; // Convert the U8G2 format Chinese font to LGFX format. // TF card configuration (hardware SPI, CS pin is GPIO2). # define TF_CS 16 ReadguyDriver myDisplay; // Create a readguy object for display driver. // IO0 button definition (ESP8266's Flash button). # define BUTTON_PIN 0 // GPIO0 pin. # define DEBOUNCE_DELAY 50 // Debounce delay (ms). unsigned long lastDebounceTime = 0 ; // Last debounce time. int lastButtonState = HIGH; // Last button state. void __printMemoryInfo( const char * tag) { Serial. printf ( "[%s] Available memory=%u, Maximum allocable memory=%u, Fragmentation=%.1f%%\n" , tag, ESP. getFreeHeap (), ESP. getMaxFreeBlockSize (), 100.0 * ( 1 - ESP. getMaxFreeBlockSize () / ( float )ESP. getFreeHeap ())); } bool isTxtFile ( const char *filename) { const char *dot = strrchr (filename, '.' ); if (!dot) return false ; return strcasecmp (dot + 1 , "txt" ) == 0 ; // Case-insensitive comparison. } void listRootFiles () { File root = SD. open ( "/" ); while (File entry = root. openNextFile ()) { String fileName = entry. name (); if (entry. isDirectory ()){ myDisplay. print ( "[DIR]" ); } else { if ( isTxtFile (entry. name ())){ myDisplay. print ( "[TXT]" ); } } Serial. println (fileName. c_str ()); myDisplay. println (fileName. c_str ()); myDisplay. display (); entry. close (); delay ( 50 ); } } void setup () { // put your setup code here, to run once: Serial. begin ( 115200 ); // Initialize the serial port. Serial. println ( "" ); __printMemoryInfo( "setup start" ); myDisplay. init ( 1 ); myDisplay. setEpdDriver ( true , true ); myDisplay. fillScreen (TFT_WHITE); // Fill the entire screen with color. myDisplay. setTextColor ( 0 , 1 ); // Set the display color. 0 represents black, 1 represents white. myDisplay. setFont (&cn_font16); myDisplay. setTextSize ( 1 ); myDisplay. setCursor ( 0 , 10 ); myDisplay. println ( "Initializing SD..." ); myDisplay. display (); if (SD. begin (TF_CS)) { myDisplay. println ( "TF Card OK!" ); myDisplay. display (); // Display the list of TF card files. listRootFiles (); } else { myDisplay. print ( "TF Card Failed!" ); myDisplay. display (); } delay ( 100 ); myDisplay. powerOffEPD (); // Initialize the IO0 button (internal pull-up, active low). pinMode (BUTTON_PIN, INPUT_PULLUP); __printMemoryInfo( "setup end" ); } void loop () { // IO0 button detection - clear the screen. int currentButtonState = digitalRead (BUTTON_PIN); // Detect if the button is pressed (falling edge trigger + debounce). if (currentButtonState == LOW && lastButtonState == HIGH) { if ( millis () - lastDebounceTime > DEBOUNCE_DELAY) { lastDebounceTime = millis (); Serial. println ( "IO0 pressed - clearing the screen" ); delay ( 50 ); myDisplay. fillScreen (TFT_WHITE); // Fill the entire screen with white. myDisplay. display (); // Refresh the display. // myDisplay.display(READGUY_SLOW);  // Refresh the display. Serial. println ( "Screen cleared" ); delay ( 200 ); // Simple debouncing. myDisplay. sleepEPD (); // Enter sleep mode, the screen will no longer refresh. // myDisplay.powerOffEPD(); } } lastButtonState = currentButtonState; delay ( 10 ); // Reduce CPU usage. }

After connecting the screen and the SD card module, only the GPIO0 (D3) pin is left, which is insufficient for the button requirements of the reader (Previous Page, Next Page, Confirm, Back). In the future, an ADC pin will be used with different resistors to implement multiple button inputs.

The original article link: https://mp.weixin.qq.com/s/PBBdgmq2N6WTPY_WZhF8Dw

暂无评论,快来发表第一条评论吧!No comments yet. Be the first!

📮 需求咨询📮 Inquiry