summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--megalogger/datalogger.h133
1 files changed, 87 insertions, 46 deletions
diff --git a/megalogger/datalogger.h b/megalogger/datalogger.h
index 6649a2b..c67583b 100644
--- a/megalogger/datalogger.h
+++ b/megalogger/datalogger.h
@@ -52,7 +52,7 @@ static File sdfile;
#endif
typedef struct {
- uint16_t pid;
+ uint8_t pid;
char name[3];
} PID_NAME;
@@ -61,12 +61,12 @@ const PID_NAME pidNames[] PROGMEM = {
{PID_GYRO, {'G','Y','R'}},
{PID_COMPASS, {'M','A','G'}},
{PID_GPS_LATITUDE, {'L','A','T'}},
-{PID_GPS_LONGITUDE, {'L','O','N'}},
+{PID_GPS_LONGITUDE, {'L','N','G'}},
{PID_GPS_ALTITUDE, {'A','L','T'}},
{PID_GPS_SPEED, {'S','P','D'}},
{PID_GPS_HEADING, {'C','R','S'}},
{PID_GPS_SAT_COUNT, {'S','A','T'}},
-{PID_GPS_TIME, {'T','I','M'}},
+{PID_GPS_TIME, {'U','T','C'}},
{PID_GPS_DATE, {'D','T','E'}},
{PID_BATTERY_VOLTAGE, {'B','A','T'}},
{PID_DATA_SIZE, {'D','A','T'}},
@@ -74,105 +74,144 @@ const PID_NAME pidNames[] PROGMEM = {
class CDataLogger {
public:
+ CDataLogger()
+ {
+ m_lastDataTime = 0;
+#if ENABLE_DATA_CACHE
+ cacheBytes = 0;
+#endif
+ }
void initSender()
{
#if ENABLE_DATA_OUT
SerialRF.begin(STREAM_BAUDRATE);
- SerialRF.print("AT+NAMEFreematics");
- delay(10);
-#endif
-#if ENABLE_DATA_LOG
- m_lastDataTime = 0;
#endif
}
- void recordData(const char* buf, byte len)
+ byte genTimestamp(char* buf, bool absolute)
+ {
+ byte n;
+ if (absolute || dataTime >= m_lastDataTime + 60000) {
+ // absolute timestamp
+ n = sprintf(buf, "#%lu,", dataTime);
+ } else {
+ // incremental timestamp
+ n = sprintf(buf, "%u,", (unsigned int)(dataTime - m_lastDataTime));
+ }
+ return n;
+ }
+ void record(const char* buf, byte len)
{
#if ENABLE_DATA_LOG
- dataSize += sdfile.print(dataTime - m_lastDataTime);
- dataSize += sdfile.write(',');
+#if STREAM_FORMAT == FORMAT_BIN
dataSize += sdfile.write(buf, len);
- m_lastDataTime = dataTime;
+#else
+ char tmp[12];
+ byte n = genTimestamp(tmp, dataSize == 0);
+ dataSize += sdfile.write(tmp, n);
+ dataSize += sdfile.write(buf, len);
+ sdfile.println();
+ dataSize += 3;
+#endif
#endif
+ m_lastDataTime = dataTime;
}
- void sendData(const char* buf, byte len)
+ void dispatch(const char* buf, byte len)
{
+#if ENABLE_DATA_CACHE
+ if (cacheBytes + len < MAX_CACHE_SIZE - 10) {
+ cacheBytes += genTimestamp(cache + cacheBytes, cacheBytes == 0);
+ memcpy(cache + cacheBytes, buf, len);
+ cacheBytes += len;
+ cache[cacheBytes++] = '\n';
+ cache[cacheBytes] = 0;
+ }
+#else
+ char tmp[12];
+ genTimestamp(tmp, false);
+ SerialRF.print(tmp);
+#endif
#if ENABLE_DATA_OUT
SerialRF.write(buf, len);
- delay(10);
+ SerialRF.println();
#endif
}
- void logData(char c)
+ void logData(const char* buf, byte len)
{
-#if ENABLE_DATA_LOG
- if (c >= ' ') {
- sdfile.write(c);
- dataSize++;
- }
+#if ENABLE_DATA_OUT
+#if STREAM_FORMAT != FORMAT_BIN
+ dispatch(buf, len);
+#endif
+#endif
+ record(buf, len);
+ }
+ void logData(uint16_t pid)
+ {
+ char buf[8];
+ byte len = translatePIDName(pid, buf);
+#if ENABLE_DATA_OUT
+#if STREAM_FORMAT != FORMAT_BIN
+ dispatch(buf, len);
+#endif
#endif
+ record(buf, len);
}
void logData(uint16_t pid, int value)
{
char buf[16];
byte n = translatePIDName(pid, buf);
- byte len = sprintf(buf + n, "%d\r", value) + n;
-#if ENABLE_DATA_OUT
+ byte len = sprintf(buf + n, "%d", value) + n;
#if STREAM_FORMAT == FORMAT_BIN
LOG_DATA_COMM ld = {dataTime, pid, 1, 0, value};
ld.checksum = getChecksum((char*)&ld, 12);
- sendData((const char*)&ld, 12);
+ dispatch((const char*)&ld, 12);
#else
- sendData(buf, len);
-#endif
+ dispatch(buf, len);
#endif
- recordData(buf, len);
+ record(buf, len);
}
void logData(uint16_t pid, int32_t value)
{
char buf[20];
byte n = translatePIDName(pid, buf);
- byte len = sprintf(buf + n, "%ld\r", value) + n;
+ byte len = sprintf(buf + n, "%ld", value) + n;
#if ENABLE_DATA_OUT
#if STREAM_FORMAT == FORMAT_BIN
LOG_DATA_COMM ld = {dataTime, pid, 1, 0, value};
ld.checksum = getChecksum((char*)&ld, 12);
- sendData((const char*)&ld, 12);
+ dispatch((const char*)&ld, 12);
#else
- sendData(buf, len);
+ dispatch(buf, len);
#endif
#endif
- recordData(buf, len);
+ record(buf, len);
}
void logData(uint16_t pid, uint32_t value)
{
char buf[20];
byte n = translatePIDName(pid, buf);
- byte len = sprintf(buf + n, "%lu\r", value) + n;
-#if ENABLE_DATA_OUT
+ byte len = sprintf(buf + n, "%lu", value) + n;
#if STREAM_FORMAT == FORMAT_BIN
LOG_DATA_COMM ld = {dataTime, pid, 1, 0, value};
ld.checksum = getChecksum((char*)&ld, 12);
- sendData((const char*)&ld, 12);
+ dispatch((const char*)&ld, 12);
#else
- sendData(buf, len);
+ dispatch(buf, len);
#endif
-#endif
- recordData(buf, len);
+ record(buf, len);
}
void logData(uint16_t pid, int value1, int value2, int value3)
{
char buf[24];
byte n = translatePIDName(pid, buf);
- byte len = sprintf(buf + n, "%d,%d,%d\r", value1, value2, value3) + n;
-#if ENABLE_DATA_OUT
+ byte len = sprintf(buf + n, "%d,%d,%d", value1, value2, value3) + n;
#if STREAM_FORMAT == FORMAT_BIN
LOG_DATA_COMM ld = {dataTime, pid, 3, 0, {value1, value2, value3}};
ld.checksum = getChecksum((char*)&ld, 20);
- sendData((const char*)&ld, 20);
+ dispatch((const char*)&ld, 20);
#else
- sendData(buf, len);
+ dispatch(buf, len);
#endif
-#endif
- recordData(buf, len);
+ record(buf, len);
}
#if ENABLE_DATA_LOG
uint16_t openFile(uint16_t logFlags = 0, uint32_t dateTime = 0)
@@ -214,6 +253,10 @@ public:
#endif
uint32_t dataTime;
uint32_t dataSize;
+#if ENABLE_DATA_CACHE
+ char cache[MAX_CACHE_SIZE];
+ int cacheBytes;
+#endif
private:
byte getChecksum(char* buffer, byte len)
{
@@ -225,9 +268,9 @@ private:
}
byte translatePIDName(uint16_t pid, char* text)
{
-#if STREAM_FORMAT == FORMAT_TEXT
+#if STREAM_FORMAT == FORMAT_TEXT && USE_FRIENDLY_PID_NAME
for (uint16_t n = 0; n < sizeof(pidNames) / sizeof(pidNames[0]); n++) {
- uint16_t id = pgm_read_word(&pidNames[n].pid);
+ uint16_t id = pgm_read_byte(&pidNames[n].pid);
if (pid == id) {
memcpy_P(text, pidNames[n].name, 3);
text[3] = ',';
@@ -237,7 +280,5 @@ private:
#endif
return sprintf(text, "%X,", pid);
}
-#if ENABLE_DATA_LOG
uint32_t m_lastDataTime;
-#endif
};