From 17abc0d3cf9b226b06e5d62790a8d5e576846571 Mon Sep 17 00:00:00 2001 From: Stanley Huang Date: Thu, 27 Jun 2013 14:42:45 +0800 Subject: update library API --- libraries/OBD/OBD.cpp | 66 ++++++++++++++++-------------- libraries/OBD/OBD.h | 33 +++++++-------- libraries/OBD/examples/rpm_led/rpm_led.ino | 6 +-- megalogger/megalogger.ino | 26 ++++++------ obdlogger/obdlogger.cbp | 1 + obdlogger/obdlogger.ino | 45 +++++++++++--------- samples/dashboard_1602/dashboard_1602.ino | 8 ++-- samples/dashboard_4884/dashboard_4884.ino | 46 ++++++++------------- samples/dashboard_oled/dashboard_oled.ino | 20 ++++----- 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 +* Copyright (c) 2012~2013 Stanley Huang * All rights reserved. *************************************************************************/ @@ -9,10 +9,9 @@ #include #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 +* Copyright (c) 2012~2013 Stanley Huang * 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 @@ +