Skip to content

Commit 4c5ee07

Browse files
author
sorek
committed
Changes for v1.0.0 release
1 parent 2da051b commit 4c5ee07

File tree

5 files changed

+72
-27
lines changed

5 files changed

+72
-27
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Maciej sorek Loboda
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

library.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "DS2",
3+
"keywords": "ds2, kwp, kwp2000, esp32, esp32-s3, esp32-s2, arduino",
4+
"description": "DS2 and KWP library to handle messages in those automotive protocols",
5+
"version": "1.0.0",
6+
"repository":
7+
{
8+
"type": "git",
9+
"url": "https://github.com/handmade0octopus/ds2.git"
10+
},
11+
"frameworks": "*",
12+
"platforms": "*",
13+
"build": {
14+
"srcFilter": [
15+
"+<DS2.cpp>"
16+
]
17+
},
18+
"authors":
19+
[
20+
{
21+
"name": "sorek",
22+
"email": "contact@sorek.uk",
23+
"url": "http://sorek.uk",
24+
"maintainer": true
25+
}
26+
]
27+
}

library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=DS2
2+
version=1.0.0
3+
author=sorek.uk
4+
maintainer=sorek <contact@sorek.uk>
5+
sentence=DS2 and KWP library to handle messages in those automotive protocols.
6+
paragraph=Supporting any Arduino based device
7+
category=Communication
8+
url=https://github.com/handmade0octopus/ds2
9+
architectures=*

DS2.cpp renamed to src/DS2.cpp

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1717
#include <Arduino.h>
1818
#include <DS2.h>
1919

20-
DS2::DS2(Stream &stream):serial(stream) {
21-
}
20+
2221

2322

2423
bool DS2::obtainValues(uint8_t command[], uint8_t data[], uint8_t respLen) {
@@ -29,7 +28,6 @@ bool DS2::obtainValues(uint8_t command[], uint8_t data[], uint8_t respLen) {
2928
blocking = true;
3029
writeData(command);
3130
bool result = readData(data);
32-
// log_e("Data: %d2, %d2, %d2 ,%d2 ,%d2 ,%d2, %d2", data[0], data[1], data[2], data[3], data[4], data[5], data[6]);
3331
blocking = block;
3432
if(!result) clearRX();
3533
return (result && checkDataOk(data));
@@ -51,7 +49,6 @@ uint8_t DS2::sendCommand(uint8_t command[], uint8_t respLen) {
5149

5250
ReceiveType DS2::receiveData(uint8_t data[]) {
5351
uint32_t time;
54-
// log_e("Time: %d", millis() - timeStamp);
5552
if(messageSend) {
5653
if(readData(data)) {
5754
messageSend = false;
@@ -118,7 +115,7 @@ uint8_t DS2::writeData(uint8_t data[], uint8_t length) {
118115
}
119116

120117
uint8_t DS2::writeToSerial(uint8_t data[], uint8_t length) {
121-
if(!slow) {
118+
if(!slowSend) {
122119
length = serial.write(data, length);
123120
// serial.flush();
124121
return length;
@@ -215,29 +212,23 @@ void DS2::clearData(uint8_t data[]) {
215212
}
216213

217214

218-
bool DS2::checkData(uint8_t data[]) {
215+
bool DS2::checkData(uint8_t data[], bool fix) {
219216
uint8_t echo = 0;
220-
if(echoLength != 0) echo += (kwp ? data[3] + 5 : data[1]);
217+
if(echoLength != 0 && !fix) echo += (kwp ? data[3] + 5 : data[1]);
221218
uint8_t checksum = data[echo];
222219
uint8_t checkLen = (kwp ? data[echo+3]+echo + 5 : data[echo+1]+echo);
223220
for(uint8_t i = echo+1; i < checkLen; i++) {
224221
checksum ^= data[i];
225222
}
226223

227224
if(checksum == 0) {
228-
/*
229-
if(echo != 0 && echo < maxDataLength/2 && data[echo+1] == echo) {
230-
bool sameData = true;
231-
for(uint8_t i = 0; i < echo; i++) {
232-
if(data[i] != data[echo + i]) sameData = false;
233-
}
234-
if(sameData) return false;
235-
} */
236-
237225
commandsPerSecond = 1000.0/(millis() - timeStamp);
238226
timeStamp = millis();
239227
return true;
240-
} else return false;
228+
} else {
229+
if(fix) data[checkLen-1] = checksum;
230+
return false;
231+
}
241232
}
242233

243234
bool DS2::checkDataOk(uint8_t data[]) {

DS2.h renamed to src/DS2.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ enum ReceiveType : uint8_t {
7878
class DS2 {
7979
public:
8080
// Constructor, you can pass any Serial, SoftwareSerial or BluetoothSerial - anything that extends 'stream'
81-
DS2(Stream &stream);
81+
DS2(Stream &stream):serial(stream) {}
8282

8383
// You san use library in blocking mode (default is false) and it waits ISO 112ms time for response.
8484
void setBlocking(bool mode);
@@ -95,7 +95,8 @@ class DS2 {
9595
uint8_t writeData(uint8_t data[], uint8_t length = 0); // sets device to first byte value; sets echo to second byte value
9696
bool readCommand(uint8_t data[]); // sets echo to 0 so you can use readData and it will read data without echo after calling this command
9797
bool readData(uint8_t data[]); // reads command and checks data
98-
bool checkData(uint8_t data[]); // just use setEcho to 0 if want to check only response or command otherwise it will check whole data (echo + response) for checksum
98+
// just use setEcho to 0 if want to check only response or command otherwise it will check whole data (echo + response) for checksum
99+
bool checkData(uint8_t data[], bool fix = false); // fix = true allows you to fix checksum of the data you want to send
99100
uint8_t available();
100101
void flush();
101102

@@ -129,11 +130,8 @@ class DS2 {
129130
bool getKwp() { return kwp; };
130131

131132
// Some ECUs like DDE4 need delay between bytes sent
132-
bool setSlowSend(bool setS, uint8_t delay = 3) {
133-
slowSend = delay;
134-
return (slow = setS);
135-
};
136-
133+
void setSlowSend(uint8_t delay = 0) { slowSend = delay; };
134+
uint8_t getSlowSend() { return slowSend; }
137135

138136
// Get commands per second calculated from write command followed by readData
139137
float getRespondsPerSecond();
@@ -142,14 +140,13 @@ class DS2 {
142140
void clearRX();
143141
void clearRX(uint8_t available, uint8_t length);
144142

145-
Stream &serial;
146143
private:
144+
Stream &serial;
147145
bool kwp = false;
148-
bool slow = false;
149146
bool blocking = false;
150147
bool messageSend = false;
151148

152-
uint8_t slowSend = 4;
149+
uint8_t slowSend = 0;
153150
uint8_t device = 0;
154151
uint8_t echoLength = 0, responseLength, maxDataLength = MAX_DATA_LENGTH;
155152

0 commit comments

Comments
 (0)