diff options
-rw-r--r-- | libraries/OBD/OBD.cpp | 66 | ||||
-rw-r--r-- | libraries/OBD/OBD.h | 33 | ||||
-rw-r--r-- | libraries/OBD/examples/rpm_led/rpm_led.ino | 6 | ||||
-rw-r--r-- | megalogger/megalogger.ino | 26 | ||||
-rw-r--r-- | obdlogger/obdlogger.cbp | 1 | ||||
-rw-r--r-- | obdlogger/obdlogger.ino | 45 | ||||
-rw-r--r-- | samples/dashboard_1602/dashboard_1602.ino | 8 | ||||
-rw-r--r-- | samples/dashboard_4884/dashboard_4884.ino | 46 | ||||
-rw-r--r-- | samples/dashboard_oled/dashboard_oled.ino | 20 | ||||
-rw-r--r-- | samples/obdtest/obdtest.ino | 42 |
10 files changed, 145 insertions, 148 deletions
diff --git a/libraries/OBD/OBD.cpp b/libraries/OBD/OBD.cpp index 55f31e6..5a78de9 100644 --- a/libraries/OBD/OBD.cpp +++ b/libraries/OBD/OBD.cpp @@ -1,7 +1,7 @@ /************************************************************************* -* OBD-II (ELM327) data accessing library for Arduino +* Arduino Library for OBD-II UART Adapter * Distributed under GPL v2.0 -* Copyright (c) 2012 Stanley Huang <stanleyhuangyc@gmail.com> +* Copyright (c) 2012~2013 Stanley Huang <stanleyhuangyc@gmail.com> * All rights reserved. *************************************************************************/ @@ -9,10 +9,9 @@ #include <avr/pgmspace.h> #include "OBD.h" -#define INIT_CMD_COUNT 4 #define MAX_CMD_LEN 6 -const char PROGMEM s_initcmd[INIT_CMD_COUNT][MAX_CMD_LEN] = {"ATZ\r","ATE0\r","ATL1\r","ATI\r"}; +const char PROGMEM s_initcmd[][MAX_CMD_LEN] = {"ATZ\r","ATE0\r","ATL1\r","ATI\r"}; const char PROGMEM s_searching[] = "SEARCHING"; const char PROGMEM s_cmd_fmt[] = "%02X%02X 1\r"; const char PROGMEM s_cmd_sleep[] = "atlp\r"; @@ -60,29 +59,29 @@ unsigned char hex2uint8(const char *p) return c1 << 4 | (c2 & 0xf); } -void COBD::Query(unsigned char pid) +void COBD::sendQuery(unsigned char pid) { char cmd[8]; sprintf_P(cmd, s_cmd_fmt, dataMode, pid); write(cmd); } -bool COBD::ReadSensor(byte pid, int& result, bool passive) +bool COBD::readSensor(byte pid, int& result, bool passive) { // send a query command - Query(pid); + sendQuery(pid); // wait for reponse bool hasData; unsigned long tick = millis(); do { - DataIdleLoop(); + dataIdleLoop(); } while (!(hasData = available()) && millis() - tick < OBD_TIMEOUT_SHORT); if (!hasData) { errors++; return false; } // receive and parse the response - return GetResponseParsed(pid, result); + return getResponseParsed(pid, result); } bool COBD::available() @@ -105,46 +104,46 @@ void COBD::write(const char c) OBDUART.write(c); } -int COBD::GetConvertedValue(byte pid, char* data) +int COBD::normalizeData(byte pid, char* data) { int result; switch (pid) { case PID_RPM: - result = GetLargeValue(data) >> 2; + result = getLargeValue(data) >> 2; break; case PID_FUEL_PRESSURE: - result = GetSmallValue(data) * 3; + result = getSmallValue(data) * 3; break; case PID_COOLANT_TEMP: case PID_INTAKE_TEMP: case PID_AMBIENT_TEMP: - result = GetTemperatureValue(data); + result = getTemperatureValue(data); break; case PID_ABS_ENGINE_LOAD: - result = GetLargeValue(data) * 100 / 255; + result = getLargeValue(data) * 100 / 255; break; case PID_MAF_FLOW: - result = GetLargeValue(data) / 100; + result = getLargeValue(data) / 100; break; case PID_THROTTLE: case PID_ENGINE_LOAD: case PID_FUEL_LEVEL: - result = GetPercentageValue(data); + result = getPercentageValue(data); break; case PID_TIMING_ADVANCE: - result = (GetSmallValue(data) - 128) >> 1; + result = (getSmallValue(data) - 128) >> 1; break; case PID_DISTANCE: case PID_RUNTIME: - result = GetLargeValue(data); + result = getLargeValue(data); break; default: - result = GetSmallValue(data); + result = getSmallValue(data); } return result; } -char* COBD::GetResponse(byte& pid, char* buffer) +char* COBD::getResponse(byte& pid, char* buffer) { unsigned long startTime = millis(); byte i = 0; @@ -174,7 +173,7 @@ char* COBD::GetResponse(byte& pid, char* buffer) errors++; break; } - DataIdleLoop(); + dataIdleLoop(); } } buffer[i] = 0; @@ -194,20 +193,20 @@ char* COBD::GetResponse(byte& pid, char* buffer) return 0; } -bool COBD::GetResponseParsed(byte& pid, int& result) +bool COBD::getResponseParsed(byte& pid, int& result) { char buffer[OBD_RECV_BUF_SIZE]; - char* data = GetResponse(pid, buffer); + char* data = getResponse(pid, buffer); if (!data) { // try recover next time write('\r'); return false; } - result = GetConvertedValue(pid, data); + result = normalizeData(pid, data); return true; } -void COBD::Sleep(int seconds) +void COBD::sleep(int seconds) { char cmd[MAX_CMD_LEN]; strcpy_P(cmd, s_cmd_sleep); @@ -218,7 +217,7 @@ void COBD::Sleep(int seconds) } } -bool COBD::IsValidPID(byte pid) +bool COBD::isValidPID(byte pid) { if (pid >= 0x7f) return false; @@ -228,14 +227,19 @@ bool COBD::IsValidPID(byte pid) return pidmap[i] & b; } -bool COBD::Init(bool passive) +void COBD::begin(int baudrate) +{ + OBDUART.begin(baudrate); +} + +bool COBD::init(bool passive) { unsigned long currentMillis; unsigned char n; char prompted; char buffer[OBD_RECV_BUF_SIZE]; - for (unsigned char i = 0; i < INIT_CMD_COUNT; i++) { + for (unsigned char i = 0; i < sizeof(s_initcmd) / sizeof(s_initcmd[0]); i++) { if (!passive) { char cmd[MAX_CMD_LEN]; strcpy_P(cmd, s_initcmd[i]); @@ -262,7 +266,7 @@ bool COBD::Init(bool passive) //WriteData("\r"); return false; } - InitIdleLoop(); + initIdleLoop(); } } } @@ -271,8 +275,8 @@ bool COBD::Init(bool passive) memset(pidmap, 0, sizeof(pidmap)); for (byte i = 0; i < 4; i++) { byte pid = i * 0x20; - Query(pid); - char* data = GetResponse(pid, buffer); + sendQuery(pid); + char* data = getResponse(pid, buffer); if (!data) break; data--; for (byte n = 0; n < 4; n++) { diff --git a/libraries/OBD/OBD.h b/libraries/OBD/OBD.h index e97ab67..144d0a9 100644 --- a/libraries/OBD/OBD.h +++ b/libraries/OBD/OBD.h @@ -1,7 +1,7 @@ /************************************************************************* -* OBD-II (ELM327) data accessing library for Arduino +* Arduino Library for OBD-II UART Adapter * Distributed under GPL v2.0 -* Copyright (c) 2012 Stanley Huang <stanleyhuangyc@gmail.com> +* Copyright (c) 2012~2013 Stanley Huang <stanleyhuangyc@gmail.com> * All rights reserved. *************************************************************************/ @@ -44,32 +44,33 @@ class COBD { public: COBD():dataMode(1),errors(0) {} - bool Init(bool passive = false); - bool ReadSensor(byte pid, int& result, bool passive = false); - bool IsValidPID(byte pid); - void Sleep(int seconds); + void begin(int baudrate = OBD_SERIAL_BAUDRATE); + bool init(bool passive = false); + bool readSensor(byte pid, int& result, bool passive = false); + bool isValidPID(byte pid); + void sleep(int seconds); // Query and GetResponse for advanced usage only - void Query(byte pid); - char* GetResponse(byte& pid, char* buffer); - bool GetResponseParsed(byte& pid, int& result); + void sendQuery(byte pid); + char* getResponse(byte& pid, char* buffer); + bool getResponseParsed(byte& pid, int& result); byte dataMode; byte errors; //char recvBuf[OBD_RECV_BUF_SIZE]; protected: - static int GetConvertedValue(byte pid, char* data); - static int GetPercentageValue(char* data) + static int normalizeData(byte pid, char* data); + static int getPercentageValue(char* data) { return (int)hex2uint8(data) * 100 / 255; } - static int GetLargeValue(char* data) + static int getLargeValue(char* data) { return hex2uint16(data); } - static int GetSmallValue(char* data) + static int getSmallValue(char* data) { return hex2uint8(data); } - static int GetTemperatureValue(char* data) + static int getTemperatureValue(char* data) { return (int)hex2uint8(data) - 40; } @@ -77,7 +78,7 @@ protected: virtual char read(); virtual void write(const char* s); virtual void write(const char c); - virtual void InitIdleLoop() {} - virtual void DataIdleLoop() {} + virtual void initIdleLoop() {} + virtual void dataIdleLoop() {} byte pidmap[4 * 4]; }; diff --git a/libraries/OBD/examples/rpm_led/rpm_led.ino b/libraries/OBD/examples/rpm_led/rpm_led.ino index c641736..9ebf34f 100644 --- a/libraries/OBD/examples/rpm_led/rpm_led.ino +++ b/libraries/OBD/examples/rpm_led/rpm_led.ino @@ -15,15 +15,15 @@ void setup() // we'll use the debug LED as output pinMode(13, OUTPUT); // start serial communication at the adapter defined baudrate - OBDUART.begin(OBD_SERIAL_BAUDRATE); + obd.begin(); // initiate OBD-II connection until success - while (!obd.Init()); + while (!obd.init()); } void loop() { int value; - if (obd.ReadSensor(PID_RPM, value)) { + if (obd.readSensor(PID_RPM, value)) { // RPM is read and stored in 'value' // light on LED when RPM exceeds 5000 digitalWrite(13, value > 5000 ? HIGH : LOW); diff --git a/megalogger/megalogger.ino b/megalogger/megalogger.ino index ffcb41d..6620f7d 100644 --- a/megalogger/megalogger.ino +++ b/megalogger/megalogger.ino @@ -121,7 +121,7 @@ public: do { ShowStates(); - } while (!Init()); + } while (!init()); state |= STATE_OBD_READY; @@ -134,7 +134,7 @@ public: ShowECUCap(); delay(3000); - ReadSensor(PID_DISTANCE, startDistance); + readSensor(PID_DISTANCE, startDistance); // open file for logging if (!(state & STATE_SD_READY)) { @@ -285,7 +285,7 @@ public: return true; } private: - void InitIdleLoop() + void initIdleLoop() { // called while initializing char buf[10]; @@ -319,7 +319,7 @@ private: #endif } #ifdef GPSUART - void DataIdleLoop() + void dataIdleLoop() { if (lastDataTime && GPSUART.available()) ProcessGPS(); @@ -423,11 +423,11 @@ private: uint32_t start = millis(); // send a query to OBD adapter for specified OBD-II pid - Query(pid); + sendQuery(pid); // wait for reponse bool hasData; do { - DataIdleLoop(); + dataIdleLoop(); } while (!(hasData = available()) && millis() - start < OBD_TIMEOUT_SHORT); // no need to continue if no data available if (!hasData) { @@ -440,7 +440,7 @@ private: // get response from OBD adapter pid = 0; - char* data = GetResponse(pid, buffer); + char* data = getResponse(pid, buffer); if (!data) { // try recover next time write('\r'); @@ -450,7 +450,7 @@ private: uint32_t dataTime = millis(); // convert raw data to normal value - value = GetConvertedValue(pid, data); + value = normalizeData(pid, data); lastPID = pid; lastData = value; @@ -479,7 +479,7 @@ private: // if OBD response is very fast, go on processing other data for a while #ifdef OBD_MIN_INTERVAL while (millis() - start < OBD_MIN_INTERVAL) { - DataIdleLoop(); + dataIdleLoop(); } #endif } @@ -492,7 +492,7 @@ private: for (byte i = 0; i < sizeof(pidlist) / sizeof(pidlist[0]); i++) { lcd.setCursor(160, i * 2 + 4); lcd.print(namelist[i]); - lcd.draw(IsValidPID(PID_RPM) ? tick : cross, 304, i * 16 + 32, 16, 16); + lcd.draw(isValidPID(PID_RPM) ? tick : cross, 304, i * 16 + 32, 16, 16); } } void Reconnect() @@ -502,7 +502,7 @@ private: lcd.print("Reconnecting..."); state &= ~(STATE_OBD_READY | STATE_ACC_READY | STATE_DATE_SAVED); //digitalWrite(SD_CS_PIN, LOW); - for (int i = 0; !Init(); i++) { + for (int i = 0; !init(); i++) { if (i == 10) lcd.clear(); } fileIndex++; @@ -636,8 +636,8 @@ void setup() #ifdef CONSOLE CONSOLE.begin(115200); #endif - // start serial communication at the adapter defined baudrate - OBDUART.begin(OBD_SERIAL_BAUDRATE); + + logger.begin(); #ifdef GPSUART #ifdef GPS_OPEN_BAUDRATE GPSUART.begin(GPS_OPEN_BAUDRATE); diff --git a/obdlogger/obdlogger.cbp b/obdlogger/obdlogger.cbp index 91b4fe8..65bb8d5 100644 --- a/obdlogger/obdlogger.cbp +++ b/obdlogger/obdlogger.cbp @@ -582,6 +582,7 @@ <Compiler> <Add directory="." /> </Compiler> + <Unit filename="images.h" /> <Unit filename="obdlogger.ino"> <Option compile="1" /> <Option link="1" /> diff --git a/obdlogger/obdlogger.ino b/obdlogger/obdlogger.ino index 0bb86d9..3c0ac65 100644 --- a/obdlogger/obdlogger.ino +++ b/obdlogger/obdlogger.ino @@ -7,7 +7,6 @@ #include <Arduino.h> #include <Wire.h> -#include <SoftwareSerial.h> #include "OBD.h" #include "SD.h" #include "MultiLCD.h" @@ -25,7 +24,7 @@ /************************************** * Config GPS here **************************************/ -#define USE_GPS +//#define USE_GPS #define GPS_BAUDRATE 4800 /* bps */ //#define GPS_OPEN_BAUDRATE 4800 /* bps */ @@ -40,10 +39,10 @@ /************************************** * Other options **************************************/ -#define USE_MPU6050 +//#define USE_MPU6050 #define OBD_MIN_INTERVAL 50 /* ms */ #define GPS_DATA_TIMEOUT 2000 /* ms */ -#define ENABLE_DATA_OUT +//#define ENABLE_DATA_OUT // logger states #define STATE_SD_READY 0x1 @@ -105,7 +104,9 @@ typedef struct { } data;*/ } LOG_DATA; /////////////////////////////////////////////////////////////////////////// +#include <SoftwareSerial.h> SoftwareSerial softSerial(9, 10); + #endif // SD card @@ -172,7 +173,7 @@ public: do { ShowStates(); - } while (!Init()); + } while (!init()); state |= STATE_OBD_READY; @@ -181,7 +182,7 @@ public: ShowECUCap(); delay(3000); - ReadSensor(PID_DISTANCE, startDistance); + readSensor(PID_DISTANCE, startDistance); // open file for logging if (!(state & STATE_SD_READY)) { @@ -221,9 +222,11 @@ public: #endif LogData(PID_THROTTLE); +#ifdef USE_MPU6050 if (state & STATE_ACC_READY) { ProcessAccelerometer(); } +#endif switch (count++) { case 0: @@ -321,7 +324,7 @@ public: return true; } private: - void InitIdleLoop() + void initIdleLoop() { // called while initializing char buf[10]; @@ -380,7 +383,7 @@ private: #endif } #ifdef GPSUART - void DataIdleLoop() + void dataIdleLoop() { if (GPSUART.available()) ProcessGPS(); @@ -465,9 +468,9 @@ private: lastGPSDataTime = dataTime; } #endif +#ifdef USE_MPU6050 void ProcessAccelerometer() { -#ifdef USE_MPU6050 accel_t_gyro_union data; MPU6050_readout(&data); uint32_t dataTime = millis(); @@ -486,8 +489,8 @@ private: len = sprintf(buf, "%u,F11,%d %d %d\n", data.value.x_gyro, data.value.y_gyro, data.value.z_gyro); sdfile.write((uint8_t*)buf, len); lastDataTime = dataTime; -#endif } +#endif void LogData(byte pid) { char buffer[OBD_RECV_BUF_SIZE]; @@ -495,11 +498,11 @@ private: uint32_t start = millis(); // send a query to OBD adapter for specified OBD-II pid - Query(pid); + sendQuery(pid); // wait for reponse bool hasData; do { - DataIdleLoop(); + dataIdleLoop(); } while (!(hasData = available()) && millis() - start < OBD_TIMEOUT_SHORT); // no need to continue if no data available if (!hasData) { @@ -512,7 +515,7 @@ private: // get response from OBD adapter pid = 0; - char* data = GetResponse(pid, buffer); + char* data = getResponse(pid, buffer); if (!data) { // try recover next time write('\r'); @@ -522,7 +525,7 @@ private: uint32_t dataTime = millis(); // convert raw data to normal value - value = GetConvertedValue(pid, data); + value = normalizeData(pid, data); #ifdef ENABLE_DATA_OUT SendData(dataTime, 0x0100 | pid, (float)value); @@ -555,10 +558,11 @@ private: // if OBD response is very fast, go on processing other data for a while #ifdef OBD_MIN_INTERVAL while (millis() - start < OBD_MIN_INTERVAL) { - DataIdleLoop(); + dataIdleLoop(); } #endif } +#ifdef ENABLE_DATA_OUT void SendData(uint32_t time, uint16_t pid, float value) { LOG_DATA ld = {time, pid, 0, 1}; @@ -570,6 +574,7 @@ private: ld.checksum = checksum; softSerial.write((uint8_t*)&ld, sizeof(LOG_DATA)); } +#endif void ShowECUCap() { char buffer[24]; @@ -580,12 +585,12 @@ private: lcd.setFont(FONT_SIZE_SMALL); for (; i < sizeof(pidlist) / sizeof(pidlist[0]) / 2; i++) { lcd.setCursor(0, i); - sprintf(buffer, "%s:%c", namelist[i], IsValidPID(pidlist[i]) ? 'Y' : 'N'); + sprintf(buffer, "%s:%c", namelist[i], isValidPID(pidlist[i]) ? 'Y' : 'N'); lcd.print(buffer); } for (byte row = 0; i < sizeof(pidlist) / sizeof(pidlist[0]); i++, row++) { lcd.setCursor(64, row); - sprintf(buffer, "%s:%c", namelist[i], IsValidPID(pidlist[i]) ? 'Y' : 'N'); + sprintf(buffer, "%s:%c", namelist[i], isValidPID(pidlist[i]) ? 'Y' : 'N'); lcd.print(buffer); } } @@ -597,7 +602,7 @@ private: lcd.print("Reconnecting"); state &= ~(STATE_OBD_READY | STATE_ACC_READY | STATE_DATE_SAVED); //digitalWrite(SD_CS_PIN, LOW); - for (int i = 0; !Init(); i++) { + for (int i = 0; !init(); i++) { if (i == 10) lcd.clear(); } fileIndex++; @@ -731,12 +736,12 @@ void setup() lcd.setCursor(0, 1); lcd.print("Initializing..."); + logger.begin(); + #ifdef ENABLE_DATA_OUT softSerial.begin(9600); #endif // ENABLE_DATA_OUT - // start serial communication at the adapter defined baudrate - OBDUART.begin(OBD_SERIAL_BAUDRATE); #ifdef GPSUART #ifdef GPS_OPEN_BAUDRATE GPSUART.begin(GPS_OPEN_BAUDRATE); diff --git a/samples/dashboard_1602/dashboard_1602.ino b/samples/dashboard_1602/dashboard_1602.ino index cfabbe9..848800e 100644 --- a/samples/dashboard_1602/dashboard_1602.ino +++ b/samples/dashboard_1602/dashboard_1602.ino @@ -26,7 +26,7 @@ uint8_t modes[2] = {0, 2}; const char modePids[] = {PID_RPM, PID_SPEED, PID_THROTTLE, PID_ENGINE_LOAD, PID_COOLANT_TEMP, PID_INTAKE_TEMP, PID_AMBIENT_TEMP, PID_MAF_FLOW, - PID_ABS_ENGINE_LOAD, PID_FUEL_PRESSURE, PID_INTAKE_PRESSURE, PID_BAROMETRIC, + PID_ABS_ENGINE_LOAD, PID_FUEL_PRESSURE, PID_INTAKE_MAP, PID_BAROMETRIC, PID_TIMING_ADVANCE, PID_FUEL_LEVEL, PID_RUNTIME, PID_DISTANCE}; const char* modeLabels[] = { @@ -73,7 +73,7 @@ bool showData(int index) uint8_t mode = modes[index]; uint8_t pid = modePids[mode]; digitalWrite(13, HIGH); // set the LED on - if (!obd.ReadSensor(pid, value)) { + if (!obd.readSensor(pid, value)) { // display received data on error lcd.cursorTo(index + 1, 0); lcd.printIn("Error"); @@ -99,7 +99,7 @@ bool setupConnection() char buf[16]; lcd.clear(); lcd.printIn("Connecting..."); - while (!obd.Init()) { + while (!obd.init()) { lcd.cursorTo(2, 0); sprintf(buf, "Attempts #%d", ++errors); lcd.printIn(buf); @@ -115,7 +115,7 @@ void setup() { pinMode(13, OUTPUT); //we'll use the debug LED to output a heartbeat lcd.init(); - OBDUART.begin(OBD_SERIAL_BAUDRATE); + obd.begin(); setupConnection(); } diff --git a/samples/dashboard_4884/dashboard_4884.ino b/samples/dashboard_4884/dashboard_4884.ino index 8239b73..6f845e2 100644 --- a/samples/dashboard_4884/dashboard_4884.ino +++ b/samples/dashboard_4884/dashboard_4884.ino @@ -10,14 +10,6 @@ #include "OBD.h" #include "LCD4884.h" -// the following line toggles between hardware serial and software serial -// #define USE_SOFTSERIAL - -#ifdef USE_SOFTSERIAL -#include <SoftwareSerial.h> -SoftwareSerial mySerial(11, 12); // RX, TX -#endif - //keypad debounce parameter #define DEBOUNCE_MAX 15 #define DEBOUNCE_ON 10 @@ -168,9 +160,10 @@ class COBDDash : public COBD public: void Connect() { + int value; lcd.LCD_clear(); lcd.LCD_write_string(0, 0, "Connecting..", MENU_NORMAL); - for (int n = 0; !Init(); n++) { + for (int n = 0; !init(); n++) { lcd.LCD_putchar('.'); if (n == 3) lcd.backlight(OFF); } @@ -178,17 +171,15 @@ public: lcd.backlight(ON); //Turn on the backlight lcd.LCD_clear(); lcd.LCD_write_string(0, 0, "Connected!", MENU_NORMAL); - - int value; lcd.LCD_write_string(0, 1, "Wait ECU start", MENU_NORMAL); do { delay(1000); - } while (!ReadSensor(PID_RPM, value)); + } while (!readSensor(PID_RPM, value)); lcd.LCD_write_string(0, 2, "ECU started ", MENU_NORMAL); lcd.LCD_write_string(0, 3, "Wait ignition ", MENU_NORMAL); do { delay(100); - } while (!ReadSensor(PID_RPM, value) || value == 0); + } while (!readSensor(PID_RPM, value) || value == 0); lcd.LCD_write_string(0, 4, "Engine started", MENU_NORMAL); delay(1000); } @@ -272,49 +263,49 @@ public: private: void DisplayData1() { - if (ReadSensor(PID_RPM, value)) { + if (readSensor(PID_RPM, value)) { ShowRPM(value); } - if (ReadSensor(PID_SPEED, value)) { + if (readSensor(PID_SPEED, value)) { ShowSpeed(value); } - if (ReadSensor(PID_ENGINE_LOAD, value)) { + if (readSensor(PID_ENGINE_LOAD, value)) { ShowEngineLoad(value); } } void DisplayData2() { - if (ReadSensor(PID_RPM, value)) { + if (readSensor(PID_RPM, value)) { ShowRPM(value); } - if (ReadSensor(PID_SPEED, value)) { + if (readSensor(PID_SPEED, value)) { ShowSpeed2(value); } } void DisplayData21() { - if (ReadSensor(PID_COOLANT_TEMP, value)) { + if (readSensor(PID_COOLANT_TEMP, value)) { ShowTemperature(value, 42, 3); } } void DisplayData22() { - if (ReadSensor(PID_INTAKE_TEMP, value)) { + if (readSensor(PID_INTAKE_TEMP, value)) { ShowTemperature(value, 42, 4); } } void DisplayData23() { - if (ReadSensor(PID_AMBIENT_TEMP, value)) { + if (readSensor(PID_AMBIENT_TEMP, value)) { ShowTemperature(value, 42, 5); } } void DisplayData3() { - if (ReadSensor(PID_SPEED, value)) { + if (readSensor(PID_SPEED, value)) { ShowSpeed2(value); } - if (ReadSensor(PID_INTAKE_PRESSURE, value)) { + if (readSensor(PID_INTAKE_MAP, value)) { char buf[8]; sprintf(buf, "%3u", value); lcd.LCD_write_string(24, 4, buf, MENU_NORMAL); @@ -323,7 +314,7 @@ private: sprintf(buf, "%d.%02d", boost / 100, boost % 100); lcd.LCD_write_string_big(0, 0, buf, MENU_NORMAL); } - if (ReadSensor(PID_FUEL_PRESSURE, value)) { + if (readSensor(PID_FUEL_PRESSURE, value)) { char buf[8]; sprintf(buf, "%3u", value); lcd.LCD_write_string(24, 5, buf, MENU_NORMAL); @@ -446,12 +437,7 @@ void setup() pinMode(13, OUTPUT); -#ifndef USE_SOFTSERIAL - OBDUART.begin(OBD_SERIAL_BAUDRATE); -#else - Serial.begin(9600); - mySerial.begin(OBD_SERIAL_BAUDRATE); -#endif + obd.begin(); } // Timer2 interrupt routine - diff --git a/samples/dashboard_oled/dashboard_oled.ino b/samples/dashboard_oled/dashboard_oled.ino index 8ee8a9c..653a006 100644 --- a/samples/dashboard_oled/dashboard_oled.ino +++ b/samples/dashboard_oled/dashboard_oled.ino @@ -35,7 +35,7 @@ public: char buf[16]; InitScreen(); - for (int n = 1; !Init(); n++) { + for (int n = 1; !init(); n++) { sprintf(buf, "Connecting [%d]", n); if (n <= 20) DisplayString(buf); @@ -48,14 +48,14 @@ public: DisplayString("Wait ECU start", 0 , 2); do { delay(500); - } while (!ReadSensor(PID_RPM, value)); + } while (!readSensor(PID_RPM, value)); DisplayString("ECU started ", 0 , 4); delay(500); DisplayString("Wait ignition ", 0 , 6); delay(500); do { delay(500); - } while (!ReadSensor(PID_RPM, value) || value == 0); + } while (!readSensor(PID_RPM, value) || value == 0); DisplayString("Engine started!", 0 , 6); delay(1000); } @@ -71,35 +71,35 @@ public: if (count == 0) { DisplayString("AIR: ", 0, 6); - if (ReadSensor(PID_INTAKE_TEMP, value)) { + if (readSensor(PID_INTAKE_TEMP, value)) { sprintf(buf, "%4dC", value); DisplayString(buf, 11 * 8, 6); } } else if (count == LOOP_COUNT / 2) { DisplayString("ENGINE: ", 0, 6); - if (ReadSensor(PID_COOLANT_TEMP, value)) { + if (readSensor(PID_COOLANT_TEMP, value)) { sprintf(buf, "%4dC", value); DisplayString(buf, 11 * 8, 6); } } if (count < LOOP_COUNT / 2) { - if (ReadSensor(PID_INTAKE_PRESSURE, value)) { + if (readSensor(PID_INTAKE_MAP, value)) { sprintf(buf, "%dkPa ", value); DisplayString(buf, 5 * 8, 6); } } else { - if (ReadSensor(PID_ENGINE_LOAD, value)) { + if (readSensor(PID_ENGINE_LOAD, value)) { sprintf(buf, "%d%% ", value); DisplayString(buf, 8 * 8, 6); } } - if (ReadSensor(PID_RPM, value)) { + if (readSensor(PID_RPM, value)) { sprintf(buf, "%4d", value); DisplayLargeNumber(buf, 16, 0); } - if (ReadSensor(PID_SPEED, value)) { + if (readSensor(PID_SPEED, value)) { sprintf(buf, "%3d", value); DisplayLargeNumber(buf, 32, 3); } @@ -153,6 +153,6 @@ void loop() void setup() { - OBDUART.begin(OBD_SERIAL_BAUDRATE); + dash.begin(); } diff --git a/samples/obdtest/obdtest.ino b/samples/obdtest/obdtest.ino index 772ef40..b04ac64 100644 --- a/samples/obdtest/obdtest.ino +++ b/samples/obdtest/obdtest.ino @@ -52,11 +52,12 @@ void ShowGPSData() char buf[32]; unsigned long fix_age; + lcd.setFont(FONT_SIZE_SMALL); if (lcd.getLines() > 2) { unsigned long date, time; gps.get_datetime(&date, &time, &fix_age); sprintf(buf, "TIME: %08ld", time); - lcd.setCursor(0, 2); + lcd.setCursor(0, 6); lcd.print(buf); } @@ -64,7 +65,7 @@ void ShowGPSData() long lat, lon; gps.get_position(&lat, &lon, &fix_age); // display LAT/LON if screen is big enough - lcd.setCursor(0, 3); + lcd.setCursor(0, 7); if (((unsigned int)millis() / 1000) & 1) sprintf(buf, "LAT: %d.%5ld ", (int)(lat / 100000), lat % 100000); else @@ -82,7 +83,7 @@ public: unsigned long currentMillis; unsigned char n; char prompted; - char buffer[OBD_RECV_BUF_SIZE]; + char buffer[128]; for (unsigned char i = 0; i < INIT_CMD_COUNT; i++) { lcd.clear(); @@ -98,14 +99,13 @@ public: if (c == '>') { buffer[n] = 0; prompted++; - } else if (n < OBD_RECV_BUF_SIZE - 1) { + } else if (n < sizeof(buffer) - 1) { buffer[n++] = c; if (c == '\n') lcd.changeLine(); else if (c >= ' ') { lcd.write(c); - delay(5); } } } else if (prompted) { @@ -133,8 +133,8 @@ public: sprintf(buffer, "PIDs [%02x-%02x]", i * 0x20 + 1, i * 0x20 + 0x20); lcd.print(buffer); byte pid = i * 0x20; - Query(pid); - data = GetResponse(pid, buffer); + sendQuery(pid); + data = getResponse(pid, buffer); if (!data) break; lcd.setCursor(0, 1); lcd.print(data); @@ -182,7 +182,7 @@ public: lcd.print(buf); dataMode = (byte)(pid >> 8); - Query((byte)pid); + sendQuery((byte)pid); } void Recover() { @@ -217,24 +217,23 @@ public: // check OBD response buffer[0] = 0; byte curpid = (byte)pid; - char* data = GetResponse(curpid, buffer); + char* data = getResponse(curpid, buffer); lcd.setCursor(6, 0); if (!data) { - lcd.print("Data Error"); + lcd.setCursor(0, 2); + lcd.print("Error"); // try recover next time Recover(); } else { - if (!hasMPU6050) { char *p = buffer; while (*p && *p < ' ') p++; for (char *q = p; *q; q++) { if (*q < ' ') *q = ' '; - } - lcd.setCursor(0, 1); - lcd.print(p); } - sprintf(buffer, "=%d", GetConvertedValue(curpid, data)); - lcd.setCursor(6, 0); + lcd.setCursor(0, 2); + lcd.print(p); + lcd.setCursor(7 * 9, 0); + sprintf(buffer, "%d", normalizeData(curpid, data)); lcd.print(buffer); } } @@ -266,9 +265,9 @@ void testMPU6050() // there is no filter enabled, and the values // are not very stable. - lcd.setCursor(0, 1); error = MPU6050_read (MPU6050_ACCEL_XOUT_H, (uint8_t *) &accel_t_gyro, sizeof(accel_t_gyro)); if (error != 0) { + lcd.setCursor(0, 4); lcd.print("MPU6050 N/A"); return; } @@ -300,7 +299,7 @@ void testMPU6050() accel_t_gyro.value.y_accel / 128, accel_t_gyro.value.z_accel / 128); //Serial.println(buf); - lcd.setCursor(0, 1); + lcd.setCursor(0, 4); lcd.print(buf); } @@ -314,12 +313,12 @@ void ShowECUCap() lcd.setFont(FONT_SIZE_SMALL); for (; i < sizeof(pidlist) / sizeof(pidlist[0]) / 2; i++) { lcd.setCursor(0, i); - sprintf(buffer, "%s:%c", namelist[i], obd.IsValidPID(pidlist[i]) ? 'Y' : 'N'); + sprintf(buffer, "%s:%c", namelist[i], obd.isValidPID(pidlist[i]) ? 'Y' : 'N'); lcd.print(buffer); } for (byte row = 0; i < sizeof(pidlist) / sizeof(pidlist[0]); i++, row++) { lcd.setCursor(64, row); - sprintf(buffer, "%s:%c", namelist[i], obd.IsValidPID(pidlist[i]) ? 'Y' : 'N'); + sprintf(buffer, "%s:%c", namelist[i], obd.isValidPID(pidlist[i]) ? 'Y' : 'N'); lcd.print(buffer); } } @@ -345,7 +344,7 @@ void setup() do { testMPU6050(); delay(100); - } while (millis() - t <= 10000); + } while (millis() - t <= 1000); } delay(1000); @@ -364,6 +363,7 @@ void setup() delay(500); } while(!obd.Init()); + ShowECUCap(); delay(3000); lcd.clear(); //query(); |