Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.6 kB
25
Indexable
void readSettingsFromTxtFile() {
  if (!SD.begin(_CS)) {
    Serial.println(F("Errore durante l'inizializzazione della scheda SD."));
    Serial.println(F("SD CARD KO."));
    return;
  } else {
    Serial.println(F("SD CARD OK."));
  }
  if (!SD.exists("config.txt")) {
    Serial.println(F("Il file config.txt non esiste."));
    return;
  }

  configFile = SD.open("config.txt", FILE_READ);
  if (!configFile) {
    Serial.println(F("Errore durante l'apertura del file."));
    return;
  }

  while (configFile.available()) {
    String line = configFile.readStringUntil('\n');
    line.trim();

    if (!parseAndAssignValue(line)) {
      Serial.println("Errore durante l'analisi della linea: " + line);
    }
  }

  configFile.close();
}

bool parseAndAssignValue(const String& line) {
  int colonIndex = line.indexOf(':');
  if (colonIndex != -1) {
    String fieldName = line.substring(0, colonIndex);
    String valueStr = line.substring(colonIndex + 1);

    if (valueStr.startsWith("-")) {
      valueStr.replace("-", "");
      int value = -valueStr.toInt();
      Serial.print("value: "); Serial.println(value);
      return assignValue(fieldName, value);
    } else {
      int value = valueStr.toInt();
      return assignValue(fieldName, value);
    }
  }
  return false;
}

bool assignValue(const String& fieldName, int value) {
  Serial.print("fieldName: "); Serial.print(fieldName); Serial.print(" | Value: "); Serial.println(value);
  if (fieldName.equals("ant_sx_trimm")) {
    ant_sx_trimm = value;
  } else if (fieldName.equals("ant_dx_trimm")) {
    ant_dx_trimm = value;
  } else if (fieldName.equals("post_sx_trimm")) {
    post_sx_trimm = value;
  } else if (fieldName.equals("post_dx_trimm")) {
    post_dx_trimm = value;
  } else if (fieldName.equals("cam_x_trimm")) {
    cam_x_trimm = value;
  } else if (fieldName.equals("cam_y_trimm")) {
    cam_y_trimm = value;
  } else if (fieldName.equals("steer_trimm")) {
    steer_trimm = value;
  } else {
    return false; // Campo sconosciuto
  }
  return true;
}

void saveSettingsToTxtFile(bool RESET) {
  if (!SD.begin(_CS)) {
    Serial.println(F("SD CARD KO."));
    return;
  } else {
    Serial.println(F("SD CARD OK."));
  }

  if (SD.exists("config.txt")) {
    SD.remove("config.txt");
    Serial.println(F("Removed old config.txt"));
  }

  if (!configFile.availableForWrite()) {
    uint8_t SD_available = configFile.availableForWrite();
    Serial.println(SD_available);
    Serial.println(F("SD not available for write... wait and retry"));
    delay(100);
  }

  // Crea o sovrascrivi il file "config.txt" sulla scheda SD
  configFile = SD.open("config.txt", FILE_WRITE);
  if (!configFile) {
    Serial.println(F("Error while opening file."));
    return;
  }

  // Scrivi i dati delle variabili nel file
  configFile.print("ant_sx_trimm:");
  configFile.println(RESET ? 0 : ant_sx_trimm);
  configFile.print("ant_dx_trimm:");
  configFile.println(RESET ? 0 : ant_dx_trimm);
  configFile.print("post_sx_trimm:");
  configFile.println(RESET ? 0 : post_sx_trimm);
  configFile.print("post_dx_trimm:");
  configFile.println(RESET ? 0 : post_dx_trimm);
  configFile.print("cam_x_trimm:");
  configFile.println(RESET ? 0 : cam_x_trimm);
  configFile.print("cam_y_trimm:");
  configFile.println(RESET ? 0 : cam_y_trimm);
  configFile.print("steer_trimm:");
  configFile.println(RESET ? 0 : steer_trimm);

  // Chiudi il file
  configFile.close();

  Serial.println(F("File config.txt overwritten successfully."));
}