-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtd5comm.h
165 lines (140 loc) · 4.35 KB
/
td5comm.h
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
Td5Comm.h -
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef Td5Comm_h
#define Td5Comm_h
#include <Arduino.h>
#define _FAULT_CODES_STRINGS_
#define Td5RequestByteDelay 1
#define Td5RequestDelay 55
#define Td5KeepAliveDelay 4000L
#define READ_ATTEMPTS 7
#define FRAME_READ_ATTEMPT_TIME 300
#define INIT_FIRST_DELAY_TIME 300
#define MAX_FAULT_CODE 280
#define MAX_FUELLING_PARAMS 22
#define KEEP_ALIVE_TIME 4500
#define INIT_FRAME 0x00
#define START_DIAG 0x01
#define REQ_SEED 0x02
#define SEND_KEY 0x03
#define START_FUELLING 0x04
#define ENGINE_RPM 0x05
#define TEMPERATURES 0x06
#define INLET_PRES_MAF 0x07
#define BATTERY_VOLT 0x08
#define AMBIENT_PRES 0x09
#define VEHICLE_SPEED 0x0A
#define THROTTLE_POS 0x0B
#define IO_CONTROL 0x0C
#define INJ_BALANCE 0x0D
#define RPM_ERROR 0x0E
#define EGR_MOD 0x0F
#define ILT_MOD 0x10
#define TWG_MOD 0x11
#define KEEP_ALIVE 0x12
#define FAULT_CODES 0x13
#define CLEAR_FAULTS 0x14
#define FUELLING 0x15
class Td5Pid;
// Class Td5Comm
class Td5Comm
{
public:
Td5Comm();
void init();
void initComm();
int8_t getPid(Td5Pid* pid);
bool ecuIsConnected();
bool newDataIsAvailable();
unsigned long getLastReceivedPidTime();
unsigned long getLastReceivedPidElapsedTime();
void setInitStep(byte init_step);
byte getInitStep();
int getLostFrames();
int getConsecutiveLostFrames();
bool connectToEcu(bool showBar = true);
void disconnectFromEcu();
int getFaultCodes();
int getFaultCode(int index);
int getFaultCodesCount(){return faultCodesCount;};
int8_t resetFaults();
private:
byte checksum(byte *data, byte len);
bool read_byte(byte * b);
void write_byte(byte b);
protected:
unsigned long lastReceivedPidTime;
unsigned long initTime;
byte initStep; // Init is multistage, this is the counter
bool ecuConnection; // Have we connected to the ECU or not
bool newDataAvailable;
int lostFrames;
int8_t consLostFrames;
int faultCodesCount;
};
// Class Td5Pid
class Td5Pid
{
public:
Td5Pid(byte ID, byte reqlen, byte resplen, long cycletime = 0);
void setCycleTime(long time){ cycleTime = time; };
bool getValue(float *fvalue, byte index = 0);
bool getValue(uint16_t *value, byte index = 0);
bool getValue(int *value, byte index = 0);
float getfValue(byte index = 0);
uint16_t getulValue(byte index = 0);
int16_t getlValue(byte index = 0);
byte getbValue(byte index = 0);
void setRequestByte(byte value, byte pos);
byte getResponseByte(byte pos);
byte *requestFrame;
byte *responseFrame;
byte id;
long cycleTime;
long lastSeenTime;
byte responseLength;
};
// Generic functions
void remote_log_byte(byte b);
void remote_log_frame(byte *datasent, byte sentlen, byte *datarecv, byte recvlen);
void retrieve_keys_from_eeprom(uint8_t *seed, uint8_t *key);
uint8_t td5_get_next_fault_code();
uint8_t td5_get_previous_fault_code();
// Declare pids
extern Td5Pid pidInitFrame;
extern Td5Pid pidStartDiag;
extern Td5Pid pidRequestSeed;
extern Td5Pid pidSendKey;
extern Td5Pid pidRPM;
extern Td5Pid pidTurboPressureMaf;
extern Td5Pid pidTemperatures;
extern Td5Pid pidBatteryVoltage;
extern Td5Pid pidAmbientPressure;
extern Td5Pid pidStartFuelling;
extern Td5Pid pidKeepAlive;
extern Td5Pid pidFaultCodes;
extern Td5Pid pidResetFaults;
extern Td5Pid pidInjectorsBalance;
extern Td5Pid pidVehicleSpeed;
extern Td5Pid pidThrottlePosition;
extern Td5Pid pidRPMError;
extern Td5Pid pidEGR;
extern Td5Pid pidILT;
extern Td5Pid pidTWG;
extern Td5Pid pidFuelling;
#define PID_NOT_READY 0
#define PID_LOST_FRAME -1
#define PID_NEGATIVE_ANSWER -2
#endif