-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathKWP2000.cpp
2068 lines (1883 loc) · 58.2 KB
/
KWP2000.cpp
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
KWP2000.cpp
Copyright (c) Aster94
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef KWP2000_cpp
#define KWP2000_cpp
#include <Arduino.h>
#include "KWP2000.h"
#include "ISO.h"
#warning "Not ready for Yamaha and Honda, before using this open an issue on github"
#define maybe 2 ///< used when we don't know yet the behaviour of the K-Line
#define TO_FAHRENHEIT(x) x * 1.8 + 32 ///< the formula for the conversion from celsius to fahrenheit
#define TO_MPH(x) x / 1.609 ///< the formula for the conversion from km/h to mp/h
#define LEN(x) ((sizeof(x) / sizeof(0 [x])) / ((size_t)(!(sizeof(x) % sizeof(0 [x]))))) ///< complex but safe macro for the length
/**
* @brief This is a a collection of possible ECU Errors
*/
enum error_enum
{
EE_TEST, ///< for test purposes
EE_START, ///< unable to start comunication
EE_STOP, ///< unable to stop comunication
EE_TO, ///< data is not for us
EE_FROM, ///< data don't came from the ECU
EE_CS, ///< checksum error
EE_ECHO, ///< echo error
EE_UNEX, ///< unexpected error
EE_HEADER, ///< header not regular
EE_USER, ///< error due to wrong call of a function
EE_CONFIG, ///< key bytes not regular
EE_P3MAX, ///< time out communication
EE_CR, ///< check response error
EE_ATP, ///< problem setting/reading the timing parameter
EE_WR, ///< We get a reject for a request we didn't sent
EE_US, ///< not supported, yet
EE_TOTAL ///< this is just to know how many possible errors are in this enum
};
////////////// CONSTRUCTOR ////////////////
/**
* @brief Constructor for the KWP2000 class
*
* @param kline_serial The Serial port you will use to communicate with the ECU
* @param k_out_pin The TX pin of this serial
* @param brand the brand of your bike: `SUZUKI`, `KAWASAKI`, `YAMAHA` or `HONDA`
* @param model The model of the bike
*/
KWP2000::KWP2000(HardwareSerial *kline_serial, const uint8_t k_out_pin, const brand brand, const model model)
{
_kline = kline_serial;
_k_out_pin = k_out_pin;
_brand = brand;
_model = model;
set_bike_specific_values(_brand, _model);
_response = new uint8_t[ISO_MAX_DATA];
}
////////////// SETUP ////////////////
/**
* @brief Enable the debug of the communication
*
* @param debug_serial The Serial port you will use for the debug information
* @param debug_level The verbosity of the debug, default to `DEBUG_LEVEL_DEFAULT`
* @param debug_baudrate The baudrate for the debug, default to `115200`
*/
void KWP2000::enableDebug(HardwareSerial *debug_serial, const uint8_t debug_level, const uint32_t debug_baudrate)
{
_debug = debug_serial;
_debug_level = debug_level;
_debug->begin(debug_baudrate);
_debug_enabled = true;
if (_debug_level >= DEBUG_LEVEL_DEFAULT)
{
_debug->println(F("Debug enabled"));
}
}
/**
* @brief Change the debug level
*
* @param debug_level choose between DEBUG_LEVEL_NONE DEBUG_LEVEL_DEFAULT DEBUG_LEVEL_VERBOSE
*/
void KWP2000::setDebugLevel(const uint8_t debug_level)
{
_debug_level = debug_level;
if (_debug_level == DEBUG_LEVEL_NONE)
{
_debug_enabled = false;
}
else
{
_debug_enabled = true;
}
if (_debug_level >= DEBUG_LEVEL_DEFAULT)
{
_debug->print(F("Debug level: "));
_debug->println(debug_level == DEBUG_LEVEL_DEFAULT ? "default" : "verbose");
}
}
/**
* @brief Disable the debug
*/
void KWP2000::disableDebug()
{
if (_debug_level >= DEBUG_LEVEL_DEFAULT)
{
_debug->println(F("Debug disabled"));
}
_debug->end();
_debug_enabled = false;
}
/**
* @brief Only for Suzuki: Enable the Dealer Mode
*
* @param dealer_pin The pin you will use to control it
*/
void KWP2000::enableDealerMode(const uint8_t dealer_pin)
{
if (_brand != SUZUKI)
{
return;
}
_dealer_enabled = true;
_dealer_pin = dealer_pin;
pinMode(_dealer_pin, OUTPUT);
digitalWrite(_dealer_pin, LOW);
}
/**
* @brief Only for Suzuki: Enable/Disable the Dealer Mode
*
* @param dealer_mode Choose between true/false
*/
void KWP2000::setDealerMode(const uint8_t dealer_mode)
{
if (_dealer_enabled == false)
{
return;
}
_dealer_mode = dealer_mode;
digitalWrite(_dealer_pin, _dealer_mode);
if (_debug_level >= DEBUG_LEVEL_DEFAULT)
{
_debug->print(F("Dealer mode: "));
_debug->println(_dealer_mode == true ? "Enabled" : "Disabled");
}
}
/**
* @brief Only for Suzuki: Check the dealer status
*
* @return true if it is in dealer mode
* @return false otherwise
*/
bool KWP2000::getDealerMode()
{
if (_dealer_enabled == false)
{
return false;
}
return digitalRead(_dealer_pin);
}
/**
* @brief Choose to use imperial system for the sensors values (mp/h, Fahrenheit)
*/
void KWP2000::use_imperial()
{
_use_metric_system = false;
}
/**
* @brief Choose to use metric system for the sensors values (km/h, Celsius)
*/
void KWP2000::use_metric()
{
_use_metric_system = true;
}
////////////// COMMUNICATION - Basic ////////////////
/**
* @brief Initialize the the communication through the K-Line
*
* @return `false` until the connection is not established; `true` if we connected; a `negative number` otherwise
*/
int8_t KWP2000::initKline()
{
if (_ECU_status == true)
{
if (_debug_level >= DEBUG_LEVEL_DEFAULT)
{
_debug->println(F("\nAlready connected"));
}
return 1;
}
if (_init_sequence_started == false)
{
_init_sequence_started = true;
if (_debug_level >= DEBUG_LEVEL_DEFAULT)
{
_debug->println(F("\nInitialize K-line"));
}
/*
if (ISO_T_IDLE == 0)
{
// first attempt to init the k-line
ISO_T_IDLE = ISO_T_IDLE_NEW;
}
else if (bitRead(_ECU_error, EE_P3MAX) == 1)
{
// after the connection has been lost due to time out of P3
ISO_T_IDLE = 100; // should be 0
}
else
{
// after a stopKline
ISO_T_IDLE = ISO_T_P3_MAX;
}
*/
_use_length_byte = false;
if (_brand == SUZUKI || _brand == KAWASAKI)
{
_use_target_source_address = true;
}
else if (_brand == YAMAHA || _brand == HONDA)
{
_use_target_source_address = false;
}
_kline->end();
digitalWrite(_k_out_pin, HIGH);
pinMode(_k_out_pin, OUTPUT);
_start_time = millis();
_elapsed_time = 0;
_init_phase = 0;
if (_debug_level == DEBUG_LEVEL_VERBOSE)
{
_debug->println(F("Starting sequence"));
}
}
_elapsed_time = millis() - _start_time;
switch (_init_phase)
{
case 0: // this is a 1000ms HIGH signal
if (_elapsed_time > ISO_T_IDLE)
{
_init_phase++;
}
break;
case 1:
digitalWrite(_k_out_pin, LOW);
_init_phase++;
break;
case 2: // this is a 25ms LOW signal
if (_elapsed_time > ISO_T_IDLE + ISO_T_INIL)
{
_init_phase++;
}
break;
case 3:
digitalWrite(_k_out_pin, HIGH);
_init_phase++;
break;
case 4: // this is a 25ms HIGH signal
if (_elapsed_time > ISO_T_IDLE + ISO_T_WUP)
{
_init_phase++;
}
break;
case 5:
_init_sequence_started = false;
_start_time = 0;
_elapsed_time = 0;
_kline->begin(ISO_BAUDRATE);
if (!handleRequest(start_com, LEN(start_com), true))
{
if (_debug_level >= DEBUG_LEVEL_DEFAULT)
{
_debug->println(F("Initialization failed"));
}
_ECU_status = false;
//ISO_T_IDLE = 0;
setError(EE_START);
return -2;
}
configureKline(); // maybe honda e yamaha shouldn't run this
if (_brand == KAWASAKI)
{
if (_debug_level >= DEBUG_LEVEL_DEFAULT)
{
_debug->println(F("First handshake ok, now starting diagnostic session"));
}
if (!handleRequest(start_diagnostic, LEN(start_diagnostic)))
{
if (_debug_level >= DEBUG_LEVEL_DEFAULT)
{
_debug->println(F("Failed to start diagnostic"));
}
_ECU_status = false;
//ISO_T_IDLE = 0;
setError(EE_START);
return -2;
}
if (_debug_level >= DEBUG_LEVEL_DEFAULT)
{
_debug->println(F("Start diagnostic successful"));
}
}
if (_debug_level >= DEBUG_LEVEL_DEFAULT)
{
_debug->println(F("ECU connected"));
}
_connection_time = millis();
_ECU_status = true;
_ECU_error = 0;
return 1;
break;
default:
break;
}
return 0;
}
/**
* @brief Close the communication with the motorbike
*
* @return `false` until the connection is not closed; `true` if there aren't any errors, a `negative number` otherwise
*/
int8_t KWP2000::stopKline()
{
if (_ECU_status == false)
{
if (_debug_level >= DEBUG_LEVEL_DEFAULT)
{
_debug->println(F("\nAlready disconnected"));
}
return 1;
}
if (_stop_sequence_started == false)
{
_stop_sequence_started = true;
if (_debug_level == DEBUG_LEVEL_VERBOSE)
{
_debug->println(F("Closing K-line"));
}
if (handleRequest(stop_com, LEN(stop_com)) == true)
{
//closed without problems
_ECU_error = 0;
}
else
{
//not closed correctly
setError(EE_STOP);
}
// reset all
for (uint16_t i = 0; i < ISO_MAX_DATA; i++)
{
_response[i] = 0;
}
_response_len = 0;
_response_data_start = 0;
_last_correct_response = 0;
_last_data_print = 0;
_last_sensors_calculated = 0;
_last_status_print = 0;
_connection_time = 0;
_kline->end();
_ECU_error = 0;
_start_time = millis();
_elapsed_time = 0;
}
_elapsed_time = millis() - _start_time;
if (_elapsed_time < ISO_T_P3_MAX)
{
return 0;
}
else
{
if (_debug_level >= DEBUG_LEVEL_DEFAULT)
{
_debug->println(F("ECU disconnected"));
}
_ECU_status = false;
_start_time = 0;
_elapsed_time = 0;
_stop_sequence_started = false;
return 1;
}
}
/**
* @brief Send a request to the ECU asking for data from all the sensors, then you can access the single sensor with the `get*()` functions or you can see all of them with the `printSensorsData()`
*/
void KWP2000::requestSensorsData()
{
if (_ECU_status == false)
{
if (_debug_level == DEBUG_LEVEL_VERBOSE)
{
_debug->println(F("Not connected to the ECU"));
}
setError(EE_USER);
return;
}
if (_debug_level >= DEBUG_LEVEL_DEFAULT)
{
_debug->println(F("Requesting Sensors Data"));
}
if (_brand == SUZUKI)
{
handleRequest(suzuki_request_sens, LEN(suzuki_request_sens));
//GPS (Gear Position Sensor)
_GPS = _response[IDX_GPS];
// Clutch
_CLUTCH = _response[IDX_CLUTCH];
//RPM (Rights Per Minutes) it is split between two byte
_RPM = _response[IDX_RPM_H] * 10 + _response[IDX_RPM_L] / 10;
// Speed
_SPEED = _response[IDX_SPEED] * 2;
// TPS (Throttle Position Sensor)
_TPS = 125 * (_response[IDX_TPS] - 55) / (256 - 55);
// STPS (Secondary Throttle Position Sensor)
_STPS = _response[IDX_STPS] / 2.55;
// IAP (Intake Air Pressure)
_IAP = _response[IDX_IAP] * 4 * 0.136;
// IAT (Intake Air Temperature)
_IAT = (_response[IDX_IAT] - 48) / 1.6;
// ECT (Engine Coolant Temperature)
_ECT = (_response[IDX_ECT] - 48) / 1.6;
// Voltage
_VOLT = _response[IDX_VOLT] * 100 / 126;
}
else if (_brand == KAWASAKI)
{
handleRequest(kawasaki_request_gps, LEN(kawasaki_request_gps));
_GPS = _response[IDX_GPS];
handleRequest(kawasaki_request_rpm, LEN(kawasaki_request_rpm));
_RPM = ((_response[IDX_RPM_H] * 255 + _response[IDX_RPM_L]) / 255.0) * 100;
handleRequest(kawasaki_request_speed, LEN(kawasaki_request_speed));
_SPEED = _response[IDX_SPEED];
handleRequest(kawasaki_request_tps, LEN(kawasaki_request_tps));
_TPS = _response[IDX_TPS];
handleRequest(kawasaki_request_iap, LEN(kawasaki_request_iap));
_IAP = _response[IDX_IAP] * 4 * 0.136;
handleRequest(kawasaki_request_iat, LEN(kawasaki_request_iat));
_IAT = (_response[IDX_IAT] - 48) / 1.6;
handleRequest(kawasaki_request_ect, LEN(kawasaki_request_ect));
_ECT = (_response[IDX_ECT] - 48) / 1.6;
}
else if (_brand == YAMAHA)
{
handleRequest(yamaha_request_sens, LEN(yamaha_request_sens));
}
else if (_brand == HONDA)
{
handleRequest(honda_request_sens, LEN(honda_request_sens));
}
_last_sensors_calculated = millis();
}
/**
* @brief Read the Diagnostic Trouble Codes (DTC) from the ECU
*
* @param Default to `READ_ONLY_ACTIVE`, accepted values `READ_TOTAL`, `READ_ALL`
*/
void KWP2000::readTroubleCodes(const trouble_code which)
{
if (_ECU_status == false)
{
if (_debug_level == DEBUG_LEVEL_VERBOSE)
{
_debug->println(F("Not connected to the ECU"));
}
setError(EE_USER);
return;
}
if (which == READ_TOTAL)
{
handleRequest(trouble_codes_all, LEN(trouble_codes_all));
}
else if (which == READ_ONLY_ACTIVE)
{
handleRequest(trouble_codes_only_active, LEN(trouble_codes_only_active));
}
else if (which == READ_ALL)
{
handleRequest(trouble_codes_with_status, LEN(trouble_codes_with_status));
}
const uint8_t DTC_total = _response[_response_data_start + 1]; // Diagnosis trouble codes
if (_debug_level >= DEBUG_LEVEL_DEFAULT)
{
_debug->print("There are ");
_debug->print(DTC_total);
_debug->println(" errors\n");
for (uint8_t n = _response_data_start + 2; n < _response_len; n++)
{
_debug->print(_response[n]); // todo needed more test to understand the DTC number, value and status parameters
}
_debug->println();
}
}
/**
* @brief Clear the DTC from the ECU
*
* @param code Optional. Only the passed `code` will be cleared
*/
void KWP2000::clearTroubleCodes(const uint8_t code)
{
if (_ECU_status == false)
{
if (_debug_level == DEBUG_LEVEL_VERBOSE)
{
_debug->println(F("Not connected to the ECU"));
}
setError(EE_USER);
return;
}
if (code == 0x00) // Clear all
{
handleRequest(clear_trouble_codes, LEN(clear_trouble_codes));
}
else // Clear a single error provided by the user
{
const uint8_t to_clear[] = {clear_trouble_codes[0], code};
handleRequest(to_clear, LEN(to_clear));
}
}
/**
* @brief Keep the connection through the K-Line alive
*
* @param time Optional. It is calculated automatically to be a safe interval
*/
void KWP2000::keepAlive(uint16_t time)
{
if (_kline->available() > 0)
{
// the ECU wants to tell something
uint8_t in;
if (_debug_level == DEBUG_LEVEL_VERBOSE)
{
_debug->println("Serial buffer not empty:");
}
while (_kline->available() > 0)
{
in = _kline->read();
if (_debug_level == DEBUG_LEVEL_VERBOSE)
{
_debug->println(in, HEX);
}
}
}
if (_ECU_status == false)
{
return; // If it is not connected it is meaningless to send a request
}
if (millis() - _last_correct_response >= ISO_T_P3_MAX)
{
// The connection has been lost
if (_stop_sequence_started == false)
{
if (_debug_level == DEBUG_LEVEL_VERBOSE)
{
_debug->println(F("\nConnection expired"));
}
_ECU_status = false;
_last_data_print = 0;
_last_sensors_calculated = 0;
_last_status_print = 0;
_connection_time = 0;
_kline->end();
setError(EE_P3MAX);
}
return;
}
if (time == 0)
{
time = _keep_iso_alive;
}
if (time > ISO_T_P3_MAX)
{
// Prevent human's errors
time = _keep_iso_alive;
setError(EE_USER);
}
if (millis() - _last_correct_response <= time)
{
// We talked with the ECU not so much time ago
return;
}
if (_debug_level == DEBUG_LEVEL_VERBOSE)
{
_debug->print(F("\nKeeping connection alive\nLast:"));
_debug->println(millis() - _last_correct_response);
}
// Send a dummy request
if (_brand == SUZUKI)
{
handleRequest(tester_present_with_answer, LEN(tester_present_with_answer));
}
else if (_brand == KAWASAKI)
{
handleRequest(kawasaki_request_gps, LEN(kawasaki_request_gps));
}
else if (_brand == YAMAHA)
{
handleRequest(tester_present_with_answer, LEN(tester_present_with_answer));
}
else if (_brand == HONDA)
{
handleRequest(tester_present_with_answer, LEN(tester_present_with_answer));
}
}
////////////// COMMUNICATION - Advanced ////////////////
/**
* @brief This function is the core of the library. You just need to give a PID and it will generate the header, calculate the checksum and try to send the request.
* Then it will check if the response is correct and if now it will try to send the request another two times, all is based on the ISO14230
*
* @param to_send The PID you want to send, see PID.h for more detail
* @param send_len The length of the PID (use `sizeof` to get it)
* @param try_once Optional, default to `false`. Choose if you want to try to send the request 3 times in case of error
* @return `true` if the request has been sent and a correct response has been received, `false` otherwise
*/
int8_t KWP2000::handleRequest(const uint8_t to_send[], const uint8_t send_len, const uint8_t try_once)
{
uint8_t attempt;
uint8_t completed = false;
if (try_once == true)
{
attempt = 3;
}
else
{
attempt = 1;
}
while (attempt <= 3 && completed == false)
{
sendRequest(to_send, send_len);
listenResponse();
if (checkResponse(to_send) == true)
{
completed = true;
}
else
{
if (_debug_level == DEBUG_LEVEL_VERBOSE && try_once == false)
{
_debug->print(F("Attempt "));
_debug->print(attempt);
_debug->print(F(" not luckly"));
_debug->println(attempt < 3 ? ", trying again"
: "\nWe wasn't able to comunicate");
}
attempt++;
}
}
if (completed == true)
{
return true;
}
else
{
// We made more than 3 attemps so there is a problem
return false;
}
}
/**
* @brief Read the time parameter and set some library timing based on them
*/
/*
void KWP2000::checkTimingParameter()
{
if (_ECU_status == false)
{
if (_debug_level == DEBUG_LEVEL_VERBOSE)
{
_debug->println(F("Not connected to the ECU"));
}
setError(EE_USER);
return;
}
if (_debug_level == DEBUG_LEVEL_VERBOSE)
{
_debug->print(F("Reading timing limits"));
}
if (handleRequest(atp_read_limits, LEN(atp_read_limits)) == true)
{
accessTimingParameter(true);
}
else
{
if (_debug_level == DEBUG_LEVEL_VERBOSE)
{
_debug->println(F("Error reading timing limits"));
}
setError(EE_ATP);
}
if (_debug_level == DEBUG_LEVEL_VERBOSE)
{
_debug->print(F("Reading current timing paramenters"));
}
if (handleRequest(atp_read_current, LEN(atp_read_current)) == true)
{
accessTimingParameter(false);
}
else
{
if (_debug_level == DEBUG_LEVEL_VERBOSE)
{
_debug->println(F("Error reading current timing paramenters"));
}
setError(EE_ATP);
}
}
*/
/**
* @brief Print the Timing Parameters from the ECU
*
* @param read_only Optional, default to `true`. This avoid the possibility to unintentionally change them
*/
/*
void KWP2000::accessTimingParameter(const uint8_t read_only)
{
uint8_t p2_min_temp = _response[_response_data_start + 2];
uint32_t p2_max_temp = _response[_response_data_start + 3];
uint16_t p3_min_temp = _response[_response_data_start + 4];
uint32_t p3_max_temp = _response[_response_data_start + 5];
uint16_t p4_min_temp = _response[_response_data_start + 6];
if (p2_max_temp <= 0xF0)
{
p2_max_temp *= 25;
}
else if ((p2_max_temp > 0xF0) && (p2_max_temp < 0xFF))
{
p2_max_temp = (p2_max_temp & 0xF) * 256 * 25;
}
else if (p2_max_temp == 0xFF)
{
p2_max_temp = 89601;
setError(EE_ATP);
}
if (p3_max_temp <= 0xF0)
{
p3_max_temp *= 25;
}
else if ((p3_max_temp > 0xF0) && (p3_max_temp < 0xFF))
{
p3_max_temp = (p3_max_temp & 0xF) * 256 * 25;
}
else if (p3_max_temp == 0xFF)
{
p3_max_temp = 89601;
setError(EE_ATP);
}
// any of these condition is an error
if ((p2_min_temp > p2_max_temp) || (p3_min_temp > p3_max_temp) || (p4_min_temp > ISO_T_P4_MAX_LIMIT) || (p3_min_temp < p4_min_temp))
{
setError(EE_ATP);
}
if (read_only == false)
{
ISO_T_P2_MIN = p2_min_temp;
ISO_T_P2_MAX = p2_max_temp;
ISO_T_P3_MIN = p3_min_temp;
ISO_T_P3_MAX = p3_max_temp;
ISO_T_P4_MIN = p4_min_temp;
// we set a safe margin to ask data enough often to the ECU
_keep_iso_alive = p3_max_temp / 4;
}
if (_debug_level == DEBUG_LEVEL_VERBOSE)
{
_debug->println(F("Timing Parameter from the ECU:"));
_debug->print("Errors:\t");
_debug->println(bitRead(_ECU_error, EE_ATP) == 1 ? "Yes" : "No");
_debug->print(F("P2 min:\t"));
_debug->println(p2_min_temp);
_debug->print(F("P2 max:\t"));
_debug->println(p2_max_temp);
_debug->print(F("P3 min:\t"));
_debug->println(p3_min_temp);
_debug->print(F("P3 max:\t"));
_debug->println(p3_max_temp);
_debug->print(F("P4 min:\t"));
_debug->println(p4_min_temp);
_debug->println();
}
}
*/
/**
* @brief Reset the Timing Parameters to the default settings from the ECU
*/
/*
void KWP2000::resetTimingParameter()
{
if (_ECU_status == false)
{
if (_debug_level == DEBUG_LEVEL_VERBOSE)
{
_debug->println(F("Not connected to the ECU"));
}
setError(EE_USER);
return;
}
if (_debug_level >= DEBUG_LEVEL_DEFAULT)
{
_debug->println(F("Resetting time parameters to default"));
}
if (handleRequest(atp_set_default, LEN(atp_set_default)) == true)
{
if (_debug_level >= DEBUG_LEVEL_DEFAULT)
{
_debug->println(F("Changed"));
}
}
else
{
if (_debug_level >= DEBUG_LEVEL_DEFAULT)
{
_debug->println(F("Not changed"));
}
}
accessTimingParameter(true);
}
*/
/**
* @brief Change the Timing Parameters to custom ones
*
* @param new_atp Array of 5 elements containing the new parameters
* @param new_atp_len The length of the array (use `sizeof` to get it)
*/
/*
void KWP2000::changeTimingParameter(uint32_t new_atp[], const uint8_t new_atp_len)
{
if (_ECU_status == false)
{
if (_debug_level == DEBUG_LEVEL_VERBOSE)
{
_debug->println(F("Not connected to the ECU"));
}
setError(EE_USER);
return;
}
if (_debug_level >= DEBUG_LEVEL_DEFAULT)
{
_debug->println(F("Changing timing parameter"));
}
if (new_atp_len != 5)
{
if (_debug_level == DEBUG_LEVEL_VERBOSE)
{
_debug->println(F("The time paramenter should be an array of 5 elements"));
}
setError(EE_USER);
return;
}
if (new_atp[0] > ISO_T_P2_MIN_LIMIT)
{
if (_debug_level == DEBUG_LEVEL_VERBOSE)
{
_debug->println(F("P2 min too hight"));
}
return;
}
if (new_atp[1] > ISO_T_P2_MAX_LIMIT)
{
if (_debug_level == DEBUG_LEVEL_VERBOSE)
{
_debug->println(F("P2 max too hight"));
}
return;
}
if (new_atp[2] > 255)
{
if (_debug_level == DEBUG_LEVEL_VERBOSE)
{
_debug->println(F("P3 min too hight"));
}
return;
}
if (new_atp[3] > ISO_T_P3_MAX_LIMIT)
{
if (_debug_level == DEBUG_LEVEL_VERBOSE)
{
_debug->println(F("P3 max too hight"));
}
return;
}
if (new_atp[4] > ISO_T_P4_MAX_LIMIT)
{
if (_debug_level == DEBUG_LEVEL_VERBOSE)
{
_debug->println(F("P4 min too hight"));
}
return;
}
// all check passed
// convert the data if P2 and P3 max are too high for a tiny byte
uint32_t p2_max_temp = new_atp[1];