djuseeq / Ch376msc

Arduino library for CH376 mass storage contoller
MIT License
77 stars 17 forks source link

Files are not created when called using separate fileOpen, fileWrite and fileClose functions #72

Open jbteja opened 2 years ago

jbteja commented 2 years ago

Files are not created when called using separate fileOpen, fileWrite and fileClose functions. Am I doing any wrong here? Please Help.

Using:

My code In the Main loop:

chUsbHostOpenFile(fileName);
chUsbHostWriteFile(adat);
chUsbHostWriteFile(adat);
chUsbHostCloseFile();

Functions:

bool chUsbHostOpenFile(String fileName) {
  bool state = true;
  if(flashDrive.driveReady()) {
    flashDrive.setFileName(fileName.c_str());

    switch(flashDrive.openFile()){
      case ANSW_USB_INT_SUCCESS: // 0x14
      case ANSW_ERR_OPEN_DIR: // 0x41
        Serial.println(F("[USB] File opened successfully"));
        flashDrive.moveCursor(CURSOREND);
        break;

      case ANSW_ERR_MISS_FILE: // 0x42
        Serial.println(F("[USB] File doesn't exist, New file"));
        break;

      default:
        state = false;
        Serial.println(F("[USB] Unknown Error!"));
        break;
    }
  } else {
    state = false;
    Serial.println(F("[USB] OpenFile: Drive not found or busy"));
  }
  end:
  return state;
}

bool chUsbHostWriteFile(String sdata) {
  bool state = true;
  String timeStamp = String("\n" + timeClient.getFormattedDateTime() + " -> ");
  if(flashDrive.driveReady()) {
    if(flashDrive.getFreeSectors()){ //check the free space on the drive
      flashDrive.writeFile((char *) timeStamp.c_str(), timeStamp.length());
      flashDrive.writeFile((char *) sdata.c_str(), sdata.length()); //string, string length
      flashDrive.closeFile();
    } else {
      state = false;
      Serial.println(F("[USB] WriteFile: Disk full"));
    }
  } else {
    state = false;
    Serial.println(F("[USB] WriteFile: Drive not found or busy"));
  }
  return state;
}

bool chUsbHostCloseFile() {
  if(flashDrive.driveReady()) {
    flashDrive.closeFile();
    Serial.println(F("[USB] File closed successfully"));
  } else {
    Serial.println(F("[USB] CloseFile: Drive not found or busy"));
    return false;
  }
  return true;
}

But if Open, write and close the file in the same function the file gets created as expected.

bool chUsbHostWriteCloseFile(String fileName, String sdata) {
  bool state = true;
  String timeStamp = String("\n" + timeClient.getFormattedDateTime() + " -> ");
  if(flashDrive.driveReady()) {
    flashDrive.setFileName(fileName.c_str());
    if(flashDrive.openFile() == ANSW_USB_INT_SUCCESS){
    flashDrive.moveCursor(CURSOREND);
    }

    if(flashDrive.getFreeSectors()){ //check the free space on the drive
      flashDrive.writeFile((char *) timeStamp.c_str(), timeStamp.length());
      flashDrive.writeFile((char *) sdata.c_str(), sdata.length()); //string, string length
      flashDrive.closeFile();
    } else {
      state = false;
      Serial.println(F("[USB] WriteFile: Disk full"));
    }
  } else {
    state = false;
    Serial.println(F("[USB] WriteFile: Drive not found or busy"));
  }
  return state;
}