-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOBD.cpp
More file actions
99 lines (70 loc) · 1.62 KB
/
OBD.cpp
File metadata and controls
99 lines (70 loc) · 1.62 KB
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
94
95
96
97
98
99
#include "OBD.h"
#include "obdtestmap.h"
#if defined(TEST_OBD)
void OBD::routine(void)
{
}
void OBD::readMemoryRequest(unsigned char offset, unsigned char amount)
{
for (int i = 0; i < amount; i++)
{
obd_buffer[i] = pgm_read_byte(&obdTestMap[offset + i]);
}
obd_errors = 0;
obd_new_frame = 1;
}
#else
void OBD::routine(void)
{
if((obd_serial_available()) && (!obd_new_frame))
{
unsigned char c = obd_read();
obd_rx_cs += c;
obd_state++;
// Serial.print(obd_state, DEC);
switch(obd_state)
{
case 1:
obd_rx_type = c;
break;
case 2:
obd_rx_length = c;
if(obd_amount != (obd_rx_length - 3)) obd_errors |= (1 << OBD_LENGTH_ERROR);
break;
case 101:
if(obd_rx_cs != 0) obd_errors |= (1 << OBD_CS_ERROR);
obd_state = 0;
if(obd_rx_type != 0x20) obd_new_frame = 1;
break;
default:
obd_buffer[obd_state - obd_data_begin] = c;
if((obd_state == (obd_rx_length - 1)) || (obd_state == 0x12)) obd_state = 100;
break;
}
}
}
void OBD::readMemoryRequest(unsigned char offset, unsigned char amount)
{
while(obd_serial_available()) obd_read();
obd_send(0x20);
obd_send(0x05);
obd_send(offset);
obd_send(amount);
obd_send(0x0100 - (0x00FF & (0x20 + 0x05 + offset + amount)));
obd_new_frame = 0;
obd_rx_length = 0;
obd_rx_cs = 0;
obd_errors = 0;
obd_state = 0;
obd_amount = amount;
// Serial.print("\n\rrequest");
}
#endif
bool OBD::available(void)
{
return obd_new_frame;
}
unsigned char OBD::getErrors(void)
{
return obd_errors;
}