ESP32学习,驱动LED点阵屏,SD卡播放GIF动图

文章讲述了如何在ESP32开发板上通过电路连接SD卡,使用特定库实现在LED点阵屏上播放GIF动图,同时提醒注意内存管理以防止溢出。作者提供了代码示例和关键函数,包括电路连接、SD卡初始化、GIF文件操作等。
摘要由CSDN通过智能技术生成

ESP32学习,驱动LED点阵屏,SD卡播放GIF动图

前面准换代码显示GIF动画,这次搬运代码实现gif文件存放sd卡,播放。
在这里插入图片描述
在这里插入图片描述


`


先做个电路,方便读取SD卡

`提示:焊板子的时候。故意多加了一些引脚和按钮,方便后续增加功能
在这里插入图片描述
在这里插入图片描述
接口连接线需要改动一下,看下图。
在这里插入图片描述

例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


直接上代码吧

注意:文件不要太多太大,要不然循环播放会引起内存溢出导致ESP32一直重启

main.cpp(示例):

#include <Arduino.h>

/*********************************************************************
 * AnimatedGif LED Matrix Panel example where the GIFs are 
 * stored on a SD card connected to the ESP32 using the
 * standard GPIO pins used for SD card acces via. SPI.
 *
 * Put the gifs into a directory called 'gifs' (case sensitive) on
 * a FAT32 formatted SDcard.
 ********************************************************************/
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include <ESP32-HUB75-MatrixPanel-I2S-DMA.h> 
#include <AnimatedGIF.h>

/********************************************************************
 * Pin mapping below is for LOLIN D32 (ESP 32)
 *
 * Default pin mapping used by this library is NOT compatable with the use of the 
 * ESP32-Arduino 'SD' card library (there is overlap). As such, some of the pins 
 * used for the HUB75 panel need to be shifted.
 * 
 * 'SD' card library requires GPIO 23, 18 and 19 
 *  https://github.com/espressif/arduino-esp32/tree/master/libraries/SD
 * 
 */

/*
 * Connect the SD card to the following pins:
 *
 * SD Card | ESP32
 *    D2       -
 *    D3       SS
 *    CMD      MOSI
 *    VSS      GND
 *    VDD      3.3V
 *    CLK      SCK
 *    VSS      GND
 *    D0       MISO
 *    D1       -
 */

/**** SD Card GPIO mappings ****/
#define SS_PIN          5
//#define MOSI_PIN        23
//#define MISO_PIN        19
//#define CLK_PIN         18


/**** HUB75 GPIO mapping ****/
// GPIO 34+ are on the ESP32 are input only!!
// https://randomnerdtutorials.com/esp32-pinout-reference-gpios/

#define A_PIN   33  // remap esp32 library default from 23 to 33
#define B_PIN   32  // remap esp32 library default from 19 to 32
#define C_PIN   22   // remap esp32 library defaultfrom 5 to 22

//#define R1_PIN  25  // library default for the esp32, unchanged
//#define G1_PIN  26  // library default for the esp32, unchanged
//#define B1_PIN  27  // library default for the esp32, unchanged
//#define R2_PIN  14  // library default for the esp32, unchanged
//#define G2_PIN  12  // library default for the esp32, unchanged
//#define B2_PIN  13  // library default for the esp32, unchanged
//#define D_PIN   17  // library default for the esp32, unchanged
#define E_PIN   21 //-1 // IMPORTANT: Change to a valid pin if using a 64x64px panel.
            
//#define LAT_PIN 4  // library default for the esp32, unchanged
//#define OE_PIN  15  // library default for the esp32, unchanged
//#define CLK_PIN 16  // library default for the esp32, unchanged

/***************************************************************
 * HUB 75 LED DMA Matrix Panel Configuration
 **************************************************************/
#define PANEL_RES_X 64      // Number of pixels wide of each INDIVIDUAL panel module. 
#define PANEL_RES_Y 64     // Number of pixels tall of each INDIVIDUAL panel module.
#define PANEL_CHAIN 2      // Total number of panels chained one to another
//我用的是128*64 的板子
/**************************************************************/

AnimatedGIF gif;
MatrixPanel_I2S_DMA *dma_display = nullptr;

static int totalFiles = 0; // GIF files count

static File FSGifFile; // temp gif file holder
static File GifRootFolder; // directory listing

std::vector<std::string> GifFiles; // GIF files path

const int maxGifDuration    = 30000; // ms, max GIF duration

#include "gif_functions.hpp"
#include "sdcard_functions.hpp"
 

/**************************************************************/
void draw_test_patterns();
int gifPlay( const char* gifPath )
{
    // 0=infinite

  if( ! gif.open( gifPath, GIFOpenFile, GIFCloseFile, GIFReadFile, GIFSeekFile, GIFDraw ) ) {
   
    log_n("Could not open gif %s", gifPath );
  }

  Serial.print("Playing: "); Serial.println(gifPath);

  int frameDelay = 0; // store delay for the last frame
  int then = 0; // store overall delay

  while (gif.playFrame(true, &frameDelay)) {
   

    then += frameDelay;
    if( then > maxGifDuration ) {
    // avoid being trapped in infinite GIF's
      //log_w("Broke the GIF loop, max duration exceeded");
      break;
    }
  }

  gif.close();

  return then;
}


void setup()
{
   
    Serial.begin(115200);

    // **************************** Setup SD Card access via SPI ****************************
    if(!SD.begin(SS_PIN)){
   
        //  bool begin(uint8_t ssPin=SS, SPIClass &spi=SPI, uint32_t frequency=4000000, const char * mountpoint="/sd", uint8_t max_files=5, bool format_if_empty=false);      
        Serial.println("Card Mount Failed");
        return;
    }
    uint8_t cardType = SD.cardType();

    if(cardType == CARD_NONE){
   
        Serial.println("No SD card attached");
        return;
    }

    Serial.print("SD Card Type: "
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值