Hand in place of the asterisk

Work on SD-card logs. No more f_open and f_close in flight (could take up to 400ms!)
SD-card logs now closed on shutdown
This commit is contained in:
bsongis
2012-05-30 11:28:14 +00:00
parent 68d8fa7794
commit ed8379c44b
11 changed files with 110 additions and 148 deletions
+63 -117
View File
@@ -37,141 +37,94 @@
char g_logFilename[21]; // "/O9XLOGS/M00_000.CSV\0" max required length = 21
FATFS g_FATFS_Obj; // TODO not here
FIL g_oLogFile;
int8_t g_logState = 0; // 0=closed, >0=opened, <0=error
const pm_char * g_logError = NULL;
// TODO initSDCard()
void initLogs()
const pm_char * openLogs()
{
// Determine and set log file filename
FRESULT result;
DIR folder;
char *buf = g_logFilename;
// close any file left open. E.G. Changing models with log switch still on.
if (g_oLogFile.fs) f_close(&g_oLogFile);
g_logState = 0;
strcpy_P(g_logFilename, PSTR(LOGS_PATH "/M00_000.CSV"));
// Set log file model number
uint8_t num = g_eeGeneral.currModel + 1;
char *n = &g_logFilename[sizeof(LOGS_PATH) + 2];
*n = (char)((num % 10) + '0');
*(--n) = (char)((num / 10) + '0');
result = f_mount(0, &g_FATFS_Obj);
if (result!=FR_OK)
{
g_logState = -result;
g_logError = PSTR("SDCARD F/S ERROR");
return;
if (result != FR_OK) {
return SDCARD_ERROR(result);
}
DIR dir;
result = f_opendir(&dir, MODELS_PATH);
wdt_reset();
strcpy_P(buf, STR_LOGS_PATH);
result = f_opendir(&folder, buf);
if (result != FR_OK) {
if (result == FR_NO_PATH)
result = f_mkdir(MODELS_PATH);
if (result != FR_OK) {
g_logState = -result;
g_logError = PSTR("Check " MODELS_PATH " folder");
return;
}
result = f_mkdir(buf);
if (result != FR_OK)
return SDCARD_ERROR(result);
}
// Loop, skipping over any existing log files ... _000, _001, etc. until we have a unique file name
while (1)
{
result = f_open(&g_oLogFile, g_logFilename, FA_OPEN_EXISTING | FA_READ);
if (result == FR_OK)
{
f_close(&g_oLogFile);
wdt_reset();
// bump log file counter (file extension)
n = &g_logFilename[sizeof(LOGS_PATH) + 6];
if (++*n > '9')
{
*n='0';
n--;
if (++*n > '9')
{
*n='0';
n--;
if (++*n > '9')
{
*n='0';
break; // Wow. We looped back around past 999 to 000! abort loop
}
}
}
}
else if (result == FR_NO_FILE /*TODO check this code*/)
{
break;
}
else if (result == FR_NO_PATH)
{
if (f_mkdir(LOGS_PATH) != FR_OK)
{
g_logState = -result;
g_logError = PSTR("Check " LOGS_PATH " folder");
return;
}
}
else
{
g_logState = -result;
g_logError = SDCARD_ERROR(result);
return;
buf[sizeof(LOGS_PATH)-1] = '/';
memcpy(&buf[sizeof(LOGS_PATH)], g_model.name, sizeof(g_model.name));
buf[sizeof(LOGS_PATH)+FILENAME_MAXLEN] = '\0';
uint8_t i = sizeof(LOGS_PATH)+FILENAME_MAXLEN-1;
uint8_t len = 0;
while (i>sizeof(LOGS_PATH)-1) {
if (!len && buf[i])
len = i+1;
if (len) {
if (buf[i])
buf[i] = idx2char(buf[i]);
else
buf[i] = '_';
}
i--;
}
// g_logFilename should now be set appropriately.
if (len == 0) {
uint8_t num = g_eeGeneral.currModel + 1;
strcpy_P(&buf[sizeof(LOGS_PATH)], STR_MODEL);
buf[sizeof(LOGS_PATH) + PSIZE(TR_MODEL)] = (char)((num / 10) + '0');
buf[sizeof(LOGS_PATH) + PSIZE(TR_MODEL) + 1] = (char)((num % 10) + '0');
len = sizeof(LOGS_PATH) + PSIZE(TR_MODEL) + 2;
}
strcpy_P(&buf[len], STR_LOGS_EXT);
result = f_open(&g_oLogFile, buf, FA_OPEN_ALWAYS | FA_WRITE);
if (result != FR_OK) {
return SDCARD_ERROR(result);
}
if (g_oLogFile.fsize == 0) {
f_puts("Date,Time,Buffer,RX,TX,A1,A2,", &g_oLogFile);
if (g_model.frsky.usrProto == USR_PROTO_FRSKY_HUB)
f_puts("GPS Date,GPS Time,Long,Lat,Course,GPS Speed,GPS Alt,Baro Alt,Temp1,Temp2,RPM,Fuel,Volts,AccelX,AccelY,AccelZ,", &g_oLogFile);
f_puts("Rud,Ele,Thr,Ail,P1,P2,P3,THR,RUD,ELE,ID0,ID1,ID2,AIL,GEA,TRN\n", &g_oLogFile);
}
result = f_lseek(&g_oLogFile, g_oLogFile.fsize); // append
if (result != FR_OK) {
return SDCARD_ERROR(result);
}
return NULL;
}
void closeLogs()
{
f_close(&g_oLogFile);
}
void writeLogs()
{
FRESULT result;
if (isFunctionActive(FUNC_LOGS))
if (g_oLogFile.fs && isFunctionActive(FUNC_LOGS))
{
if (g_logState==0)
{
result = f_mount(0, &g_FATFS_Obj);
if (result != FR_OK)
{
g_logState = -result;
// TODO beepAgain = result - 1;
AUDIO_WARNING2();
}
else
{
// create new log file using filename set up in initLogs()
result = f_open(&g_oLogFile, g_logFilename, FA_OPEN_ALWAYS | FA_WRITE);
if (result != FR_OK)
{
g_logState = -result;
// TODO beepAgain = result - 1; // TODO FOR DEBUG -- count out error_number beeps
AUDIO_KEYPAD_UP();
}
else
{
if (g_oLogFile.fsize == 0) {
// if data type == Hub TODO
f_puts("Date,Time,Buffer,RX,TX,A1,A2,", &g_oLogFile);
if (g_model.frsky.usrProto == USR_PROTO_FRSKY_HUB)
f_puts("GPS Date,GPS Time,Long,Lat,Course,GPS Speed,GPS Alt,Baro Alt,Temp1,Temp2,RPM,Fuel,Volts,AccelX,AccelY,AccelZ,", &g_oLogFile);
f_puts("Rud,Ele,Thr,Ail,P1,P2,P3,THR,RUD,ELE,ID0,ID1,ID2,AIL,GEA,TRN\n", &g_oLogFile);
}
f_lseek(&g_oLogFile, g_oLogFile.fsize); // append
g_logState = 1;
AUDIO_WARNING2();
}
}
}
if (g_logState>0)
{
static struct gtm t;
struct gtm *at = &t;
filltm(&g_unixTime, &t);
@@ -223,13 +176,6 @@ void writeLogs()
// TODO Don't close the log file here. We have 'soft off' available on the v4.1 board. Once
// that is implemented, it can take care of closing the file, should the radio be
// powered off before the FUNC SWITCH is turned off.
}
}
else if (g_logState > 0)
{
f_close(&g_oLogFile);
AUDIO_WARNING2();
g_logState = 0;
}
}