-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtft.cpp
More file actions
342 lines (289 loc) · 11 KB
/
tft.cpp
File metadata and controls
342 lines (289 loc) · 11 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
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
#include <SPI.h>
#include <TFT_eSPI.h>
#include <TFT_eWidget.h>
#include "constants.h"
#include "debug.h"
#include "flightLogger.h"
#include "sensor.h"
#include "simulation.h"
#include "tft.h"
#include "utilities.h"
#include "wifi.h"
#include "images/thzero_altimeters128x128.h"
TFT_eSPI _tft = TFT_eSPI();
GraphWidget _graphWidget = GraphWidget(&_tft); // Graph widget
TraceWidget _graphTraceAltitude = TraceWidget(&_graphWidget);
TraceWidget _graphTraceTemperature = TraceWidget(&_graphWidget);
TraceWidget _graphTracePressure = TraceWidget(&_graphWidget);
TraceWidget _graphTraceAccelX = TraceWidget(&_graphWidget);
TraceWidget _graphTraceAccelY = TraceWidget(&_graphWidget);
TraceWidget _graphTraceAccelZ = TraceWidget(&_graphWidget);
TraceWidget _graphTraceHumidity = TraceWidget(&_graphWidget);
void drawTftFlightAirborne(unsigned long timestamp, unsigned long delta) {
_tft.setCursor(0, STATUS_HEIGHT_BAR);
if (_flightLogger.aborted) {
_tft.println(F("ABORTED!!!"));
return;
}
_tft.println(F("Recording in progress ....."));
_tft.println(F(""));
char altitude[40];
_tft.println("Altitude");
sprintf(altitude, "Initial: %.2fm", _flightLogger.altitudeInitial);
_tft.println(altitude);
sprintf(altitude, "Current: %.2fm", _flightLogger.instance.getData().altitudeCurrent + _flightLogger.altitudeInitial);
_tft.println(altitude);
drawTftSensorImu();
}
void drawTftFlightAirborneStart() {
#ifdef DEV_SIM
_tft.fillScreen(TFT_RED);
_tft.setTextColor(TFT_WHITE, TFT_RED);
#else
_tft.fillScreen(TFT_BLACK);
_tft.setTextColor(TFT_WHITE, TFT_BLACK);
#endif
_tft.setRotation(1);
}
void drawTftGraphAxesXY(float minX, float maxX, float minY, float maxY, int flightNbr, char *curveName) {
_tft.fillScreen(TFT_BLACK);
// x scale units is from 0 to 100, y scale units is 0 to 50
_graphWidget.setGraphScale(minX, maxX, minY, maxY);
// X grid starts at 0 with lines every 10 x-scale units
// Y grid starts at 0 with lines every 10 y-scale units
// blue grid
_graphWidget.setGraphGrid(0.0, maxX / 5, 0.0, maxY / 5, TFT_BLUE);
// Draw empty graph, top left corner at 20,10 on TFT
_graphWidget.drawGraph(30, 10);
// Draw the x axis scale
_tft.setTextDatum(TC_DATUM); // Top centre text datum
_tft.drawNumber(minX, _graphWidget.getPointX(minX), _graphWidget.getPointY(0) + 3);
if (maxX < 1000) {
_tft.drawNumber(maxX / 2, _graphWidget.getPointX(maxX / 2), _graphWidget.getPointY(0) + 3);
_tft.drawNumber(maxX, _graphWidget.getPointX(maxX), _graphWidget.getPointY(0) + 3);
_tft.drawString(F("time(ms)"), _graphWidget.getPointX(maxX / 4), _graphWidget.getPointY(0) + 3);
}
else {
char temp[10];
sprintf(temp, "%3.1f", maxX / 1000 / 2);
_tft.drawString(temp, _graphWidget.getPointX(maxX / 2), _graphWidget.getPointY(0) + 3);
sprintf(temp, "%3.1f", maxX / 1000);
_tft.drawString(temp, _graphWidget.getPointX(maxX) - 10, _graphWidget.getPointY(0) + 3);
_tft.drawString(F("time (s)"), _graphWidget.getPointX(maxX / 4), _graphWidget.getPointY(0) + 3);
}
char flight[15];
sprintf(flight, "Flight %i ", flightNbr);
_tft.drawString(flight, _graphWidget.getPointX(maxX) - 10, _graphWidget.getPointY(maxY));
_tft.drawString(curveName, _graphWidget.getPointX(maxX / 3), _graphWidget.getPointY(maxY));
// Draw the y axis scale
_tft.setTextDatum(MR_DATUM); // Middle right text datum
if (maxY < 1000)
_tft.drawNumber(maxY, _graphWidget.getPointX(0.0), _graphWidget.getPointY(maxY));
else
_tft.drawNumber(round2dec(maxY / 1000), _graphWidget.getPointX(0.0), _graphWidget.getPointY(maxY));
}
void drawTftGraphForlightNbr(int flightNbr, int curveType) {
Serial.println(F("Display graph on tft..."));
if (_tft.getRotation() == 1) {
_tft.setRotation(0);
_tft.fillScreen(TFT_BLACK);
}
_tft.drawString(F("altitude"), 6, 0);
// Graph area is 200 pixels wide, 150 high, dark grey background
_graphWidget.createGraph(200, 100, _tft.color565(5, 5, 5));
// x scale units is from 0 to 100, y scale units is 0 to 50
_graphWidget.setGraphScale(0.0, 100.0, 0, 50.0);
_tft.setTextColor(TFT_WHITE, TFT_BLACK);
_tft.fillScreen(TFT_BLACK);
_tft.setSwapBytes(true);
_tft.setTextFont(2);
Serial.println(F("...display graph on tft successful."));
if (!_flightLogger.instance.readFile(flightNbr))
return;
flightDataTraceStruct *currentFlight;
currentFlight = _flightLogger.instance.getDataTrace();
_flightLogger.instance.determineTraceMinAndMax(flightNbr);
//altitude
if (curveType == 0) {
// Start altitude trace
_graphTraceAltitude.startTrace(TFT_GREEN);
if ((float)_flightLogger.instance.getAltitudeMax() < 1000)
drawTftGraphAxesXY(0.0, _flightLogger.instance.getDuration(), 0, (float)_flightLogger.instance.getAltitudeMax(), flightNbr, "Altitude (meters)");
else
drawTftGraphAxesXY(0.0, _flightLogger.instance.getDuration(), 0, (float)_flightLogger.instance.getAltitudeMax(), flightNbr, "Altitude (km)");
}
//accel
if (curveType == 1) {
_graphTraceAccelX.startTrace(TFT_RED);
_graphTraceAccelY.startTrace(TFT_PURPLE);
_graphTraceAccelY.startTrace(TFT_YELLOW);
float maxAccel = 0.0f;
if (_flightLogger.instance.getAccelXMax() > maxAccel)
maxAccel = (float)_flightLogger.instance.getAccelXMax();
if (_flightLogger.instance.getAccelYMax() > maxAccel)
maxAccel = (float)_flightLogger.instance.getAccelYMax();
if (_flightLogger.instance.getAccelZMax() > maxAccel)
maxAccel = (float)_flightLogger.instance.getAccelZMax();
// Serial.println(maxAccel);
drawTftGraphAxesXY(0.0, _flightLogger.instance.getDuration(), 0, (float)roundUp(maxAccel), flightNbr, "Accel X,Y,Z (m/s)");
}
// pressure
if (curveType == 2) {
_graphTracePressure.startTrace(TFT_GREY);
//Serial.println(_flightLogger.instance.getPressureMax());
drawTftGraphAxesXY(0.0, _flightLogger.instance.getDuration(), 0, (float)roundUp(_flightLogger.instance.getPressureMax()), flightNbr, "Pressure (mBar)");
}
// temperature
if (curveType == 3) {
_graphTraceTemperature.startTrace(TFT_BROWN);
//Serial.println(_flightLogger.instance.getTemperatureMax());
drawTftGraphAxesXY(0.0, _flightLogger.instance.getDuration(), 0, (float)roundUp(_flightLogger.instance.getTemperatureMax()), flightNbr, "Temp (°C)");
}
// humidity
if (curveType == 4) {
_graphTraceHumidity.startTrace(TFT_YELLOW);
Serial.println(_flightLogger.instance.getHumidityMax());
drawTftGraphAxesXY(0.0, _flightLogger.instance.getDuration(), 0, (float)roundUp(_flightLogger.instance.getHumidityMax() + 1), flightNbr, "Hum %");
}
unsigned long currentTime = 0;
for (long i = 0; i < _flightLogger.instance.getDataTraceSize(); i++) {
currentTime = currentTime + currentFlight[i].diffTime; //logger.getFlightTimeData();
//altitude
if (curveType == 0)
_graphTraceAltitude.addPoint(currentTime, currentFlight[i].altitude);
if (curveType == 1) {
_graphTraceAccelX.addPoint(currentTime, (float)currentFlight[i].accelX);
_graphTraceAccelY.addPoint(currentTime, (float)currentFlight[i].accelY);
_graphTraceAccelZ.addPoint(currentTime, (float)currentFlight[i].accelZ);
}
if (curveType == 2)
_graphTracePressure.addPoint(currentTime, currentFlight[i].pressure);
if (curveType == 3)
_graphTraceTemperature.addPoint(currentTime, currentFlight[i].temperature);
if (curveType == 4)
_graphTraceHumidity.addPoint(currentTime, currentFlight[i].humidity);
}
}
void drawTftReset() {
_tft.setTextColor(TFT_WHITE, TFT_BLACK);
_tft.fillScreen(TFT_BLACK);
_tft.setRotation(0);
}
void drawTftSensorImu() {
accelerometerValues accelerometerValuesO = readSensorAccelerometer();
char temp[15];
sprintf(temp, "x=%3.2f m/s", accelerometerValuesO.x);
_tft.println("");
_tft.println(temp);
sprintf(temp, "y=%3.2f m/s", accelerometerValuesO.y);
_tft.println(temp);
sprintf(temp, "z=%3.2f m/s", accelerometerValuesO.z);
_tft.println(temp);
// gyroscopeValues readSensorGyroscope();
}
void drawTftSleep() {
_tft.drawString("turning off...", 6, 185);
}
String drawTftSplashPad(float value, int width, char * format) {
char buffer[20];
// Convert float to string
sprintf(buffer, format, value);
// Serial.print("value: ");
// Serial.println(value);
int valueLength = strlen(buffer);
// Serial.print("valueLength: ");
// Serial.println(valueLength);
valueLength = width - valueLength;
// Serial.print("valueLength2: ");
// Serial.println(valueLength);
if (valueLength < 0)
valueLength = 0;
if (valueLength > width)
valueLength = width;
// Serial.print("valueLength3: ");
// Serial.println(valueLength);
char blank[valueLength + 1];
snprintf(blank, sizeof(blank), "%*s", valueLength, "");
// Serial.print("blank: ");
// Serial.println(blank);
return blank;
}
void drawTftSplash() {
Serial.println(F("Display splash on tft..."));
int row = 125;
_tft.pushImage(6, 0, 128, 128, thzero_altimeters128x128);
_tft.drawString(BOARD_NAME, 6, row);
row += 10;
String version = "ver ";
version.concat(MAJOR_VERSION);
version.concat(".");
version.concat(MINOR_VERSION);
#if defined(DEV) || defined(DEV_SIM)
version.concat(" (DEV)");
#endif
_tft.drawString(version, 6, row);
row += 10;
_tft.drawString(COPYRIGHT, 6, row);
row += 10;
_tft.drawString(COPYRIGHT_YEARS, 6, row);
row += 15;
if (_wifi.enabled()) {
String wifi = "SSID: ";
wifi.concat(_wifi.ssid());
_tft.drawString(wifi, 6, row);
row += 10;
String ipAddress = "IP: ";
ipAddress.concat(_wifi.ipAddress());
ipAddress.concat(" ");
Serial.print(F("WiFi IP address: "));
Serial.println(_wifi.ipAddress());
_tft.drawString(ipAddress, 6, row);
}
else {
_tft.drawString(F("Enable WiFi"), 6, row);
row += 10;
_tft.drawString(F("5 sec long Press"), 6, row);
}
row += 15;
_tft.drawString(F("Alt. Temp. Pres."), 6, row);
row += 10;
char temp[40];
sprintf(temp, "%.0fm%s%.0fC%s%.0fhPa", _flightLogger.altitudeInitial, drawTftSplashPad(_flightLogger.altitudeInitial, 6, "%.0f"), _flightLogger.temperatureInitial, drawTftSplashPad(_flightLogger.temperatureInitial, 5, "%.0f"), _flightLogger.pressureInitial);
_tft.drawString(temp, 6, row);
row += 15;
if (_flightLogger.aborted) {
_tft.drawString(F("ABORTED!!!"), 6, row);
return;
}
Serial.println(F("...display splash on tft successful."));
}
void setupTft() {
Serial.println("\nSetup TFT...");
// turn on backlite
pinMode(TFT_BACKLITE, OUTPUT);
digitalWrite(TFT_BACKLITE, HIGH);
// turn on the TFT / I2C power supply
pinMode(TFT_I2C_POWER, OUTPUT);
digitalWrite(TFT_I2C_POWER, HIGH);
delay(10);
_tft.init();
drawTftReset();
_tft.setSwapBytes(true);
drawTftSplash();
Serial.println("...TFT successful.");
}
void sleepTft() {
// neilrbowen...
pinMode(TFT_BACKLITE, OUTPUT);
pinMode(TFT_I2C_POWER, OUTPUT);
pinMode(4, OUTPUT);
digitalWrite(TFT_BACKLITE, LOW);
digitalWrite(TFT_I2C_POWER, LOW);
// neilrbowen...
}
void sleepTftHold() {
// neilrbowen...
gpio_hold_en((gpio_num_t)TFT_BACKLITE);
gpio_hold_en((gpio_num_t)TFT_I2C_POWER);
// neilrbowen...
}