Small project to display some OBD values from the Toyota GT86/ Subaru BRZ/ Scion FRS on an OLED display.

Dependencies:   Adafruit_GFX MODSERIAL mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
chrta
Date:
Sun Apr 27 19:13:35 2014 +0000
Parent:
4:0e2d6cc31afb
Child:
6:506b703a8acf
Commit message:
Split pids into files.

Changed in this revision

DebugPrint.h Show annotated file Show diff for this revision Revisions of this file
EngineCoolantTemperature.cpp Show annotated file Show diff for this revision Revisions of this file
EngineCoolantTemperature.h Show annotated file Show diff for this revision Revisions of this file
EngineRpm.cpp Show annotated file Show diff for this revision Revisions of this file
EngineRpm.h Show annotated file Show diff for this revision Revisions of this file
OilTemperature.cpp Show annotated file Show diff for this revision Revisions of this file
OilTemperature.h Show annotated file Show diff for this revision Revisions of this file
PidDecoder.cpp Show annotated file Show diff for this revision Revisions of this file
PidValue.cpp Show annotated file Show diff for this revision Revisions of this file
PidValue.h Show annotated file Show diff for this revision Revisions of this file
Throttle.cpp Show annotated file Show diff for this revision Revisions of this file
Throttle.h Show annotated file Show diff for this revision Revisions of this file
VehicleSpeed.cpp Show annotated file Show diff for this revision Revisions of this file
VehicleSpeed.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DebugPrint.h	Sun Apr 27 19:13:35 2014 +0000
@@ -0,0 +1,13 @@
+#ifndef DEBUGPRINT_H
+#define DEBUGPRINT_H
+
+#include "MODSERIAL.h"
+extern MODSERIAL pc;
+
+#ifdef ACTIVATE_DEBUG_OUTPUT
+#define DEBUG_PRINT(format, ...) pc.printf(format, ##__VA_ARGS__) 
+#else
+#define DEBUG_PRINT(format, ...)
+#endif
+
+#endif //DEBUGPRINT_H
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EngineCoolantTemperature.cpp	Sun Apr 27 19:13:35 2014 +0000
@@ -0,0 +1,22 @@
+#include "EngineCoolantTemperature.h"
+
+EngineCoolantTemp::EngineCoolantTemp()
+: PidValue("Engine Coolant Temperature", "deg C")
+{
+}
+
+bool EngineCoolantTemp::decode(const uint8_t* data, uint16_t length)
+{
+    if (length < 2)
+    {
+        return false;
+    }
+    
+    if ((data[1] != 0x05) || length != 3)
+    {
+        return false;
+    }
+    
+    m_value = data[2] - 40; // degree celcius
+    return true;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EngineCoolantTemperature.h	Sun Apr 27 19:13:35 2014 +0000
@@ -0,0 +1,14 @@
+#ifndef ENGINECOOLANTTEMPERATURE_H
+#define ENGINECOOLANTTEMPERATURE_H
+
+#include "PidValue.h"
+
+class EngineCoolantTemp : public PidValue
+{
+public:
+    EngineCoolantTemp();
+    virtual bool decode(const uint8_t* data, uint16_t length);
+};
+
+
+#endif //ENGINECOOLANTTEMPERATURE_H
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EngineRpm.cpp	Sun Apr 27 19:13:35 2014 +0000
@@ -0,0 +1,22 @@
+#include "EngineRpm.h"
+
+EngineRpm::EngineRpm()
+: PidValue("Engine RPM", "rpm")
+{
+}
+
+bool EngineRpm::decode(const uint8_t* data, uint16_t length)
+{
+    if (length < 2)
+    {
+        return false;
+    }
+    
+    if ((data[1] != 0x0C) || length != 4)
+    {
+        return false;
+    }
+    
+    m_value = ((data[2] << 8) | data[3]) >> 2; //rpm
+    return true;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EngineRpm.h	Sun Apr 27 19:13:35 2014 +0000
@@ -0,0 +1,13 @@
+#ifndef ENGINERPM_H
+#define ENGINERPM_H
+
+#include "PidValue.h"
+
+class EngineRpm : public PidValue
+{
+public:
+    EngineRpm();
+    virtual bool decode(const uint8_t* data, uint16_t length);
+};
+
+#endif //ENGINERPM_H
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OilTemperature.cpp	Sun Apr 27 19:13:35 2014 +0000
@@ -0,0 +1,23 @@
+#include "OilTemperature.h"
+
+
+OilTemperature::OilTemperature()
+: PidValue("Oil Temperature", "deg C")
+{
+}
+
+bool OilTemperature::decode(const uint8_t* data, uint16_t length)
+{
+    if (length < 2)
+    {
+        return false;
+    }
+    
+    if ((length != 31) || (data[0] != 0x61) || (data[1] != 0x01))
+    {
+        return false;
+    }
+    
+    m_value = data[30] - 40; // deg C
+    return true;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OilTemperature.h	Sun Apr 27 19:13:35 2014 +0000
@@ -0,0 +1,12 @@
+#ifndef OILTEMPERATURE_H
+#define OILTEMPERATURE_H
+
+#include "PidValue.h"
+
+class OilTemperature : public PidValue
+{
+public:
+    OilTemperature();
+    virtual bool decode(const uint8_t* data, uint16_t length);
+};
+#endif //OILTEMPERATURE_H
--- a/PidDecoder.cpp	Sun Apr 27 17:42:32 2014 +0000
+++ b/PidDecoder.cpp	Sun Apr 27 19:13:35 2014 +0000
@@ -1,180 +1,11 @@
 #include "PidDecoder.h"
-#include "MODSERIAL.h"
-
-extern MODSERIAL pc;
-
-class PidValue
-{
-public:
-    PidValue(const char* name, const char* unit);
-    virtual bool decode(const uint8_t* data, uint16_t length) = 0;
-    void print();
-    const char* getName();
-protected:    
-    unsigned int m_value;
-    const char* m_unit;
-    const char* m_name;
-};
-
-class EngineRpm : public PidValue
-{
-public:
-    EngineRpm();
-    virtual bool decode(const uint8_t* data, uint16_t length);
-};
-
-
-class EngineCoolantTemp : public PidValue
-{
-public:
-    EngineCoolantTemp();
-    virtual bool decode(const uint8_t* data, uint16_t length);
-};
-
-
-class VehicleSpeed : public PidValue
-{
-public:
-    VehicleSpeed();
-    virtual bool decode(const uint8_t* data, uint16_t length);
-};
-
-class Throttle : public PidValue
-{
-public:
-    Throttle();
-    virtual bool decode(const uint8_t* data, uint16_t length);
-};
-
-class OilTemperature : public PidValue
-{
-public:
-    OilTemperature();
-    virtual bool decode(const uint8_t* data, uint16_t length);
-};
-
-PidValue::PidValue(const char* name, const char* unit)
-: m_value(0)
-, m_unit(unit)
-, m_name(name)
-{
-}
-
-void PidValue::print()
-{
-    pc.printf("%d %s\r\n", m_value, m_unit);
-}
-
-const char* PidValue::getName()
-{
-    return m_name;
-}
-
-EngineRpm::EngineRpm()
-: PidValue("Engine RPM", "rpm")
-{
-}
+#include "EngineCoolantTemperature.h"
+#include "EngineRpm.h"
+#include "VehicleSpeed.h"
+#include "Throttle.h"
+#include "OilTemperature.h"
 
-bool EngineRpm::decode(const uint8_t* data, uint16_t length)
-{
-    if (length < 2)
-    {
-        return false;
-    }
-    
-    if ((data[1] != 0x0C) || length != 4)
-    {
-        return false;
-    }
-    
-    m_value = ((data[2] << 8) | data[3]) >> 2; //rpm
-    return true;
-}
-
-EngineCoolantTemp::EngineCoolantTemp()
-: PidValue("Engine Coolant Temperature", "deg C")
-{
-}
-
-bool EngineCoolantTemp::decode(const uint8_t* data, uint16_t length)
-{
-    if (length < 2)
-    {
-        return false;
-    }
-    
-    if ((data[1] != 0x05) || length != 3)
-    {
-        return false;
-    }
-    
-    m_value = data[2] - 40; // degree celcius
-    return true;
-}
-
-VehicleSpeed::VehicleSpeed()
-: PidValue("Speed", "km/h")
-{
-}
-
-bool VehicleSpeed::decode(const uint8_t* data, uint16_t length)
-{
-    if (length < 2)
-    {
-        return false;
-    }
-    
-    if ((data[1] != 0x0D) || length != 3)
-    {
-        return false;
-    }
-    
-    m_value = data[2]; // km/h
-    return true;
-}
-
-Throttle::Throttle()
-: PidValue("Throttle", "%")
-{
-}
-
-bool Throttle::decode(const uint8_t* data, uint16_t length)
-{
-    if (length < 2)
-    {
-        return false;
-    }
-    
-    if ((data[1] != 0x11) || length != 3)
-    {
-        return false;
-    }
-    
-    m_value = data[2] * 100 /  255; // %
-    return true;
-}
-
-
-OilTemperature::OilTemperature()
-: PidValue("Oil Temperature", "deg C")
-{
-}
-
-bool OilTemperature::decode(const uint8_t* data, uint16_t length)
-{
-    if (length < 2)
-    {
-        return false;
-    }
-    
-    if ((length != 31) || (data[0] != 0x61) || (data[1] != 0x01))
-    {
-        return false;
-    }
-    
-    m_value = data[30] - 40; // deg C
-    return true;
-}
+#include "DebugPrint.h"
 
 static VehicleSpeed speed;
 static EngineRpm rpm;
@@ -196,20 +27,3 @@
         }
     }
 }
-
-#if 0
-if((can_MsgRx.id == PID_REPLY) && (can_MsgRx.data[2] == pid))
-        { 
-                            
- 
-                            case MAF_SENSOR:               // ((256*A)+B) / 100  [g/s]
-                                engine_data =  ((can_MsgRx.data[3]*256) + can_MsgRx.data[4])/100;
-                                sprintf(buffer,"%d g/s",(int) engine_data);
-                            
-                            break;
- 
-                            case O2_VOLTAGE:            // A * 0.005   (B-128) * 100/128 (if B==0xFF, sensor is not used in trim calc)
-                                engine_data = can_MsgRx.data[3]*0.005;
-                                sprintf(buffer,"%d v ",(int) engine_data);
-     
-#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PidValue.cpp	Sun Apr 27 19:13:35 2014 +0000
@@ -0,0 +1,19 @@
+#include "PidValue.h"
+#include "DebugPrint.h"
+
+PidValue::PidValue(const char* name, const char* unit)
+: m_value(0)
+, m_unit(unit)
+, m_name(name)
+{
+}
+
+void PidValue::print()
+{
+    pc.printf("%d %s\r\n", m_value, m_unit);
+}
+
+const char* PidValue::getName()
+{
+    return m_name;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PidValue.h	Sun Apr 27 19:13:35 2014 +0000
@@ -0,0 +1,19 @@
+#ifndef PIDVALUE_H
+#define PIDVALUE_H
+
+#include <stdint.h>
+
+class PidValue
+{
+public:
+    PidValue(const char* name, const char* unit);
+    virtual bool decode(const uint8_t* data, uint16_t length) = 0;
+    void print();
+    const char* getName();
+protected:    
+    unsigned int m_value;
+    const char* m_unit;
+    const char* m_name;
+};
+
+#endif //PIDVALUE_H
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Throttle.cpp	Sun Apr 27 19:13:35 2014 +0000
@@ -0,0 +1,23 @@
+#include "Throttle.h"
+
+
+Throttle::Throttle()
+: PidValue("Throttle", "%")
+{
+}
+
+bool Throttle::decode(const uint8_t* data, uint16_t length)
+{
+    if (length < 2)
+    {
+        return false;
+    }
+    
+    if ((data[1] != 0x11) || length != 3)
+    {
+        return false;
+    }
+    
+    m_value = data[2] * 100 /  255; // %
+    return true;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Throttle.h	Sun Apr 27 19:13:35 2014 +0000
@@ -0,0 +1,13 @@
+#ifndef THROTTLE_H
+#define THROTTLE_H
+
+#include "PidValue.h"
+
+class Throttle : public PidValue
+{
+public:
+    Throttle();
+    virtual bool decode(const uint8_t* data, uint16_t length);
+};
+
+#endif //THROTTLE_H
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/VehicleSpeed.cpp	Sun Apr 27 19:13:35 2014 +0000
@@ -0,0 +1,22 @@
+#include "VehicleSpeed.h"
+
+VehicleSpeed::VehicleSpeed()
+: PidValue("Speed", "km/h")
+{
+}
+
+bool VehicleSpeed::decode(const uint8_t* data, uint16_t length)
+{
+    if (length < 2)
+    {
+        return false;
+    }
+    
+    if ((data[1] != 0x0D) || length != 3)
+    {
+        return false;
+    }
+    
+    m_value = data[2]; // km/h
+    return true;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/VehicleSpeed.h	Sun Apr 27 19:13:35 2014 +0000
@@ -0,0 +1,13 @@
+#ifndef VEHICLESPEED_H
+#define VEHICLESPEED_H
+
+#include "PidValue.h"
+
+class VehicleSpeed : public PidValue
+{
+public:
+    VehicleSpeed();
+    virtual bool decode(const uint8_t* data, uint16_t length);
+};
+
+#endif //VEHICLESPEED_H
\ No newline at end of file
--- a/main.cpp	Sun Apr 27 17:42:32 2014 +0000
+++ b/main.cpp	Sun Apr 27 19:13:35 2014 +0000
@@ -20,6 +20,7 @@
 
 IsoTpHandler tpHandler(&can2);
 
+//#define ACTIVATE_DEBUG_OUTPUT
 #ifdef ACTIVATE_DEBUG_OUTPUT
 #define DEBUG_PRINT(format, ...) pc.printf(format, ##__VA_ARGS__) 
 #else
@@ -81,6 +82,71 @@
     //pc.printf("can_rx_int_handler ok\r\n");
 }
 
+Mail<CANMessage, 16> can2_tx_queue;
+void can2_send_packets(void const *args) {
+    DEBUG_PRINT("TX2 start\r\n");
+    while (true) {
+        osEvent evt = can2_tx_queue.get(osWaitForever);
+        if (evt.status == osEventMail) {
+            CANMessage *msg = (CANMessage*) evt.value.p;
+            DEBUG_PRINT("TX2 check\r\n");
+            if (can2.write(*msg))
+            {
+                DEBUG_PRINT("TX2 send\r\n");
+                can2_tx_queue.free(msg);
+                Thread::wait(150);
+            }
+            else
+            {
+                DEBUG_PRINT("TX2 wait \r\n");
+                Thread::wait(150);
+            }
+        }
+    }
+}
+
+
+struct behaviour_t {
+    unsigned char rxData[8];
+    char txData[8];
+};
+
+
+
+behaviour_t behaviour[] = 
+{
+    {{0x02, 0x21, 0x01, 0, 0, 0, 0, 0}, {0x10, 0x1F, 0x61, 0x01, 0x51, 0, 0x37, 0x01}}, //first oil temp packet
+    {{0x30, 0, 0, 0, 0, 0, 0, 0}, {0x21, 0x1F, 0x61, 0x01, 0x51, 0, 0x37, 0x01}}, //second oil temp packet, TODO more pakets must be sent
+    {{0x02, 0x01, 0x21, 0, 0, 0, 0, 0}, {0x04, 0x41, 0x21, 0, 0, 0, 0, 0}},
+    {{0x02, 0x01, 0x0C, 0, 0, 0, 0, 0}, {0x04, 0x41, 0x0C, 0x0F, 0xA2, 0, 0, 0}},
+    {{0x02, 0x01, 0x11, 0, 0, 0, 0, 0}, {0x03, 0x41, 0x11, 0x26, 0, 0, 0, 0}},
+    {{0x02, 0x01, 0x05, 0, 0, 0, 0, 0}, {0x03, 0x41, 0x05, 0x4D, 0, 0, 0, 0}},  //engine coolant temp
+};
+
+void can2_send_requests(void const *args) {
+    while (true) {
+        Thread::wait(2000);
+        CANMessage sendMsg(0x7E0, (char*) behaviour[3].rxData, 8);
+        CANMessage* msg = can2_tx_queue.alloc();
+        *msg = sendMsg;
+        can2_tx_queue.put(msg);
+        Thread::wait(200);
+        msg = can2_tx_queue.alloc();
+        sendMsg.data[2] = behaviour[4].rxData[2];
+        *msg = sendMsg;
+        can2_tx_queue.put(msg);
+        Thread::wait(200);
+        sendMsg.data[2] = behaviour[5].rxData[2];
+        *msg = sendMsg;
+        can2_tx_queue.put(msg);
+        Thread::wait(200);
+        CANMessage sendMsg2(0x7E0, (char*) behaviour[0].rxData, 8);
+        msg = can2_tx_queue.alloc();
+        *msg = sendMsg2;
+        can2_tx_queue.put(msg);
+    }
+}
+
 #ifdef CAN1_TEST
 Mail<CANMessage, 16> can1_rx_queue;
 CANMessage msg1;
@@ -104,21 +170,6 @@
 
 #ifdef CAN1_OBD_CAR_SIMULATOR
 
-struct behaviour_t {
-    unsigned char rxData[8];
-    char txData[8];
-};
-
-behaviour_t behaviour[] = 
-{
-    {{0x02, 0x21, 0x01, 0, 0, 0, 0, 0}, {0x10, 0x1F, 0x61, 0x01, 0x51, 0, 0x37, 0x01}}, //first oil temp packet
-    {{0x30, 0, 0, 0, 0, 0, 0, 0}, {0x21, 0x1F, 0x61, 0x01, 0x51, 0, 0x37, 0x01}}, //second oil temp packet, TODO more pakets must be sent
-    {{0x02, 0x01, 0x21, 0, 0, 0, 0, 0}, {0x04, 0x41, 0x21, 0, 0, 0, 0, 0}},
-    {{0x02, 0x01, 0x0C, 0, 0, 0, 0, 0}, {0x04, 0x41, 0x0C, 0x0F, 0xA2, 0, 0, 0}},
-    {{0x02, 0x01, 0x11, 0, 0, 0, 0, 0}, {0x03, 0x41, 0x11, 0x26, 0, 0, 0, 0}},
-    {{0x02, 0x01, 0x05, 0, 0, 0, 0, 0}, {0x03, 0x41, 0x05, 0x4D, 0, 0, 0, 0}},  //engine coolant temp
-};
-
 Mail<CANMessage, 16> can1_tx_queue;
 
 void can1_obd_car_simulator_process_packet(CANMessage &msg)
@@ -264,6 +315,8 @@
     Thread can1_tx_thread(can1_send_packets);
 #endif //CAN1_OBD_CAR_SIMULATOR    
 #endif //CAN1_TEST
+    Thread can2_send_request_thread(can2_send_requests);
+    Thread can2_tx_thread(can2_send_packets);
     pc.printf("Start\r\n");
     while (true) {
         led1 = !led1;