From e239aebb7bc0fe7be85baaaa9b5e1b342a2a5979 Mon Sep 17 00:00:00 2001 From: Stanley Huang Date: Mon, 10 Nov 2014 21:03:19 +1100 Subject: Update OBD library --- libraries/OBD/OBD.cpp | 41 ++++++++++++++++++++++++----------------- libraries/OBD/OBD.h | 15 ++++++++------- 2 files changed, 32 insertions(+), 24 deletions(-) (limited to 'libraries/OBD') diff --git a/libraries/OBD/OBD.cpp b/libraries/OBD/OBD.cpp index 0e09640..8df7410 100644 --- a/libraries/OBD/OBD.cpp +++ b/libraries/OBD/OBD.cpp @@ -278,17 +278,9 @@ bool COBD::isValidPID(byte pid) return pidmap[i] & b; } -void COBD::begin(unsigned long baudrate) +void COBD::begin() { OBDUART.begin(OBD_SERIAL_BAUDRATE); - if (baudrate) { - OBDUART.print("ATBR1 "); - OBDUART.print(baudrate); - OBDUART.print('\r'); - OBDUART.end(); - delay(100); - OBDUART.begin(baudrate); - } } byte COBD::receive(char* buffer, int timeout) @@ -378,9 +370,20 @@ bool COBD::init(byte protocol) return true; } -void COBD::uninit() +void COBD::end() { m_state = OBD_DISCONNECTED; + OBDUART.end(); +} + +void COBD::setBaudRate(long baudrate) +{ + OBDUART.print("ATBR1 "); + OBDUART.print(baudrate); + OBDUART.print('\r'); + OBDUART.end(); + delay(100); + OBDUART.begin(baudrate); } #ifdef DEBUG @@ -397,14 +400,18 @@ void COBD::debugOutput(const char *s) * OBD-II I2C Adapter *************************************************************************/ -void COBDI2C::begin(byte addr) +void COBDI2C::begin() { - m_addr = addr; Wire.begin(); memset(obdPid, 0, sizeof(obdPid)); memset(obdInfo, 0, sizeof(obdInfo)); } +void COBDI2C::end() +{ + m_state = OBD_DISCONNECTED; +} + bool COBDI2C::init(byte protocol) { m_state = OBD_CONNECTING; @@ -439,7 +446,7 @@ bool COBDI2C::read(byte pid, int& result) void COBDI2C::write(const char* s) { COMMAND_BLOCK cmdblock = {millis(), CMD_SEND_AT_COMMAND}; - Wire.beginTransmission(m_addr); + Wire.beginTransmission(I2C_ADDR); Wire.write((byte*)&cmdblock, sizeof(cmdblock)); Wire.write(s); Wire.endTransmission(); @@ -448,7 +455,7 @@ void COBDI2C::write(const char* s) bool COBDI2C::sendCommand(byte cmd, uint8_t data, byte* payload, byte payloadBytes) { COMMAND_BLOCK cmdblock = {millis(), cmd, data}; - Wire.beginTransmission(m_addr); + Wire.beginTransmission(I2C_ADDR); bool success = Wire.write((byte*)&cmdblock, sizeof(COMMAND_BLOCK)) == sizeof(COMMAND_BLOCK); if (payload) Wire.write(payload, payloadBytes); Wire.endTransmission(); @@ -460,7 +467,7 @@ byte COBDI2C::receive(char* buffer, int timeout) uint32_t start = millis(); byte offset = 0; do { - Wire.requestFrom((byte)m_addr, (byte)MAX_PAYLOAD_SIZE, (byte)1); + Wire.requestFrom((byte)I2C_ADDR, (byte)MAX_PAYLOAD_SIZE, (byte)1); bool hasEnd = false; for (byte i = 0; i < MAX_PAYLOAD_SIZE; i++) { @@ -487,7 +494,7 @@ byte COBDI2C::receive(char* buffer, int timeout) bool COBDI2C::gpsQuery(GPS_DATA* gpsdata) { if (!sendCommand(CMD_GPS_QUERY, 0)) return false; - Wire.requestFrom((byte)m_addr, (byte)MAX_PAYLOAD_SIZE, (byte)1); + Wire.requestFrom((byte)I2C_ADDR, (byte)MAX_PAYLOAD_SIZE, (byte)1); Wire.readBytes((char*)gpsdata, MAX_PAYLOAD_SIZE); return true; } @@ -521,6 +528,6 @@ void COBDI2C::loadData() { sendCommand(CMD_LOAD_OBD_DATA); dataIdleLoop(); - Wire.requestFrom((byte)m_addr, (byte)MAX_PAYLOAD_SIZE, (byte)0); + Wire.requestFrom((byte)I2C_ADDR, (byte)MAX_PAYLOAD_SIZE, (byte)0); Wire.readBytes((char*)obdInfo, MAX_PAYLOAD_SIZE); } diff --git a/libraries/OBD/OBD.h b/libraries/OBD/OBD.h index 0a15e97..02d735d 100644 --- a/libraries/OBD/OBD.h +++ b/libraries/OBD/OBD.h @@ -104,13 +104,15 @@ public: Serial baudrate is only adjustable for Arduino OBD-II Adapters V2 Check out http://freematics.com/pages/products/arduino-obd-adapter */ - virtual void begin(unsigned long baudrate = 0); + virtual void begin(); // initialize OBD-II connection virtual bool init(byte protocol = 0); // un-initialize OBD-II connection - virtual void uninit(); + virtual void end(); + // set serial baud rate + virtual void setBaudRate(long baudrate); // get connection state - OBD_STATES getState() { return m_state; } + virtual OBD_STATES getState() { return m_state; } // read specified OBD-II PID value virtual bool read(byte pid, int& result); // set device into @@ -128,15 +130,13 @@ public: // retrive and parse the response of specifie PID virtual bool getResult(byte& pid, int& result); // determine if the PID is supported - bool isValidPID(byte pid); + virtual bool isValidPID(byte pid); // set current PID mode byte dataMode; // occurrence of errors byte errors; // bit map of supported PIDs byte pidmap[4 * 4]; - // current VIN - byte vin[17]; protected: virtual char* getResponse(byte& pid, char* buffer); virtual byte receive(char* buffer = 0, int timeout = OBD_TIMEOUT_SHORT); @@ -206,7 +206,8 @@ typedef struct { class COBDI2C : public COBD { public: - void begin(byte addr = I2C_ADDR); + void begin(); + void end(); bool init(byte protocol = 0); bool read(byte pid, int& result); void write(const char* s); -- cgit v1.2.3