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);
}

沒有留言:

張貼留言