-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtft.h
214 lines (187 loc) · 5.31 KB
/
tft.h
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
#ifdef TFT_ENABLE
#ifndef __TFT_H__
#define __TFT_H__
#include <SPI.h>
#include <TFT_eSPI.h>
#include "shared.h"
#define LINE_HEIGHT 20
#define LINE_WIDTH TFT_WIDTH
#define MAX_LINES 10
// TFT class
class TFT {
public:
void init() {
tft.init();
tft.setRotation(0);
setFontSize(2);
lastPktDraw = 0;
background = TFT_BLACK;
textColor = TFT_WHITE;
myLastPacketsPerSecond = 0;
myLastFailedPerSecond = 0;
tft.fillScreen(background);
tft.setTextColor(textColor, background);
}
void setFontSize(char size) {
tft.setTextSize(size);
fontSize = size;
}
bool tooFast(unsigned long* lastDrawRequest) {
unsigned long ms = millis();
if (ms > 250 && *lastDrawRequest < ms - 250) {
*lastDrawRequest = ms;
return false;
} else {
return true;
}
}
void zeroPrefix(char* buf, short input, char zero) {
short offset = 0;
for (int i = 1; i < zero+1; ++i) {
if (input < pow(10, i)) {
buf[offset] = '0';
offset+=1;
}
}
sprintf(buf+offset, "%d", input);
}
short getLineIndex(short line) {
return line*LINE_HEIGHT+10;
}
void renderControlState(ControlPacket& packet) {
if (tooFast(&lastCtlDraw)) {
return;
}
char pkt[64];
short y = this->getLineIndex(9);
short len = 0;
sprintf(pkt, "R%d", map(packet.channels[0],1000,2000,0,9));
setGoodColor(packet.channels[0], 1550, 1450);
len += write(len, y, pkt);
len += getCharSize();
sprintf(pkt, "P%d", map(packet.channels[1],1000,2000,0,9));
setGoodColor(packet.channels[1], 1550, 1450);
len += write(len, y, pkt);
len += getCharSize();
sprintf(pkt, "Y%d", map(packet.channels[3],1000,2000,0,9));
setGoodColor(packet.channels[3], 1550, 1450);
len += write(len, y, pkt);
len += getCharSize();
sprintf(pkt, "T%d", map(packet.channels[2],1000,2000,0,9));
setGoodColor(packet.channels[2], 1750, 1250);
len += write(len, y, pkt);
len += getCharSize();
len = 0;
y = this->getLineIndex(8);
if (packet.digitalChannels[MAX_DIGITAL_BYTES-1] & 0x1) {
sprintf(pkt, "PRE.");
tft.setTextColor(TFT_GREEN, background);
len += write(len, y, pkt);
len += getCharSize();
} else {
sprintf(pkt, " ");
len += write(len, y, pkt);
len += getCharSize();
}
if (packet.digitalChannels[MAX_DIGITAL_BYTES-1] & 0x2) {
sprintf(pkt, " ARMED");
tft.setTextColor(TFT_GREEN, background);
len += write(len-1, y, pkt);
len += getCharSize();
} else {
sprintf(pkt, " ");
len += write(len, y, pkt);
}
}
void updatePackets() {
if (tooFast(&lastPktDraw)) {
return;
}
char pkt[64];
short len = 0;
short y = this->getLineIndex(MAX_LINES);
// pretty much always needs another render
zeroPrefix(pkt, packetsPerSecond, 2);
tft.setTextColor(TFT_SILVER, background);
len += write(len, y, pkt);
len += getCharSize();
// cheapen screen write timers by skipping a little execution
if (lastPacketsPerSecond != myLastPacketsPerSecond) {
zeroPrefix(pkt, lastPacketsPerSecond, 2);
setGoodColor(lastPacketsPerSecond, 250, 100);
len += write(len, y, pkt);
len += getCharSize();
myLastPacketsPerSecond = lastPacketsPerSecond;
} else {
len *= 2; // pretend we wrote another pktspsec
}
if (packetsFailedPerSecond != myLastFailedPerSecond) {
setBadColor(packetsFailedPerSecond, 5, 10);
zeroPrefix(pkt, packetsFailedPerSecond, 2);
len += write(len, y, pkt);
myLastFailedPerSecond = packetsFailedPerSecond;
}
}
void setGoodColor(short value, short mid, short high) {
if (value < high) {
tft.setTextColor(TFT_RED, background);
} else if (value < mid) {
tft.setTextColor(TFT_YELLOW, background);
} else {
tft.setTextColor(TFT_GREEN, background);
}
}
void setBadColor(short value, short mid, short high) {
if (value > high) {
tft.setTextColor(TFT_RED, background);
} else if (value > mid) {
tft.setTextColor(TFT_YELLOW, background);
} else {
tft.setTextColor(TFT_SILVER, background);
}
}
char getCharSize() {
if (fontSize == 1) {
return 10;
} else if (fontSize == 2) {
return 12;
}
return 1;
}
long write(int x, int y, const char* str) {
tft.setCursor(1+x, 1+y, 1);
tft.print(str);
return strlen(str)*getCharSize();
}
void addColumn() {
}
void addRow(const char* rowText) {
}
void updateRow(short column, short row) {
}
void debugWrite(const char* str) {
}
void cube(int x, int y, int height, int width) {
// draw two horizontal lines, one from x to 2x along y, one from x2y and y2x2
tft.drawFastHLine(x, y, width, TFT_WHITE);
tft.drawFastHLine(x, y+height, width, TFT_WHITE);
// draw two vertical lines, one from xy to x2y and xy2 x2y2
tft.drawFastVLine(x, y, height, TFT_WHITE);
tft.drawFastVLine(x+width, y, height, TFT_WHITE);
}
private:
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
char fontSize;
int background;
int textColor;
short myLastPacketsPerSecond;
short myLastFailedPerSecond;
unsigned long lastPktDraw;
unsigned long lastCtlDraw;
};
#endif // __TFT_H__
#else
class TFT {
void init() {}
}
#endif // TFT_ENABLE