1.取得gif檔,然後用工具轉成不同的bmp圖檔,記得大小要跟TFT一樣。
2. 使用小畫家轉成深度24bit。
arduino:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | // ESP32 + TFT_eSPI + microSD // ST7789 320x240 : FAST BMP player (no sprite, sequential SD read, per-line push) // Supports: 24-bit uncompressed BMP // Files: /tile000.bmp ... /tile003.bmp #include <TFT_eSPI.h> #include <SD.h> #include <SPI.h> TFT_eSPI tft = TFT_eSPI(); // ===== SD pins ===== #define SD_CS 33 #define SD_MOSI 26 #define SD_MISO 36 #define SD_SCLK 25 SPIClass sdSPI(HSPI); // ===== Playback ===== const int totalFrames = 4; int frameDelay = 1; // 你可再降 const char *framePrefix = "tile"; const int digits = 3; // ===== Line buffer ===== #define LINE_W 320 static uint8_t sdbuf[LINE_W * 3]; // BGR888 bytes static uint16_t lcdbuf[LINE_W]; // RGB565 pixels // ---------- BMP helpers ---------- static uint16_t read16(File &f) { uint16_t r; ((uint8_t*)&r)[0] = f.read(); ((uint8_t*)&r)[1] = f.read(); return r; } static uint32_t read32(File &f) { uint32_t r; ((uint8_t*)&r)[0] = f.read(); ((uint8_t*)&r)[1] = f.read(); ((uint8_t*)&r)[2] = f.read(); ((uint8_t*)&r)[3] = f.read(); return r; } // Fast draw: sequential read rows, push each row to TFT static bool drawBmpFast(const char *filename) { File bmp = SD.open(filename); if (!bmp) { Serial.println(" 開檔失敗"); return false; } if (read16(bmp) != 0x4D42) { Serial.println(" 不是 BMP"); bmp.close(); return false; } (void)read32(bmp); // fileSize (void)read32(bmp); // reserved uint32_t dataOffset = read32(bmp); uint32_t headerSize = read32(bmp); if (headerSize < 40) { Serial.println(" headerSize 不支援"); bmp.close(); return false; } int32_t w = (int32_t)read32(bmp); int32_t h0 = (int32_t)read32(bmp); (void)read16(bmp); // planes uint16_t depth = read16(bmp); uint32_t compression = read32(bmp); if (depth != 24 || compression != 0) { Serial.println(" 只支援 24-bit 無壓縮 BMP"); bmp.close(); return false; } bool topDown = false; int32_t h = h0; if (h0 < 0) { topDown = true; h = -h0; } // 這版針對 320x240 全螢幕最快:若不是滿版也能置中,但會裁切 const int screenW = tft.width(); // 320 const int screenH = tft.height(); // 240 int drawW = w; int drawH = h; int offsetX = (screenW - drawW) / 2; int offsetY = (screenH - drawH) / 2; int srcX0 = 0, srcY0 = 0; if (offsetX < 0) { srcX0 = -offsetX; offsetX = 0; drawW = screenW; } if (offsetY < 0) { srcY0 = -offsetY; offsetY = 0; drawH = screenH; } if (offsetX + drawW > screenW) drawW = screenW - offsetX; if (offsetY + drawH > screenH) drawH = screenH - offsetY; // 為了速度與簡化:要求 drawW <= LINE_W if (drawW > LINE_W) { Serial.println(" 圖太寬,請把 LINE_W 調大或改用分段"); bmp.close(); return false; } // BMP rows padded to 4-byte boundary uint32_t rowSize = (uint32_t)((w * 3 + 3) & ~3); uint32_t padBytes = rowSize - (uint32_t)w * 3; // 重要:避免每行 seek,改成「一次 seek 到資料起點後連續讀」 // 但若要裁切 X(srcX0),就必須每行跳過 srcX0*3 bytes // 我們仍維持連續讀:每行讀完整一行(w*3),再取中間需要的區段 if (!bmp.seek(dataOffset)) { Serial.println(" seek(dataOffset) 失敗"); bmp.close(); return false; } tft.startWrite(); // 若你是滿版 320x240,整張都覆蓋,就不需要清屏 // 若非滿版想避免殘影,可自行在外面清一次背景 for (int fileRow = 0; fileRow < h; fileRow++) { // 讀一整行原始像素(w * 3) int want = (int)w * 3; int got = bmp.read(sdbuf, want); if (got != want) { Serial.println(" 讀取不足"); tft.endWrite(); bmp.close(); return false; } // 跳過 padding if (padBytes) bmp.seek(bmp.position() + padBytes); // 這行在螢幕上的 Y(BMP 若 bottom-up,需要倒過來畫) int srcY = fileRow; // 0..h-1 in file order int dstY_in_img = topDown ? srcY : (h - 1 - srcY); // 轉成由上到下的影像座標 // 若有 Y 裁切(srcY0..srcY0+drawH-1),不在範圍就略過(但仍已讀掉) if (dstY_in_img < srcY0 || dstY_in_img >= (srcY0 + drawH)) continue; int yOnScreen = offsetY + (dstY_in_img - srcY0); // 從 sdbuf 中取出需要的 X 範圍(srcX0..srcX0+drawW-1) // sdbuf 是 B,G,R uint8_t *p = sdbuf + (srcX0 * 3); for (int x = 0; x < drawW; x++) { uint8_t b = p[x * 3 + 0]; uint8_t g = p[x * 3 + 1]; uint8_t r = p[x * 3 + 2]; lcdbuf[x] = tft.color565(r, g, b); } // 推到 TFT:一次設定視窗 + 推一行 tft.setAddrWindow(offsetX, yOnScreen, drawW, 1); // 依 TFT_eSPI 版本不同,pushPixels / pushColors 可能都可用 // pushPixels 在 ESP32/ST7789 常見可用 tft.pushPixels(lcdbuf, drawW); } tft.endWrite(); bmp.close(); return true; } void setup() { Serial.begin(115200); delay(300); Serial.println("\nBMP FAST(逐行直推 TFT)開始"); tft.init(); tft.setRotation(3); // 建議:顏色更穩(不同版本行為略有差) tft.setSwapBytes(true); // SD init sdSPI.begin(SD_SCLK, SD_MISO, SD_MOSI, SD_CS); if (!SD.begin(SD_CS, sdSPI)) { Serial.println("SD 初始化失敗!"); while (1) delay(1000); } Serial.println("SD 初始化成功!"); } void loop() { for (int i = 0; i < totalFrames; i++) { char filename[32]; snprintf(filename, sizeof(filename), "/%s%0*d.bmp", framePrefix, digits, i); Serial.printf("\n幀 %d/%d → %s\n", i + 1, totalFrames, filename); unsigned long start = millis(); // 如果你的 BMP 是滿版 320x240,完全覆蓋,就不要清屏(省非常多時間) // tft.fillScreen(TFT_BLACK); bool ok = drawBmpFast(filename); if (!ok) { tft.fillScreen(TFT_BLACK); tft.setTextColor(TFT_RED, TFT_BLACK); tft.setTextDatum(MC_DATUM); tft.drawString("BMP LOAD FAIL", tft.width() / 2, tft.height() / 2); } unsigned long end = millis(); Serial.printf(" 完成,花費 %lu ms\n", end - start); delay(frameDelay); } Serial.println("一輪結束,稍等後重播..."); delay(500); } |
User_Setup.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #define USER_SETUP_INFO "My_ST7789_240x320" // 驅動器:ST7789 #define ST7789_DRIVER // 螢幕尺寸(你的實際尺寸) #define TFT_WIDTH 240 #define TFT_HEIGHT 320 // 腳位(你的板子) #define TFT_MOSI 19 #define TFT_SCLK 18 #define TFT_CS 5 #define TFT_DC 16 #define TFT_RST 23 // 如果 RST 接 3.3V 或不穩,改成 -1 試試 // 顏色順序(這是關鍵!先試 TFT_BGR_ORDER) #define TFT_RGB_ORDER TFT_BGR // 先試這個(BGR 順序常解決彩虹噪點) //#define TFT_RGB_ORDER TFT_RGB // 如果 BGR 不行,再換這個 // SPI 頻率(先降速避免噪點) //#define SPI_FREQUENCY 27000000 // 27MHz 先試;若還噪點改成 20000000 #define SPI_FREQUENCY 40000000 // 等穩定後再試 40MHz #define SPI_READ_FREQUENCY 20000000 #define SPI_TOUCH_FREQUENCY 2500000 // 字型載入(保持你原來的) #define LOAD_GLCD #define LOAD_FONT2 #define LOAD_FONT4 #define LOAD_FONT6 #define LOAD_FONT7 #define LOAD_FONT8 #define LOAD_GFXFF #define SMOOTH_FONT // 可選:反轉顏色(如果黑白顛倒再開) //#define TFT_INVERSION_ON //#define TFT_INVERSION_OFF #define USE_DMA |
沒有留言:
張貼留言