From 61c4955f807004623e1daeb2ad5aadba99ef0ed6 Mon Sep 17 00:00:00 2001 From: Stanley Huang Date: Sat, 8 Mar 2014 12:49:39 +0800 Subject: Update OBD library --- libraries/OBD/OBD.cpp | 55 +++++++++++++++++++++------------------------------ libraries/OBD/OBD.h | 14 ++++++------- 2 files changed, 28 insertions(+), 41 deletions(-) diff --git a/libraries/OBD/OBD.cpp b/libraries/OBD/OBD.cpp index b02cea9..c7c6024 100644 --- a/libraries/OBD/OBD.cpp +++ b/libraries/OBD/OBD.cpp @@ -14,9 +14,8 @@ #define MAX_CMD_LEN 6 -const char PROGMEM s_initcmd[][MAX_CMD_LEN] = {"ATZ\r","ATE0\r","ATL1\r","0902\r"}; -const char PROGMEM s_cmd_fmt[] = "%02X%02X 1\r"; -const char PROGMEM s_cmd_sleep[] = "atlp\r"; +const char PROGMEM s_initcmd[][MAX_CMD_LEN] = {"ATZ\r","ATE0\r","ATL1\r"}; +#define STR_CMD_FMT "%02X%02X 1\r" #define STR_SEARCHING "SEARCHING..." unsigned int hex2uint16(const char *p) @@ -65,10 +64,10 @@ unsigned char hex2uint8(const char *p) *************************************************************************/ #include -void COBD::sendQuery(unsigned char pid) +void COBD::sendQuery(byte pid) { char cmd[8]; - sprintf_P(cmd, s_cmd_fmt, dataMode, pid); + sprintf(cmd, STR_CMD_FMT, dataMode, pid); #ifdef DEBUG debugOutput(cmd); #endif @@ -201,15 +200,25 @@ bool COBD::getResult(byte& pid, int& result) return true; } -void COBD::sleep(int seconds) +void COBD::setProtocol(byte h) { - char cmd[MAX_CMD_LEN]; - strcpy_P(cmd, s_cmd_sleep); - write(cmd); - if (seconds) { - delay((unsigned long)seconds << 10); - write('\r'); - } + if (h == -1) { + write("ATSP00\r"); + } else { + char cmd[8]; + sprintf(cmd, "ATSP%d\r", h); + write(cmd); + } +} + +void COBD::sleep() +{ + write("ATLP\r"); +} + +void COBD::wakeup() +{ + write('\r'); } bool COBD::isValidPID(byte pid) @@ -409,26 +418,6 @@ byte COBDI2C::receive(char* buffer, int timeout) return 0; } -bool COBDI2C::btInit(uint16_t baudrate) -{ - return sendCommand(CMD_UART_BEGIN, baudrate / 1200); -} - -bool COBDI2C::btSend(byte* data, byte length) -{ - return sendCommand(CMD_UART_SEND, 0, data, length); -} - -bool COBDI2C::btReceive(byte* buffer, byte bufsize) -{ - if (!sendCommand(CMD_UART_RECV, bufsize)) return false; - memset(buffer, 0, MAX_PAYLOAD_SIZE); - delay(10); - Wire.requestFrom((byte)m_addr, (byte)MAX_PAYLOAD_SIZE, (byte)1); - Wire.readBytes((char*)buffer, MAX_PAYLOAD_SIZE); - return true; -} - bool COBDI2C::gpsQuery(GPS_DATA* gpsdata) { if (!sendCommand(CMD_GPS_QUERY, 0)) return false; diff --git a/libraries/OBD/OBD.h b/libraries/OBD/OBD.h index ba44f20..02d677a 100644 --- a/libraries/OBD/OBD.h +++ b/libraries/OBD/OBD.h @@ -5,6 +5,8 @@ * (C)2012-2014 Stanley Huang *************************************************************************/ +#include + #define OBD_TIMEOUT_SHORT 2000 /* ms */ #define OBD_TIMEOUT_LONG 7000 /* ms */ #define OBD_SERIAL_BAUDRATE 38400 @@ -59,7 +61,9 @@ public: virtual void begin(); virtual bool init(); virtual bool read(byte pid, int& result); - virtual void sleep(int seconds); + virtual void sleep(); + virtual void wakeup(); + virtual void setProtocol(byte h = -1); // Query and GetResponse for advanced usage only virtual void sendQuery(byte pid); virtual bool getResult(byte& pid, int& result); @@ -111,9 +115,6 @@ private: #define CMD_LOAD_OBD_DATA 0x13 #define CMD_GPS_SETUP 0x20 #define CMD_GPS_QUERY 0x22 -#define CMD_UART_BEGIN 0x30 -#define CMD_UART_SEND 0x31 -#define CMD_UART_RECV 0x32 typedef struct { uint16_t age; @@ -145,15 +146,12 @@ public: bool init(); bool read(byte pid, int& result); void write(char* s); + void setProtocol(bool auto, byte h); // Asynchronized access API void setPID(byte pid); void applyPIDs(); void loadData(); uint16_t getData(byte pid, int& result); - // Bluetooth communication API - bool btInit(uint16_t baudrate = 9600); - bool btSend(byte* data, byte length); - bool btReceive(byte* buffer, byte bufsize); // GPS API bool gpsQuery(GPS_DATA* gpsdata); void gpsSetup(uint32_t baudrate, const char* cmds = 0); -- cgit v1.2.3