-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrangerthings.ino
115 lines (96 loc) · 3.44 KB
/
rangerthings.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
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <SPI.h>
#include <OBD2UART.h>
COBD obd;
// Display setup
#define TFT_CS 10
#define TFT_RST 9 // Or set to -1 and connect to Arduino RESET pin
#define TFT_DC 8
#define BACKGROUND_COLOR ST77XX_BLACK
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST); // For 1.14", 1.3", 1.54", and 2.0" TFT with ST7789:
bool isDisplaySleeping = false;
void setup() {
tft.init(240, 240); // Init ST7789 240x240
tft.fillScreen(BACKGROUND_COLOR);
tft.setTextSize(5);
drawtextByLine("FORD\nRANGER", ST77XX_BLUE, 0);
// Wait for connection to adapter
for (;;) {
delay(1000);
byte version = obd.begin();
drawtextByLine("OBDbegin", ST77XX_WHITE, 0);
drawtextByLine("Connect?", ST77XX_WHITE, 40);
if (version > 0) {
drawtextByLine("Detected", ST77XX_GREEN, 80);
break;
} else {
drawtextByLine("Not\ndetected", ST77XX_RED, 80);
}
}
tft.fillScreen(BACKGROUND_COLOR);
drawtextByLine("OBD Init?", ST77XX_WHITE, 0);
drawtextByLine("Waiting..", ST77XX_WHITE, 60);
while (!obd.init()); // Initiate OBD-II connection until success
drawtextByLine("OBD2 OK!", ST77XX_GREEN, 80);
// Reset display
delay(1000);
tft.fillScreen(BACKGROUND_COLOR);
// Draw titles on startup to reduce the time it takes to redraw the display (improves fps)
drawtextByLine("RPM:", ST77XX_BLUE, 0);
drawtextByLine("BOOST:", ST77XX_BLUE, 80);
drawtextByLine("WATTEMP:", ST77XX_BLUE, 160);
// Check for error codes on startup? Flash display if true
// tft.invertDisplay(true);
// delay(500);
// tft.invertDisplay(false);
// delay(500);
}
void loop() {
// Get values from OBD
int RPMvalue;
if (obd.readPID(PID_RPM, RPMvalue)) {
if (isDisplaySleeping) {
tft.enableSleep(false); // Wake up display if its asleep AND we're getting a value
isDisplaySleeping = false;
}
if (RPMvalue < 1000) {
drawtextByLine(String(RPMvalue) + " ", ST77XX_WHITE, 40);
} else {
drawtextByLine(String(RPMvalue), ST77XX_WHITE, 40);
}
} else { // Sleep display if no PID values read
tft.enableSleep(true);
isDisplaySleeping = true;
}
int WATERvalue;
if (obd.readPID(PID_COOLANT_TEMP, WATERvalue)) {
if (WATERvalue > 96) {
drawtextByLine(String(WATERvalue), ST77XX_RED, 200);
} else {
drawtextByLine(String(WATERvalue), ST77XX_WHITE, 200);
}
}
int MAPvalue;
int BAROvalue;
if (obd.readPID(PID_INTAKE_MAP, MAPvalue)) {
if (obd.readPID(PID_BAROMETRIC, BAROvalue)) {
int BOOSTvalue = MAPvalue - BAROvalue; //calculate boost pressure
BOOSTvalue = map(BOOSTvalue, 0, 255, 0, 37); //convert to PSI (https://mechanics.stackexchange.com/questions/45239/calculate-boost-from-map-sensor-via-obd-ii)
drawtextByLine(String(BOOSTvalue) + " psi ", ST77XX_WHITE, 120);
}
}
// Example for reading multiple PIDs at once -- https://github.com/stanleyhuangyc/ArduinoOBD/blob/master/libraries/OBD2UART/examples/obd_uart_test/obd_uart_test.ino#L69
}
void drawtext(String text, uint16_t color) {
tft.setCursor(0, 0);
tft.setTextColor(color, BACKGROUND_COLOR);
tft.setTextWrap(true);
tft.print(text);
}
void drawtextByLine(String text, uint16_t color, uint16_t line) {
tft.setCursor(0, line);
tft.setTextColor(color, BACKGROUND_COLOR);
tft.setTextWrap(false);
tft.print(text);
}