diff options
author | Stanley Huang <stanleyhuangyc@gmail.com> | 2013-12-22 01:02:12 +1100 |
---|---|---|
committer | Stanley Huang <stanleyhuangyc@gmail.com> | 2013-12-22 01:02:12 +1100 |
commit | 1f7a0c08ecf9884398bc032a62f021512ecd23f6 (patch) | |
tree | c3026688a2503574cfb6895d914235b255f6d4a2 /obdlogger/datalogger.h | |
parent | c575202e73cbbe86923ebdafc353fad8437ed5a1 (diff) | |
download | 2021-arduino-obd-1f7a0c08ecf9884398bc032a62f021512ecd23f6.tar.gz 2021-arduino-obd-1f7a0c08ecf9884398bc032a62f021512ecd23f6.tar.bz2 2021-arduino-obd-1f7a0c08ecf9884398bc032a62f021512ecd23f6.zip |
Update OBD logger
Diffstat (limited to 'obdlogger/datalogger.h')
-rw-r--r-- | obdlogger/datalogger.h | 128 |
1 files changed, 103 insertions, 25 deletions
diff --git a/obdlogger/datalogger.h b/obdlogger/datalogger.h index ffc6c75..315e803 100644 --- a/obdlogger/datalogger.h +++ b/obdlogger/datalogger.h @@ -1,6 +1,3 @@ -#define FORMAT_BIN 0 -#define FORMAT_CSV 1 - typedef enum { LOG_TYPE_DEFAULT = 0, LOG_TYPE_0_60, @@ -17,6 +14,9 @@ typedef enum { #define FLAG_GPS 0x20 #define FLAG_ACC 0x40 +#define FORMAT_BIN 0 +#define FORMAT_CSV 1 + typedef struct { uint32_t time; uint16_t pid; @@ -87,10 +87,13 @@ typedef struct { #define MSG_FILE_INFO 0x3 #define MSG_FILE_REQUEST 0x4 +#if LOG_FORMAT == FORMAT_BIN #define FILE_NAME_FORMAT "/DAT%05d.LOG" +#else +#define FILE_NAME_FORMAT "/DAT%05d.CSV" +#endif -#if USE_SOFTSERIAL -#if ENABLE_DATA_OUT +#if ENABLE_DATA_OUT && !USE_OBD_BT #if defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__) SoftwareSerial mySerial(A8, A9); /* for BLE Shield on MEGA*/ #elif defined(__AVR_ATmega644P__) @@ -100,20 +103,19 @@ typedef struct { #endif #endif -#define BT_SERIAL mySerial - -#else - -#define BT_SERIAL Serial - -#endif +void btInit(int baudrate); +void btSend(byte* data, byte length); class CDataLogger { public: void initSender() { #if ENABLE_DATA_OUT - BT_SERIAL.begin(9600); +#if USE_OBD_BT + btInit(9600); +#else + mySerial.begin(9600); +#endif #endif #if ENABLE_DATA_LOG && LOG_FORMAT == FORMAT_CSV m_lastDataTime = 0; @@ -138,21 +140,30 @@ public: info.logType = hdr.logType; info.logFlags = hdr.flags; info.checksum = getChecksum((char*)&info, sizeof(info)); - BT_SERIAL.write((uint8_t*)&info, sizeof(info)); +#if USE_OBD_BT + btSend((uint8_t*)&info, sizeof(info)); +#else + mySerial.write((uint8_t*)&info, sizeof(info)); +#endif } void sendCommand(byte message, void* data = 0, byte bytes = 0) { LOG_DATA_COMMAND msg = {0, PID_MESSAGE, message}; if (data) memcpy(msg.data, data, bytes); msg.checksum = getChecksum((char*)&msg, sizeof(msg)); - BT_SERIAL.write((uint8_t*)&msg, sizeof(msg)); +#if USE_OBD_BT + btSend((uint8_t*)&msg, sizeof(msg)); +#else + mySerial.write((uint8_t*)&msg, sizeof(msg)); +#endif } bool receiveCommand(LOG_DATA_COMMAND& msg) { - if (!BT_SERIAL.available()) +#if !USE_OBD_BT + if (!mySerial.available()) return false; - if (BT_SERIAL.readBytes((char*)&msg, sizeof(msg)) != sizeof(msg)) + if (mySerial.readBytes((char*)&msg, sizeof(msg)) != sizeof(msg)) return false; uint8_t checksum = msg.checksum; @@ -160,15 +171,46 @@ public: if (getChecksum((char*)&msg, sizeof(msg)) != msg.checksum) { return false; } +#endif return true; } #endif + void logData(uint16_t pid, int value) + { + LOG_DATA_COMM ld = {dataTime, pid, 1, 0, value}; + ld.checksum = getChecksum((char*)&ld, 12); +#if ENABLE_DATA_OUT +#if USE_OBD_BT + btSend((uint8_t*)&ld, 12); +#else + mySerial.write((uint8_t*)&ld, 12); +#endif +#endif +#if ENABLE_DATA_LOG +#if LOG_FORMAT == FORMAT_BIN + sdfile.write((uint8_t*)&ld, 12); + dataSize += 12; +#else + dataSize += sdfile.print(dataTime - m_lastDataTime); + dataSize += sdfile.write(','); + dataSize += sdfile.print(pid, HEX); + dataSize += sdfile.write(','); + dataSize += sdfile.print(value); + dataSize += sdfile.write('\n'); + m_lastDataTime = dataTime; +#endif +#endif + } void logData(uint16_t pid, float value) { LOG_DATA_COMM ld = {dataTime, pid, 1, 0, value}; ld.checksum = getChecksum((char*)&ld, 12); #if ENABLE_DATA_OUT - BT_SERIAL.write((uint8_t*)&ld, 12); +#if USE_OBD_BT + btSend((uint8_t*)&ld, 12); +#else + mySerial.write((uint8_t*)&ld, 12); +#endif #endif #if ENABLE_DATA_LOG #if LOG_FORMAT == FORMAT_BIN @@ -179,7 +221,7 @@ public: dataSize += sdfile.write(','); dataSize += sdfile.print(pid, HEX); dataSize += sdfile.write(','); - dataSize += sdfile.print((int)value); + dataSize += sdfile.print(value); dataSize += sdfile.write('\n'); m_lastDataTime = dataTime; #endif @@ -190,7 +232,39 @@ public: LOG_DATA_COMM ld = {dataTime, pid, 2, 0, {value1, value2}}; ld.checksum = getChecksum((char*)&ld, 16); #if ENABLE_DATA_OUT - BT_SERIAL.write((uint8_t*)&ld, 16); +#if USE_OBD_BT + btSend((uint8_t*)&ld, 16); +#else + mySerial.write((uint8_t*)&ld, 16); +#endif +#endif +#if ENABLE_DATA_LOG +#if LOG_FORMAT == FORMAT_BIN + sdfile.write((uint8_t*)&ld, 16); + dataSize += 16; +#else + dataSize += sdfile.print(dataTime - m_lastDataTime); + dataSize += sdfile.write(','); + dataSize += sdfile.print(pid, HEX); + dataSize += sdfile.write(','); + dataSize += sdfile.print(value1, 6); + dataSize += sdfile.write(' '); + dataSize += sdfile.print(value2, 6); + dataSize += sdfile.write('\n'); + m_lastDataTime = dataTime; +#endif +#endif + } + void logData(uint16_t pid, uint32_t value1, uint32_t value2) + { + LOG_DATA_COMM ld = {dataTime, pid, 2, 0, {value1, value2}}; + ld.checksum = getChecksum((char*)&ld, 16); +#if ENABLE_DATA_OUT +#if USE_OBD_BT + btSend((uint8_t*)&ld, 16); +#else + mySerial.write((uint8_t*)&ld, 16); +#endif #endif #if ENABLE_DATA_LOG #if LOG_FORMAT == FORMAT_BIN @@ -209,12 +283,16 @@ public: #endif #endif } - void logData(uint16_t pid, float value1, float value2, float value3) + void logData(uint16_t pid, int value1, int value2, int value3) { LOG_DATA_COMM ld = {dataTime, pid, 3, 0, {value1, value2, value3}}; ld.checksum = getChecksum((char*)&ld, 20); #if ENABLE_DATA_OUT - BT_SERIAL.write((uint8_t*)&ld, 20); +#if USE_OBD_BT + btSend((uint8_t*)&ld, 20); +#else + mySerial.write((uint8_t*)&ld, 20); +#endif #endif #if ENABLE_DATA_LOG #if LOG_FORMAT == FORMAT_BIN @@ -225,11 +303,11 @@ public: dataSize += sdfile.write(','); dataSize += sdfile.print(pid, HEX); dataSize += sdfile.write(','); - dataSize += sdfile.print((int)value1); + dataSize += sdfile.print(value1); dataSize += sdfile.write(' '); - dataSize += sdfile.print((int)value2); + dataSize += sdfile.print(value2); dataSize += sdfile.write(' '); - dataSize += sdfile.print((int)value3); + dataSize += sdfile.print(value3); dataSize += sdfile.write('\n'); m_lastDataTime = dataTime; #endif |