赞
踩
FireBeetle 2 ESP32-E 开发板
1.54" 240x240 IPS 广视角TFT显示屏
- //加载库
- #include "Arduino.h"
- #include "FS.h"
- #include "SD.h"
- #include "SPI.h"
- #include "DFRobot_GDL.h"
-
- //定义显示屏针脚
- #define TFT_DC D2
- #define TFT_CS D6
- #define TFT_RST D3
- #define TFT_BL D13
-
- //定义SD针脚
- #define TFT_SD D7
-
- //配置显示屏
- DFRobot_ST7789_240x240_HW_SPI screen(/*dc=*/TFT_DC,/*cs=*/TFT_CS,/*rst=*/TFT_RST);
-
- void setup()
- {
- //开启串口
- Serial.begin(115200);
-
- //挂载SD卡
- MountSD();
-
- //路径
- String path = "/hello.txt";
- //内容
- String str = "hello world !";
- //写入(覆盖)
- writeTxt(SD,path,str);
-
- //读取
- String txt = readTxt(SD,path);
- Serial.println( txt );
-
- //开启显示屏
- screen.begin();
- //屏幕背景颜色
- screen.fillScreen(COLOR_RGB565_BLACK);
- //显示文本
- printText(10,100,txt);
-
- }
-
- void loop()
- {
-
- }
-
-
- //挂载SD卡
- void MountSD()
- {
- //开启SD卡
- if(SD.begin())
- {
- //打印成功
- Serial.println("Card Mount succeed");
- }
- else
- {
- //打印失败
- Serial.println("Card Mount Failed");
- //挂载失败,延时重启
- delay(1000);
- //重启
- return;
- }
-
- //获取SD类型
- uint8_t cardType = SD.cardType();
- switch(cardType)
- {
- case 1:
- Serial.println("CARD_MMC");
- break;
- case 2:
- Serial.println("CARD_SD");
- break;
- case 3:
- Serial.println("CARD_SDHC");
- break;
- case 4:
- Serial.println("CARD_UNKNOWN");
- break;
- default:
- Serial.println("CARD_NONE");
- Serial.println("Please insert SD card");
- delay(1000);
- return;
- }
- }
-
- //写入(FILE_WRITE)
- void writeTxt(fs::FS &fs,String path,String str)
- {
- //打开文件
- File myFile = fs.open(path, FILE_WRITE); //FILE_WRITE 覆写
-
- //如果文件打开,则进行写入
- if (myFile)
- {
- //打印开始
- Serial.println("Start write:" + path);
- //写入
- myFile.println(str);
- // 关闭文件
- myFile.close();
- //打印完成
- Serial.println("Complete write:" + path);
- }
- //如果文件没有打开,则打印错误
- else
- {
- Serial.println("error:Write failure");
- }
- }
-
- //写入(FILE_APPEND)
- void writeTxt_APPEND(fs::FS &fs,String path,String str)
- {
- //打开文件
- File myFile = fs.open(path, FILE_APPEND); //FILE_APPEND 续写
-
- //如果文件打开,则进行写入
- if (myFile)
- {
- //打印开始
- Serial.println("Start write_APPEND:" + path);
- //写入
- myFile.println(str);
- // 关闭文件
- myFile.close();
- //打印完成
- Serial.println("Complete write_APPEND:" + path);
- }
- //如果文件没有打开,则打印错误
- else
- {
- Serial.println("error:Write failure");
- }
- }
-
- //读取
- String readTxt(fs::FS &fs,String path)
- {
- //创建返回值
- String txt;
- //打开文件
- File myFile = fs.open(path,FILE_READ); //FILE_READ 只读
- //如果文件打开,则进行读取
- if (myFile)
- {
- //打印开始
- Serial.println("Start Read:" + path);
- //读取
- if( myFile.available() )
- {
- txt = myFile.readString();
- }
- //关闭文件打开状态
- myFile.close();
- //打印完成
- Serial.println("Complete read:" + path);
- }
- //如果文件没有打开,则打印错误
- else
- {
- Serial.println("error:Read failure");
- }
- //返回
- return txt;
- }
-
- //显示文本
- void printText(int x,int y,String str)
- {
- screen.setFont(&FreeMono12pt7b); //字体
- screen.setTextSize(1); //文字大小,范围为1~4
- screen.setTextColor(COLOR_RGB565_GREEN); //文字颜色
- screen.setCursor(x,y); //文字起点
- screen.print(str); //输出文字内容
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。