Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions include/app/pages/vehicle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ class DataTab : public QWidget {

private:
Arbiter &arbiter;
QWidget *speedo_tach_widget();
QWidget *speed_widget();
QWidget *rpm_widget();
// QWidget *mileage_data_widget();
QWidget *engine_data_widget();
QWidget *coolant_temp_widget();
QWidget *engine_load_widget();
QWidget *intake_temp_widget();
QWidget *maf_widget();

std::vector<Gauge *> gauges;
};
Expand Down
72 changes: 70 additions & 2 deletions include/obd/decoders.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,75 @@
#pragma once

#include <limits>
#include <obd/message.hpp>

namespace {

inline bool has_obd_data(const Response &resp, int required_bytes)
{
return resp.success && resp.data.size() >= required_bytes;
}

inline int obd_byte(const Response &resp, int index)
{
return static_cast<unsigned char>(resp.data.at(index));
}

inline double invalid_obd_value()
{
return std::numeric_limits<double>::quiet_NaN();
}

} // namespace

inline double percentage(const Response &resp)
{
if (!has_obd_data(resp, 1))
return invalid_obd_value();

const int a = obd_byte(resp, 0);
return (100.0 / 255.0) * a;
}

inline double temp(const Response &resp)
{
if (!has_obd_data(resp, 1))
return invalid_obd_value();

const int a = obd_byte(resp, 0);
return a - 40.0;
}

inline double rpm(const Response &resp)
{
if (!has_obd_data(resp, 2))
return invalid_obd_value();

const int a = obd_byte(resp, 0);
const int b = obd_byte(resp, 1);

return ((256.0 * a) + b) / 4.0;
}

inline double speed(const Response &resp)
{
if (!has_obd_data(resp, 1))
return invalid_obd_value();

return obd_byte(resp, 0);
}

inline double flow(const Response &resp)
{
if (!has_obd_data(resp, 2))
return invalid_obd_value();

const int a = obd_byte(resp, 0);
const int b = obd_byte(resp, 1);

return ((256.0 * a) + b) / 100.0;
}

/*
double percentage(Response resp) { return (100.0 / 255.0) * (int)resp.data.at(0); }

double temp(Response resp) { return (int)resp.data.at(0) - 40; }
Expand All @@ -11,4 +79,4 @@ double rpm(Response resp) { return ((256.0 * (int)resp.data.at(0)) + (int)resp.d
double speed(Response resp) { return (int)resp.data.at(0); }

double flow(Response resp) { return ((256.0 * (int)resp.data.at(0)) + (int)resp.data.at(1)) / 100.0; }

*/
41 changes: 41 additions & 0 deletions include/obd/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <sstream>
#include <string>
#include <QByteArray>
#include <algorithm>

struct Message {
unsigned char length;
Expand All @@ -27,6 +28,45 @@ struct Request : Message {
}
};

struct Response : Message {
bool success = false;
QByteArray data;

explicit Response(const QByteArray &payload)
: Message{0, 0, 0}
{
if (payload.size() < 3)
return;

this->length =
static_cast<unsigned char>(payload.at(0));

if (this->length < 2)
return;

this->mode =
static_cast<unsigned char>(payload.at(1)) - 0x40;

this->PID =
static_cast<unsigned char>(payload.at(2));

const int data_length =
std::min<int>(this->length - 2, payload.size() - 3);

if (data_length > 0)
this->data = payload.mid(3, data_length);

this->success = true;
}

Response()
: Message{0, 0, 0}
, success(false)
{
}
};

/*
struct Response : Message {
bool success = true;
QByteArray data;
Expand All @@ -42,3 +82,4 @@ struct Response : Message {

Response() { this->success = false; }
};
*/
Loading