-
Notifications
You must be signed in to change notification settings - Fork 2
/
mpguino_fork.ino
1219 lines (1038 loc) · 34.5 KB
/
mpguino_fork.ino
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
#include <WString.h>
#include <EEPROM.h>
#include <LiquidCrystal.h>
#include "mpguino.h"
#include "utils.h"
#include "trip.h"
#include "mathfuncs.h"
#include "parms.h"
#include "lcdchars.h"
#include <ScreenUi.h> //may need to rename ScreenUi.cpp --> .ino before import
//define the event IDs
#define enableVSSID 0
#define enableLButtonID 1
#define enableMButtonID 2
#define enableRButtonID 3
/* --- Global Variable Declarations -------------------------- */
LiquidCrystal mylcd(DIPin, EnablePin, DB4Pin, DB5Pin, DB6Pin, DB7Pin);
unsigned long MAXLOOPLENGTH = 0; // see if we are overutilizing the CPU
//main objects we will be working with:
unsigned long injHiStart; //for timing injector pulses
Trip tmpTrip;
Trip instant;
Trip current;
Trip tank;
#if (BARGRAPH_DISPLAY_CFG == 1)
Trip periodic;
#endif
unsigned volatile long instInjStart=nil;
unsigned volatile long tmpInstInjStart=nil;
unsigned volatile long instInjEnd;
unsigned volatile long tmpInstInjEnd;
unsigned volatile long instInjTot;
unsigned volatile long tmpInstInjTot;
unsigned volatile long instInjCount;
unsigned volatile long tmpInstInjCount;
unsigned volatile long lastVSS1;
unsigned volatile long lastVSSTime;
unsigned volatile long lastVSS2;
volatile bool vssFlop = 0;
volatile bool lastVssFlop = vssFlop;
//middle button cycles through these brightness settings
unsigned char brightness[]={255,214,171,128};
unsigned char brightnessIdx=1;
volatile unsigned long timer2_overflow_count;
unsigned char buttonState = buttonsUp;
//overflow counter used by millis2()
unsigned long lastMicroSeconds=millis2() * 1000;
unsigned char newRun = 0;
extern int __bss_end;
extern int *__brkval;
/* the display function poitners */
pFunc displayFuncs[] = {
doDisplayCustom,
doDisplayInstantCurrent,
doDisplayInstantTank,
doDisplayBigInstant,
doDisplayBigCurrent,
doDisplayBigTank,
doDisplayCurrentTripData,
doDisplayTankTripData,
doDisplayEOCIdleData,
doDisplaySystemInfo,
#if (BARGRAPH_DISPLAY_CFG == 1)
doDisplayBarGraph,
#endif
#if (DTE_CFG == 1)
doDisplayBigDTE,
#endif
};
const char * displayFuncNames[length(displayFuncs)];
//array of the event functions
pFunc eventFuncs[] ={enableVSS, enableLButton, enableMButton, enableRButton};
//ms counters
unsigned int eventFuncCounts[length(eventFuncs)];
/* --- End Global Variable Declarations ---------------------- */
/*** Set up the Events ***
We have our own ISR for timer2 which gets called about once a millisecond.
So we define certain event functions that we can schedule by calling addEvent
with the event ID and the number of milliseconds to wait before calling the event.
The milliseconds is approximate.
Keep the event functions SMALL!!! This is an interrupt!
*/
//event functions
void enableLButton() {
PCMSK1 |= (1 << PCINT11);
}
void enableMButton(){
PCMSK1 |= (1 << PCINT12);
}
void enableRButton(){
PCMSK1 |= (1 << PCINT13);
}
//schedule an event to occur ms milliseconds from now
void addEvent(unsigned char eventID, unsigned int ms) {
if(ms == 0) {
eventFuncs[eventID]();
}
else {
eventFuncCounts[eventID]=ms;
}
}
/* this ISR gets called every 1.024 milliseconds, we will call that a
* millisecond for our purposes go through all the event counts, if any
* are non zero subtract 1 and call the associated function if it just
* turned zero. */
ISR(TIMER2_OVF_vect) {
unsigned char eventID;
timer2_overflow_count++;
for(eventID = 0; eventID < length(eventFuncs); eventID++) {
if(eventFuncCounts[eventID]!= 0) {
eventFuncCounts[eventID]--;
if(eventFuncCounts[eventID] == 0) {
eventFuncs[eventID]();
}
}
}
} /* ISR(TIMER2_OVF_vect) */
unsigned long microSeconds(void) {
unsigned long tmp_timer2_overflow_count;
unsigned long tmp;
unsigned char tmp_tcnt2;
cli(); //disable interrupts
tmp_timer2_overflow_count = timer2_overflow_count;
tmp_tcnt2 = TCNT2;
sei(); // enable interrupts
tmp = ((tmp_timer2_overflow_count << 8) + tmp_tcnt2) * 4;
if((tmp<=lastMicroSeconds) && (lastMicroSeconds<4290560000ul)) {
return microSeconds();
}
lastMicroSeconds=tmp;
return tmp;
}
unsigned long elapsedMicroseconds(unsigned long startMicroSeconds, unsigned long currentMicroseconds) {
if(currentMicroseconds >= startMicroSeconds) {
return currentMicroseconds-startMicroSeconds;
}
return 0xFFFFFFFF - (startMicroSeconds-currentMicroseconds);
}
unsigned long elapsedMicroseconds(unsigned long startMicroSeconds ){
return elapsedMicroseconds(startMicroSeconds, microSeconds());
}
void processInjOpen(void){
injHiStart = microSeconds();
}
void processInjClosed(void){
long t = microSeconds();
long x = elapsedMicroseconds(injHiStart, t) - parms[injectorSettleTimeIdx];
if (x >0) {
tmpTrip.var[Trip::injHius] += x;
}
tmpTrip.var[Trip::injPulses]++;
if (tmpInstInjStart != nil) {
if (x >0) {
tmpInstInjTot += x;
}
tmpInstInjCount++;
}
else {
tmpInstInjStart = t;
}
tmpInstInjEnd = t;
}
void enableVSS(){
vssFlop = !vssFlop;
}
//attach the vss/buttons interrupt
ISR(PCINT1_vect) {
static unsigned char vsspinstate=0;
unsigned char p = PINC; //bypassing digitalRead for interrupt performance
if ((p & vssBit) != (vsspinstate & vssBit)){
addEvent(enableVSSID, parms[vsspauseIdx]); //check back in a couple milli
}
if (lastVssFlop != vssFlop) {
lastVSS1=lastVSS2;
unsigned long t = microSeconds();
lastVSS2=elapsedMicroseconds(lastVSSTime,t);
lastVSSTime=t;
tmpTrip.var[Trip::vssPulses]++;
tmpTrip.var[Trip::vssPulseLength] += lastVSS2;
lastVssFlop = vssFlop;
}
vsspinstate = p;
buttonState &= p;
} /* ISR(PCINT1_vect) */
void setup (void) {
unsigned char x = 0;
CLOCK = 0;
SCREEN = 0;
HOLD_DISPLAY = 0;
#if (CFG_SERIAL_TX)
Serial.begin(9600);
#endif
#if (CFG_IDLE_MESSAGE != 0)
IDLE_DISPLAY_DELAY = 0;
#endif
init2();
newRun = load();//load the default parameters
displayFuncNames[x++]= PSTR("Custom ");
displayFuncNames[x++]= PSTR("Instant/Current ");
displayFuncNames[x++]= PSTR("Instant/Tank ");
displayFuncNames[x++]= PSTR("BIG Instant ");
displayFuncNames[x++]= PSTR("BIG Current ");
displayFuncNames[x++]= PSTR("BIG Tank ");
displayFuncNames[x++]= PSTR("Current ");
displayFuncNames[x++]= PSTR("Tank ");
displayFuncNames[x++]= PSTR("EOC mi/Idle gal ");
displayFuncNames[x++]= PSTR("CPU Monitor ");
#if (BARGRAPH_DISPLAY_CFG == 1)
displayFuncNames[x++]= PSTR("Bargraph ");
#endif
#if (DTE_CFG == 1)
displayFuncNames[x++]= PSTR("BIG DTE ");
#endif
pinMode(BrightnessPin,OUTPUT);
analogWrite(BrightnessPin,brightness[brightnessIdx]);
pinMode(EnablePin,OUTPUT);
pinMode(DIPin,OUTPUT);
pinMode(DB4Pin,OUTPUT);
pinMode(DB5Pin,OUTPUT);
pinMode(DB6Pin,OUTPUT);
pinMode(DB7Pin,OUTPUT);
delay2(500);
pinMode(ContrastPin,OUTPUT);
analogWrite(ContrastPin,parms[contrastIdx]);
mylcd.begin(16, 2);
mylcd.clear();
mylcd.print(getStr(PSTR("OpenGauge ")));
mylcd.setCursor(0,LCD_BottomLine);
mylcd.print(getStr(PSTR(" MPGuino v0.75S")));
pinMode(InjectorOpenPin, INPUT);
pinMode(InjectorClosedPin, INPUT);
pinMode(VSSPin, INPUT);
attachInterrupt(0, processInjOpen, FALLING);
attachInterrupt(1, processInjClosed, RISING);
pinMode( lbuttonPin, INPUT );
pinMode( mbuttonPin, INPUT );
pinMode( rbuttonPin, INPUT );
//"turn on" the internal pullup resistors
digitalWrite( lbuttonPin, HIGH);
digitalWrite( mbuttonPin, HIGH);
digitalWrite( rbuttonPin, HIGH);
// digitalWrite( VSSPin, HIGH);
//low level interrupt enable stuff
PCMSK1 |= (1 << PCINT8);
enableLButton();
enableMButton();
enableRButton();
PCICR |= (1 << PCIE1);
delay2(1500);
} /* void setup (void) */
void loop (void) {
unsigned long lastActivity;
unsigned long tankHold; //state at point of last activity
unsigned long loopStart;
unsigned long temp; //scratch variable
lastActivity = microSeconds();
if (newRun !=1) {
//go through the initialization screen
initGuino();
}
while (true) {
#if (CFG_FUELCUT_INDICATOR != 0)
FCUT_POS = 0;
#endif
loopStart = microSeconds();
instant.reset(); //clear instant
cli();
instant.update(tmpTrip); //"copy" of tmpTrip in instant now
tmpTrip.reset(); //reset tmpTrip first so we don't lose too many interrupts
instInjStart=tmpInstInjStart;
instInjEnd=tmpInstInjEnd;
instInjTot=tmpInstInjTot;
instInjCount=tmpInstInjCount;
tmpInstInjStart=nil;
tmpInstInjEnd=nil;
tmpInstInjTot=0;
tmpInstInjCount=0;
sei();
#if (CFG_SERIAL_TX == 1)
/* send out instantmpg * 1000, instantmph * 1000, the injector/vss raw data */
simpletx(format(instantmpg()));
simpletx(",");
simpletx(format(instantmph()));
simpletx(",");
simpletx(format(instant.var[Trip::injHius]*1000));
simpletx(",");
simpletx(format(instant.var[Trip::injPulses]*1000));
simpletx(",");
simpletx(format(instant.var[Trip::vssPulses]*1000));
simpletx("\n");
#endif
/* --- update all of the trip objects */
current.update(instant); //use instant to update current
tank.update(instant); //use instant to update tank
#if (BARGRAPH_DISPLAY_CFG == 1)
if (lastActivity != nil) {
periodic.update(instant); //use instant to update periodic
}
#endif
#if (BARGRAPH_DISPLAY_CFG == 1)
/* --- For bargraph: reset periodic every 2 minutes */
if (periodic.var[Trip::loopCount] >= Time_TwoMinutes) {
temp = MIN((periodic.mpg()/10), 0xFFFF);
/* add temp into first element and shift items in array */
insert((int*)PERIODIC_HIST, (unsigned short)temp, length(PERIODIC_HIST), 0);
periodic.reset(); /* reset */
}
#endif
/* --- Decide whether to go to sleep or wake up */
if ( (instant.var[Trip::vssPulses] == 0)
&& (instant.var[Trip::injPulses] == 0)
&& (HOLD_DISPLAY==0)
)
{
if( (elapsedMicroseconds(lastActivity) > parms[currentTripResetTimeoutUSIdx])
&& (lastActivity != nil)
)
{
#if (TANK_IN_EEPROM_CFG)
writeEepBlock32(eepBlkAddr_Tank, &tank.var[0], eepBlkSize_Tank);
#endif
#if (SLEEP_CFG & Sleep_bkl)
analogWrite(BrightnessPin,brightness[0]); //nitey night
#endif
#if (SLEEP_CFG & Sleep_lcd)
mylcd.noDisplay();
#endif
lastActivity = nil;
}
}
else {
/* wake up! */
if (lastActivity == nil) {
#if (SLEEP_CFG & Sleep_bkl)
analogWrite(BrightnessPin,brightness[brightnessIdx]);
#endif
#if (SLEEP_CFG & Sleep_lcd)
mylcd.display();
#endif
lastActivity=loopStart;
current.reset();
tank.var[Trip::loopCount] = tankHold;
current.update(instant);
tank.update(instant);
#if (BARGRAPH_DISPLAY_CFG == 1)
periodic.reset();
periodic.update(instant);
#endif
}
else {
lastActivity=loopStart;
tankHold = tank.var[Trip::loopCount];
}
}
if (HOLD_DISPLAY == 0) {
#if (CFG_IDLE_MESSAGE == 0)
displayFuncs[SCREEN](); //call the appropriate display routine
#elif (CFG_IDLE_MESSAGE == 1)
/* --- during idle, jump to EOC information */
if ( (instant.var[Trip::injPulses] > 0)
&& (instant.var[Trip::vssPulses] == 0)
)
{
/* the intention of the below logic is to avoid the display flipping
in stop and go traffic. When you come to a stop, the delay timer
starts incrementing. When you drive off, it decrements. When the
timer is zero, the display is always at the user-specified screen */
if (IDLE_DISPLAY_DELAY < 6) {
/* count up until delay time is reached */
IDLE_DISPLAY_DELAY++;
}
}
else {
if (IdleDisplayRequested) {
/* count from delay time back down to zero */
IDLE_DISPLAY_DELAY--;
}
else if (IDLE_DISPLAY_DELAY < 0) {
/* if the user selected a new screen while stopped, reset
the delay timer after driveoff */
IDLE_DISPLAY_DELAY = 0;
}
}
if (IdleDisplayRequested) {
doDisplayEOCIdleData();
}
else {
displayFuncs[SCREEN]();
}
#endif
#if (CFG_FUELCUT_INDICATOR != 0)
/* --- insert visual indication that fuel cut is happening */
if ( (instant.var[Trip::injPulses] == 0)
&& (instant.var[Trip::vssPulses] > 0)
)
{
#if (CFG_FUELCUT_INDICATOR == 1)
LCDBUF1[FCUT_POS] = 'x';
#elif ((CFG_FUELCUT_INDICATOR == 2) || (CFG_FUELCUT_INDICATOR == 3))
LCDBUF1[FCUT_POS] = spinner[CLOCK & 0x03];
#endif
}
#endif
/* --- ensure that we have terminating nulls */
LCDBUF1[16] = 0;
LCDBUF2[16] = 0;
mylcd.home();
mylcd.print(LCDBUF1);
mylcd.setCursor(0, LCD_BottomLine);
mylcd.print(LCDBUF2);
mylcd.home();
/* --- see if any buttons were pressed, display a brief message if so --- */
if (LeftButtonPressed && RightButtonPressed) {
// left and right = initialize
mylcd.print(getStr(PSTR("Setup ")));
initGuino();
}
else if (LeftButtonPressed && MiddleButtonPressed) {
// left and middle = tank reset
tank.reset();
mylcd.print(getStr(PSTR("Tank Reset ")));
}
else if (MiddleButtonPressed && RightButtonPressed) {
// right and middle = current reset
current.reset();
mylcd.print(getStr(PSTR("Current Reset ")));
}
#if (CFG_IDLE_MESSAGE == 1)
else if ((LeftButtonPressed || RightButtonPressed) && (IdleDisplayRequested)) {
/* if the idle display is up and the user hits the left or right button,
* intercept this press (nonoe of the elseifs will be hit below)
* only in this circumstance and get out of the idle display for a while.
* This will return the user to his default screen. */
IDLE_DISPLAY_DELAY = -60;
}
#endif
else if (LeftButtonPressed) {
// left is rotate through screeens to the left
if (SCREEN!=0) {
SCREEN = (SCREEN-1);
}
else {
SCREEN=length(displayFuncs)-1;
}
mylcd.print(getStr(displayFuncNames[SCREEN]));
}
else if (MiddleButtonPressed) {
// middle is cycle through brightness settings
brightnessIdx = (brightnessIdx + 1) % length(brightness);
analogWrite(BrightnessPin,brightness[brightnessIdx]);
mylcd.print(getStr(PSTR("Brightness ")));
mylcd.print(getAsciiFromDigit(brightnessIdx));
mylcd.print(" ");
}
else if (RightButtonPressed) {
// right is rotate through screeens to the left
SCREEN=(SCREEN+1)%length(displayFuncs);
mylcd.print(getStr(displayFuncNames[SCREEN]));
}
#if (CFG_IDLE_MESSAGE == 1)
if (LeftButtonPressed || RightButtonPressed) {
/* When the user wants to change screens, continue to
* avoid the idle screen for a while */
IDLE_DISPLAY_DELAY = -60;
}
#endif
if (buttonState!=buttonsUp) {
HOLD_DISPLAY = 1;
}
} /* if (HOLD_DISPLAY == 0) */
else {
HOLD_DISPLAY = 0;
}
// reset the buttons
buttonState = buttonsUp;
// keep track of how long the loops take before we go into waiting.
MAXLOOPLENGTH = MAX(MAXLOOPLENGTH, elapsedMicroseconds(loopStart));
while (elapsedMicroseconds(loopStart) < (looptime)) {
// wait for the end of the loop to arrive (0.5 sec)
continue;
}
CLOCK++;
} /* while (true) */
} /* loop (void) */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* format:
* Take an unsigned long and convert it to a string value that we can display.
* The function will always divide the input by 1000 and display the result.
* Some examples with output:
*
* format(1000) --> "001.00"
* format(123456) --> "123.46" (note rounding of last digit)
* format(1000000) --> "1000.0"
* format(100000) --> "100.00"
*
* Question: Why doesn't this function require the special
* unsigned long math?
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
char *format(unsigned long num) {
static char fBuff[7]; //used by format
unsigned char decimalpointpos = 3;
unsigned char x = 6;
while (num > 999994) {
num /= 10;
decimalpointpos++;
if( decimalpointpos == 5 ) break; /* We'll lose the top numbers like an odometer */
}
if (decimalpointpos == 5) {
decimalpointpos = 99;
} /* We don't need a decimal point here. */
/* Round off the non-printed value. */
if((num % 10) > 4) {
num += 10;
}
num /= 10;
while (x > 0) {
x--;
if (x == decimalpointpos) {
/* time to poke in the decimal point? */
fBuff[x]='.';
}
else {
if ( ((x+1) == decimalpointpos) && (num == 0) ) {
/* decimal point just inserted and nothing left, put in a zero */
fBuff[x]='0';
}
else if ( (x < decimalpointpos) && (num == 0) ) {
/* we have more to go and decimal point already done, put in spaces */
fBuff[x]=' ';
}
else {
/* poke the ascii character for the digit. */
fBuff[x]= getAsciiFromDigit((num % 10));
}
num /= 10;
}
}
fBuff[6] = 0; //terminating null
return fBuff;
}
//get a string from flash
char *getStr(const char * str) {
static char mBuff[17]; //used by getStr
strcpy_P(mBuff, str);
return mBuff;
}
void doDisplayCustom() {
displayTripCombo('I','m',instantmpg(),'s',instantmph(),'G','H',instantgph(),'m',current.mpg());
}
#if (0)
void doDisplayCustom() {
displayTripCombo('I','M',instantmpg(),'S',instantgph(),'R','P',instantrpm(),'C',current.var[Trip::injIdleHiSec]*1000);
}
void doDisplayCustom() {
displayTripCombo('I','M',995,'S',994,'R','P',999994,'C',999995);
}
#endif
void doDisplayEOCIdleData() {
displayTripCombo('C','e',current.eocMiles(),'g',current.idleGallons(),'T','e',tank.eocMiles(),'g',tank.idleGallons());
}
void doDisplayInstantCurrent() {
displayTripCombo('I','m',instantmpg(),'X',calcDistToEmpty(),'C','m',current.mpg(),'d',current.miles());
}
void doDisplayInstantTank() {
if (CLOCK & 0x04) {
displayTripCombo('I','m',instantmpg(),'s',instantmph(),'T','m',tank.mpg(),'d',tank.miles());
}
else {
displayTripCombo('I','m',instantmpg(),'s',instantmph(),'T','$',tank.fuelCost(),'d',tank.miles());
}
}
void doDisplayBigInstant() {
bigNum(instantmpg(),"INST","MPG ");
}
void doDisplayBigCurrent() {
bigNum(current.mpg(),"CURR","MPG ");
}
void doDisplayBigTank() {
bigNum(tank.mpg(),"TANK","MPG ");
}
void doDisplayCurrentTripData(void) {
/* display current trip formatted data */
tDisplay(¤t);
}
void doDisplayTankTripData(void) {
/* display tank trip formatted data */
tDisplay(&tank);
}
void doDisplaySystemInfo(void) {
/* display max cpu utilization and ram */
strcpy(&LCDBUF1[0], "C%");
strcpy(&LCDBUF1[2], format(MAXLOOPLENGTH*1000/(looptime/100)));
strcpy(&LCDBUF1[8], " T");
strcpy(&LCDBUF1[10], format(tank.time()));
unsigned long mem = memoryTest();
mem*=1000;
strcpy(&LCDBUF2[0], "FREE MEM: ");
strcpy(&LCDBUF2[10], format(mem));
}
#if (BARGRAPH_DISPLAY_CFG == 1)
void doDisplayBarGraph(void) {
signed char temp = 0;
unsigned char i = 0;
unsigned char j = 0;
unsigned short stemp = 0;
/* Load the bargraph characters if necessary */
if (DISPLAY_TYPE != dtBarGraph) {
putCharsToLCD(&mylcd, &barchars[0], LcdBarChars);
DISPLAY_TYPE = dtBarGraph;
}
/* plot bars */
for(i=8; i>0; i--) {
/* limit size -- print oldest first */
stemp = MIN(PERIODIC_HIST[i-1], BAR_LIMIT)/10;
/* round up if necessary */
if ((stemp % 10) > 4) {
stemp += 10;
}
/* convert to a number from 0-16 */
temp = (signed char)((stemp * 16) / (BAR_LIMIT/10));
temp = MIN(temp, 16); /* should not be necessary... */
/* line 1 graph */
LCDBUF1[j] = ascii_barmap[MAX(temp-8,0)];
/* line 2 graph */
LCDBUF2[j] = ascii_barmap[MIN(temp,8)];
j++;
}
if (CLOCK & 0x04) {
/* end of line 1: show current mpg */
LCDBUF1[8] = ' ';
LCDBUF1[9] = 'C';
strcpy(&LCDBUF1[10], format(current.mpg()));
}
else {
LCDBUF1[8] = ' ';
LCDBUF1[9] = '$';
strcpy(&LCDBUF1[10], format(current.fuelCost()));
}
/* end of line 2: show periodic mpg */
LCDBUF2[8] = ' ';
LCDBUF2[9] = 'P';
strcpy(&LCDBUF2[10], format(periodic.mpg()));
#if (CFG_FUELCUT_INDICATOR != 0)
/* where should the fuel cut indication go? */
FCUT_POS = 8;
#endif
}
#endif
#if (DTE_CFG == 1)
void doDisplayBigDTE(void) {
unsigned long dte = calcDistToEmpty();
bigNum(dte, "DIST", "TO E");
}
#endif
void displayTripCombo(char t1, char t1L1, unsigned long t1V1, char t1L2, unsigned long t1V2,
char t2, char t2L1, unsigned long t2V1, char t2L2, unsigned long t2V2) {
/* Process line 1 of the display */
LCDBUF1[0] = t1;
LCDBUF1[1] = t1L1;
strcpy(&LCDBUF1[2], format(t1V1));
LCDBUF1[8] = ' ';
LCDBUF1[9] = t1L2;
strcpy(&LCDBUF1[10], format(t1V2));
/* Process line 2 of the display */
LCDBUF2[0] = t2;
LCDBUF2[1] = t2L1;
strcpy(&LCDBUF2[2], format(t2V1));
LCDBUF2[8] = ' ';
LCDBUF2[9] = t2L2;
strcpy(&LCDBUF2[10], format(t2V2));
#if (CFG_FUELCUT_INDICATOR != 0)
FCUT_POS = 8;
#endif
}
//arduino doesn't do well with types defined in a script as parameters, so have to pass as void * and use -> notation.
void tDisplay( void * r){ //display trip functions.
Trip *t = (Trip *)r;
strcpy(&LCDBUF1[0], " s");
strcpy(&LCDBUF1[2], format(t->mph()));
strcpy(&LCDBUF1[8], " m");
strcpy(&LCDBUF1[10], format(t->mpg()));
strcpy(&LCDBUF2[0], " d");
strcpy(&LCDBUF2[2], format(t->miles()));
strcpy(&LCDBUF2[8], " g");
strcpy(&LCDBUF2[10], format(t->gallons()));
}
// this function will return the number of bytes currently free in RAM
int memoryTest(){
int free_memory;
if((int)__brkval == 0)
free_memory = ((int)&free_memory) - ((int)&__bss_end);
else
free_memory = ((int)&free_memory) - ((int)__brkval);
return free_memory;
}
unsigned long instantmph(){
unsigned long tmp1[2];
unsigned long tmp2[2];
unsigned long vssPulseTimeuS = instant.var[Trip::vssPulseLength]/instant.var[Trip::vssPulses];
init64(tmp1,0,1000000000ul);
init64(tmp2,0,parms[vssPulsesPerMileIdx]);
div64(tmp1,tmp2);
init64(tmp2,0,3600);
mul64(tmp1,tmp2);
init64(tmp2,0,vssPulseTimeuS);
div64(tmp1,tmp2);
return tmp1[1];
}
unsigned long instantmpg(){
unsigned long tmp1[2];
unsigned long tmp2[2];
unsigned long imph=instantmph();
unsigned long igph=instantgph();
if(imph == 0) return 0;
if(igph == 0) return 999999000;
init64(tmp1,0,1000ul);
init64(tmp2,0,imph);
mul64(tmp1,tmp2);
init64(tmp2,0,igph);
div64(tmp1,tmp2);
return tmp1[1];
}
unsigned long instantgph(){
unsigned long tmp1[2];
unsigned long tmp2[2];
init64(tmp1,0,instInjTot);
init64(tmp2,0,3600000000ul);
mul64(tmp1,tmp2);
init64(tmp2,0,1000ul);
mul64(tmp1,tmp2);
init64(tmp2,0,parms[microSecondsPerGallonIdx]);
div64(tmp1,tmp2);
init64(tmp2,0,instInjEnd-instInjStart);
div64(tmp1,tmp2);
return tmp1[1];
}
/*
unsigned long instantrpm(){
unsigned long tmp1[2];
unsigned long tmp2[2];
init64(tmp1,0,instInjCount);
init64(tmp2,0,120000000ul);
mul64(tmp1,tmp2);
init64(tmp2,0,1000ul);
mul64(tmp1,tmp2);
init64(tmp2,0,parms[var[Trip::injPulses]Per2Revolutions]);
div64(tmp1,tmp2);
init64(tmp2,0,instInjEnd-instInjStart);
div64(tmp1,tmp2);
return tmp1[1];
} */
#if (DTE_CFG)
unsigned long calcDistToEmpty(void) {
unsigned long dte;
signed long gals_remaining;
/* TODO: user configurable safety factor see minus zero below */
gals_remaining = (parms[tankSizeIdx] - tank.gallons()) - 0; /* 0.001 gal/bit */
gals_remaining = MAX(gals_remaining, 0);
dte = gals_remaining * (tank.mpg()/100); /* mpg() = 0.1 mpg/bit */
dte /= 10; /* divide by 10 here to avoid precision loss */
/* dividing a signed long by 10 for some reason adds 100 bytes to program size?
* otherwise I would've divided gals by 10 earlier! */
return dte;
}
#endif
void bigNum (unsigned long t, char * txt1, char * txt2){
char decimalpoint = ' '; // decimal point is a space
char *r = "009.99"; // default to 999
if (DISPLAY_TYPE != dtBigChars) {
putCharsToLCD(&mylcd, &chars[0], LcdNewChars);
DISPLAY_TYPE = dtBigChars;
}
if(t<=99500){
r=format(t/10); // 009.86
decimalpoint=5; // special character 5
}
else if(t<=999500){
r=format(t/100); // 009.86
}
strcpy(&LCDBUF1[0], (bignumchars1+(getDigitFromAscii(r[2]))*4));
LCDBUF1[3] = ' ';
strcpy(&LCDBUF1[4], (bignumchars1+(getDigitFromAscii(r[4]))*4));
LCDBUF1[7] = ' ';
strcpy(&LCDBUF1[8], (bignumchars1+(getDigitFromAscii(r[5]))*4));
LCDBUF1[11] = ' ';
strcpy(&LCDBUF1[12], txt1);
strcpy(&LCDBUF2[0], (bignumchars2+(getDigitFromAscii(r[2]))*4));
LCDBUF2[3] = ' ';
strcpy(&LCDBUF2[4], (bignumchars2+(getDigitFromAscii(r[4]))*4));
LCDBUF2[7] = decimalpoint;
strcpy(&LCDBUF2[8], (bignumchars2+(getDigitFromAscii(r[5]))*4));
LCDBUF2[11] = ' ';
strcpy(&LCDBUF2[12], txt2);
#if (CFG_FUELCUT_INDICATOR != 0)
FCUT_POS = 3;
#endif
}
int insert(int *array, int val, size_t size, size_t at)
{
size_t i;
/* In range? */
if (at >= size) return -1;
/* Shift elements to make a hole */
for (i = size - 1; i > at; i--) {
array[i] = array[i - 1];
}
/* Insertion! */
array[at] = val;
return 0;
}
void save(){
EEPROM.write(0,guinosig);
EEPROM.write(1,parmsCount);
writeEepBlock32(0x04, &parms[0], parmsCount);
}
void writeEepBlock32(unsigned int start_addr, unsigned long *val, unsigned int size) {
unsigned char p = 0;
unsigned char shift = 0;
int i = 0;
for (start_addr; p < size; start_addr+=4) {
for (i=0; i<4; i++) {
shift = (8 * (3-i)); /* 24, 16, 8, 0 */
EEPROM.write(start_addr + i, (val[p]>>shift) & 0xFF);
}
p++;
}
}
void readEepBlock32(unsigned int start_addr, unsigned long *val, unsigned int size) {
unsigned long v = 0;
unsigned char p = 0;
unsigned char temp = 0;
unsigned char i = 0;
for(start_addr; p < size; start_addr+=4) {
v = 0; /* clear the scratch variable every loop! */
for (i=0; i<4; i++) {
temp = (i > 0) ? 1 : 0; /* 0, 1, 1, 1 */
v = (v << (temp * 8)) + EEPROM.read(start_addr + i);
}
val[p] = v;
p++;
}
}
unsigned char load(){ //return 1 if loaded ok
#ifdef usedefaults
return 1;
#endif
unsigned char b = EEPROM.read(0);
unsigned char c = EEPROM.read(1);
if (b == guinosigold) c=9; //before fancy parameter counter