/* * Copyright (C) OpenTX * * Based on code named * th9x - http://code.google.com/p/th9x * er9x - http://code.google.com/p/er9x * gruvin9x - http://code.google.com/p/gruvin9x * * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * This program 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 General Public License for more details. */ #include "opentx.h" #include "ff.h" FIL g_oLogFile __DMA; const pm_char * g_logError = NULL; uint8_t logDelay; void writeHeader(); #if defined(PCBTARANIS) || defined(PCBFLAMENCO) || defined(PCBHORUS) #define GET_2POS_STATE(sw) (switchState(SW_ ## sw ## 0) ? -1 : 1) #else #define GET_2POS_STATE(sw) (switchState(SW_ ## sw) ? -1 : 1) #endif #define GET_3POS_STATE(sw) (switchState(SW_ ## sw ## 0) ? -1 : (switchState(SW_ ## sw ## 2) ? 1 : 0)) void logsInit() { memset(&g_oLogFile, 0, sizeof(g_oLogFile)); } const pm_char * logsOpen() { // Determine and set log file filename FRESULT result; char filename[34]; // /LOGS/modelnamexxx-2013-01-01.log if (!sdMounted()) return STR_NO_SDCARD; if (sdGetFreeSectors() == 0) return STR_SDCARD_FULL; // check and create folder here strcpy_P(filename, STR_LOGS_PATH); const char * error = sdCheckAndCreateDirectory(filename); if (error) { return error; } filename[sizeof(LOGS_PATH)-1] = '/'; memcpy(&filename[sizeof(LOGS_PATH)], g_model.header.name, sizeof(g_model.header.name)); filename[sizeof(LOGS_PATH)+sizeof(g_model.header.name)] = '\0'; uint8_t i = sizeof(LOGS_PATH)+sizeof(g_model.header.name)-1; uint8_t len = 0; while (i>sizeof(LOGS_PATH)-1) { if (!len && filename[i]) len = i+1; if (len) { if (filename[i]) filename[i] = idx2char(filename[i]); else filename[i] = '_'; } i--; } if (len == 0) { #if defined(EEPROM) uint8_t num = g_eeGeneral.currModel + 1; #else // TODO uint8_t num = 1; #endif strcpy_P(&filename[sizeof(LOGS_PATH)], STR_MODEL); filename[sizeof(LOGS_PATH) + PSIZE(TR_MODEL)] = (char)((num / 10) + '0'); filename[sizeof(LOGS_PATH) + PSIZE(TR_MODEL) + 1] = (char)((num % 10) + '0'); len = sizeof(LOGS_PATH) + PSIZE(TR_MODEL) + 2; } char * tmp = &filename[len]; #if defined(RTCLOCK) tmp = strAppendDate(&filename[len]); #endif strcpy_P(tmp, STR_LOGS_EXT); result = f_open(&g_oLogFile, filename, FA_OPEN_ALWAYS | FA_WRITE | FA_OPEN_APPEND); if (result != FR_OK) { return SDCARD_ERROR(result); } if (f_size(&g_oLogFile) == 0) { writeHeader(); } return NULL; } tmr10ms_t lastLogTime = 0; void logsClose() { if (sdMounted()) { if (f_close(&g_oLogFile) != FR_OK) { // close failed, forget file g_oLogFile.obj.fs = 0; } lastLogTime = 0; } } #if !defined(CPUARM) getvalue_t getConvertedTelemetryValue(getvalue_t val, uint8_t unit) { convertUnit(val, unit); return val; } #endif void writeHeader() { #if defined(RTCLOCK) f_puts("Date,Time,", &g_oLogFile); #else f_puts("Time,", &g_oLogFile); #endif #if defined(TELEMETRY_FRSKY) #if !defined(CPUARM) f_puts("Buffer,RX,TX,A1,A2,", &g_oLogFile); #if defined(FRSKY_HUB) if (IS_USR_PROTO_FRSKY_HUB()) { f_puts("GPS Date,GPS Time,Long,Lat,Course,GPS Speed(kts),GPS Alt,Baro Alt(", &g_oLogFile); f_puts(TELEMETRY_BARO_ALT_UNIT, &g_oLogFile); f_puts("),Vertical Speed,Air Speed(kts),Temp1,Temp2,RPM,Fuel," TELEMETRY_CELLS_LABEL "Current,Consumption,Vfas,AccelX,AccelY,AccelZ,", &g_oLogFile); } #endif #if defined(WS_HOW_HIGH) if (IS_USR_PROTO_WS_HOW_HIGH()) { f_puts("WSHH Alt,", &g_oLogFile); } #endif #endif #if defined(CPUARM) char label[TELEM_LABEL_LEN+7]; for (int i=0; i 0) { tmr10ms_t tmr10ms = get_tmr10ms(); if (lastLogTime == 0 || (tmr10ms_t)(tmr10ms - lastLogTime) >= (tmr10ms_t)logDelay*10) { lastLogTime = tmr10ms; if (!g_oLogFile.obj.fs) { const pm_char * result = logsOpen(); if (result != NULL) { if (result != error_displayed) { error_displayed = result; POPUP_WARNING(result); } return; } } #if defined(RTCLOCK) { static struct gtm utm; static gtime_t lastRtcTime = 0; if (g_rtcTime != lastRtcTime) { lastRtcTime = g_rtcTime; gettime(&utm); } f_printf(&g_oLogFile, "%4d-%02d-%02d,%02d:%02d:%02d.%02d0,", utm.tm_year+TM_YEAR_BASE, utm.tm_mon+1, utm.tm_mday, utm.tm_hour, utm.tm_min, utm.tm_sec, g_ms100); } #else f_printf(&g_oLogFile, "%d,", tmr10ms); #endif #if defined(TELEMETRY_FRSKY) #if !defined(CPUARM) f_printf(&g_oLogFile, "%d,%d,%d,", telemetryStreaming, RAW_FRSKY_MINMAX(telemetryData.rssi[0]), RAW_FRSKY_MINMAX(telemetryData.rssi[1])); for (uint8_t i=0; i