diff options
author | Stanley Huang <stanleyhuangyc@gmail.com> | 2014-11-10 21:03:19 +1100 |
---|---|---|
committer | Stanley Huang <stanleyhuangyc@gmail.com> | 2014-11-10 21:03:19 +1100 |
commit | e239aebb7bc0fe7be85baaaa9b5e1b342a2a5979 (patch) | |
tree | 0f7c92eba6e7696b390c1cf53ef44b7e70c44e34 /libraries/OBD/OBD.cpp | |
parent | 7d247ca5c378dd1097e34abaf08c711d3c1bc9f0 (diff) | |
download | 2021-arduino-obd-e239aebb7bc0fe7be85baaaa9b5e1b342a2a5979.tar.gz 2021-arduino-obd-e239aebb7bc0fe7be85baaaa9b5e1b342a2a5979.tar.bz2 2021-arduino-obd-e239aebb7bc0fe7be85baaaa9b5e1b342a2a5979.zip |
Update OBD library
Diffstat (limited to 'libraries/OBD/OBD.cpp')
-rw-r--r-- | libraries/OBD/OBD.cpp | 41 |
1 files changed, 24 insertions, 17 deletions
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); } |