-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTD5Fuel.ino
executable file
·187 lines (144 loc) · 4.47 KB
/
TD5Fuel.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
#include <SPI.h>
#if defined ARDUINO_MAPLEMINI_F103CB || defined ARDUINO_BLUEPILL_F103CB || defined ARDUINO_BLUEPILL_F103C8 || defined ARDUINO_BLACKPILL_F303CC
#include "LRDuinoDefsMM.h"
#define Serial SerialUSB
#define ARCH_DEFINED
#endif
//#define ENCODER
#ifndef ARCH_DEFINED
#error Unsupported core \ board combination
#endif
// uncommment the define that matches your display type
//#define USE_SSD1306
#define USE_SSD1331
//#define USE_SSD1351
#include <Adafruit_GFX.h>
#include "Macros.h"
#if defined USE_SSD1306
#include <Adafruit_SSD1306.h>
#define SCREEN_CHOSEN
Adafruit_SSD1306 display1(SCREENWIDTH, SCREENHEIGHT, &SPI, OLED_DC, OLED_RESET, OLEDCS_1);
#endif
#if defined USE_SSD1331
#include <Adafruit_SSD1331.h>
#define SCREEN_CHOSEN
Adafruit_SSD1331 display1(OLEDCS_1, OLED_DC, OLED_RESET);
#endif
#if defined USE_SSD1351
#include <Adafruit_SSD1351.h>
#define SCREEN_CHOSEN
Adafruit_SSD1351 display1(SCREENWIDTH, SCREENHEIGHT, &SPI, OLEDCS_1, OLED_DC, OLED_RESET);
#endif
#ifndef SCREEN_CHOSEN
#error Please choose a screen type by uncommenting one choice only eg "#define USE_SSD1306"
#endif
#include "LRDuinoGFX.h"
#include <Fonts/FreeSansBoldOblique9pt7b.h>
#include <Fonts/FreeSansBoldOblique12pt7b.h>
#include <Fonts/FreeSansBoldOblique18pt7b.h>
#include <Fonts/FreeSansBoldOblique24pt7b.h>
#include "td5comm.h"
// #include <Buttons.h>
Td5Comm td5; // setup ODBC object
// Button btn_enter(SELBUT, LOW);
#include "td5sensors.h"
// Insert code to read settings in here, and also set default states of sensors to hidden if hw is not present
// end settings reading section
uint8_t sensecount = 0;
// the follow variable is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long previousMillis = 0; // will store last time the displays were updated
unsigned long menuMillis = 0;
unsigned long inptimeoutMillis = 0;
int secindex = 0;
bool secfull = false;
int seconds[60] = {};
int minindex = 0;
bool minfull = false;
int minutes[60] = {};
#define textScale 1
#define fontX 5
#define fontY 9
void setup()
{
//start serial connection
//Serial.begin(57600); //uncomment to send serial debug info
#if defined ARDUINO_ARCH_STM32
#if defined BOARD_maple_mini || defined BOARD_generic_stm32f103c
// do nothing
#else
analogReadResolution(12); //12 bits
#endif
#endif
pinMode(SELBUT, INPUT_PULLUP);
// Pin setup
pinMode(OLEDCS_1, OUTPUT);
digitalWrite(OLEDCS_1, HIGH);
display1.begin(8000000); //construct our displays
SCREEN_CLEAR(); // clears the screen and buffer
SCREEN_DISPLAY(); //output to the screen to avoid adafruit logo
getSensecount(); //check how many sensors we have (in case any are programatically disabled
initOBD(); // this also fires getSensecount()
} // End Setup
void loop()
{
unsigned long currentMillis = millis(); //store the time
int minavg = 0;
int secavg = 0;
int i = 0;
// USER INPUT
if (currentMillis - previousMillis > INTERVAL) { // only read the sensors and draw the screen if 250 millis have passed
previousMillis = currentMillis; // save the last time we updated
if(td5.ecuIsConnected()) { // keep ecu alive
if(td5.getLastReceivedPidElapsedTime() > KEEP_ALIVE_TIME) {
td5.getPid(&pidKeepAlive);
}
if (td5.getConsecutiveLostFrames() > 3) { // shutdown in case of too many frames lost
//disableOBDSensors();
td5.disconnectFromEcu();
SCREEN_CLEAR();
}
}
// Get teh OBD data
readSensor(0);
/*
// do seconds & minute handling to obtain an average - TODO, discount values where vehicle is stationary.
secindex++;
if(secindex==60) {
secindex=0;
secfull=true;
minindex++;
}
seconds[secindex]=Sensors[0].sensevals;
int avgloop = 0;
if(secfull) {
avgloop=60;
} else {
avgloop=secindex;
}
for(i=1; i<avgloop+1; i++)
{
secavg = secavg + seconds[i];
}
secavg = secavg / avgloop;
minutes[minindex]=secavg;
if(minindex==60) {
minindex=0;
minfull=true;
}
if(minfull) {
avgloop=60;
} else {
avgloop=minindex;
}
for(i=1; i<avgloop+1; i++)
{
minavg = minavg + minutes[i];
}
minavg = minavg / avgloop;
Sensors[0].sensevals=minavg;
*/
// draw the display
drawDISPLAY(display1, 1);
}
} // Void Loop()