-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtripComp.cpp
More file actions
316 lines (314 loc) · 9.22 KB
/
tripComp.cpp
File metadata and controls
316 lines (314 loc) · 9.22 KB
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
//-----------------------------------------------------------------------------
#include <stdio.h>
#include "avr-debug/debug.h"
#include "tripComp.h"
#include "units.h"
#include "body.h"
#include "options.h"
//-----------------------------------------------------------------------------
enum
{
TRIP_MODE_TIME,
TRIP_MODE_DISTANCE,
TRIP_MODE_DAY_DISTANCE,
TRIP_MODE_SPEED,
TRIP_MODE_AVG_SPEED,
TRIP_MODE_FUEL,
TRIP_MODE_AVG_FUEL,
tripModesCount
};
//-----------------------------------------------------------------------------
static const char empty_string[] = "---";
static char string[10];
static BYTE_DATA tripMode = TRIP_MODE_TIME;
static bool tripInit = false;
BYTE_DATA speed = 0;
BYTE_DATA tripDay = 32; // день от даты для отслеживания перехода на следующие сутки
WORD_DATA tripDistance = 0;
WORD_DATA dayDistance = 0;
WORD_DATA totalDriveDistance = 0;
WORD_DATA tripTimer = 0; // sec
WORD_DATA tripTimerMinutes = 0;
DWORD_DATA totalDriveTime = 0;
WORD_DATA averageSpeed = -1;
WORD_DATA fuelRateTime = -1;
WORD_DATA fuelRateDist = -1;
WORD_DATA averageFuelRate = -1;
CFontAreaSet disp_tripComp;
CFontArea* disp_tripCompCaption1;
CFontArea* disp_tripCompCaption2;
CImprovedAreaSet disp_tripCompValue;
CFontArea* disp_tripCompUnit;
//-----------------------------------------------------------------------------
void tripModeChange()
{
tripMode = (tripMode.value < tripModesCount - 1) ? tripMode.value + 1 : 0;
}
//-----------------------------------------------------------------------------
void calculateAverageSpeed()
{
if(totalDriveDistance.changed() && totalDriveTime > 60)
{
averageSpeed = (DWORD) totalDriveDistance * 3600 / totalDriveTime;
}
}
//-----------------------------------------------------------------------------
__inline void display_trip_distance()
{
sprintf(string, "%d", tripDistance.value);
disp_tripCompValue.print(string);
}
//-----------------------------------------------------------------------------
__inline void display_day_distance()
{
sprintf(string, "%d", dayDistance.value);
disp_tripCompValue.print(string);
}
//-----------------------------------------------------------------------------
__inline void display_speed()
{
if(options.convertKm2Miles)
{
sprintf(string, "%d", (WORD) speed.value * 10 / 16);
}
else
{
sprintf(string, "%d", speed.value);
}
disp_tripCompValue.print(string);
}
//-----------------------------------------------------------------------------
__inline void display_average_speed()
{
if((int)averageSpeed.value >= 0)
{
sprintf(string, "%0d", averageSpeed.value);
disp_tripCompValue.print(string);
}
else
{
disp_tripCompValue.print(empty_string);
}
}
//-----------------------------------------------------------------------------
__inline void display_trip_time()
{
sprintf(string, "%01d:%02d", (tripTimerMinutes.value / 60) %10, tripTimerMinutes.value % 60);
disp_tripCompValue.print(string);
}
//-----------------------------------------------------------------------------
__inline void display_fuel_rate_time()
{
if((int)fuelRateTime.value >= 0)
{
WORD rate = fuelRateTime.value;
if(options.convertLitres2GalonsUSA)
{
rate = rate * 10 / 38;
}
if(options.convertLitres2GalonsENG)
{
rate = rate * 100 / 455;
}
sprintf(string, "%0d.%0d", rate / 100, rate % 100 / 10);
disp_tripCompValue.print(string);
}
else
{
disp_tripCompValue.print(empty_string);
}
}
//-----------------------------------------------------------------------------
__inline void display_fuel_rate_dist()
{
static WORD rate;
if((int)fuelRateDist.value > 0)
{
if(options.invertFuelRate)
{
rate = 10000 / fuelRateDist.value;
}
else
{
rate = fuelRateDist.value;
}
if(rate > 999) rate = 999;
sprintf(string, "%0d.%0d", rate / 10, rate % 10);
disp_tripCompValue.print(string);
}
else
{
disp_tripCompValue.print(empty_string);
}
}
//-----------------------------------------------------------------------------
__inline void display_average_fuel_rate()
{
static WORD rate;
if((int)averageFuelRate.value >= 0)
{
if(options.invertFuelRate)
{
rate = 10000 / averageFuelRate.value;
}
else
{
rate = averageFuelRate.value;
}
if(rate > 999) rate = 999;
sprintf(string, "%0d.%0d", rate / 10, rate % 10);
disp_tripCompValue.print(string);
}
else
{
disp_tripCompValue.print(empty_string);
}
}
//-----------------------------------------------------------------------------
void displayTripInfo()
{
// cruise information visibility
if(ignition)
{
disp_tripComp.show();
disp_tripCompValue.show();
tripInit = true;
if(ignition.changed() && tripDay.changed())
{
dayDistance = 0;
}
}
else
{
// TODO: сохранить trip в EEPROM при выключении зажигания
disp_tripComp.hide();
disp_tripCompValue.hide();
tripTimer.value = 0;
}
switch(tripMode.value)
{
case TRIP_MODE_SPEED:
if(tripMode.changed() || tripInit)
{
disp_tripCompCaption1->print("");
disp_tripCompCaption2->print("SPEED");
disp_tripCompUnit->print(options.speedUnits);
display_speed();
}
if(speed.updated && speed.changed())
{
speed.updated = 0;
display_speed();
}
break;
case TRIP_MODE_DISTANCE:
if(tripMode.changed() || tripInit)
{
disp_tripCompCaption1->print("TRIP");
disp_tripCompCaption2->print("DIST");
disp_tripCompUnit->print(options.distUnits);
display_trip_distance();
}
if(tripDistance.updated && tripDistance.changed())
{
tripDistance.updated = 0;
display_trip_distance();
}
break;
case TRIP_MODE_DAY_DISTANCE:
if(dayDistance.changed() || tripInit)
{
disp_tripCompCaption1->print("DAY");
disp_tripCompCaption2->print("DIST");
disp_tripCompUnit->print(options.distUnits);
display_day_distance();
}
if(dayDistance.updated && dayDistance.changed())
{
dayDistance.updated = 0;
display_day_distance();
}
break;
case TRIP_MODE_AVG_SPEED:
if(tripMode.changed() || tripInit)
{
disp_tripCompCaption1->print("AVG");
disp_tripCompCaption2->print("SPEED");
disp_tripCompUnit->print(options.speedUnits);
display_average_speed();
}
if(averageSpeed.updated && averageSpeed.changed())
{
averageSpeed.updated = 0;
display_average_speed();
}
break;
case TRIP_MODE_TIME:
if(tripMode.changed() || tripInit)
{
disp_tripCompCaption1->print("TRIP");
disp_tripCompCaption2->print("TIME");
disp_tripCompUnit->print("");
display_trip_time();
}
if(tripTimer.updated)
{
tripTimer.updated = 0;
tripTimerMinutes = tripTimer.value / 60;
}
if(tripTimerMinutes.changed())
{
display_trip_time();
}
break;
case TRIP_MODE_FUEL:
if(tripMode.changed() || tripInit)
{
disp_tripCompCaption1->print("FUEL");
disp_tripCompCaption2->print("RATE");
if(speed > 0)
{
display_fuel_rate_dist();
}
else
{
display_fuel_rate_time();
}
}
if(speed > 0)
{
disp_tripCompUnit->print(options.fuelRateUnits);
if(fuelRateDist.updated && fuelRateDist.changed())
{
fuelRateDist.updated = 0;
display_fuel_rate_dist();
}
}
else
{
disp_tripCompUnit->print(options.fuelRatePerTimeUnits);
if(fuelRateTime.updated && fuelRateTime.changed())
{
fuelRateTime.updated = 0;
display_fuel_rate_time();
}
}
break;
case TRIP_MODE_AVG_FUEL:
if(tripMode.changed() || tripInit)
{
disp_tripCompCaption1->print("AVG");
disp_tripCompCaption2->print("F/R");
disp_tripCompUnit->print(options.fuelRateUnits);
display_average_fuel_rate();
}
if(averageFuelRate.updated && averageFuelRate.changed())
{
averageFuelRate.updated = 0;
display_average_fuel_rate();
}
break;
}
tripInit = false;
}
//-----------------------------------------------------------------------------