From c5a9fdb80df58015c0705d23e2208cb576affd7c Mon Sep 17 00:00:00 2001 From: Stanley Huang Date: Wed, 6 Jul 2016 14:57:50 +0800 Subject: Renaming read() to readPID() in OBD library --- libraries/OBD/OBD.cpp | 109 +++++++++++++++++---- libraries/OBD/OBD.h | 21 ++-- .../OBD/examples/obd_i2c_test/obd_i2c_test.ino | 6 +- .../OBD/examples/obd_uart_test/obd_uart_test.ino | 4 +- libraries/OBD/examples/rpm_led_i2c/rpm_led_i2c.ino | 2 +- .../OBD/examples/rpm_led_uart/rpm_led_uart.ino | 2 +- libraries/OBD2UART/OBD2UART.cpp | 1 - 7 files changed, 111 insertions(+), 34 deletions(-) (limited to 'libraries') diff --git a/libraries/OBD/OBD.cpp b/libraries/OBD/OBD.cpp index 162ca85..d551f93 100644 --- a/libraries/OBD/OBD.cpp +++ b/libraries/OBD/OBD.cpp @@ -73,7 +73,7 @@ void COBD::sendQuery(byte pid) write(cmd); } -bool COBD::read(byte pid, int& result) +bool COBD::readPID(byte pid, int& result) { // send a query command sendQuery(pid); @@ -81,7 +81,7 @@ bool COBD::read(byte pid, int& result) return getResult(pid, result); } -byte COBD::read(const byte pid[], byte count, int result[]) +byte COBD::readPID(const byte pid[], byte count, int result[]) { // send a multiple query command char buffer[128]; @@ -249,11 +249,26 @@ void COBD::sleep() sendCommand("ATLP\r", buf, sizeof(buf)); } +char* COBD::getResultValue(char* buf) +{ + char* p = buf; + for (;;) { + if (isdigit(*p) || *p == '-') { + return p; + } + p = strchr(p, '\r'); + if (!p) break; + if (*(++p) == '\n') p++; + } + return 0; +} + float COBD::getVoltage() { char buf[32]; if (sendCommand("ATRV\r", buf, sizeof(buf)) > 0) { - return atof(buf); + char* p = getResultValue(buf); + if (p) return atof(p); } return 0; } @@ -295,6 +310,16 @@ void COBD::begin() DEBUG.begin(115200); #endif recover(); + + char buffer[32]; + version = 0; + if (sendCommand("ATI\r", buffer, sizeof(buffer), 200)) { + char *p = strstr(buffer, "OBDUART"); + if (p) { + p += 9; + version = (*p - '0') * 10 + (*(p + 2) - '0'); + } + } } byte COBD::receive(char* buffer, byte bufsize, int timeout) @@ -343,15 +368,6 @@ bool COBD::init(OBD_PROTOCOLS protocol) m_state = OBD_CONNECTING; - write("ATI\r"); - if (receive(buffer, sizeof(buffer), 100)) { - char *p = strstr(buffer, "OBDUART"); - if (p) { - p += 9; - version = (*p - '0') * 10 + (*(p + 2) - '0'); - } - } - for (unsigned char i = 0; i < sizeof(initcmd) / sizeof(initcmd[0]); i++) { #ifdef DEBUG debugOutput(initcmd[i]); @@ -408,6 +424,50 @@ bool COBD::setBaudRate(unsigned long baudrate) } +float COBD::getTemperature() +{ + char buf[32]; + if (sendCommand("ATTEMP\r", buf, sizeof(buf)) > 0) { + char* p = getResultValue(buf); + if (p) return (float)(atoi(p) + 12412) / 340; + } + else { + return -1000; + } +} + +bool COBD::readAccel(int& x, int& y, int& z) +{ + char buf[32]; + if (sendCommand("ATACL\r", buf, sizeof(buf)) > 0) do { + char* p = getResultValue(buf); + if (!p) break; + x = atoi(p++); + if (!(p = strchr(p, ','))) break; + y = atoi(++p); + if (!(p = strchr(p, ','))) break; + z = atoi(++p); + return true; + } while (0); + return false; +} + +bool COBD::readGyro(int& x, int& y, int& z) +{ + char buf[32]; + if (sendCommand("ATGYRO\r", buf, sizeof(buf)) > 0) do { + char* p = getResultValue(buf); + if (!p) break; + x = atoi(p++); + if (!(p = strchr(p, ','))) break; + y = atoi(++p); + if (!(p = strchr(p, ','))) break; + z = atoi(++p); + return true; + } while (0); + return false; +} + #ifdef DEBUG void COBD::debugOutput(const char *s) { @@ -429,6 +489,16 @@ void COBDI2C::begin() DEBUG.begin(115200); #endif recover(); + + char buffer[32]; + version = 0; + if (sendCommand("ATI\r", buffer, sizeof(buffer), 200)) { + char *p = strstr(buffer, "OBDUART"); + if (p) { + p += 9; + version = (*p - '0') * 10 + (*(p + 2) - '0'); + } + } } void COBDI2C::end() @@ -436,7 +506,7 @@ void COBDI2C::end() m_state = OBD_DISCONNECTED; } -bool COBDI2C::read(byte pid, int& result) +bool COBDI2C::readPID(byte pid, int& result) { sendQuery(pid); dataIdleLoop(); @@ -469,7 +539,7 @@ byte COBDI2C::receive(char* buffer, byte bufsize, int timeout) do { Wire.requestFrom((byte)I2C_ADDR, (byte)MAX_PAYLOAD_SIZE, (byte)1); int c = Wire.read(); - if (offset == 0 && (c == 0 || c == -1)) { + if (offset == 0 && c < 0xa) { // data not ready dataIdleLoop(); continue; @@ -492,21 +562,22 @@ byte COBDI2C::receive(char* buffer, byte bufsize, int timeout) } } } while(millis() - start < timeout); + if (buffer) buffer[offset] = 0; return 0; } -byte COBDI2C::read(const byte pid[], byte count, int result[]) +byte COBDI2C::readPID(const byte pid[], byte count, int result[]) { byte results = 0; for (byte n = 0; n < count; n++) { - if (read(pid[n], result[n])) { + if (readPID(pid[n], result[n])) { results++; } } return results; } -void COBDI2C::setPID(byte pid, byte obdPid[]) +void COBDI2C::setQueryPID(byte pid, byte obdPid[]) { byte n = 0; for (; n < MAX_PIDS && obdPid[n]; n++) { @@ -520,13 +591,13 @@ void COBDI2C::setPID(byte pid, byte obdPid[]) obdPid[n] = pid; } -void COBDI2C::applyPIDs(byte obdPid[]) +void COBDI2C::applyQueryPIDs(byte obdPid[]) { sendCommandBlock(CMD_APPLY_OBD_PIDS, 0, (byte*)obdPid, sizeof(obdPid[0])* MAX_PIDS); delay(200); } -void COBDI2C::loadData(PID_INFO obdInfo[]) +void COBDI2C::loadQueryData(PID_INFO obdInfo[]) { sendCommandBlock(CMD_LOAD_OBD_DATA); dataIdleLoop(); diff --git a/libraries/OBD/OBD.h b/libraries/OBD/OBD.h index 8e8185e..4109533 100644 --- a/libraries/OBD/OBD.h +++ b/libraries/OBD/OBD.h @@ -113,9 +113,9 @@ public: // get connection state virtual OBD_STATES getState() { return m_state; } // read specified OBD-II PID value - virtual bool read(byte pid, int& result); + virtual bool readPID(byte pid, int& result); // read multiple (up to 8) OBD-II PID values, return number of values obtained - virtual byte read(const byte pid[], byte count, int result[]); + virtual byte readPID(const byte pid[], byte count, int result[]); // set device into virtual void sleep(); // set working protocol (default auto) @@ -128,6 +128,12 @@ public: virtual float getVoltage(); // get VIN as a string, buffer length should be >= OBD_RECV_BUF_SIZE virtual bool getVIN(char* buffer, byte bufsize); + // get device temperature + virtual float getTemperature(); + // get accelerometer data + virtual bool readAccel(int& x, int& y, int& z); + // get gyroscope data + virtual bool readGyro(int& x, int& y, int& z); // send query for specified PID virtual void sendQuery(byte pid); // retrive and parse the response of specifie PID @@ -170,6 +176,7 @@ private: { return (int)hex2uint8(data) - 40; } + char* getResultValue(char* buf); }; #define I2C_ADDR 0x62 @@ -199,15 +206,15 @@ class COBDI2C : public COBD { public: void begin(); void end(); - bool read(byte pid, int& result); - byte read(const byte pid[], byte count, int result[]); + bool readPID(byte pid, int& result); + byte readPID(const byte pid[], byte count, int result[]); void write(const char* s); // API not applicable bool setBaudRate(unsigned long baudrate) { return false; } // Asynchronized access API - void setPID(byte pid, byte obdPid[]); - void applyPIDs(byte obdPid[]); - void loadData(PID_INFO obdInfo[]); + void setQueryPID(byte pid, byte obdPid[]); + void applyQueryPIDs(byte obdPid[]); + void loadQueryData(PID_INFO obdInfo[]); protected: byte receive(char* buffer, byte bufsize, int timeout = OBD_TIMEOUT_SHORT); bool sendCommandBlock(byte cmd, uint8_t data = 0, byte* payload = 0, byte payloadBytes = 0); diff --git a/libraries/OBD/examples/obd_i2c_test/obd_i2c_test.ino b/libraries/OBD/examples/obd_i2c_test/obd_i2c_test.ino index fe9141a..c5b89fe 100644 --- a/libraries/OBD/examples/obd_i2c_test/obd_i2c_test.ino +++ b/libraries/OBD/examples/obd_i2c_test/obd_i2c_test.ino @@ -74,7 +74,7 @@ void readMEMS() Serial.println(gz); } -void readPID() +void readPIDs() { static const byte pidlist[] = {PID_ENGINE_LOAD, PID_COOLANT_TEMP, PID_RPM, PID_SPEED, PID_TIMING_ADVANCE, PID_INTAKE_TEMP, PID_THROTTLE, PID_FUEL_LEVEL}; Serial.print('['); @@ -87,7 +87,7 @@ void readPID() Serial.print('='); if (valid) { int value; - if (obd.read(pid, value)) { + if (obd.readPID(pid, value)) { Serial.print(value); } } @@ -117,7 +117,7 @@ void setup() { } void loop() { - readPID(); + readPIDs(); readMEMS(); } diff --git a/libraries/OBD/examples/obd_uart_test/obd_uart_test.ino b/libraries/OBD/examples/obd_uart_test/obd_uart_test.ino index 484262e..08106ec 100644 --- a/libraries/OBD/examples/obd_uart_test/obd_uart_test.ino +++ b/libraries/OBD/examples/obd_uart_test/obd_uart_test.ino @@ -55,7 +55,7 @@ void readPIDSingle() mySerial.print(millis()); mySerial.print(']'); mySerial.print("RPM="); - if (obd.read(PID_RPM, value)) { + if (obd.readPID(PID_RPM, value)) { mySerial.print(value); } mySerial.println(); @@ -65,7 +65,7 @@ void readPIDMultiple() { static const byte pids[] = {PID_SPEED, PID_ENGINE_LOAD, PID_THROTTLE, PID_COOLANT_TEMP, PID_INTAKE_TEMP}; int values[sizeof(pids)]; - if (obd.read(pids, sizeof(pids), values) == sizeof(pids)) { + if (obd.readPID(pids, sizeof(pids), values) == sizeof(pids)) { for (byte i = 0; i < sizeof(pids) ; i++) { mySerial.print('['); mySerial.print(millis()); diff --git a/libraries/OBD/examples/rpm_led_i2c/rpm_led_i2c.ino b/libraries/OBD/examples/rpm_led_i2c/rpm_led_i2c.ino index d999159..619ae32 100644 --- a/libraries/OBD/examples/rpm_led_i2c/rpm_led_i2c.ino +++ b/libraries/OBD/examples/rpm_led_i2c/rpm_led_i2c.ino @@ -24,7 +24,7 @@ void setup() void loop() { int value; - if (obd.read(PID_RPM, value)) { + if (obd.readPID(PID_RPM, value)) { // RPM is successfully read and its value stored in variable 'value' // light on LED when RPM exceeds 3000 digitalWrite(13, value > 3000 ? HIGH : LOW); diff --git a/libraries/OBD/examples/rpm_led_uart/rpm_led_uart.ino b/libraries/OBD/examples/rpm_led_uart/rpm_led_uart.ino index 691ee9c..a248307 100644 --- a/libraries/OBD/examples/rpm_led_uart/rpm_led_uart.ino +++ b/libraries/OBD/examples/rpm_led_uart/rpm_led_uart.ino @@ -24,7 +24,7 @@ void setup() void loop() { int value; - if (obd.read(PID_RPM, value)) { + if (obd.readPID(PID_RPM, value)) { // RPM is successfully read and its value stored in variable 'value' // light on LED when RPM exceeds 3000 digitalWrite(13, value > 3000 ? HIGH : LOW); diff --git a/libraries/OBD2UART/OBD2UART.cpp b/libraries/OBD2UART/OBD2UART.cpp index c2a28fe..f756705 100644 --- a/libraries/OBD2UART/OBD2UART.cpp +++ b/libraries/OBD2UART/OBD2UART.cpp @@ -465,7 +465,6 @@ bool COBD::readGyro(int& x, int& y, int& z) return false; } - #ifdef DEBUG void COBD::debugOutput(const char *s) { -- cgit v1.2.3