-
Notifications
You must be signed in to change notification settings - Fork 0
/
RaceBoy-G35.ino
1457 lines (1326 loc) · 32.8 KB
/
RaceBoy-G35.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
/*
Name: RaceBoy-G35.ino
Created: 1/31/2016 5:23:27 PM
Updated: 9/6/2017
Author: John Daley
Most block comments are due to lab testing and no canbus to watch data
*/
// include the library code:
#include <EEPROM.h>
#include <Adafruit_CharacterOLED.h>
#include <avr/pgmspace.h>
#include <Button.h>
#include <mcp_can.h>
#include <SPI.h>
#include <LinkedList.h>
#include <SdFat.h>
#include "MenuMaster.h"
#include "Stopwatch.h"
//GPS Libs
#include <NMEAGPS.h>
#include <GPSport.h>
// STUFF FOR SOFTRESET
void(*resetFunc) (void) = 0; //declare reset function @ address 0
//MenuMaster Defines
#define BLOCK -1
#define NOTUSED 0
#define UPDOWN 1
#define ONOFF 2
#define CALLBACK 3
#define LIST 4
//Define Button Debounce Time
#define DEBOUNCE_MS 20
//Define Long Press Duration
#define LONG_PRESS 1000
//LCD Refresh Rate
#define lcdInterval 130 // interval at which to refresh LCD for logger screens
#define loggerInterval 1 // interval at which to refresh LCD (milliseconds)
//Calibrations and vars for Accl
#define zero_G_x 510
#define zero_G_y 507
#define zero_G_z 520
#define scale_x 18
#define scale_y 19
#define scale_z 20
//Accel Pin Declare
#define xpin A10 // x-axis of the accelerometer
#define ypin A11 // y-axis
#define zpin A12 // z-axis
//Define SPI speed for SDCard Class
#define SPI_SPEED SD_SCK_MHZ(50)
// Define for GPS
#define PMTK_SET_NMEA_UPDATE_10HZ "$PMTK220,100*2F"
#define PMTK_API_SET_FIX_CTL_5HZ "$PMTK300,200,0,0,0,0*2F"
#define PMTK_SET_NMEA_OUTPUT_RMCONLY "$PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29"
//Used for setting pausing without halting thread
unsigned long oldMillis = 0;
// initialize the OLED object
Adafruit_CharacterOLED Oled(OLED_V2, 33, 32, 34, 35, 36, 37, 38);
// initialize the Button Objects
Button UpButton = Button(31, true, true, DEBOUNCE_MS);
Button DownButton = Button(29, true, true, DEBOUNCE_MS);
Button LeftButton = Button(28, true, true, DEBOUNCE_MS);
Button RightButton = Button(30, true, true, DEBOUNCE_MS);
//Encoder Vars / Screen Scroller
bool loggerIndexChanged = false;
byte lastLoggerIndexPosition = 0;
byte newLoggerIndexPosition = 0;
int loggerCount; //For Arrow Drawing code
//Init Menu Position Vars
int menu1Index = 0;
int menu2Index = 0;
int menu3Index = 0;
int menuDepth = 0;
//Init the menu object
MenuMaster menu(menuDepth, menu1Index, menu2Index, menu3Index);
int *menuSize = menu.GetMenuSize(); //pointer for array of menu size to calculate menu limits
//init linked list
LinkedList<String> lgLcdList = LinkedList<String>();
//init accel values
float xAxisGValue;
float yAxisGValue;
//Declare custom LCD Characters for Scrolling LCD
byte upArrow[8] = {
0b00100,
0b01010,
0b01010,
0b10001,
0b10001,
0b00100,
0b00100,
0b00100
};
byte dnArrow[8] = {
0b00100,
0b00100,
0b00100,
0b10001,
0b10001,
0b01010,
0b01010,
0b00100
};
byte pipe[8] = {
0b00100,
0b00100,
0b00100,
0b00100,
0b00100,
0b00100,
0b00100,
0b00100
};
byte logIcon[8] = {
0b11000,
0b10100,
0b10100,
0b11000,
0b00100,
0b00100,
0b00100,
0b00111
};
bool longpressUp = false; //long press bool
// Create CANBUS reader object
MCP_CAN CAN0(9);
//Declare CANBUS vars
long unsigned int rxId; //Var for holding PID of incoming frame
unsigned char len = 0; //Var for showing frame length in bytes
unsigned char rxBuf[8]; //Array for incoming frame buffer
byte retry;
//Declare temp data holders
int TPS = 0;
int PDL = 0;
int RPM = 0;
int SPD = 0;
int BRK = 0;
int SAS = 0;
int MAP = 0;
bool canSilent = false; //Should be true is no activity on canbus for 100 loop cycles
bool screenAwake = true;
bool screenDim = false;
bool recvA = false;
bool recvB = false;
bool recvC = false;
bool recvD = false;
int tmpA;
int tmpB;
int loggerListSize = 0;
//const char* const PROGMEM menuTable[9] = {(pgm_read_word(&(MAP))),(pgm_read_word(&(TPS))),(pgm_read_word(&(RPM))),(pgm_read_word(&(speed))),(pgm_read_word(&(IAT))),(pgm_read_word(&(ECT))),(pgm_read_word(&(advance))),(pgm_read_word(&(brake))),"Accel"};
//Define **IN SAME ORDER AS MENU** what should be displayed on logger screen - Must be 10 char EXACTLY - ??? means i dont know what the value actually means, but it moves with engine
const char* loggerTexts[10] = { "Throttle ", "Gas Pedal ", "Brake ", "Speed ", "Engine RPM", "Turn Angle", "??? ", "Empty ", "Lateral-G ", "Long-G " };
char loggerIndex[10] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
char menuModifier;
//Inits for SDCard Logging
//OLD LIB: File sdLog;
SdFat sd;
SdFile sdLog;
SdFile trackFile;
bool loggerRunning;
int file = 0;
char fileString[12];
byte sd_cs_pin = 4;
StopWatch logTime;
StopWatch lapTime;
bool sdReady = false;
// Maximum line length plus space for zero byte.
const size_t LINE_DIM = 100;
char line[LINE_DIM];
size_t n;
//GPSVariable Definitions
static NMEAGPS gps;
static gps_fix fix;
//LapTimer Variable Definitions
int zone = 0;
int maxZones = 0;
int laps = 0;
long latitudeTraps[4];
long longitudeTraps[4];
String zoneName;
bool trapsLoaded = false;
bool onTrack = false;
void setup() {
Serial.begin(115200); //Begin Serial Out
// OLD Deprecated Start Function CAN0.begin(CAN_500KBPS); // Begin can bus : baudrate = 500k
if (CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK)
Serial.println("MCP2515 Initialized Successfully!");
else
Serial.println("Error Initializing MCP2515...");
CAN0.setMode(MCP_NORMAL);
pinMode(2, INPUT); //Setting pin 2 for /INT input
// set up the LCDs number of columns and rows
//lcdSm.begin(16, 2);
Oled.begin(20, 4);
//Setup Accel
pinMode(xpin, INPUT);
pinMode(ypin, INPUT);
pinMode(zpin, INPUT);
analogReference(EXTERNAL); //Look to ARef pin for analog reference voltage
//create custom lcd objects
Oled.createChar(0, upArrow);
Oled.createChar(1, dnArrow);
Oled.createChar(2, pipe);
Oled.createChar(3, logIcon);
delay(100);
CAN0.init_Mask(0, 0, 0x189C0000);
CAN0.init_Filt(0, 0, 0x18940000);
CAN0.init_Filt(1, 0, 0x00080000);
CAN0.init_Mask(1, 0, 0x1FFC0000);
CAN0.init_Filt(2, 0, 0x08F40000);
CAN0.init_Filt(3, 0, 0x1E480000);
CAN0.init_Filt(4, 0, 0x0B440000);
CAN0.init_Filt(5, 0, 0x0B440000);
//Setup SD Card
pinMode(sd_cs_pin, OUTPUT);
if (!sd.begin(sd_cs_pin, SPI_SPEED))
{
retry = 0;
while (retry < 2)
{
retry++;
if (sd.begin(sd_cs_pin, SPI_SPEED))
{
Serial.println(F("Card Ready"));
sdReady = true;
break;
}
}
if (!sdReady)
{
sdReady = false;
Serial.println(F("Card Failure"));
}
}
else
{
sdReady = true;
Serial.println(F("Card Ready"));
}
//Read from EEPROM and set vars up
ReadFromEEPROM();
//Turn LCD to full Bright
Oled.command(0x17);
}
//Go Home Function - Resets menu position
void GoHome()
{
Oled.clear();
menuDepth = 0;
menu1Index = 0;
menu2Index = 0;
menu3Index = 0;
}
//button up code
void CheckUpButton()
{
if (UpButton.wasReleased() && !longpressUp)
{
Oled.clear();
if (menu.IsSelectable()) // Check if YesNo Menu
{
menu.SelectUp();
}
else if (menu.GetMenuType(menu1Index, menu2Index, menu3Index) == LIST) // Check if YesNo Menu
{
menu.SelectUp();
menu.LoadTracks(sd, trackFile);
}
else if (!menu.IsSelectable()) // Check if YesNo Menu
{
switch (menuDepth) {
case -1:
//Code for loggerup
newLoggerIndexPosition--;
case 0:
if (menu1Index >= (*(menuSize + 0) - 1) || menu.GetMenuType((menu1Index + 1), menu2Index, menu3Index) == BLOCK) {} //The Index +1 is a look ahead to see if menu item can be accessed
else //Sets Limits for menu bounds
menu1Index++;
break;
case 1:
if (menu2Index >= *(menuSize + 1) || menu.GetMenuType(menu1Index, (menu2Index + 1), menu3Index) == BLOCK) {} //The Index +1 is a look ahead to see if menu item can be accessed
else //Sets Limits for menu bounds
menu2Index++;
break;
case 2:
if (menu3Index >= *(menuSize + 2) || menu.GetMenuType(menu1Index, menu2Index, (menu3Index + 1)) == BLOCK) {} //The Index +1 is a look ahead to see if menu item can be accessed
else //Sets Limits for menu bounds
menu3Index++;
break;
case 3:
//code
break;
default:
//code
break;
}
}
}
longpressUp = false;
//button up longpress
if (UpButton.pressedFor(LONG_PRESS)) {
GoHome();
menu.SetLoggersTrue();
longpressUp = true;
}
}
//button down code
void CheckDownButton()
{
if (DownButton.wasReleased())
{
Oled.clear();
if (menu.IsSelectable()) // Check if YesNo Menu
{
menu.SelectDown();
}
else if (menu.GetMenuType(menu1Index, menu2Index, menu3Index) == LIST) // Check if YesNo Menu
{
menu.SelectDown();
menu.LoadTracks(sd, trackFile);
}
else if (!menu.IsSelectable()) // Check if YesNo Menu
{
switch (menuDepth) {
case -1:
//Code for loggerdown
newLoggerIndexPosition++;
case 0:
if (menu1Index == 0) {}
else { //Sets Limits for menu bounds
menu1Index--;
}
break;
case 1:
if (menu2Index == 1) {}
else { //Sets Limits for menu bounds
menu2Index--;
}
break;
case 2:
if (menu3Index == 1) {}
else { //Sets Limits for menu bounds
menu3Index--;
}
break;
case 3:
//code
break;
default:
//code
break;
}
}
}
}
//button left code
void CheckLeftButton()
{
if (LeftButton.wasReleased())
{
Oled.clear();
if (menu.IsSelectable()) // Check if YesNo Menu
{
switch (menuDepth) {
case 0:
menu1Index = 0;
break;
case 1:
menu2Index = 0; //Go to index zero to selected menu
break;
case 2:
menu3Index = 0; //Go to index zero to selected menu
break;
case 3:
//code
break;
default:
//code
break;
}
if (menuDepth <= -1) // If at level zero menu
{
//DO NOTHING BECAUSE AINT SHIT HERE
}
else
{
menuDepth--;
}
}
else if (!menu.IsSelectable())
{
switch (menuDepth) {
case 0:
break;
case 1:
menu2Index = 0; //Go to index zero to selected menu
break;
case 2:
menu3Index = 0;//Go to index zero to selected menu
break;
case 3:
//code
break;
default:
//code
break;
}
if (menuDepth <= -1) // If at level zero menu
{
//DO NOTHING BECUZ SHIT CANT EXIST LOWER
}
else
{
menuDepth--; //else to back a menu
}
}
}
}
//button right code
void CheckRightButton()
{
if (RightButton.wasReleased()) {
Oled.clear();
if (menu.IsSelectable()) // Check if YesNo (selectable) Menu
{
menu3Index = 0;
menuDepth--; // Go back a menu
}
else if (!menu.IsSelectable()) // Check if YesNo (selectable) Menu
{
if (menu.GetMenuType(menu1Index, menu2Index, menu3Index) == CALLBACK)// Check if the menu calls back to a subroutine
{
switch (menuDepth) {
case -1:
break;
case 0:
if (menu1Index == 2)
{
//This shouldnt be called as menu1index2 isnt a callback
}
else if (menu1Index == 3)
{
//Do The Soft reset
resetFunc();
}
GoHome();
//code
break;
case 1:
GoHome();
break;
case 2:
GoHome();
//code
break;
case 3:
GoHome();
//code
break;
default:
GoHome();
//code
break;
}
}
else if (menu.GetMenuType(menu1Index, menu2Index, menu3Index) == LIST)
{
switch (menuDepth) {
case -1:
break;
case 0:
break;
case 1:
if (menu1Index == 2)
{
if ((menu.trackSelectIndex + menu.trackReadIndex) == 0)
{
GoHome();
break;
}
//Do the List Logic
byte k = 0;
if (!sd.chdir("Tracks")) {
//error("chdir failed for track traps folder.\n");
}
Serial.print("Selecting: ");
Serial.println((menu.trackSelectIndex + menu.trackReadIndex));
while (trackFile.openNext(sd.vwd(), O_READ)) {
k++;
if (k == (menu.trackSelectIndex + menu.trackReadIndex))
{
Serial.print("Selected Track: ");
trackFile.printName(&Serial);
Serial.println();
break;
}
trackFile.close();
}
sd.chdir("/");
sd.vwd()->rewind();
menuDepth--;
menu2Index = 0;
}
else if (menu1Index == 3)
{
//Wont be called
GoHome();
}
else
{
GoHome();
}
//code
break;
case 2:
if (menu1Index == 2)
{
GoHome();
}
else if (menu1Index == 3)
{
//Wont be called
GoHome();
}
else
{
GoHome();
}
break;
case 3:
GoHome();
//code
break;
default:
GoHome();
//code
break;
}
}
else
{
if (menuDepth > 2) {}
else // limit to menu size
menuDepth++; //Progress to next menu depth
switch (menuDepth) {
case -1:
break;
case 0:
menu1Index = 0;
//code
break;
case 1:
menu2Index = 1;
if (menu1Index == 2)
{
trackFile.close();
//Do the List Logic
byte k = 0;
if (!sd.chdir("Tracks")) {
Serial.println(F("chdir failed for track traps folder."));
}
while (trackFile.openNext(sd.vwd(), O_READ)) {
k++;
Serial.print(k, DEC);
Serial.print(". ");
trackFile.printName(&Serial);
Serial.println();
trackFile.close();
}
sd.chdir("/");
sd.vwd()->rewind();
menu.trackCount = k;
menu.trackSelectIndex = 0;
menu.trackReadIndex = 0;
menu.LoadTracks(sd, trackFile);
}
break;
case 2:
menu3Index = 1;
break;
case 3:
//code
break;
default:
//code
break;
}
}
}
}
}
//Clear a row of logger screen
void ClearRow(int rowNum)
{
Oled.setCursor(0, rowNum);
Oled.print(F(" "));
}
//Code to print to lcd screens
void PrintLcd(char screen, int col, int row, String message)
{
switch (screen)
{
case 's':
Oled.print(F("No sm LCD in this version"));
break;
case 'l':
Oled.setCursor(col, row);
Oled.print(message);
break;
default:
Oled.print(F("Some Fatal Error"));
}
}
// Returns Free Ram of Arduino - Used for debugging
int FreeRam()
{
extern int __heap_start, *__brkval;
int v;
return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int)__brkval);
}
//Updates Large LCD position based on menu modifier number
void RefreshLoggerList()
{
//Large LCD Scrolling Code to set menu modifier
if (newLoggerIndexPosition != lastLoggerIndexPosition) {
lastLoggerIndexPosition = newLoggerIndexPosition;
if ((newLoggerIndexPosition < (lgLcdList.size() - 3)) && newLoggerIndexPosition >= 0)
{
menuModifier = newLoggerIndexPosition;
loggerIndexChanged = true;
}
else if ((newLoggerIndexPosition >= (lgLcdList.size() - 3)))
{
newLoggerIndexPosition = (lgLcdList.size() - 4);
loggerIndexChanged = true;
}
else
{
newLoggerIndexPosition = 0;
}
}
}
//Overloaded for custom linked list sizes (not really used)
void RefreshLoggerList(byte size)
{
//Large LCD Scrolling Code to set menu modifier
if (newLoggerIndexPosition != lastLoggerIndexPosition) {
lastLoggerIndexPosition = newLoggerIndexPosition;
if ((newLoggerIndexPosition < (size - 3)) && newLoggerIndexPosition >= 0)
{
menuModifier = newLoggerIndexPosition;
loggerIndexChanged = true;
}
else if ((newLoggerIndexPosition >= (size - 3)))
{
newLoggerIndexPosition = (lgLcdList.size() - 4);
loggerIndexChanged = true;
}
else
{
newLoggerIndexPosition = 0;
}
}
}
//Draw Arrow for more than 4 loggers
void UpdateScrollbar()
{
if (loggerCount > (4 + menuModifier))
{
Oled.setCursor(19, 2);
Oled.write(byte(2));
Oled.setCursor(19, 3);
Oled.write(byte(1));
}
else
{
Oled.setCursor(19, 2);
Oled.print(" ");
Oled.setCursor(19, 3);
Oled.print(" ");
}
if (loggerCount > 4 && menuModifier > 0)
{
Oled.setCursor(19, 0);
if (loggerRunning)
{
Oled.write(byte(3));
}
else
{
Oled.write(byte(0));
}
Oled.setCursor(19, 1);
Oled.write(byte(2));
}
else
{
Oled.setCursor(19, 0);
if (loggerRunning)
{
Oled.write(byte(3));
}
else
{
Oled.print(" ");
}
Oled.setCursor(19, 1);
Oled.print(" ");
}
}
//Returns digit length
int GetIntLength(int number)
{
int digits = 0;
if (number < 0) digits = 1; // remove this line if '-' counts as a digit
if (number != 0)
{
while (number) {
number /= 10;
digits++;
}
}
else
{
return 1;
}
return digits;
}
//Formats The Large LCD String
String FormatString(String text, float value, byte pres)
{
String output;
byte strLEN = text.length();
int roundVal;
if (pres == 0)
{
roundVal = round(value);
char valLEN = GetIntLength(roundVal);
String endBuf;
if (valLEN == 5)
endBuf = F("");
else if (valLEN == 4)
endBuf = F(" ");
else if (valLEN == 3)
endBuf = F(" ");
else if (valLEN == 2)
endBuf = F(" ");
else if (valLEN == 1)
endBuf = F(" ");
output = text + F(" ") + roundVal + endBuf;
}
else
{
String endBuf;
if (GetIntLength((int)value) == 1)
{
if (value < 0)
{
endBuf = F(" ");
}
else
{
endBuf = F(" ");
}
}
//aint no way im hitting 100 g's so this works fine
else
{
if (value < 0)
{
endBuf = F("");
}
else
{
endBuf = F(" ");
}
}
value = (int)(value * 100);
value = (float)(value / 100);
output = text + F(" ") + value + endBuf;
}
return output;
}
//Updates Logger Variables with current CANBUS data
void UpdateLoggers()
{
recvA = false;
recvB = false;
recvC = false;
recvD = false;
do {
if (!digitalRead(2)) // If pin 2 is low, read receive buffer
{
if (!screenAwake)
{
Oled.display();
screenAwake = true;
}
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
//OLD Depricated rxID method rxId = CAN0.getCanId(); // Get message ID
if (rxId == 0x792 && !recvA)
{//Brake Light and individual wheel speeds
BRK = ((rxBuf[2] * 100) / 255);
recvA = true;
}
if (rxId == 0x2D1 && !recvB)
{//Contains maybe MAP?
tmpA = 0;
tmpB = 0;
tmpA = rxBuf[2] * 256;
tmpB = rxBuf[1];
SPD = ((tmpA + tmpB) / 16);
MAP = rxBuf[5];
recvB = true;
}
if (rxId == 0x23D && !recvC)
{//Two TPS and two byte RPM
tmpA = 0;
tmpB = 0;
tmpA = rxBuf[4] * 256;
tmpB = rxBuf[3];
RPM = ((tmpA + tmpB) * 3.15);
TPS = ((rxBuf[2] * 100) / 255);
PDL = ((rxBuf[1] * 100) / 255);
recvC = true;
}
if (rxId == 0x2 && !recvD)
{//Steering Angle Sensor and velocity
tmpA = 0;
tmpB = 0;
tmpB = rxBuf[1] * 256;
tmpA = rxBuf[0];
tmpA = (tmpA + tmpB);
if (tmpA > 32767)
SAS = -(65535 - tmpA / 10);
else
SAS = tmpA / 10;
recvD = true;
}
if (rxId == 0x625)
{
if (rxBuf[3] != 0x90 && rxBuf[3] != 0x10)
{
canSilent = true;
}
}
if (rxId == 0x625)
{
// Reading with bit AND operator for masking the whole byte to just the three bits we care about for this rxId
if ((rxBuf[1] & 0x70) == 0x0)
{
if (screenDim)
{
Oled.command(0x17);
screenDim = false;
}
}
else
{
if (!screenDim)
{
Oled.command(0x13);
screenDim = true;
}
}
}
}
} while (!recvA && !recvB && !recvC && !recvD);
/*
BRK = random(10000);
SPD = random(10000);
MAP = random(10000);
TPS = random(10000);
RPM = random(10000);
PDL = random(10000);
SAS = random(10000);
*/
/*
Serial.print(TPS);
Serial.print(" , ");
Serial.print(PDL);
Serial.print(" , ");
Serial.print(BRK);
Serial.print(" , ");
Serial.print(SPD);
Serial.print(" , ");
Serial.print(RPM);
Serial.print(" , ");
Serial.print(SAS);
Serial.println();
*/
}
//Save logger states and indexes to EEPROM
void SaveToEEPROM()
{
// Get Array Sizes for EEPROM write loops
int menuLoggersSize = sizeof(menu.loggers) / sizeof(bool);
int loggerIndexSize = sizeof(loggerIndex) / sizeof(char);
//For Each item in the T/F logger state; write to eeprom
for (int i = 0; i < menuLoggersSize; i++)
EEPROM.write(i, menu.loggers[i]);
// Continuing the EEPROM addresses from above, write the logger index values
for (int i = menuLoggersSize; i < (loggerIndexSize + menuLoggersSize); i++)
EEPROM.write(i, loggerIndex[i - menuLoggersSize]);
}
// Read logger states and positions from EEPROM
void ReadFromEEPROM()
{
//Not sure if i really need this, but it helps it from reading trash if nothing is loaded into eeprom (Good place to do data validation if i get around to it)
if (EEPROM.read(0) != 0xff)