-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodice.ino
259 lines (203 loc) · 6.16 KB
/
codice.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
#include "DueCANLayer.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include "Adafruit_RA8875.h"
#include <stdint.h>
#include <SPI.h>
#include <Wire.h>
// CAN Layer functions
extern byte canInit(byte cPort, long lBaudRate);
extern byte canRx(byte cPort, long* lMsgID, bool* bExtendedFormat, byte* cData, byte* cDataLen);
// definitions for display
uint8_t addr = 0x38;
#define RA8875_INT 4
#define RA8875_CS 10
#define RA8875_RESET 9
#define FT5206_WAKE 6
#define FT5206_INT 7
Adafruit_RA8875 tft = Adafruit_RA8875(RA8875_CS, RA8875_RESET);
uint16_t tx, ty;
enum {
eNORMAL = 0,
eTEST = 0x04,
eSYSTEM = 0x01
};
# define ADJUST_X -50
# define ADJUST_Y -100
# define GEAR_X 100 + ADJUST_X
# define GEAR_Y 100 + ADJUST_Y
# define RPM_X 250 + ADJUST_X
# define RPM_Y 100 + ADJUST_Y
# define OIL_X 450 + ADJUST_X
# define OIL_Y 100 + ADJUST_Y
# define WATER_X 650 + ADJUST_X
# define WATER_Y 100 + ADJUST_Y
# define SPEED_X 370 + ADJUST_X
# define SPEED_Y 270 + ADJUST_Y
# define LAM1_X 100 + ADJUST_X
# define LAM1_Y 400 + ADJUST_Y
# define LAM2_X 300 + ADJUST_X
# define LAM2_Y 400 + ADJUST_Y
# define MAP_X 510 + ADJUST_X
# define MAP_Y 400 + ADJUST_Y
# define TPS_X 670 + ADJUST_X
# define TPS_Y 400 + ADJUST_Y
void setup()
{
// Set the serial interface baud rate
Serial.begin(115200);
//setup for display
Wire.begin(); // join i2c bus (address optional for master)
pinMode (FT5206_WAKE, INPUT);
digitalWrite(FT5206_WAKE, HIGH );
while (!tft.begin(RA8875_800x480))
{
Serial.println("RA8875 Not Found!");
delay(100);
}
Serial.println("Found RA8875");
tft.displayOn(true);
tft.GPIOX(true); // Enable TFT - display enable tied to GPIOX
tft.PWM1config(true, RA8875_PWM_CLK_DIV1024); // PWM output for backlight
tft.PWM1out(255); //255: maximum brightness, 0: minimum brightness
tft.fillScreen(RA8875_BLACK);
tft.textMode();
tft.textColor(RA8875_BLUE, RA8875_BLACK);
tft.textEnlarge(2);
tft.textSetCursor(SPEED_X,SPEED_Y);
tft.textWrite("SPEED");
tft.textColor(RA8875_YELLOW, RA8875_BLACK);
tft.textSetCursor(GEAR_X,GEAR_Y);
tft.textWrite("GEAR");
tft.textSetCursor(RPM_X,RPM_Y);
tft.textWrite("RPM") ;
tft.textSetCursor(OIL_X,OIL_Y);
tft.textWrite("OIL");
tft.textSetCursor(WATER_X, WATER_Y);
tft.textWrite("WATER");
tft.textSetCursor(LAM1_X,LAM1_Y);
tft.textWrite("LAM1");
tft.textSetCursor(LAM2_X,LAM2_Y);
tft.textWrite("LAM2");
tft.textSetCursor(MAP_X, MAP_Y);
tft.textWrite("MAP");
tft.textSetCursor(TPS_X,TPS_Y);
tft.textWrite("TPS");
// setup for CAN controllers
if(canInit(0, CAN_BPS_1000K) != CAN_ERROR)
Serial.print("CAN0: Initialized Successfully.\n\r");
else
Serial.print("CAN0: Initialization Failed.\n\r");
}// end setup
int big_to_little(byte h, byte l) {
int out = h *16 *16;
return out + l;
}
void loop()
{
tft.textEnlarge(3);
while(1) // Endless loop
{
delay(1);
// Check for received message
long lMsgID;
bool bExtendedFormat;
byte cRxData[8];
byte cDataLen;
char str[20];
memset(str, 0, 20);
if(canRx(0, &lMsgID, &bExtendedFormat, &cRxData[0], &cDataLen) != CAN_ERROR)
{
/* DEBUG
for(byte cIndex = 0; cIndex < cDataLen; cIndex++)
{
Serial.print(cRxData[cIndex], HEX);
Serial.print(" ");
}// end for
Serial.print("\n\r");
*/
//print of display
switch (lMsgID) { //hoping the data is big endian
case 0x618:
{
tft.textColor(RA8875_BLUE, RA8875_BLACK);
int speed_avg = big_to_little(cRxData[0], cRxData[1]) + big_to_little(cRxData[2], cRxData[3]);
// The speed is an average of the two wheels speed.
speed_avg >>= 1;
sprintf(str, "%-6d", speed_avg);
tft.textSetCursor(SPEED_X,SPEED_Y + 50);
tft.textWrite(str);
tft.textColor(RA8875_YELLOW, RA8875_BLACK);
break;
}
case 0x604: // gear, byte 7-8
{
int gear = big_to_little(cRxData[6], cRxData[7]);
if (gear >= 2 and gear <= 8) {
sprintf(str, "%d", gear-2);
} else {
sprintf(str, "N");
}
tft.textSetCursor(GEAR_X,GEAR_Y + 50);
tft.textWrite(str);
break;
}
case 0x60A: // water temperature ect1, byte 5-6
{
float ect1 = big_to_little(cRxData[4], cRxData[5])/10.0;
sprintf(str, "%-6.2f", ect1);
tft.textSetCursor(WATER_X,WATER_Y + 50);
tft.textWrite(str);
break;
}
case 0x60C: // oil temperature, eot1, byte 1-2
{
float eot1 = big_to_little(cRxData[0], cRxData[1])/10.0;
sprintf(str, "%-6.2f", eot1);
tft.textSetCursor(OIL_X,OIL_Y + 50);
tft.textWrite(str);
break;
}
// LAM1 610, 1-2
// LAM2 610, 3-4
case 0x610:
{
float lam = big_to_little(cRxData[0], cRxData[1])/1000.0;
sprintf(str, "%-5.3f", lam);
tft.textSetCursor(LAM1_X,LAM1_Y + 50);
tft.textWrite(str);
lam = big_to_little(cRxData[2],cRxData[3])/1000.0;
sprintf(str, "%-5.3f", lam);
tft.textSetCursor(LAM2_X,LAM2_Y + 50);
tft.textWrite(str);
int rpm = big_to_little(cRxData[4],cRxData[5]);
sprintf(str, "%-5d ", rpm);
tft.textSetCursor(RPM_X,RPM_Y + 50);
tft.textWrite(str);
break;
break;
}
// MAP 600, 7-8
case 0x624:
{
int MAP = big_to_little(cRxData[0],cRxData[1]);
sprintf(str, "%-5d", MAP);
tft.textSetCursor(MAP_X,MAP_Y + 50);
tft.textWrite(str);
break;
}
// TPS 608, 1-2
case 0x608:
{
float tps = big_to_little(cRxData[0],cRxData[1])/81.92;
tps = (tps> 100)? 100.0: tps;
sprintf(str, "%-4.1f ", tps );
tft.textSetCursor(TPS_X,TPS_Y + 50);
tft.textWrite(str);
break;
}
}//end switch
}// end if
}// end while
}// end loop