全局定义的 RingBuf 在 loop() 函数内部不起作用

Globally defined RingBuf does not work inside the loop() function

我只能在 setup() 函数中使用 SdFat 库中的 RingBuf,但我不明白为什么。 RingBufFile32 是否可以定义为全局并在设置期间仅配置一次?我正在开发 Teensy 4.1。

例如,这段代码有效

#include "sdios.h"
#include "SdFat.h"
#include "RingBuf.h"

#define SD_CONFIG  SdioConfig(FIFO_SDIO)        // Use Teensy SDIO

File32  stateFile;
RingBuf<File32, 5> outRingBuff;

// SETUP function
void setup() {
  SdFat32 sdCard;
  bool retValBool;
  
  Serial.begin(9600);                 // Teensy serial is always at full USB speed and buffered... the baud rate here is required but ignored

  Serial.println("Initialization SD card START");

  // SD Initialization
  if (!sdCard.begin(SD_CONFIG)) {
    sdCard.initErrorHalt(&Serial);
    Serial.println("ERROR SD Initialization failed!");
  } else {
    Serial.println("Initialization SD card DONE");

    // Output file opening
    retValBool = stateFile.open("aa.csv", O_RDWR | O_CREAT | O_TRUNC);

    // Output file opening
    if (retValBool) {
      outRingBuff.begin(&stateFile);
      
      outRingBuff.write(',');
      outRingBuff.write(',');
      outRingBuff.write(',');
      outRingBuff.write(',');

      size_t numBytes = outRingBuff.writeOut(4);

      Serial.println(numBytes);

      outRingBuff.sync();
      stateFile.sync();
      stateFile.truncate();
    }
  }
}

// LOOP function
void loop() {
      Serial.print("loop");

      delay(800);
}

但是这段代码不起作用,没有检索任何类型的编译器错误、警告等

#include "sdios.h"
#include "SdFat.h"
#include "RingBuf.h"

#define SD_CONFIG  SdioConfig(FIFO_SDIO)        // Use Teensy SDIO

File32  stateFile;
RingBuf<File32, 5> outRingBuff;

// SETUP function
void setup() {
  SdFat32 sdCard;
  bool retValBool;
  
  Serial.begin(9600);                 // Teensy serial is always at full USB speed and buffered... the baud rate here is required but ignored

  Serial.println("Initialization SD card START");

  // SD Initialization
  if (!sdCard.begin(SD_CONFIG)) {
    sdCard.initErrorHalt(&Serial);
    Serial.println("ERROR SD Initialization failed!");
  } else {
    Serial.println("Initialization SD card DONE");

    // Output file opening
    retValBool = stateFile.open("aa.csv", O_RDWR | O_CREAT | O_TRUNC);

    // Output file opening
    if (retValBool) {
      outRingBuff.begin(&stateFile);
      
      outRingBuff.write(',');
      outRingBuff.write(',');
      outRingBuff.write(',');
      outRingBuff.write(',');

      size_t numBytes = outRingBuff.writeOut(4);

      Serial.println(numBytes);

      outRingBuff.sync();
      stateFile.sync();
      stateFile.truncate();
    }
  }
}

// LOOP function
void loop() {
      Serial.print("loop");
      
      outRingBuff.write('A');
      outRingBuff.write('A');
      outRingBuff.write('A');
      outRingBuff.write('A');

      size_t numBytes = outRingBuff.writeOut(4);

      Serial.println(numBytes);
      
      outRingBuff.sync();
      stateFile.sync();
      stateFile.truncate();
      
      delay(800);
}

我找到问题了。 SdFat32 对象在 setup() 函数内部定义,因此在 loop() 函数内部不可访问。很可能,RingBufFile32 对象都不会直接调用它,因此它不会检索任何错误,但是当 SdFat32 对象是无法访问。

我认为应该修复此行为,我认为应该提出错误或警告。