diff --git a/Examples/HelloOBD/HelloOBD.ino b/Examples/HelloOBD/HelloOBD.ino new file mode 100644 index 0000000..5dae70a --- /dev/null +++ b/Examples/HelloOBD/HelloOBD.ino @@ -0,0 +1,128 @@ +/************************************************************************* +HelloOBD +A simple example for interfacing with the Freematics Arduino OBD-II +Adapter (Model A). +Copyright (C) 2015 Raghavendra Rao +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. +*************************************************************************/ + +#include +#include +#include +#include + +class CMyOBD : public COBD +{ +public: + CMyOBD() { + u8g = U8GLIB_SH1106_128X64(U8G_I2C_OPT_NONE); + pidRPM = 0; + pidThrottle = 0; + pidSpeed = 0; + pidMAFFlow = 0; + pidIntakeTemp = 0; + bReady = false; + } + + void loop() + { + // read + read(PID_RPM, pidRPM); + read(PID_THROTTLE, pidThrottle); + read(PID_SPEED, pidSpeed); + read(PID_MAF_FLOW, pidMAFFlow); + read(PID_INTAKE_TEMP, pidIntakeTemp); + + if (errors >= 2) { + setup(); + } + } + +private: + U8GLIB_SH1106_128X64 u8g; + int pidRPM, pidThrottle, pidSpeed; + int pidMAFFlow, pidIntakeTemp; + char szStr[50]; + bool bReady; + + void dataIdleLoop() + { + // picture loop + u8g.firstPage(); + do { + draw(); + } while( u8g.nextPage() ); + } + + void checkState() + { + if (errors > 0) + { + bReady = false; + sprintf(szStr, "Connecting"); + for (int i = 0; i <= (errors % 10); i++) + { + szStr[i+10] = '.'; + } + u8g.drawStr(0, 0, szStr); + } + else + { + bReady = true; + sprintf(szStr, "Connected"); + u8g.drawStr(0, 0, szStr); + } + } + + void draw(void) { + u8g_prepare(); + // clear string + memset(szStr, 0, sizeof(szStr)); + // Check & show connection state + checkState(); + // Draw stats + if (bReady) + { + // PID_SPEED + sprintf(szStr, "Speed: %i", pidSpeed); + u8g.drawStr(0, 12, szStr); + // PID_RPM + sprintf(szStr, "RPM: %i", pidRPM); + u8g.drawStr(70, 12, szStr); + // PID_THROTTLE + sprintf(szStr, "Throttle: %i", pidThrottle); + u8g.drawStr(0, 24, szStr); + // PID_MAF_FLOW + sprintf(szStr, "MAF Flow: %i", pidMAFFlow); + u8g.drawStr(0, 36, szStr); + // PID_INTAKE_TEMP + sprintf(szStr, "Intake Temp: %i", pidIntakeTemp); + u8g.drawStr(0, 48, szStr); + } + } + + void u8g_prepare(void) { + u8g.setFont(u8g_font_6x10); + u8g.setFontRefHeightExtendedText(); + u8g.setDefaultForegroundColor(); + u8g.setFontPosTop(); + } +}; + +static CMyOBD myobd; + +void setup(void) { + // start communication with OBD-II UART adapter + myobd.begin(); +} + +void loop(void) { + myobd.loop(); +} diff --git a/Examples/HelloOBD/README b/Examples/HelloOBD/README new file mode 100644 index 0000000..048e60b --- /dev/null +++ b/Examples/HelloOBD/README @@ -0,0 +1,6 @@ +HelloOBD +A simple example for interfacing with the Freematics Arduino OBD-II Adapter (Model A). + +This program improves on the nanologger sketch: +1) Faster screen updates +2) Utilises the U8 graphics library (https://github.com/olikraus/U8glib_Arduino) diff --git a/libraries/OBD/OBD.cpp b/libraries/OBD/OBD.cpp index 4242cf3..ff19260 100644 --- a/libraries/OBD/OBD.cpp +++ b/libraries/OBD/OBD.cpp @@ -81,6 +81,19 @@ bool COBD::read(byte pid, int& result) return getResult(pid, result); } +void COBD::read_raw(byte pid, char* result) +{ + char buffer[OBD_RECV_BUF_SIZE]; + sendQuery(pid); + char* data = getResponse(pid, buffer); + if (!data) { + recover(); + errors++; + return; + } + memcpy(result, buffer, sizeof(char)*OBD_RECV_BUF_SIZE); +} + void COBD::clearDTC() { write("04\r"); diff --git a/libraries/OBD/OBD.h b/libraries/OBD/OBD.h index 90944e9..2951e51 100644 --- a/libraries/OBD/OBD.h +++ b/libraries/OBD/OBD.h @@ -127,6 +127,8 @@ class COBD virtual OBD_STATES getState() { return m_state; } // read specified OBD-II PID value virtual bool read(byte pid, int& result); + // read raw PID value including entire response + virtual void read_raw(byte pid, char* result); // set device into virtual void sleep(); // set working protocol (default auto)