/**
 * Function starts the SD card service and makes sure that the appropriate directory
 * structure exists, then creates the file to use for logging data. Data will be logged
 * to a file called fileNamePrefix_0.csv or fileNamePrefix_0.bin (number will be incremented
 * for each new log file)
 *
 * @param chipSelectPin Arduino pin SD card reader CS pin is attached to
 * @param fileNamePrefix string to prefix at beginning of data file
 * @param csvData boolean flag whether we are writing csv data or binary data (used for filename)
 *
 * @return true if successful, false if failed
 */
bool beginDataLog(int chipSelectPin, const char *fileNamePrefix, bool csvData)
{
  bool ret;
  int i = 0;
  int max_len;
  char fileName[19];
  char prefix[8];
  char rootPath[] = "/data";


  if (_output_buffer != NULL) {
    delete []_output_buffer;
  }
  OUTPUT_BUF_SIZE = 512;
  _output_buffer = vol.cacheAddress()->output_buf;

  if (freeMemory() < 400) {
    strcpy_P(_getOutBuf(), sd_card_error);
    Serial.print(_getOutBuf());
    Serial.print(freeMemory());
    Serial.println(", need 400)");
    ret = false;
  } else {
    ret = sd.begin(chipSelectPin, SPI_FULL_SPEED);
  }

  //Filenames need to fit the 8.3 filename convention, so truncate down the
  //given filename if it is too long.
  memcpy(prefix, fileNamePrefix, 7);
  prefix[7] = '\0';

  if (ret) {
    if (!sd.exists(rootPath))
      ret = sd.mkdir(rootPath);
    if (ret) {
      while (true) {
	if (i < 10) {
	  max_len = 7;
	} else if (i < 100) {
	  max_len = 6;
	} else if (i < 1000) {
	  max_len = 5;
	} else {
	  break;
	}

	prefix[max_len - 1] = '\0';
	sprintf(fileName, "%s/%s%d.%s", rootPath, prefix, i,
		csvData ? "csv" : "bin");
	if (!sd.exists(fileName)) {
	  file = sd.open(fileName, FILE_WRITE);
	  break;
	}
	i++;
      }
    }
  }
  return file.isOpen();
}
示例#2
0
void enterStart() {
  // Read configuration from Flash
  Time.zone(-6); // TODO: store in Flash and add configuration

  // Display splash screen
  Display.print("Mane Avis v");
  Display.println(MANE_AVIS_VERSION);
  Display.print("Firmware: ");
  Display.println(System.version());
  // TODO: display a real splash screen

  // Read alarms from SD card
  if(sd.exists(STORE)) {
    digitalWrite(D7, HIGH);
    if(!store.open(STORE, O_READ)) {
      error("Failed to open store");
      return;
    }

    String line;
    while((line = store.readStringUntil('\r')) != NULL) {
      alarms.add(line);
      store.read(); // clear '\n'
    }
  }

  store.close();
  digitalWrite(D7, LOW);
}
示例#3
0
bool open(char* fileName) {
  // Create LDxxxx.CSV for the lowest value of x.

  #ifdef SDCARD
  uint16_t i = 0;
  do {
    fileName[2] = (i/1000) % 10 + '0';
    fileName[3] = (i/100)  % 10 + '0';
    fileName[4] = (i/10)   % 10 + '0';
    fileName[5] = i        % 10 + '0';
    i++;
  }
  while(sd.exists(fileName));


  if(!file.open(fileName, O_CREAT | O_WRITE | O_EXCL)) {
//    error_P("file open");
    return false;
  }
  file.clearWriteError();
  
  // write data header
  file.print("time (s)");

  #endif
  
  Serial.print("v");
  Serial.println(FIRMWARE_VERSION);

  Serial.print("File: ");
  Serial.println(fileName);

  // write data header
  Serial.print("time (s)");
  
  /* We are no longer using the junction temperature in our data output.
  // write data header
  file.print("time (s), ambient");
  Serial.print("time (s), ambient");
  
  switch(temperatureUnit) {
  case TEMPERATURE_UNITS_C:
    file.print(" (C)");
    Serial.print(" (C)");
    break;
  case TEMPERATURE_UNITS_F:
    file.print(" (F)");
    Serial.print(" (F)");
    break;
  case TEMPERATURE_UNITS_K:
    file.print(" (K)");
    Serial.print(" (K)");
    break;
    
  }*/

  for (uint8_t i = 0; i < SENSOR_COUNT; i++) {
    #ifdef SDCARD
    file.print(", temp_");
    file.print(i, DEC);
    #endif
    
    Serial.print(", temp_");
    Serial.print(i, DEC);
    
    switch(temperatureUnit) {
    case TEMPERATURE_UNITS_C:
      #ifdef SDCARD
      file.print(" (C)");
      #endif
      Serial.print(" (C)");
      break;
    case TEMPERATURE_UNITS_F:
      #ifdef SDCARD
      file.print(" (F)");
      #endif
      Serial.print(" (F)");
      break;
    case TEMPERATURE_UNITS_K:
      #ifdef SDCARD
      file.print(" (K)");
      #endif
      Serial.print(" (K)");
      break;
    }
  }
  #ifdef SDCARD
  file.println();
  file.flush();
  #endif
  Serial.println();

  #ifdef SDCARD
  return (file.getWriteError() == false);
  #else
    return true;
  #endif
}
示例#4
0
void SDcard::removefile(){
   if (SD.exists("data.txt")) {
      SD.remove("data.txt");
   }
}
示例#5
0
void startBinLogger(void (*dateTime)(uint16_t *date, uint16_t *time)){

#ifdef LOGGER_DEBUG
    Serial.print("Size of Struct: ");
    Serial.println(sizeof(salus_data_t));
    Serial.print("Data_DIM: ");
    Serial.println(DATA_DIM);
    Serial.print("FILL_DIM: ");
    Serial.println(FILL_DIM);
    Serial.print("Sizeof Block: ");
    Serial.println(sizeof(block_t));
    Serial.println();
#endif

    if (!sd.begin(SD_CHIPSELECT, SD_SPI_SPEED)) {
        sd.initErrorHalt();
    }

    int number = 0;
    char sName[80];

    // Find a filename that hasn't been used already
    do
    {
        sprintf(sName, "Salus_Results_%d.bin", number++);
    } while (sd.exists(sName));

    binFile.close();

    binFile.dateTimeCallback(dateTime);

    if (!binFile.createContiguous(sd.vwd(), sName, 512 * FILE_BLOCK_COUNT)){
        error("createContiguous failed");
    }

    if (!binFile.contiguousRange(&bgnBlock, &endBlock)){
        error("contiguousRange failed");
    }

    // Use SdFat's internal buffer ( ???? )
    uint8_t* cache = (uint8_t*)sd.vol()->cacheClear();
    if (cache == 0) {
        error("cacheClear failed");
    }

    binFile.dateTimeCallbackCancel();

    uint32_t bgnErase = bgnBlock;
    uint32_t endErase;
    while (bgnErase < endBlock) {
        endErase = bgnErase + ERASE_SIZE;
        if (endErase > endBlock) {
            endErase = endBlock;
        }
        if (!sd.card()->erase(bgnErase, endErase)) {
            error("erase failed");
        }
        bgnErase = endErase + 1;
    }


    // Start a multiple block write.
    if (!sd.card()->writeStart(bgnBlock, FILE_BLOCK_COUNT)) {
        error("writeBegin failed");
    }
}
//------------------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  
  // Wait for USB Serial 
  while (!Serial) {
    SysCall::yield();
  }
  delay(1000);

  cout << F("Type any character to start\n");
  // Wait for input line and discard.
  cin.readline();
  cout << endl;
  
  // Initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  // breadboards.  use SPI_FULL_SPEED for better performance.
  if (!sd.begin(SD_CHIP_SELECT, SPI_HALF_SPEED)) {
    sd.initErrorHalt();
  }
  if (sd.exists("Folder1") 
    || sd.exists("Folder1/file1.txt")
    || sd.exists("Folder1/File2.txt")) {
    error("Please remove existing Folder1, file1.txt, and File2.txt");
  }
  int rootFileCount = 0;
  sd.vwd()->rewind();   
  while (file.openNext(sd.vwd(), O_READ)) {
    if (!file.isHidden()) {
      rootFileCount++;
    }
    file.close();
    if (rootFileCount > 10) {
      error("Too many files in root. Please use an empty SD.");
    }
  }
  if (rootFileCount) {
    cout << F("\nPlease use an empty SD for best results.\n\n");
    delay(1000);    
  }
  // Create a new folder.
  if (!sd.mkdir("Folder1")) {
    error("Create Folder1 failed");
  }
  cout << F("Created Folder1\n");

  // Create a file in Folder1 using a path.
  if (!file.open("Folder1/file1.txt", O_CREAT | O_WRITE)) {
    error("create Folder1/file1.txt failed");
  }
  file.close();
  cout << F("Created Folder1/file1.txt\n");

  // Change volume working directory to Folder1.
  if (!sd.chdir("Folder1")) {
    error("chdir failed for Folder1.\n");
  }
  cout << F("chdir to Folder1\n");

  // Create File2.txt in current directory.
  if (!file.open("File2.txt", O_CREAT | O_WRITE)) {
    error("create File2.txt failed");
  }
  file.close();
  cout << F("Created File2.txt in current directory\n");

  cout << F("\nList of files on the SD.\n");
  sd.ls("/", LS_R);

  // Remove files from current directory.
  if (!sd.remove("file1.txt") || !sd.remove("File2.txt")) {
    error("remove failed");
  }
  cout << F("\nfile1.txt and File2.txt removed.\n");

  // Change current directory to root.
  if (!sd.chdir()) {
    error("chdir to root failed.\n");
  }

  cout << F("\nList of files on the SD.\n");
  sd.ls(LS_R);

  // Remove Folder1.
  if (!sd.rmdir("Folder1")) {
    error("rmdir for Folder1 failed\n");
  }

  cout << F("\nFolder1 removed.\n");
  cout << F("\nList of files on the SD.\n");
  sd.ls(LS_R);
  cout << F("Done!\n");
}