From 948fac18830154769e8c569bdb04f0f328eb9d64 Mon Sep 17 00:00:00 2001
From: Stanley Huang <stanleyhuangyc@gmail.com>
Date: Mon, 7 Oct 2013 11:19:56 +0800
Subject: add support for the new OBD I2C adapter

---
 libraries/OBD/OBD.h | 83 +++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 61 insertions(+), 22 deletions(-)

(limited to 'libraries/OBD/OBD.h')

diff --git a/libraries/OBD/OBD.h b/libraries/OBD/OBD.h
index 1bdaeb1..f64ea35 100644
--- a/libraries/OBD/OBD.h
+++ b/libraries/OBD/OBD.h
@@ -48,45 +48,84 @@ class COBD
 {
 public:
 	COBD():dataMode(1),errors(0),m_state(OBD_DISCONNECTED) {}
-	void begin();
-	bool init(bool passive = false);
-	bool readSensor(byte pid, int& result, bool passive = false);
-	bool isValidPID(byte pid);
-	void sleep(int seconds);
+	virtual void begin();
+	virtual bool init(bool passive = false);
+	virtual bool readSensor(byte pid, int& result, bool passive = false);
+	virtual void sleep(int seconds);
 	// Query and GetResponse for advanced usage only
-	void sendQuery(byte pid);
-	char* getResponse(byte& pid, char* buffer);
-	bool getResponseParsed(byte& pid, int& result);
+	virtual void sendQuery(byte pid);
+	bool isValidPID(byte pid);
 	byte getState() { return m_state; }
 	byte dataMode;
 	byte errors;
 	byte pidmap[4 * 4];
 	byte vin[17];
-	//char recvBuf[OBD_RECV_BUF_SIZE];
 protected:
-	byte receive(char* buffer);
+	virtual char* getResponse(byte& pid, char* buffer);
+	virtual bool getResponseParsed(byte& pid, int& result);
+	virtual byte receive(char* buffer);
+	virtual bool available();
+	virtual char read();
+	virtual void write(char* s);
+	virtual void write(char c);
+	virtual void dataIdleLoop() {}
 	void debugOutput(const char* s);
-	static int normalizeData(byte pid, char* data);
-	static int getPercentageValue(char* data)
+	int normalizeData(byte pid, char* data);
+	byte m_state;
+private:
+	virtual uint8_t getPercentageValue(char* data)
 	{
-		return (int)hex2uint8(data) * 100 / 255;
+		return (uint16_t)hex2uint8(data) * 100 / 255;
 	}
-	static unsigned int getLargeValue(char* data)
+	virtual uint16_t getLargeValue(char* data)
 	{
 		return hex2uint16(data);
 	}
-	static int getSmallValue(char* data)
+	virtual uint8_t getSmallValue(char* data)
 	{
 		return hex2uint8(data);
 	}
-	static int getTemperatureValue(char* data)
+	virtual int16_t getTemperatureValue(char* data)
 	{
 		return (int)hex2uint8(data) - 40;
 	}
-	virtual bool available();
-	virtual char read();
-	virtual void write(const char* s);
-	virtual void write(const char c);
-	virtual void dataIdleLoop() {}
-	byte m_state;
+};
+
+#define I2C_ADDR 0x62
+
+#define MAX_PAYLOAD_SIZE 32
+
+#define CMD_QUERY_STATUS 0x10
+#define CMD_SEND_COMMAND 0x11
+#define CMD_QUERY_DATA 0x12
+#define CMD_UART_BEGIN 0x13
+#define CMD_UART_SEND 0x14
+#define CMD_UART_RECV 0x15
+
+typedef struct {
+    uint32_t time;
+    uint16_t pid;
+    float value;
+} PID_INFO;
+
+typedef struct {
+    uint16_t time;
+    uint8_t message;
+    uint8_t data;
+} COMMAND_BLOCK;
+
+class COBDI2C : public COBD {
+public:
+    void begin(byte addr = I2C_ADDR);
+    bool init();
+    bool readSensor(byte pid, int& result, bool passive = false);
+    void write(char* s);
+    // Bluetooth communication API
+    bool btInit(uint16_t baudrate = 9600);
+    bool btSend(byte* data, byte length);
+    bool btReceive(byte* buffer, byte bufsize);
+private:
+    bool sendCommand(byte cmd, uint8_t data = 0, byte* payload = 0, byte payloadBytes = 0);
+    byte receive(char* buffer);
+    byte m_addr;
 };
-- 
cgit v1.2.3