Buenos dias:
Estoy realizando un datalogger con una tarjeta SD en la cual genero un directorio mensual (AAAAMM) y dentro del mismo un fichero diario (AAAAMMDD.LOG). El codigo que utilizo es el siguiente:
///////////////////////////////////////////// CREATE FILE /////////////////////////////////////////
void getFileName() {
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
second = bcdToDec(Wire.read() & 0x7f);
minute = bcdToDec(Wire.read());
hour = bcdToDec(Wire.read() & 0x3f); // Tiene que cambiar esto para 12 hora am/pm.
dayOfWeek = bcdToDec(Wire.read());
dayOfMonth = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
if (dayOfMonth < 10) {
if (month < 10) {
sprintf(directory, "20%d0%d", year, month); //merge together
sprintf(name, "20%d0%d0%d.log", year, month, dayOfMonth); //merge together
}
else {
sprintf(directory, "20%d%d", year, month); //merge together
sprintf(name, "20%d%d0%d.log", year, month, dayOfMonth); //merge together
}
}
else {
if (month < 10) {
sprintf(directory, "20%d0%d", year, month); //merge together
sprintf(name, "20%d0%d%d.log", year, month, dayOfMonth); //merge together
}
else {
sprintf(directory, "20%d%d", year, month); //merge together
sprintf(name, "20%d%d%d.log", year, month, dayOfMonth); //merge together
}
}
}
#if LOG_TO_SD
// create directory if it does not exist
if (!sd.exists(directory)) sd.mkdir(directory);
// make the current working directory
sd.chdir(directory);
// open the file for write at end like the Native SD library
if (!myFile.open(name, O_RDWR | O_CREAT | O_AT_END)) {
sd.errorHalt("opening logfile for write failed");
}
// if the file opened okay, write to it:
Serial.print("Writing to file /");
Serial.print(directory);
Serial.print("/");
Serial.println(name);
myFile.open(name);
Cada noche a las 23:59 horas aproximadamente cierro el fichero del dia y llamo de nuevo a la funcion anterior para generar el nuevo fichero con el siguiente codigo
if (hour == 23 && minute == 59 && second >= 50) {
myFile.close();
Serial.println("Closed logfile.");
// Now root is working directory
SdVolume volume;
root.openRoot(&volume);
delay(15000);
Serial.println("Create new logfile");
getFileName(); //Call function.
}
El problema al ejecutar este ultimo codigo es que al encontrarse dentro de /DIRECTORY me genera un nuevo directorio dentro del mismo y el nuevo fichero colgando del mismo, esto es..
/
/201411/
--------> 20141109.log
--------> /201411/
------------------>20141110.log
¿Como puedo hacer para volver al directorio raiz (root) antes de generar el nuevo fichero.?. ¿Alguien me puede ayudar...?
Gracias