void HAL::syncEEPROM() { // store to disk if changed millis_t time = millis(); if (eprSyncTime && (time - eprSyncTime > 15000)) // Buffer writes only every 15 seconds to pool writes { eprSyncTime = 0; bool failed = false; if (!sd.sdactive) // not mounted { if (eepromFile.isOpen()) eepromFile.close(); Com::printErrorF("Could not write eeprom to sd card - no sd card mounted"); Com::println(); return; } if (!eepromFile.seekSet(0)) failed = true; if(!failed && !eepromFile.write(virtualEeprom, EEPROM_BYTES) == EEPROM_BYTES) failed = true; if(failed) { Com::printErrorF("Could not write eeprom to sd card"); Com::println(); } } }
idigi_callback_status_t iDigiFileSystem::app_process_file_stat(idigi_file_stat_request_t * const request_data, idigi_file_stat_response_t * const response_data) { idigi_file_stat_t *pstat = &response_data->statbuf; idigi_callback_status_t status = idigi_callback_continue; SdFile entry = DigiSD.open(request_data->path); if (!entry.isOpen()) { APP_DEBUG("stat cannot open: %s\n", request_data->path); goto done; } pstat->last_modified = 0; // Last modified date unsupported: pstat->hash_alg = idigi_file_hash_none; // File hash not supported pstat->file_size = (size_t) entry.fileSize(); pstat->flags = 0; if (entry.isDir()) pstat->flags |= IDIGI_FILE_IS_DIR; else pstat->flags |= IDIGI_FILE_IS_REG; entry.close(); done: return status; }
idigi_callback_status_t iDigiFileSystem::app_process_file_opendir(idigi_file_path_request_t * const request_data, idigi_file_open_response_t * const response_data) { idigi_callback_status_t status = idigi_callback_continue; SdFile *dir = new SdFile; if (dir == NULL) { response_data->error->error_status = idigi_file_out_of_memory; } *dir = DigiSD.open(request_data->path); if (!dir->isOpen() || !dir->isDir()) { response_data->error->error_status = idigi_file_path_not_found; return status; } dir->rewind(); response_data->handle = dir; APP_DEBUG("opendir for %s returned %p\n", request_data->path, (void *) dir); return status; }
idigi_callback_status_t iDigiFileSystem::app_process_file_open(idigi_file_open_request_t * const request_data, idigi_file_open_response_t * const response_data) { idigi_callback_status_t status = idigi_callback_continue; SdFile *file = new SdFile; uint8_t filemode = 0; if (request_data->oflag & IDIGI_O_RDONLY) filemode |= O_RDONLY; if (request_data->oflag & IDIGI_O_WRONLY) filemode |= O_WRONLY; if (request_data->oflag & IDIGI_O_RDWR) filemode |= O_RDWR; if (request_data->oflag & IDIGI_O_APPEND) filemode |= O_APPEND; if (request_data->oflag & IDIGI_O_CREAT) filemode |= O_CREAT; if (request_data->oflag & IDIGI_O_TRUNC) filemode |= O_TRUNC; if ((request_data->oflag & (IDIGI_O_RDONLY | IDIGI_O_WRONLY)) == 0) filemode |= O_RDONLY; *file = DigiSD.open(request_data->path, filemode); if (!file->isOpen()) { response_data->error->error_status = idigi_file_path_not_found; return status; } APP_DEBUG("Open %s, req flag %d, flag %d, returned %p\n", request_data->path, request_data->oflag, filemode, (void *) file); response_data->handle = (void *) file; response_data->user_context = NULL; return status; }
void setup(void) { Serial.begin(BPS_115200); Serial.println(); #if WAIT_TO_START Serial.println("Type any character to start"); while (!Serial.available()); #endif //WAIT_TO_START // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with // breadboards. use SPI_FULL_SPEED for better performance. if (!card.init(SPI_HALF_SPEED)) error("card.init failed"); // initialize a FAT volume if (!volume.init(&card)) error("volume.init failed"); // open root directory if (!root.openRoot(&volume)) error("openRoot failed"); // create a new file char name[] = "LOGGER00.CSV"; for (uint8_t i = 0; i < 100; i++) { name[6] = i/10 + '0'; name[7] = i%10 + '0'; if (file.open(&root, name, O_CREAT | O_EXCL | O_WRITE)) break; } if (!file.isOpen()) error ("file.create"); Serial.print("Logging to: "); Serial.println(name); // write header file.writeError = 0; file.print("millis"); #if ECHO_TO_SERIAL Serial.print("millis"); #endif //ECHO_TO_SERIAL #if SENSOR_COUNT > 6 #error SENSOR_COUNT too large #endif //SENSOR_COUNT for (uint8_t i = 0; i < SENSOR_COUNT; i++) { file.print(",sens");file.print(i, DEC); #if ECHO_TO_SERIAL Serial.print(",sens");Serial.print(i, DEC); #endif //ECHO_TO_SERIAL } file.println(); #if ECHO_TO_SERIAL Serial.println(); #endif //ECHO_TO_SERIAL if (file.writeError || !file.sync()) { error("write header failed"); } }
boolean put_handler(AtMegaWebServer& web_server) { const char* length_str = web_server.get_header_value("Content-Length"); long length = atol(length_str); const char *path = web_server.get_path(); SdFile file; if(!file.open(path, O_CREAT | O_WRITE | O_TRUNC)){ // maybe the folder must be created char *c = strrchr(path, '/'); if(c){ *c = 0; if(sdfat.mkdir(path)){ #if DEBUG Serial << "put_handler make DIR: ok " << path <<'\n'; #endif *c = '/'; if(!file.open(path, O_CREAT | O_WRITE | O_TRUNC)){ #if DEBUG Serial << "put_handler open FILE: failed " << path <<'\n'; #endif } } *c = '/'; } } if(file.isOpen()){ EthernetClient client = web_server.get_client(); long size = 0; int read = 0; while(size < length && web_server.waitClientAvailable()){ read = client.read((uint8_t*)buffer, sizeof(buffer)); file.write(buffer, read); size += read; } file.close(); #if DEBUG Serial << "file written: " << size << " of: " << length << '\n'; #endif if(size < length){ web_server.sendHttpResult(404); }else{ web_server.sendHttpResult(200); web_server << path; } }else{ web_server.sendHttpResult(422); // assuming it's a bad filename (non 8.3 name) #if DEBUG Serial << F("put_handler open file failed: send 422 ") << path <<'\n'; #endif } return true; }
File SDClass::open(const char *filepath, uint8_t mode) { /* Open the supplied file path for reading or writing. The file content can be accessed via the `file` property of the `SDClass` object--this property is currently a standard `SdFile` object from `sdfatlib`. Defaults to read only. If `write` is true, default action (when `append` is true) is to append data to the end of the file. If `append` is false then the file will be truncated first. If the file does not exist and it is opened for writing the file will be created. An attempt to open a file for reading that does not exist is an error. */ int pathidx; // do the interative search SdFile parentdir = getParentDir(filepath, &pathidx); // no more subdirs! filepath += pathidx; if (! filepath[0]) { // it was the directory itself! return File(parentdir, "/"); } // Open the file itself SdFile file; // failed to open a subdir! if (!parentdir.isOpen()) return File(); if ( ! file.open(parentdir, filepath, mode)) { return File(); } // close the parent parentdir.close(); if (mode & (O_APPEND | O_WRITE)) file.seekSet(file.fileSize()); return File(file, filepath); }
void HAL::importEEPROM() { if (eepromFile.isOpen()) eepromFile.close(); if (!eepromFile.open("eeprom.bin", O_RDWR | O_CREAT | O_SYNC) || eepromFile.read(virtualEeprom, EEPROM_BYTES) != EEPROM_BYTES) { Com::printFLN(Com::tOpenFailedFile, "eeprom.bin"); } else { Com::printFLN("EEPROM read from sd card."); } EEPROM::readDataFromEEPROM(true); }
void openLog() { int tries = 0; while (tries++ < 3 && !dataFile.open(root, logFilename, O_CREAT | O_WRITE)) { Serial.println(F("Unable to create log file, rotating...")); rotateLog(); } if (!dataFile.isOpen()) { Serial.println(F("Unable to create log file, disabling data logger.")); disableLogging = true; return; } }
idigi_callback_status_t iDigiFileSystem::app_process_file_closedir(idigi_file_request_t * const request_data, idigi_file_response_t * const response_data) { SdFile *dir = (SdFile *) request_data->handle; APP_DEBUG("closedir %p\n", (void *) dir); if (dir->isOpen()) dir->close(); free(dir); return idigi_callback_continue; }
bool FileSystem::initSDCard() { boolean status; if (root.isOpen()) root.close(); // allows repeated calls // First, detect the card status = card.init(pinCS); // Audio shield has SD card SD on pin 10 if (status) { Serial.println("SD card is connected :-)"); } else { Serial.println("SD card is not connected or unusable :-("); sdCardInitialized = false; return sdCardInitialized; } // 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"); sdCardInitialized = false; return sdCardInitialized; } root.openRoot(volume); // print the type and size of the first FAT-type volume Serial.print("\nVolume type is FAT"); Serial.println(volume.fatType(), DEC); Serial.println(); float size = volume.blocksPerCluster() * volume.clusterCount(); size = size * (512.0 / 1e6); // convert blocks to millions of bytes Serial.print("File system space is "); Serial.print(size); Serial.println(" Mbytes."); status = SD.begin(pinCS); // Audio shield has SD card CS on pin 10 if (status) { Serial.println("SD library is able to access the filesystem"); } else { Serial.println("SD library can not access the filesystem!"); Serial.println( "Please report this problem, with the make & model of your SD card."); Serial.println( " http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports"); } sdCardInitialized = true; return sdCardInitialized; }
boolean MP3Player::Play(const char* SongName) { if(!PLAY) { k=65; if(!myFile.isOpen()) { // open the file for read if (!myFile.open(SongName, O_READ)) { #if DEBUG sd.errorHalt("open audio file failed"); #endif return false; } } else name = SongName; Run_STA013(); delayMicroseconds(500000); Play_Pause(1); //AMPON(); #if DEBUG Serial.println("playing"); #endif filesize=myFile.fileSize(); Timer1.initialize(30);// 30 us = can check data request and send a byte to STA013 at 1/30u = 33.33kHz // able to play a song up to 33.33 x 8 /1024 = 260 kbps // recommend play song at 200kbps or lower (such as 128 kbps)for stable performance PLAY=true; Timer1.attachInterrupt(Callback); } return true; }
idigi_callback_status_t iDigiFileSystem::app_process_file_readdir(idigi_file_request_t * const request_data, idigi_file_data_response_t * const response_data) { idigi_callback_status_t status = idigi_callback_continue; SdFile *dir = (SdFile *) request_data->handle; char fileName[DIGISD_MAX_FILENAME_LEN]; SdFile entry = DigiSD.openNextFile(dir, fileName); if (!entry.isOpen()) { APP_DEBUG("No more directory entries\n"); response_data->size_in_bytes = 0; goto done; } // read valid entry { size_t const name_len = strlen(fileName); APP_DEBUG("fileName is %s\n", fileName); if(name_len < response_data->size_in_bytes) { memcpy(response_data->data_ptr, fileName, name_len + 1); response_data->size_in_bytes = name_len + 1; } else { APP_DEBUG("directory entry name too long\n"); response_data->error->error_status = idigi_file_out_of_memory; } } entry.close(); done: return status; }
void writeLog() { if (disableLogging) { return; } if (!dataFile.isOpen()) { if (!dataFile.open(root, logFilename, O_WRITE | O_APPEND)) { #ifdef DEBUG Serial.println(F("Could not open file for writing")); #endif disableLogging = true; return; } } // Local sensor readings logFileSize += dataFile.print(lastUpdate); logFileSize += dataFile.print(F("\t")); logFileSize += dataFile.print(sinceLastAck); logFileSize += dataFile.print(F("\t")); logFileSize += dataFile.print(lastVcc); logFileSize += dataFile.print(F("\t")); if (validReadingi(lastRoundtrip)) { logFileSize += dataFile.print(lastRoundtrip); } logFileSize += dataFile.print(F("\t")); if (validReadingi(lastRssi)) { logFileSize += dataFile.print(lastRssi); } logFileSize += dataFile.print(F("\t")); // Remote sensor readings if (validReadingi(m.vcc)) { logFileSize += dataFile.print(m.vcc); } logFileSize += dataFile.print(F("\t")); if (validReadingi(m.rssi)) { logFileSize += dataFile.print(m.rssi); } logFileSize += dataFile.print(F("\t")); if (validReadingi(m.vibration)) { logFileSize += dataFile.print(m.vibration); } logFileSize += dataFile.print(F("\t")); if (validReadingf(m.altitudeGps)) { logFileSize += dataFile.print(m.altitudeGps); } logFileSize += dataFile.print(F("\t")); if (validReadingf(m.altitude)) { logFileSize += dataFile.print(m.altitude); } logFileSize += dataFile.print(F("\t")); if (validReadingf(m.temp)) { logFileSize += dataFile.print(m.temp); } logFileSize += dataFile.print(F("\t")); if (validReadingf(m.temp2)) { logFileSize += dataFile.print(m.temp2); } logFileSize += dataFile.print(F("\t")); if (validReadingf(m.latitude)) { logFileSize += dataFile.print(m.latitude, 6); } logFileSize += dataFile.print(F("\t")); if (validReadingf(m.longitude)) { logFileSize += dataFile.print(m.longitude, 6); } logFileSize += dataFile.print(F("\t")); if (validReadingf(m.accelX)) { logFileSize += dataFile.print(m.accelX); } logFileSize += dataFile.print(F("\t")); if (validReadingf(m.accelY)) { logFileSize += dataFile.print(m.accelY); } logFileSize += dataFile.print(F("\t")); if (validReadingf(m.accelZ)) { logFileSize += dataFile.print(m.accelZ); } logFileSize += dataFile.print(F("\t")); if (validReadingf(m.magX)) { logFileSize += dataFile.print(m.magX); } logFileSize += dataFile.print(F("\t")); if (validReadingf(m.magY)) { logFileSize += dataFile.print(m.magY); } logFileSize += dataFile.print(F("\t")); if (validReadingf(m.magZ)) { logFileSize += dataFile.print(m.magZ); } logFileSize += dataFile.print(F("\t")); if (validReadingf(m.gyroX)) { logFileSize += dataFile.print(m.gyroX); } logFileSize += dataFile.print(F("\t")); if (validReadingf(m.gyroY)) { logFileSize += dataFile.print(m.gyroY); } logFileSize += dataFile.print(F("\t")); if (validReadingf(m.gyroZ)) { logFileSize += dataFile.print(m.gyroZ); } logFileSize += dataFile.print(F("\t")); if (validReadingf(m.accel2X)) { logFileSize += dataFile.print(m.accel2X); } logFileSize += dataFile.print(F("\t")); if (validReadingf(m.accel2Y)) { logFileSize += dataFile.print(m.accel2Y); } logFileSize += dataFile.print(F("\t")); if (validReadingf(m.accel2Z)) { logFileSize += dataFile.print(m.accel2Z); } logFileSize += dataFile.print(F("\t")); if (validReadingf(m.mag2X)) { logFileSize += dataFile.print(m.mag2X); } logFileSize += dataFile.print(F("\t")); if (validReadingf(m.mag2Y)) { logFileSize += dataFile.print(m.mag2Y); } logFileSize += dataFile.print(F("\t")); if (validReadingf(m.mag2Z)) { logFileSize += dataFile.print(m.mag2Z); } logFileSize += dataFile.print(F("\t")); if (validReadingf(m.gyro2X)) { logFileSize += dataFile.print(m.gyro2X); } logFileSize += dataFile.print(F("\t")); if (validReadingf(m.gyro2Y)) { logFileSize += dataFile.print(m.gyro2Y); } logFileSize += dataFile.print(F("\t")); if (validReadingf(m.gyro2Z)) { logFileSize += dataFile.print(m.gyro2Z); } logFileSize += dataFile.println(F("\t")); dataFile.sync(); if (logFileSize >= logFileMax) { dataFile.close(); rotateLog(); openLog(); } }
/* * Setup */ void setup() { wdt_enable(WDTO_8S); wdt_reset(); //Setup Ports Serial.begin(115200); //Start Debug Serial 0 Serial1.begin(9600); //Start GPS Serial 1 Serial2.begin(9600); pinMode(PIN_LED_GREEN, OUTPUT); //Blue GREEN pinMode(PIN_LED_RED, OUTPUT); //Blue RED pinMode(PIN_LED_BLUE, OUTPUT); //Blue LED pinMode(PIN_SPI_CS,OUTPUT); //Chip Select Pin for the SD Card pinMode(10, OUTPUT); //SDcard library expect 10 to set set as output. // Initialise the GPS wdt_disable(); gps.init(); gps.configureUbloxSettings(); // Configure Ublox for MY_HIGH altitude mode wdt_enable(WDTO_8S); // join I2C bus //start I2C transfer to the Module/Transmitter Wire.begin(); //Set up the two EasyTransfer methods ETI2Cout.begin(details(mD.i2cOut), &Wire); //setup the data structure to transfer out ETSerialIn.begin(details(vals), &Serial2); //Start up the LGgyro if (LGgyro.init()) { #ifdef DEBUG_ON Serial.println("LGgyro OK"); #endif LGgyro.enableDefault(); } else { #ifdef DEBUG_ON Serial.println("LGgyro not working"); #endif SET_LED_Status(SET_LED_WHITE,500); //White LED SET_LED_Status(SET_LED_RED,1000); //Red LED } //Start up the accelerometer accel = ADXL345(); // Create an instance of the accelerometer if(accel.EnsureConnected()) { // Check that the accelerometer is connected. #ifdef DEBUG_ON Serial.println("Connected to ADXL345."); #endif accel.SetRange(2, true); // Set the range of the accelerometer to a maximum of 2G. accel.EnableMeasurements(); // Tell the accelerometer to start taking measurements. } else{ #ifdef DEBUG_ON Serial.println("Could not connect to ADXL345."); #endif SET_LED_Status(SET_LED_WHITE,500); //White LED SET_LED_Status(SET_LED_RED,2000); //Red LED } //Start up the compass compass = HMC5883L(); // Construct a new HMC5883 compass. #ifdef DEBUG_ON if(compass.EnsureConnected() == 1) { Serial.println("Connected to HMC5883L."); } else { Serial.println("Not Connected to HMC5883L."); } #endif error = compass.SetScale(1.3); // Set the scale of the compass. #ifdef DEBUG_ON if(error != 0) { // If there is an error, print it out. Serial.println("Compass Error 1"); Serial.println(compass.GetErrorText(error)); } else { Serial.println("Compass Ok 1"); } #endif error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous #ifdef DEBUG_ON if(error != 0) { // If there is an error, print it out. Serial.println("Compass error 2"); Serial.println(compass.GetErrorText(error)); } else { Serial.println("Compass Ok 2"); } #endif //Start up the Pressure Sensor dps = BMP085(); dps.init(); #ifdef DEBUG_ON Serial.print("BMP Mode "); Serial.println(dps.getMode()); #endif wdt_reset(); // Start up the OneWire Sensors library and turn off blocking takes too long! sensors.begin(); sensors.setWaitForConversion(false); sensors.requestTemperaturesByAddress(outsideThermometer); // Send the command to get temperature //Initialise all of the record values mD.vals.tCount = 0; mD.vals.uslCount = 0; mD.vals.year = 0; mD.vals.month = 0; mD.vals.day = 0; mD.vals.hour = 0; mD.vals.minute = 0; mD.vals.second = 0; mD.vals.hundredths = 0; mD.vals.iLat = 0; mD.vals.iLong = 0; mD.vals.iAlt = 0; mD.vals.bSats = 0; mD.vals.iAngle = 0; mD.vals.iHspeed = 0; mD.vals.iVspeed = 0; mD.vals.age = 0; mD.vals.ihdop = 0; mD.vals.AcXPayload = 0; mD.vals.AcYPayload = 0; mD.vals.AcZPayload = 0; mD.vals.GyXPayload = 0; mD.vals.GyYPayload = 0; mD.vals.GyZPayload = 0; mD.vals.MgXPayload = 0; mD.vals.MgYPayload = 0; mD.vals.MgZPayload = 0; mD.vals.TmpPayload = 0; //Connect to the SD Card if(!SD.begin(PIN_SPI_CS, SPI_HALF_SPEED)) { #ifdef DEBUG_ON Serial.println("SD not working!!"); #endif SET_LED_Status(SET_LED_WHITE,500); //White LED SET_LED_Status(SET_LED_RED,3000); //Red LED } else { #ifdef DEBUG_ON Serial.println("SD OK"); #endif dataFile.open(SD_LOG_FILE, O_CREAT | O_WRITE | O_APPEND); //Open Logfile if (!dataFile.isOpen()) { #ifdef DEBUG_ON Serial.println("SD Data File Not Opened"); #endif SET_LED_Status(SET_LED_WHITE,500); SET_LED_Status(SET_LED_RED,3000); } } //Cycle lights SET_LED_Status(SET_LED_OFF,0); SET_LED_Status(SET_LED_RED,500); SET_LED_Status(SET_LED_GREEN,500); SET_LED_Status(SET_LED_BLUE,500); SET_LED_Status(SET_LED_OFF,0); elapseSIM900 = millis(); //Elapse counter for data to SIM900 elapseNTXB = millis(); //Elapse counter for data to NTXB NEWGPSDATA = false; wdt_enable(WDTO_2S); wdt_reset(); }