-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathObd.pde
93 lines (81 loc) · 2.08 KB
/
Obd.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* getObdValue
*/
//void getObdValue( char* mode, char* parameter )
void getObdValue( char *pid, class PString *logEntry )
{
if( pid == "010C" ) // RPM
{
int obdResponse[2];
byte responseLength = 2;
getRawObdResponse( pid, obdResponse );
/*
byte A = obdResponse[0];
byte B = obdResponse[1];
HOST.print( "RPM: " );
HOST.println( obdRawValue[0], HEX );
HOST.println( obdRawValue[1], HEX );
HOST.println(((A*256) + B)/4); */
} else if( pid == "010D" )
{
// 010D (Vehicle speed)
// blah blah blah
} else {
HOST.print("unknown parameter");
}
}
/**
* getRawObdResponse
*/
void getRawObdResponse( char *pid, int *obdResponse )
{
byte i = 0;
byte incomingByte;
OBD.flush(); // Flush the receive buffer so we get a fresh reading
OBD.println( pid );
while( incomingByte != '>' ) // 0x3E is the ">" prompt returned by the adaptor
{
if( OBD.available() > 0 )
{
incomingByte = OBD.read();
if( incomingByte != 0x0D )
{
//HOST.print(incomingByte, BYTE);
obdResponse[i] = incomingByte;
//obdResponse[i] = strtoul(incomingByte, NULL, 16); // 16 = hex
i++;
} else {
//HOST.println();
}
obdResponse[i++] = '\0'; // Add a null character to the end
}
}
}
/**
* configureObdAdapter
*/
void configureObdAdapter()
{
OBD.println( "ATZ" ); // Force a reset of the adapter
OBD.println( "ATE0" ); // Disable command echo
OBD.println( "ATS0" ); // Disable spaces between response bytes
}
/**
*/
float obdConvert_0104( unsigned int A, unsigned int B, unsigned int C, unsigned int D ) {
return (float)A*100.0f/255.0f;
}
// From http://code.google.com/p/opengauge/source/browse/trunk/obduino/obduino.pde
// for inspiration
byte elm_compact_response(byte *buf, char *str)
{
byte i=0;
// start at 6 which is the first hex byte after header
// ex: "41 0C 1A F8"
// return buf: 0x1AF8
// NB: it's not 6 for us, because we suppress command echo and spaces
//str+=6;
while(*str!='\0')
buf[i++]=strtoul(str, &str, 16); // 16 = hex
return i;
}