//------------------------------------ // BOne_SD.ino // This file is part of BOne firmware // // Project home page : http://gyropode.vilain.de // date : 30.09.2014 // //------------------------------------- // // This firmware is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This firmware is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. //------------------------------------- // sd initialize void sd_initialize(){ Serial.print("\nInitializing SD card..."); // test if the card is working! if (!card.init(SPI_HALF_SPEED, SD_CS_PIN)) { Serial.println("initialization failed. Things to check:"); Serial.println("* is a card is inserted?"); Serial.println("* Is your wiring correct?"); Serial.println("* did you change the chipSelect pin to match your shield or module?"); return; }else { Serial.println("Wiring is correct and a card is present."); Serial.println("------------------------"); } // print the type of card Serial.print("\nCard type : "); switch(card.type()) { case SD_CARD_TYPE_SD1: SD_card_type = "SD1"; Serial.println(SD_card_type); break; case SD_CARD_TYPE_SD2: SD_card_type = "SD2"; Serial.println(SD_card_type); break; case SD_CARD_TYPE_SDHC: SD_card_type = "SDHC"; Serial.println(SD_card_type); break; default: SD_card_type = "Unknown"; Serial.println(SD_card_type); } // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32 if (!volume.init(card)) { Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card"); return; } // print the type and size of the first FAT-type volume uint32_t volumesize; Serial.print("\nVolume type is FAT"); Serial.println(volume.fatType(), DEC); Serial.println(); volumesize = volume.blocksPerCluster(); // clusters are collections of blocks volumesize *= volume.clusterCount(); // we'll have a lot of clusters volumesize *= 512; // SD card blocks are always 512 bytes Serial.print("Volume size (bytes): "); Serial.println(volumesize); Serial.print("Volume size (Kbytes): "); volumesize /= 1024; Serial.println(volumesize); Serial.print("Volume size (Mbytes): "); volumesize /= 1024; Serial.println(volumesize); Serial.println("\nFiles found on the card (name, date and size in bytes): "); root.openRoot(volume); // list all files in the card with date and size root.ls(LS_R | LS_DATE | LS_SIZE); Serial.println("------------------------"); // create log file strcpy(SD_filename, "GPSLOG00.csv"); for (uint8_t i = 0; i < 100; i++) { SD_filename[6] = '0' + i/10; SD_filename[7] = '0' + i%10; // create if does not exist, do not open existing, write, sync after write if (! SD.exists(SD_filename)) { break; } } File logFile = SD.open(SD_filename, FILE_WRITE); if(!logFile) { Serial.print("Couldnt create "); Serial.println(SD_filename); Serial.println(); } // take the chip select high to de-select: digitalWrite(SD_CS_PIN, HIGH); } //------------------------------------- // void SD_Update(){ SD_header = "year,month,day,hour,minute,seconds,latitude,longitude,altitude(m),speed(kmph),course"; SD_dataString = SD_year + "," + SD_month + "," + SD_day + "," + SD_hour + "," + SD_minute + "," + SD_second + "," + SD_latitude + "," + SD_longitude + "," + SD_altitude + "," + SD_speed + "," + SD_course; // Open the Data CSV File // and write the newest information to the SD Card File logFile = SD.open(SD_filename, FILE_WRITE); if (logFile){ if(!SD_header_flag){ logFile.println(SD_header); } SD_header_flag = true; logFile.println(SD_dataString); logFile.close(); SD_Timer = millis(); } else{ Serial.println("\nCouldn't open the log file!"); } } //------------------------------------- // void printDirectory(File dir, int numTabs) { tft.setTextColor(WHITE); tft.setTextSize(1); tft.setCursor(10,80); while(true) { File entry = dir.openNextFile(); if (! entry) { // no more files //Serial.println("**nomorefiles**"); break; } for (uint8_t i=0; i