forked from designer2k2/multidisplay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultidisplayController.cpp
More file actions
3063 lines (2655 loc) · 83.4 KB
/
MultidisplayController.cpp
File metadata and controls
3063 lines (2655 loc) · 83.4 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
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
/*
Copyright 2009-13 Stephan Martin, Dominik Gummel
This file is part of Multidisplay.
Multidisplay is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Multidisplay 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with Multidisplay. If not, see <http://www.gnu.org/licenses/>.
*/
#include "MultidisplayController.h"
#include "LCDController.h"
#include "RPMBoostController.h"
#include "Map16x1.h"
#include "Map32x1.h"
#include <stdlib.h>
#include <inttypes.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/wdt.h>
#include <Arduino.h>
#include <EEPROM.h>
#include <digitalWriteFast.h>
//Lookup Table for the TypK:
//from 0-1350°C in steps of 50°C, the list is in microV according to that Temp.
const unsigned int MultidisplayController::tempTypK[] =
{
0,
1922,
3891,
5831,
7731,
9645,
11599,
13578,
15577,
17590,
19612,
21637,
23660,
25674,
27673,
29652,
31611,
33547,
35460,
37348,
39212,
41050,
42863,
44645,
46396,
48112,
49790,
51431
};
//Lookup Table for the Oilpressure: (12 Values)
//from 0-10Bar in steps of 1Bar, the list is in 12Bit Digital Reading when supplied with 5V and a 220Ohm Resistor in series
//(measuring the Voltage on the Sensor)
//it has a increasing Resistance with the Pressure.
const unsigned int MultidisplayController::tempVDOPressure[] =
{
0,
178,
505,
783,
999,
1189,
1340,
1476,
1592,
1699,
1785,
1865
};
//Lookup Table for the VDOtemperature (22 Values) Its Calibration curve 92-027-006
//from -30C-180C in steps of 10C, the list is in 12Bit Digital Reading when supplied with 5V and a 220Ohm Resistor in series
//(measuring the Voltage on the Sensor)
//it has a decreasing Resistance with the Temperature
const unsigned int MultidisplayController::tempVDOTemp[] =
{
4049,
4012,
3944,
3835,
3672,
3447,
3157,
2811,
2433,
2053,
1694,
1382,
1121,
899,
724,
582,
474,
387,
319,
263,
218,
182,
};
MultidisplayController::MultidisplayController() {
}
void MultidisplayController::myconstructor() {
IOport2 = 0b11111111;
wire = TwoWire();
FlashETimeU = 0;
FlashTimeU = 0;
ScreenSave = 0;
time = 0;
data.maxLd = 0;
data.maxLdt=0; //max LD for the screen
DoCheck = 1;
SerOut = SERIALOUT_BINARY;
#if defined (KWP1281_KLINE_DEBUG) || defined (KWP1281_KLINE_MIRROR_TO_SERIAL)
SerOut = SERIALOUT_DISABLED;
#endif
buttonTime = 0;
pinMode (LCDBRIGHTPIN, OUTPUT);
//test
pinMode (NORDSCHLEIFENPIN, INPUT);
digitalWrite( NORDSCHLEIFENPIN, HIGH); // turn on pullup resistors
#ifdef LAMBDASTANDALONE
pinMode (LAMBDASTANDALONE, INPUT);
digitalWrite( LAMBDASTANDALONE, LOW);
#endif
pinMode (SPEEDPIN, INPUT);
digitalWrite (SPEEDPIN, LOW);
pinMode (N75PIN, OUTPUT);
#if defined(MULTIDISPLAY_V2)
//http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235060559/15
//Audi S2 N75
//30Hz
TCCR1B = (TCCR1B & 0b11111000) | 0x5;
#else
//http://www.arduino.cc/playground/Main/TimerPWMCheatsheet
//http://www.arcfn.com/2009/07/secrets-of-arduino-pwm.html
//set pwm freq tp 30Hz
//11
//Audi S2 N75
TCCR2B = (TCCR2B & 0b11111000) | 0x7;
// pinMode (FREEPWM2, OUTPUT);
// TCCR1B = (TCCR1B & 0b11111000) | 0x5;
#endif
//set pin modes
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK, OUTPUT);
digitalWrite(DATAOUT,LOW);
digitalWrite(SPICLOCK,LOW);
// Serial.begin(57600);
Serial.begin(115200);
#if defined(MULTIDISPLAY_V2) && defined(DIGIFANT_KLINE)
// Serial1.begin(9600);
// Serial1.begin(6666);
Serial1.begin(6667);
DF_KlineSerialTime = 0;
df_kline_status = DF_KLINE_STATUS_FRAME_COMPLETE;
df_kline_active_frame = 0;
df_kline_index = 0;
df_kline_discarded_frames = 0;
df_kline_last_frame_completely_received = 255;
df_kline_freq_milliseconds = 0;
df_kline_millis_last_frame_received = millis();
#endif
#if defined(MULTIDISPLAY_V2) && defined(KWP1281_KLINE)
kwp1281Init();
#ifdef KWP1281_KLINE_DEBUG
kwp1281_debug_printtime=0;
#endif
#endif
//bluetooth module
#if defined(MULTIDISPLAY_V2) && defined(BLUETOOTH_ON_SERIAL2)
Serial2.begin(115200);
#endif
//Oled
#ifdef OLED
oled = OledController();
oled.myconstructor();
// oled_flags = 0;
// oled.reset();
#endif
/* ************** TES T***************** */
// // Serial port choice
// #define mySerial Serial3
// ProxySerial myPort(&mySerial);
// Serial_LCD myLCD( &myPort);
//
// // Serial port initialisation
// mySerial.begin(9600);
// myLCD.begin();
//
// // Serial port speed up
// myLCD.setSpeed(38400);
// mySerial.begin(38400);
/* ************** TES T***************** */
wire.begin(); //Start the Wire Libary for the PCF8574
expanderWrite2(IOport2); //Switch off all Devices
//Print the Info:
#ifdef MULTIDISPLAY_V2
Serial.println("MultiDisplay 2.0!");
#else
Serial.println("MultiDisplay 1.1!");
#endif
#ifdef LCD
lcdController.myconstructor();
lcdController.lcdShowIntro(INITTIME); //Shows the Into
lcdController.init();
#endif
//Init the Buttons:
expanderWrite(0b10000011); //This may needs to be modified when a third button is attached.
serialFreq = SERIALFREQ;
serialTime = millis();
typK_state = TYPK_STATE_NEXT_SELECT_CHANNEL;
typK_state_cur_channel = 0;
#ifdef READFROMEEPROM
readSettingsFromEeprom();
#endif
#ifdef V2DEVDEBUG
v2devdebugflag = 0;
#endif
#if defined(MULTIDISPLAY_V2) && defined(GEAR_RECOGNITION)
gear_state = GEAR_STATE_NEED_RECOGNITION;
last_gear = 0;
#endif
drawTime = 0;
//LiquidCrystal lib doesnt works with the displaytech 204A :(
// lcd.begin(20,4, LCD_5x10DOTS);
// lcd.clear();
#ifdef BW_EFR_SPEEDSENSOR
pinMode (BW_EFR_SPEEDSENSOR_PIN, INPUT);
//enable pull-up
// digitalWrite (BW_EFR_SPEEDSENSOR_PIN, HIGH);
//normal Port Mode. OC4A, OC4B, OC4C disconnected
TCCR4A = 0;
//set prescalter to 8
//1 timer tick earch 0.5usecs (prescaler 8)
TCCR4B = 2;
//enable noise canceller
TCCR4B |= _BV (ICNC4);
//enable input capture 4
// 1 = trigger on rising edge!
TCCR4B |= _BV (ICES4);
//enable input capture IRQ
bitSet (TIMSK4,ICIE4);
// Enable overflow interrupt
bitSet (TIMSK4,TOIE4);
sei();
#endif
pinMode (V2_SHIFTLED1PIN, OUTPUT);
pinMode (V2_SHIFTLED2PIN, OUTPUT);
pinMode (V2_SHIFTLED3PIN, OUTPUT);
#ifdef WATCHDOG
watchdogOn();
#endif
}
#ifdef BW_EFR_SPEEDSENSOR
ISR(TIMER4_CAPT_vect) {
if ( bitRead(TCCR4B,ICES4) == 0 ) {
//falling edge
data.efr_speed_reading = ICR4;
//reset
TCNT4 = 0;
} else {
//rising edgke
TCNT4 = 0;
}
//toggle capture
TCCR4B ^= _BV(ICES4);
}
ISR(TIMER4_OVF_vect) {
//we have a timer overflow
//set reading to max
data.efr_speed_reading = 0xFFFF;
}
#endif
/*
* only for V2 pcb!
*/
int MultidisplayController::read_adc_fast_mega (uint8_t channel){
int adcvalue = 0;
byte commandbits = B11000000; //command bits - start, mode, chn (3), dont care (3)
uint8_t savedChannel = channel; //Save the channel, to make it possible to deselect the MCP later.
//choose the right MCP3208, depending on number to read.
if(channel>8) {
//Channels 9-16 on second MCP3208
IOport2 = IOport2 & B01111101; //Clears the Selection Pin for MCP 2 (Bitwise AND) (right is the MCP, left the LED)
//to adapt the right Channel selection
channel = channel - 8;
} else {
//Channels 1-8 on first MCP3208
IOport2 = IOport2 & B01111110; //Clears the Selection Pin for MCP 1 (Bitwise AND) http://www.programmers-corner.com/article/27
}
//allow channel selection
commandbits|=((channel-1)<<3);
expanderWrite2(IOport2); //Select ADC
// setup bits to be written
for (int i=7 ; i>=3 ; i--){
//TODO testme
uint8_t val = commandbits&1<<i;
//DATAOUT
if ( val ) {
//asm(code : output operand list : input operand list [: clobber list]);
asm("sbi %0,3" : : "I" (_SFR_IO_ADDR(PORTF)) );
} else {
asm("cbi %0,3" : : "I" (_SFR_IO_ADDR(PORTF)) );
}
//cycle clock
//SPICLOCK
asm("sbi %0,4" : : "I" (_SFR_IO_ADDR(PORTA)) );
asm("cbi %0,4" : : "I" (_SFR_IO_ADDR(PORTA)) );
}
//SPICLOCK
asm("sbi %0,4" : : "I" (_SFR_IO_ADDR(PORTA)) );
asm("cbi %0,4" : : "I" (_SFR_IO_ADDR(PORTA)) );
asm("sbi %0,4" : : "I" (_SFR_IO_ADDR(PORTA)) );
asm("cbi %0,4" : : "I" (_SFR_IO_ADDR(PORTA)) );
//read bits from adc
for (int i=11 ; i>=0 ; i--){
adcvalue += digitalReadFast(DATAIN) << i;
//cycle clock
//SPICLOCK
asm("sbi %0,4" : : "I" (_SFR_IO_ADDR(PORTA)) );
asm("cbi %0,4" : : "I" (_SFR_IO_ADDR(PORTA)) );
}
//deselct the MCP
if(savedChannel > 8) {
//Channels 9-16 on second MCP3208
IOport2 = IOport2 | B10000010; //Sets the Selection Pin for MCP 2
} else {
//Channels 1-8 on first MCP3208
IOport2 = IOport2 | B10000001; //Sets the Selection Pin for MCP 1
}
expanderWrite2(IOport2); //Deselect ADC
return adcvalue;
}
int MultidisplayController::read_adc_fast (uint8_t channel){
int adcvalue = 0;
byte commandbits = B11000000; //command bits - start, mode, chn (3), dont care (3)
uint8_t savedChannel = channel; //Save the channel, to make it possible to deselect the MCP later.
//choose the right MCP3208, depending on number to read.
if(channel>8) {
//Channels 9-16 on second MCP3208
IOport2 = IOport2 & B01111101; //Clears the Selection Pin for MCP 2 (Bitwise AND) (right is the MCP, left the LED)
//to adapt the right Channel selection
channel = channel - 8;
} else {
//Channels 1-8 on first MCP3208
IOport2 = IOport2 & B01111110; //Clears the Selection Pin for MCP 1 (Bitwise AND) http://www.programmers-corner.com/article/27
}
//allow channel selection
commandbits|=((channel-1)<<3);
expanderWrite2(IOport2); //Select ADC
// setup bits to be written
for (int i=7 ; i>=3 ; i--){
//TODO uses still digitalwrite
// digitalWriteFast(DATAOUT,commandbits&1<<i);
//TODO testme
uint8_t val = commandbits&1<<i;
if ( val ) {
//asm(code : output operand list : input operand list [: clobber list]);
asm("sbi %0,3" : : "I" (_SFR_IO_ADDR(PORTF)) );
} else {
asm("cbi %0,3" : : "I" (_SFR_IO_ADDR(PORTF)) );
}
//cycle clock
digitalWriteFast(SPICLOCK,HIGH);
digitalWriteFast(SPICLOCK,LOW);
}
digitalWriteFast(SPICLOCK,HIGH); //ignores 2 null bits
digitalWriteFast(SPICLOCK,LOW);
digitalWriteFast(SPICLOCK,HIGH);
digitalWriteFast(SPICLOCK,LOW);
//read bits from adc
for (int i=11 ; i>=0 ; i--){
adcvalue+=digitalReadFast(DATAIN)<<i;
//cycle clock
digitalWriteFast(SPICLOCK,HIGH);
digitalWriteFast(SPICLOCK,LOW);
}
//deselct the MCP
if(savedChannel > 8) {
//Channels 9-16 on second MCP3208
IOport2 = IOport2 | B10000010; //Sets the Selection Pin for MCP 2
} else {
//Channels 1-8 on first MCP3208
IOport2 = IOport2 | B10000001; //Sets the Selection Pin for MCP 1
}
expanderWrite2(IOport2); //Deselect ADC
return adcvalue;
}
int MultidisplayController::read_adc(uint8_t channel){
//TODO replace digital write http://code.google.com/p/multidisplay/issues/detail?id=3
int adcvalue = 0;
byte commandbits = B11000000; //command bits - start, mode, chn (3), dont care (3)
uint8_t savedChannel = channel; //Save the channel, to make it possible to deselect the MCP later.
//choose the right MCP3208, depending on number to read.
if(channel>8) {
//Channels 9-16 on second MCP3208
IOport2 = IOport2 & B01111101; //Clears the Selection Pin for MCP 2 (Bitwise AND) (right is the MCP, left the LED)
//to adapt the right Channel selection
channel = channel - 8;
} else {
//Channels 1-8 on first MCP3208
IOport2 = IOport2 & B01111110; //Clears the Selection Pin for MCP 1 (Bitwise AND) http://www.programmers-corner.com/article/27
}
//allow channel selection
commandbits|=((channel-1)<<3);
expanderWrite2(IOport2); //Select ADC
// setup bits to be written
for (int i=7 ; i>=3 ; i--){
digitalWrite(DATAOUT,commandbits&1<<i);
//cycle clock
digitalWrite(SPICLOCK,HIGH);
digitalWrite(SPICLOCK,LOW);
}
digitalWrite(SPICLOCK,HIGH); //ignores 2 null bits
digitalWrite(SPICLOCK,LOW);
digitalWrite(SPICLOCK,HIGH);
digitalWrite(SPICLOCK,LOW);
//read bits from adc
for (int i=11 ; i>=0 ; i--){
adcvalue+=digitalRead(DATAIN)<<i;
//cycle clock
digitalWrite(SPICLOCK,HIGH);
digitalWrite(SPICLOCK,LOW);
}
//Deselct the MCP
if(savedChannel > 8) {
//Channels 9-16 on second MCP3208
IOport2 = IOport2 | B10000010; //Sets the Selection Pin for MCP 2
} else {
//Channels 1-8 on first MCP3208
IOport2 = IOport2 | B10000001; //Sets the Selection Pin for MCP 1
}
expanderWrite2(IOport2); //Deselect ADC
return adcvalue;
}
//http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1173047782/5
void MultidisplayController::expanderWrite(byte _data) {
wire.beginTransmission(EXPANDER);
wire.send(_data);
wire.endTransmission();
}
void MultidisplayController::expanderWrite2(byte _data) {
wire.beginTransmission(EXPANDER2);
wire.send(_data);
wire.endTransmission();
}
byte MultidisplayController::expanderRead2() {
byte _data = 0;
wire.requestFrom(EXPANDER2, 1);
if(wire.available()) {
_data = wire.receive();
}
return _data;
}
byte MultidisplayController::expanderRead() {
byte _data = 0;
wire.requestFrom(EXPANDER, 1);
if(wire.available()) {
_data = wire.receive();
}
return _data;
}
void MultidisplayController::AnaConversion() {
//This converts all 16 Analog Values into the real deal :)
//Boost:
#ifdef BOOST_PLX_SMVACBOOST
//see http://plxdevices.com/images/SM-VacBoostVolts.jpg
// voltage = boost(psi)/9 + 1,66
// boost (psi) = voltage * 9 - 15
data.calAbsoluteBoost = 5.0* ( (float) data.anaIn[BOOSTPIN])/4095.0;
data.calAbsoluteBoost = data.calAbsoluteBoost * 9.0 - 15; //psi
data.calAbsoluteBoost = data.calAbsoluteBoost / BAR2PSI; //bar
data.calBoost = data.calAbsoluteBoost; //formula above gets relative boost
data.calAbsoluteBoost += data.boostAmbientPressureBar;
#endif
#ifdef BOOST_MOTOROLA_MPX4250
//or Motorola MPX 4250 datasheet
data.calAbsoluteBoost = ( ((5.0 * (data.anaIn[BOOSTPIN]/4095.0)) + 0.2) / 0.02 ) / 100; //Bar
data.calBoost = data.calAbsoluteBoost - data.boostAmbientPressureBar; //apply the offset (ambient pressure)
#endif
#ifdef BOOST_FREESCALE_MPXA6400A
data.calAbsoluteBoost = 5.0* ((float) data.anaIn[BOOSTPIN])/4095.0; //only gets 0-5V
// Freescale MPXA6400A
// transfer function P = Vout/0.012105 + 3.477902
data.calAbsoluteBoost = (data.calAbsoluteBoost/0.012105 + 3.477902) / 100; //makes 0-400kPa out of it
data.calBoost = data.calAbsoluteBoost - data.boostAmbientPressureBar; //apply the offset (ambient pressure)
#endif
#ifdef BOOST_BOSCH_200KPA
data.calAbsoluteBoost = 5.0* ((float) data.anaIn[BOOST2PIN])/4095.0; //only gets 0-5V
data.calAbsoluteBoost = (data.calAbsoluteBoost + 0.25)/0.0252778; //makes 0-200kPa out of it
data.calAbsoluteBoost = data.calAbsoluteBoost / 100.0; //bar
// data.calBoostPSI = data.calBoostBar * BAR2PSI;
data.calBoost = data.calAbsoluteBoost - data.boostAmbientPressureBar; //apply the offset (ambient pressure)
#endif
//Calibration for RPM (its 2.34!)
//Check if the Boost is a new Max Boost Event
if( data.calBoost >= data.maxValues[MAXVAL_BOOST].boost ) {
data.saveMax(MAXVAL_BOOST);
}
//Lambda:
/*
* http://www.nxtgenmustang.com/forum/showthread.php/111-Wideband-Transfer-Functions
* Wideband Transfer Functions:
AEM: (v*2)+10
AFM1000: (v*2)+8
DynoJet Wideband Commander: (v*1.6)+10
EGT PLX: (v*368.2)+32
G100: (v*1.4)+9
Innovative LM-1: (v*2)+10
LC-1 (Innovative): (v*3)+7.35
LM-1 (Innovative): (v*2)+10
PLX: (v*2)+10
SCT EGT: (v*368.2)+32
Tech Edge 2AO: (v*2)+9
Zeitronix: (v*2)+9.6
*/
#ifdef LAMBDA_WIDEBAND
#ifdef LAMBDA_PLX_SMAFR
//http://www.plxdevices.com/InstallationInstructions/SM-AFRUsersGuide.pdf
// air fuel ratio = 2*Voltage + 10
data.calLambdaF = ( (5.0*( ((float) data.anaIn[LAMBDAPIN]) /4095)) * 2 + 10 ) / 14.7;
#endif
#ifdef LAMBDA_INNOVATE_LC1
data.calLambdaF = ( (5.0*( ((float) data.anaIn[LAMBDAPIN]) /4095)) * 3 + 7.35 ) / 14.7;
#endif
data.calLambda = map(data.anaIn[LAMBDAPIN], 0, 4095, 0, 200);
#else
data.calLambda = map(data.anaIn[LAMBDAPIN], LAMBDAMIN, LAMBDAMAX, 0, 200); //gets about the 0-1V into 0-200 values
data.calLambdaF = (float) data.calLambda;
#endif
data.calLambda = constrain(data.calLambda, 0, 200);
#ifdef LAMBDASTANDALONE
//Lambda Standalone Patch -> hängt am Arduino direkt! data.calLambdaF = ( (5.0*( ((float) data.anaIn[LAMBDAPIN]) /4095)) * 2 + 10 ) / 14.7;
//nur 10Bit ADC!
data.calLambdaF = ( (5.0*( ((float) data.anaIn[LAMBDAPIN]) /1023)) * 2 + 10 ) / 14.7;
#endif
//CaseTemp: (damped)
data.calCaseTemp = data.calCaseTemp*0.9 + (500.0*data.anaIn[CASETEMPPIN]/4095.0)*0.1; //thats how to get °C out from a LM35 with 12Bit ADW
#ifdef VR6_MOTRONIC
//Throttle:
#ifdef VR6_MOTRONIC_M381
//VR6 16Bit has 5V for closed and 0V for open throttle...
data.calThrottle = map(data.anaIn[THROTTLEPIN], THROTTLE_VR6_16BIT_MIN, THROTTLE_VR6_16BIT_MAX, 0, 100);
data.calThrottle = constrain(data.calThrottle, 0, 100);
data.calThrottle = 100 - data.calThrottle;
#endif
#ifdef VR6_MOTRONIC_M2X
//ABV Vetteiler 8Bit: 0,5 closed, 4,5 full open throttle
// data.calThrottle = 5.0*data.anaIn[THROTTLEPIN]/4095.0; //makes 0.0 to 5.0 Volt out of it, with VagCom this could be maped to gr Air i think
// data.calThrottle = data.anaIn[THROTTLEPIN];
data.calThrottle = map(data.anaIn[THROTTLEPIN], THROTTLE_VR6_8BIT_MIN, THROTTLE_VR6_8BIT_MAX, 0, 100);
data.calThrottle = constrain(data.calThrottle, 0, 100);
#endif
#endif
#if defined(DIGIFANT) && defined(DIGIFANT_DK_POTI)
//TODO move wiring to throttle pin!
// data.calThrottle = data.anaIn[LMMPIN] ;
data.calThrottle = map(data.anaIn[LMMPIN], THROTTLE_S2_MIN, THROTTLE_S2_MAX, 0, 100);
data.calThrottle = constrain(data.calThrottle, 0, 100);
#endif
#if defined(DIGIFANT) && not defined(DIGIFANT_DK_POTI)
//digifant idle switch (LL-Schalter) gets 0V for closed throttle valve; ~5V if open
// assumption: if > 3 V: open
if ( data.anaIn[LMMPIN] < 2457 )
data.calThrottle = 0;
else {
//full throttle ?
if ( data.anaIn[THROTTLEPIN] > 2457 )
data.calThrottle = 50;
else
data.calThrottle = 100;
}
#endif
if ( data.calThrottle >= 90 ) {
//only on wot
if ( data.calLambdaF <= data.maxValues[MAXVAL_LAMBDA].lambda ) {
data.saveMax(MAXVAL_LAMBDA);
}
}
#ifdef VR6_MOTRONIC
//LMM
// data.calLMM = map(data.anaIn[LMMPIN], THROTTLE_VR6_8BIT_MIN, THROTTLE_VR6_8BIT_MAX, 0, 100);
// data.calLMM = constrain(data.calLMM, 0, 100);
// data.calLMM = data.anaIn[LMMPIN];
data.calLMM = 5.0*data.anaIn[LMMPIN]/4095.0; //makes 0.0 to 5.0 Volt out of it, with VagCom this could be maped to gr Air i think
if ( data.calLMM >= data.maxValues[MAXVAL_LMM].lmm ) {
data.saveMax(MAXVAL_LMM);
}
#endif
#ifdef V2DEVDEBUG
if ( (v2devdebugflag & 1) == 0 ) {
#endif
//RPM
#ifdef USE_RPM_ONBOARD
//(from the smoothing example)
//TODO more smoothing below 1000 rpm
data.rpmTotal -= data.rpmReadings[data.rpmIndex]; // subtract the last reading
data.rpmReadings[data.rpmIndex] = data.anaIn[RPMPIN]; // read from the sensor
//debug TEST
// data.calThrottle = data.anaIn[RPMPIN];
data.rpmTotal += data.rpmReadings[data.rpmIndex]; // add the reading to the total
data.rpmIndex = (data.rpmIndex + 1); // advance to the next index
if (data.rpmIndex >= RPMSMOOTH) // if we're at the end of the array...
{
data.rpmIndex = 0; // ...wrap around to the beginning
}
data.rpmAverage = data.rpmTotal / RPMSMOOTH; // calculate the average
//digifant and motronic have the same rpmFactor
data.calRPM = data.rpmAverage*RPMFACTOR; // apply the factor for calibration
//Check if the RPM is a new Max RPM Event
if ( data.calRPM >= data.maxValues[MAXVAL_RPM].rpm ) {
data.saveMax(MAXVAL_RPM);
}
data.rpm_map_idx = (uint8_t) map (
constrain (data.calRPM, RPM_MIN_FOR_BOOST_CONTROL, RPM_MAX_FOR_BOOST_CONTROL),
RPM_MIN_FOR_BOOST_CONTROL, RPM_MAX_FOR_BOOST_CONTROL, 0, 255);
#endif
#ifdef V2DEVDEBUG
}
#endif
//Speed
data.speedTotal -= data.speedReadings[data.speedIndex];
data.speedReadings[data.speedIndex] = analogRead(SPEEDPIN);
data.speedTotal += data.speedReadings[data.speedIndex];
data.speedIndex = data.speedIndex++;
if (data.speedIndex >= SPEEDSMOOTH)
data.speedIndex = 0;
data.speedAverage = data.speedTotal / SPEEDSMOOTH; // calculate the average
data.speedF = (float) data.speedAverage * SPEEDFACTOR * SPEEDCORRECTIONFACTOR; // apply the factor for calibration
data.speed = (uint16_t) data.speedF;
//Check if the speed is a new Max speed Event
if ( data.speed >= data.maxValues[MAXVAL_SPEED].speed ) {
data.saveMax(MAXVAL_SPEED);
}
#ifdef BW_EFR_SPEEDSENSOR
//we measure the time from rising to falling edge
if ( data.efr_speed_reading == 0xFFFF )
data.efr_speed = 0;
else
data.efr_speed = (uint32_t) BW_EFR_TIME_2_RPM / data.efr_speed_reading;
if ( data.efr_speed >= data.maxValues[MAXVAL_EFR_SPEED].efr_speed )
data.saveMax(MAXVAL_EFR_SPEED);
#endif
//Battery Voltage: (Directly from the Arduino!, so only 1023, not 4095.)
//measured Voltage * 4,09 + 0,7V should give the supply voltage
data.batVolt = ((5.0*analogRead(BATTERYPIN)/1023.0)*4.09)+0.7;
#ifdef TYPE_K
//Lets do the Typ K Conversion:
fetchTypK3_fast();
#endif
//VDO Stuff:
if ( data.anaIn[VDOT1PIN] > 0 )
data.VDOTemp1 = data.anaIn[VDOT1PIN];
if ( data.anaIn[VDOT2PIN] > 0 )
data.VDOTemp2 = GetVDOTemp(data.anaIn[VDOT2PIN]);
if ( data.anaIn[VDOT3PIN] > 0 )
data.VDOTemp3 = GetVDOTemp(data.anaIn[VDOT3PIN]);
if (data.VDOTemp1 == 65506)
data.VDOTemp1 = 0;
if (data.VDOTemp2 == 65506)
data.VDOTemp2 = 0;
if (data.VDOTemp3 == 65506)
data.VDOTemp3 = 0;
if ( data.anaIn[VDOP1PIN] > 4070 )
data.VDOPres1 = 0;
else
data.VDOPres1 = mapVdo5Bar.map32 ( data.anaIn[VDOP1PIN] );
if ( data.anaIn[VDOP2PIN] > 4070 )
data.VDOPres2 = 0;
else
data.VDOPres2 = mapVdo10Bar.map32 ( data.anaIn[VDOP2PIN] );
// data.VDOPres2 = data.anaIn[VDOP2PIN] >> 4;
if ( data.anaIn[VDOP3PIN] > 4070 )
data.VDOPres3 = 0;
else
data.VDOPres3 = mapVdo10Bar.map32 ( data.anaIn[VDOP3PIN] );
//data.VDOPres3 = GetVDOPressure(data.anaIn[VDOP3PIN]);
//TODO max val for oil and gas pressure!
}
void MultidisplayController::V2_WarnLED() {
#define BlueGreen 450 // Temperature (°C) when the Led changes from Blue to Green
#define GreenYellow 900 // Temperature (°C) when the Led changes from Green to Yellow
#define YellowRed 945 // Temperature (°C) when the Led changes from Yellow to Red
if ( data.efr_speed > V2_RGB_WARNLED_EFR_SPEED_REDLINE ) {
//red
analogWrite (V2_SHIFTLED1PIN, 255);
analogWrite (V2_SHIFTLED2PIN, 0);
analogWrite (V2_SHIFTLED3PIN, 0);
return; //dont matter what egt says -> we're over the redline and want red light!
}
if ( data.getMaxEgt() < BlueGreen ) {
//blue
analogWrite (V2_SHIFTLED1PIN, 0);
analogWrite (V2_SHIFTLED2PIN, 0);
analogWrite (V2_SHIFTLED3PIN, 255);
} else {
if ( data.getMaxEgt() < GreenYellow ) {
//green
analogWrite (V2_SHIFTLED1PIN, 0);
analogWrite (V2_SHIFTLED2PIN, 255);
analogWrite (V2_SHIFTLED3PIN, 0);
} else {
if ( data.getMaxEgt() < YellowRed ) {
//yellow
analogWrite (V2_SHIFTLED1PIN, 255);
analogWrite (V2_SHIFTLED2PIN, 100);
analogWrite (V2_SHIFTLED3PIN, 0);
} else {
//red
analogWrite (V2_SHIFTLED1PIN, 255);
analogWrite (V2_SHIFTLED2PIN, 0);
analogWrite (V2_SHIFTLED3PIN, 0);
}
}
}
}
void MultidisplayController::V1_Shiftlight()
{
//This sets the brightness according to the RPM
if(data.calRPM >= V1_RPM_SHIFT_LOW && data.calRPM <= V1_RPM_SHIFT_HIGH) {
analogWrite(V1_RPMSHIFTLIGHTPIN,V1_RPM_LOW_BRIGHT);
} else if(data.calRPM>= V1_RPM_SHIFT_HIGH) {
analogWrite(V1_RPMSHIFTLIGHTPIN,V1_RPM_HIGH_BRIGHT);
} else if(data.calRPM< V1_RPM_SHIFT_LOW) {
analogWrite(V1_RPMSHIFTLIGHTPIN,V1_RPM_NO_BRIGHT);
}
}
void MultidisplayController::serialReceive() {
int bytes_read=0;
byte command = -1;
while(Serial.available()&&bytes_read<49)
{
if(bytes_read==0) {
command = Serial.read();
} else
srData.asBytes[bytes_read-1] = Serial.read();
bytes_read++;
}
// if the information we got was in the correct format,
// read it into the system
// case 1: command for pid lib
if(bytes_read==25 && (command==0 || command==1))
{
#ifdef BOOSTN75
/*
* TODO review for V2
* Kp,Ki,Kd
* SampleTime
* On / Of
* ActivationThreshold factor
*/
/* we get fixed point values (base 1000) from the pc ! */
boostController.pidBoostSetPoint = double(srData.asFixedInt32[0] / 1000.0);
if(command==0) {
// * only change the output if we are in manual mode
boostController.boostOutput = double(srData.asFixedInt32[2] / 1000.0);
}
#endif
;
} else {
switch (command) {
case 2:
if (bytes_read >= 2) {
//command for multidisplay
switch ( srData.asBytes[0] ) {
case 1:
buttonAPressed();
break;
case 2:
buttonAHold();
break;
case 3:
buttonBPressed();
break;
case 4:
buttonBHold();
break;
}
}
break;
case 3:
if (bytes_read >= 2) {
switch ( srData.asBytes[0] ) {
case 3:
SerOut = SERIALOUT_TUNERPRO_ADX;
break;
case 2:
SerOut = SERIALOUT_ENABLED;
break;
case 0:
SerOut = SERIALOUT_DISABLED;
break;
case 4:
SerOut = SERIALOUT_BINARY;
break;
}
}
break;
case 4:
if (bytes_read >= 2){
switch ( srData.asBytes[0] ) {
#ifdef MULTIDISPLAY_V2
case 1:
saveSettings2Eeprom();
break;
case 2:
calibrateLD();
break;
case 3:
readSettingsFromEeprom();
break;
case 5:
#ifdef LCD
lcdController.setBrightness(srData.asBytes[1]);
#endif
break;
#endif
case 4:
//set new manual n75 boost dutycycles
#ifdef BOOSTN75
if (bytes_read >= 4) {
boostController.n75_manual_normal = srData.asBytes[1];
boostController.n75_manual_race = srData.asBytes[2];
}
#endif
break;
}
}
break;
#ifdef V2DEVDEBUG
case 5:
//V2 debugging stuff
if (bytes_read >= 2){
int rpm = 0;
switch ( srData.asBytes[0] ) {
case 0: //disable RPM computation if 1
if ( srData.asBytes[1] == 1 )
v2devdebugflag = v2devdebugflag | 1;
else
v2devdebugflag = v2devdebugflag & 254;
break;