2026年2月17日 星期二

[OTTO GO] 動畫測試-LVGL + lv_spinner Loading 動畫



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
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#include <lvgl.h>

// TFT 腳位(保持原樣)
#define TFT_MOSI 19
#define TFT_SCLK 18
#define TFT_CS   5
#define TFT_DC   16
#define TFT_RST  23

Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);

// LVGL v9 需要的緩衝區(大小單位:bytes,不是 pixels)
#define DRAW_BUF_SIZE (240 * 40 * sizeof(lv_color_t))   // 約 19KB,1/10 螢幕
// 緩衝區(v9 用 uint8_t 陣列)
#define DRAW_BUF_SIZE (320 * 240 / 10 * sizeof(lv_color_t))  // 約 1/10 螢幕大小
static uint8_t draw_buf1[DRAW_BUF_SIZE];
static uint8_t draw_buf2[DRAW_BUF_SIZE];  // 雙緩衝更好(防閃爍),RAM 夠的話用

void my_disp_flush(lv_display_t * display, const lv_area_t * area, uint8_t * px_map) {
  uint32_t w = lv_area_get_width(area);
  uint32_t h = lv_area_get_height(area);

  tft.startWrite();
  tft.setAddrWindow(area->x1, area->y1, w, h);
  tft.writePixels((uint16_t *)px_map, w * h, true, false);
  tft.endWrite();

  lv_display_flush_ready(display);
}

void setup() {
  Serial.begin(115200);
  delay(100);
  Serial.println("Starting LVGL v9 + Adafruit ST7789");

  SPI.begin(TFT_SCLK, -1, TFT_MOSI, TFT_CS);
  tft.init(240, 320);           // 你的螢幕解析度(如果 240x240 就改)
  tft.setRotation(1);
  tft.fillScreen(ST77XX_BLACK);

  lv_init();

  // 建立 display(注意寬高依 rotation 調整:寬 320、高 240)
  lv_display_t * disp = lv_display_create(320, 240);

  // 設定緩衝區(PARTIAL 模式最常用,適合小緩衝)
  lv_display_set_buffers(disp, draw_buf1, draw_buf2, DRAW_BUF_SIZE, LV_DISPLAY_RENDER_MODE_PARTIAL);

  // 設定 flush callback
  lv_display_set_flush_cb(disp, my_disp_flush);

  // 建立 spinner 測試
  lv_obj_t * spinner = lv_spinner_create(lv_screen_active());
  lv_obj_set_size(spinner, 100, 100);
  lv_obj_center(spinner);
  lv_obj_set_style_arc_width(spinner, 12, 0);
  lv_obj_set_style_arc_color(spinner, lv_color_hex(0x2196F3), LV_PART_INDICATOR);

  Serial.println("Setup complete - spinner should rotate now!");
}

unsigned long last_tick = 0;
void loop() {
  unsigned long now = millis();
  if (now - last_tick >= 5) {
    lv_tick_inc(now - last_tick);   // 更新時間
    lv_timer_handler();
    last_tick = now;
  }
  // delay(1);  // 可選,小 delay 避免 CPU 100%
}

[OTTO GO] 動畫測試-利用LVGL技術,一次播4張

 

1.請參閱前一篇文章,轉換成4張圖檔。

2.並按照前一篇文章,把產生的4個.c檔案變成4個.h和4個.c的檔案,然後放到主檔案的目錄。

3.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
/*
  ESP32 + Adafruit_ST7789(240x320) 播放 tile000~tile003 的 RGB565 圖 (320x240)

  TFT pins:
    MOSI=19, SCLK=18, CS=5, DC=16, RST=23

  前提:
  - tile000.c ~ tile003.c 各自只保留像素陣列(uint8_t[]),不要包含 LVGL 的 lv_img_dsc_t
  - 若有 LV_ATTRIBUTE_* 巨集,需在 .c 最上方補上空巨集定義
*/

#include <Arduino.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>

#include "tile000.h"
#include "tile001.h"
#include "tile002.h"
#include "tile003.h"

// ===== TFT pins =====
#define TFT_MOSI 19
#define TFT_SCLK 18
#define TFT_CS    5
#define TFT_DC   16
#define TFT_RST  23

Adafruit_ST7789 tft(TFT_CS, TFT_DC, TFT_RST);

// 固定尺寸
static const uint16_t W = 320;
static const uint16_t H = 240;
static const uint32_t FRAME_BYTES = (uint32_t)W * (uint32_t)H * 2u;

// 一行 buffer:320*2 = 640 bytes
static uint16_t lineBuf[W];

// 你的 .c 匯出多數是 little-endian RGB565(低位元組在前)
static const bool SOURCE_IS_LITTLE_ENDIAN = true;

static inline uint16_t read565(const uint8_t *p) {
  if (SOURCE_IS_LITTLE_ENDIAN) return (uint16_t)p[0] | ((uint16_t)p[1] << 8);
  else return ((uint16_t)p[0] << 8) | (uint16_t)p[1];
}

void drawRGB565_320x240(const uint8_t *dataBytes) {
  tft.startWrite();
  tft.setAddrWindow(0, 0, W, H);

  const uint32_t rowBytes = (uint32_t)W * 2u;

  for (uint16_t y = 0; y < H; y++) {
    const uint8_t *row = dataBytes + (uint32_t)y * rowBytes;

    for (uint16_t x = 0; x < W; x++) {
      lineBuf[x] = read565(row + (uint32_t)x * 2u);
    }

    // 一次推一行
    tft.writePixels(lineBuf, W, true);
  }

  tft.endWrite();
}

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

  SPI.begin(TFT_SCLK, -1, TFT_MOSI, TFT_CS);

  tft.init(240, 320);
  tft.setRotation(1);          // 320x240
  tft.fillScreen(ST77XX_BLACK);

  Serial.printf("TFT size: %d x %d\n", tft.width(), tft.height());
  Serial.printf("Frame bytes expected: %u\n", (unsigned)FRAME_BYTES);

  tft.setTextColor(ST77XX_YELLOW);
  tft.setTextSize(2);
  tft.setCursor(8, 8);
  tft.print("tile player");
}

void loop() {
  // 依序播放 4 幀
  drawRGB565_320x240(tile000_map);
  delay(60);

  drawRGB565_320x240(tile001_map);
  delay(60);

  drawRGB565_320x240(tile002_map);
  delay(60);

  drawRGB565_320x240(tile003_map);
  delay(60);
}

[OTTO GO] 圖片顯示,利用LVGL技術



1.圖檔轉換工具:先把圖檔轉換成LVGL格式

https://lvgl.io/tools/imageconverter

2.選擇RGB565產生一個.c的圖檔,例子是animation2.c

 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
#ifdef __has_include
    #if __has_include("lvgl.h")
        #ifndef LV_LVGL_H_INCLUDE_SIMPLE
            #define LV_LVGL_H_INCLUDE_SIMPLE
        #endif
    #endif
#endif

#if defined(LV_LVGL_H_INCLUDE_SIMPLE)
    #include "lvgl.h"
#else
    #include "lvgl/lvgl.h"
#endif


#ifndef LV_ATTRIBUTE_MEM_ALIGN
#define LV_ATTRIBUTE_MEM_ALIGN
#endif

#ifndef LV_ATTRIBUTE_IMAGE_TILE000
#define LV_ATTRIBUTE_IMAGE_TILE000
#endif

const LV_ATTRIBUTE_MEM_ALIGN LV_ATTRIBUTE_LARGE_CONST LV_ATTRIBUTE_IMAGE_TILE000 uint8_t tile000_map[] = {
/*圖檔資料太, 省略*/
};

const lv_image_dsc_t tile000 = {
  .header.cf = LV_COLOR_FORMAT_RGB565,
  .header.magic = LV_IMAGE_HEADER_MAGIC,
  .header.w = 320,
  .header.h = 240,
  .data_size = 76800 * 2,
  .data = tile000_map,
};

3.把檔案切割成animation2.h和animation2.c
animation2.h:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#pragma once
#include <Arduino.h>

#ifdef __cplusplus
extern "C" {
#endif
extern const uint8_t animation2_map[];
#ifdef __cplusplus
}
#endif

static const uint16_t ANIM2_W = 320;
static const uint16_t ANIM2_H = 240;
static const uint32_t ANIM2_SIZE = (uint32_t)ANIM2_W * (uint32_t)ANIM2_H * 2u;

antimation2.c:

1
2
3
4
#include <stdint.h>
const uint8_t animation2_map[] = {
/*圖檔資料太, 省略*/
}

4. 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
/*
  ESP32 + Adafruit_ST7789(240x320) 顯示 animation2.c 的 RGB565 圖 (320x240)

  TFT pins (你前面驗證可用那組):
    MOSI=19, SCLK=18, CS=5, DC=16, RST=23

  重要:
  - tft.setRotation(1) 讓螢幕座標變成 320x240,剛好完整顯示
  - animation2.c 需移除底部 lv_image_dsc_t descriptor(只留 animation2_map 陣列)
*/

#include <Arduino.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>

#include "animation2.h"   // extern animation2_map + W/H

// ===== TFT pins =====
#define TFT_MOSI 19
#define TFT_SCLK 18
#define TFT_CS    5
#define TFT_DC   16
#define TFT_RST  23

Adafruit_ST7789 tft(TFT_CS, TFT_DC, TFT_RST);

// 320 像素一行的暫存(RAM 640 bytes)
static uint16_t lineBuf[ANIM2_W];

// 你的 animation2.c 是 RGB565 的 bytes
// 多數 LVGL 匯出 RGB565 會是 little-endian:低位元組在前
// 若你顏色不對(例如整體偏紫/偏綠),把這個改成 0 試試
static const bool SOURCE_IS_LITTLE_ENDIAN = true;

static inline uint16_t read565(const uint8_t *p) {
  if (SOURCE_IS_LITTLE_ENDIAN) {
    return (uint16_t)p[0] | ((uint16_t)p[1] << 8);
  } else {
    return ((uint16_t)p[0] << 8) | (uint16_t)p[1];
  }
}

void drawRGB565_320x240(const uint8_t *dataBytes) {
  // dataBytes 長度必須 = 320*240*2 = 153600
  tft.startWrite();
  tft.setAddrWindow(0, 0, ANIM2_W, ANIM2_H);

  const uint32_t rowBytes = (uint32_t)ANIM2_W * 2u;

  for (uint16_t y = 0; y < ANIM2_H; y++) {
    const uint8_t *row = dataBytes + (uint32_t)y * rowBytes;

    for (uint16_t x = 0; x < ANIM2_W; x++) {
      lineBuf[x] = read565(row + (uint32_t)x * 2u);
    }

    // 一次推一行
    // Adafruit_ST7789(Adafruit_SPITFT) 提供 writePixels
    tft.writePixels(lineBuf, ANIM2_W, true);
  }

  tft.endWrite();
}

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

  // 用你穩定的 SPI begin(Adafruit_ST7789 會使用預設 SPI instance)
  SPI.begin(TFT_SCLK, -1, TFT_MOSI, TFT_CS);

  tft.init(240, 320);
  tft.setRotation(1);          // 變成 320x240
  tft.fillScreen(ST77XX_BLACK);

  Serial.printf("TFT size: %d x %d\n", tft.width(), tft.height());
  Serial.printf("Expect:   %u bytes\n", (unsigned)ANIM2_SIZE);

  // 顯示一次
  drawRGB565_320x240(animation2_map);

  // 顯示文字提示
  tft.setTextColor(ST77XX_YELLOW);
  tft.setTextSize(2);
  tft.setCursor(8, 8);
  tft.print("animation2.c RGB565");
}

void loop() {
  // 你這份 animation2.c 只有單張圖,所以 loop 不會動
  delay(1000);
}

2026年2月16日 星期一

[OTTO GO]動畫測試-穩定循環播放 + 不會顏色亂 + 不會卡在 setup return

 

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
// ESP32 + TFT_eSPI + microSD (SdFat)
// ST7789 320x240 : RAW565 player (FAST SD read)
// Profile: SD read vs TFT push

#include <TFT_eSPI.h>
#include <SPI.h>
#include <SdFat.h>

TFT_eSPI tft;

// ===== SD pins =====
#define SD_CS   33
#define SD_MOSI 26
#define SD_MISO 36
#define SD_SCLK 25
SPIClass sdSPI(HSPI);

// SdFat objects
SdFat sd;
FsFile f;

// ===== Display size =====
static const int W = 320;
static const int H = 240;

// ===== Playback =====
int totalFrames = 0;           // 由掃描自動取得
int frameDelayMs = 10;         // 你可調整
const char *framePrefix = "tile";
const int digits = 3;

// ===== Block push settings =====
static const int BLOCK_H = 24; // 建議 16~24 更穩
static uint16_t blockBuf[W * BLOCK_H];

static bool drawRaw565_Profile_SdFat(const char *filename) {
  if (!f.open(filename, O_RDONLY)) {
    Serial.println("  開檔失敗");
    return false;
  }

  const uint32_t expected = (uint32_t)W * (uint32_t)H * 2u;
  if (f.fileSize() != expected) {
    Serial.printf("  檔案大小不符:%lu (應為 %lu)\n",
                  (unsigned long)f.fileSize(), (unsigned long)expected);
    f.close();
    return false;
  }

  uint32_t tReadUs = 0, tPushUs = 0;

  tft.startWrite();

  for (int y = 0; y < H; y += BLOCK_H) {
    int thisH = (y + BLOCK_H <= H) ? BLOCK_H : (H - y);
    int pxCount = W * thisH;
    int byteCount = pxCount * 2;

    uint32_t a = micros();
    int got = f.read((uint8_t*)blockBuf, byteCount);
    uint32_t b = micros();
    tReadUs += (b - a);

    if (got != byteCount) {
      Serial.println("  讀取不足");
      tft.endWrite();
      f.close();
      return false;
    }

    a = micros();
    tft.setAddrWindow(0, y, W, thisH);
    tft.pushPixels(blockBuf, pxCount);
    b = micros();
    tPushUs += (b - a);
  }

  tft.endWrite();
  f.close();

//  Serial.printf("  SD讀取: %lu ms, TFT推畫: %lu ms\n",
//                (unsigned long)(tReadUs / 1000),
//                (unsigned long)(tPushUs / 1000));
  return true;
}

// 連續測試 /tile000.raw, /tile001.raw ... 直到缺檔
static int countFrames() {
  int n = 0;
  while (true) {
    char filename[32];
    snprintf(filename, sizeof(filename), "/%s%0*d.raw", framePrefix, digits, n);

    FsFile tf = sd.open(filename, O_RDONLY);
    if (!tf) break;
    tf.close();

    n++;
    if (n > 999) break; // digits=3 的保護
  }
  return n;
}

void setup() {
  Serial.begin(115200);
  delay(300);
  Serial.println("\nSdFat RAW565 開始(修正版)");

  tft.init();
  tft.setRotation(3);

  // ★ 若你的 raw 是 big-endian(用 Python struct.pack('>H') 產生),要開 true
  tft.setSwapBytes(true);

  sdSPI.begin(SD_SCLK, SD_MISO, SD_MOSI, SD_CS);

  bool ok = false;

  // 先試 40MHz
  {
    SdSpiConfig cfg40(SD_CS, DEDICATED_SPI, SD_SCK_MHZ(40), &sdSPI);
    if (sd.begin(cfg40)) {
      Serial.println("SdFat 初始化成功!(40MHz)");
      ok = true;
    }
  }

  // 失敗再試 20MHz
  if (!ok) {
    Serial.println("SdFat 40MHz 失敗,改用 20MHz...");
    SdSpiConfig cfg20(SD_CS, DEDICATED_SPI, SD_SCK_MHZ(20), &sdSPI);
    if (sd.begin(cfg20)) {
      Serial.println("SdFat 初始化成功!(20MHz)");
      ok = true;
    }
  }

  if (!ok) {
    Serial.println("SdFat 初始化失敗!");
    tft.fillScreen(TFT_BLACK);
    tft.setTextColor(TFT_RED, TFT_BLACK);
    tft.setTextDatum(MC_DATUM);
    tft.drawString("SD INIT FAIL", tft.width() / 2, tft.height() / 2);
    while (1) delay(1000);
  }

  Serial.printf("TFT: %dx%d\n", tft.width(), tft.height());

  totalFrames = countFrames();
  Serial.printf("掃描到 %d 幀\n", totalFrames);

  if (totalFrames <= 0) {
    tft.fillScreen(TFT_BLACK);
    tft.setTextColor(TFT_YELLOW, TFT_BLACK);
    tft.setTextDatum(MC_DATUM);
    tft.drawString("NO FRAMES", tft.width() / 2, tft.height() / 2);
  } else {
    tft.fillScreen(TFT_BLACK);
    tft.setTextColor(TFT_GREEN, TFT_BLACK);
    tft.setTextDatum(MC_DATUM);
    tft.drawString("PLAYING...", tft.width() / 2, tft.height() / 2);
    delay(200);
  }
}

void loop() {
  if (totalFrames <= 0) {
    delay(1000);
    return;
  }

  for (int i = 0; i < totalFrames; i++) {
    char filename[32];
    snprintf(filename, sizeof(filename), "/%s%0*d.raw", framePrefix, digits, i);

//    Serial.printf("\n幀 %d/%d → %s\n", i + 1, totalFrames, filename);
    unsigned long start = millis();

    bool ok = drawRaw565_Profile_SdFat(filename);
    if (!ok) {
      tft.fillScreen(TFT_BLACK);
      tft.setTextColor(TFT_RED, TFT_BLACK);
      tft.setTextDatum(MC_DATUM);
      tft.drawString("RAW LOAD FAIL", W / 2, H / 2);
      delay(800);
      break;
    }

    unsigned long end = millis();
//    Serial.printf("  完成,花費 %lu ms\n", end - start);

    delay(frameDelayMs);
  }

  Serial.println("一輪結束...");
  delay(3000);
}

2026年2月15日 星期日

[OTTO GO] 動畫測試,使用RAW和SdFat 版



Python:BMP轉檔成RAW

 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
from PIL import Image, ImageOps
import struct
import glob
import os

W, H = 320, 240

def rgb_to_565(r, g, b):
    return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3)

def convert_one(bmp_path):
    img = Image.open(bmp_path).convert("RGB")

    # 轉成剛好 320x240:置中裁切/縮放(避免尺寸不符)
    img = ImageOps.fit(img, (W, H), method=Image.Resampling.LANCZOS, centering=(0.5, 0.5))

    raw_path = os.path.splitext(bmp_path)[0] + ".raw"
    pix = img.load()

    with open(raw_path, "wb") as f:
        # 逐像素寫入 RGB565(Little-endian:低位元組在前)
        for y in range(H):
            for x in range(W):
                r, g, b = pix[x, y]
                v = rgb_to_565(r, g, b)
                f.write(struct.pack("<H", v))

    print(f"OK: {bmp_path} -> {raw_path}")

def main():
    # 轉換 tile*.bmp(例如 tile000.bmp)
    bmps = sorted(glob.glob("tile*.bmp"))
    if not bmps:
        print("找不到 tile*.bmp,請確認檔名例如 tile000.bmp")
        return
    for p in bmps:
        convert_one(p)

if __name__ == "__main__":
    main()

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
// ESP32 + TFT_eSPI + microSD (SdFat)
// ST7789 320x240 : RAW565 player (FAST SD read)
// Profile: SD read vs TFT push
#include <TFT_eSPI.h>
#include <SPI.h>
#include <SdFat.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);

// SdFat objects
SdFat sd;
FsFile f;

// ===== Display size =====
static const int W = 320;
static const int H = 240;

// ===== Playback =====
const int totalFrames = 4;
int frameDelay = 10;
const char *framePrefix = "tile";
const int digits = 3;

// ===== Block push settings =====
static const int BLOCK_H = 40;
static uint16_t blockBuf[W * BLOCK_H];

static bool drawRaw565_Profile_SdFat(const char *filename) {
  if (!f.open(filename, O_RDONLY)) {
    Serial.println("  開檔失敗");
    return false;
  }

  const uint32_t expected = (uint32_t)W * (uint32_t)H * 2u;
  if (f.fileSize() != expected) {
    Serial.printf("  檔案大小不符:%lu (應為 %lu)\n",
                  (unsigned long)f.fileSize(), (unsigned long)expected);
    f.close();
    return false;
  }

  uint32_t tReadUs = 0, tPushUs = 0;

  tft.startWrite();

  for (int y = 0; y < H; y += BLOCK_H) {
    int thisH = (y + BLOCK_H <= H) ? BLOCK_H : (H - y);
    int pxCount = W * thisH;
    int byteCount = pxCount * 2;

    uint32_t a = micros();
    int got = f.read((uint8_t*)blockBuf, byteCount);
    uint32_t b = micros();
    tReadUs += (b - a);

    if (got != byteCount) {
      Serial.println("  讀取不足");
      tft.endWrite();
      f.close();
      return false;
    }

    a = micros();
    tft.setAddrWindow(0, y, W, thisH);
    tft.pushPixels(blockBuf, pxCount);
    b = micros();
    tPushUs += (b - a);
  }

  tft.endWrite();
  f.close();

  Serial.printf("  SD讀取: %lu ms, TFT推畫: %lu ms\n",
                (unsigned long)(tReadUs / 1000),
                (unsigned long)(tPushUs / 1000));
  return true;
}

void setup() {
  Serial.begin(115200);
  delay(300);
  Serial.println("\nSdFat RAW565 開始(cfg 修正版)");

  tft.init();
  tft.setRotation(3);
  tft.setSwapBytes(false);

  sdSPI.begin(SD_SCLK, SD_MISO, SD_MOSI, SD_CS);

  // 先試 40MHz
  {
    SdSpiConfig cfg40(SD_CS, DEDICATED_SPI, SD_SCK_MHZ(40), &sdSPI);
    if (sd.begin(cfg40)) {
      Serial.println("SdFat 初始化成功!(40MHz)");
      Serial.printf("TFT: %dx%d\n", tft.width(), tft.height());
      return;
    }
  }

  // 失敗再試 20MHz
  Serial.println("SdFat 40MHz 失敗,改用 20MHz...");
  {
    SdSpiConfig cfg20(SD_CS, DEDICATED_SPI, SD_SCK_MHZ(20), &sdSPI);
    if (sd.begin(cfg20)) {
      Serial.println("SdFat 初始化成功!(20MHz)");
      Serial.printf("TFT: %dx%d\n", tft.width(), tft.height());
      return;
    }
  }

  Serial.println("SdFat 初始化失敗!");
  while (1) delay(1000);
}

void loop() {
  for (int i = 0; i < totalFrames; i++) {
    char filename[32];
    snprintf(filename, sizeof(filename), "/%s%0*d.raw", framePrefix, digits, i);

    Serial.printf("\n幀 %d/%d → %s\n", i + 1, totalFrames, filename);
    unsigned long start = millis();

    bool ok = drawRaw565_Profile_SdFat(filename);
    if (!ok) {
      tft.fillScreen(TFT_BLACK);
      tft.setTextColor(TFT_RED, TFT_BLACK);
      tft.setTextDatum(MC_DATUM);
      tft.drawString("RAW LOAD FAIL", W / 2, H / 2);
    }

    unsigned long end = millis();
    Serial.printf("  完成,花費 %lu ms\n", end - start);

    delay(frameDelay);
  }

  Serial.println("一輪結束...");
  delay(300);
}