-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw_helper.h
399 lines (357 loc) · 12.8 KB
/
draw_helper.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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include <M5Cardputer.h>
#include "common.h"
#include "resources.h"
inline unsigned short interpolateColors(unsigned short color1, unsigned short color2, int percentage)
{
if (percentage <= 0)
{
return color1;
}
else if (percentage >= 100)
{
return color2;
}
int red1 = (color1 >> 11) & 0x1F;
int green1 = (color1 >> 5) & 0x3F;
int blue1 = color1 & 0x1F;
int red2 = (color2 >> 11) & 0x1F;
int green2 = (color2 >> 5) & 0x3F;
int blue2 = color2 & 0x1F;
int interpolatedRed = (red1 * (100 - percentage) + red2 * percentage) / 100;
int interpolatedGreen = (green1 * (100 - percentage) + green2 * percentage) / 100;
int interpolatedBlue = (blue1 * (100 - percentage) + blue2 * percentage) / 100;
return (unsigned short)((interpolatedRed << 11) | (interpolatedGreen << 5) | interpolatedBlue);
}
inline void drawRemoteTitle(M5Canvas *canvas, bool isLeftRemote, RemoteDevice activeRemote, int x, int y)
{
const uint16_t *titleData;
uint16_t titleWidth, titleHeight;
switch (activeRemote)
{
case RemoteDevice::PoweredUp:
titleData = (isLeftRemote) ? lpf2LeftTitle : lpf2RightTitle;
titleWidth = (isLeftRemote) ? lpf2LeftTitleWidth : lpf2RightTitleWidth;
titleHeight = (isLeftRemote) ? lpf2LeftTitleHeight : lpf2RightTitleHeight;
break;
case RemoteDevice::SBrick:
titleData = (isLeftRemote) ? sbrickLeftTitle : sbrickRightTitle;
titleWidth = (isLeftRemote) ? sbrickLeftTitleWidth : sbrickRightTitleWidth;
titleHeight = (isLeftRemote) ? sbrickLeftTitleHeight : sbrickRightTitleHeight;
break;
case RemoteDevice::CircuitCubes:
titleData = (isLeftRemote) ? circuitCubesLeftTitle : circuitCubesRightTitle;
titleWidth = (isLeftRemote) ? circuitCubesLeftTitleWidth : circuitCubesRightTitleWidth;
titleHeight = (isLeftRemote) ? circuitCubesLeftTitleHeight : circuitCubesRightTitleHeight;
break;
case RemoteDevice::PowerFunctionsIR:
titleData = (isLeftRemote) ? powerFunctionsIrLeftTitle : powerFunctionsIrRightTitle;
titleWidth = (isLeftRemote) ? powerFunctionsIrLeftTitleWidth : powerFunctionsIrRightTitleWidth;
titleHeight = (isLeftRemote) ? powerFunctionsIrLeftTitleHeight : powerFunctionsIrRightTitleHeight;
break;
}
canvas->pushImage(x - titleWidth / 2, y - titleHeight / 2, titleWidth, titleHeight, (uint16_t *)titleData, transparencyColor);
}
inline void drawActiveRemoteIndicator(M5Canvas *canvas, int x, int y, uint8_t activeRemoteLeft, uint8_t activeRemoteRight)
{
// alignment middle_left
int w = 8;
int h = 12;
y = y - h / 2;
for (int i = 0; i < 4; i++)
{
if (i == activeRemoteLeft)
{
// canvas->fillRoundRect(x, y, w, h, 1, TFT_RED);
canvas->drawRoundRect(x, y, w, h, 1, TFT_SILVER);
canvas->floodFill(x + 1, y + 1, TFT_RED);
}
else if (i == activeRemoteRight)
{
// canvas->fillRoundRect(x, y, w, h, 1, TFT_BLUE);
canvas->drawRoundRect(x, y, w, h, 1, TFT_SILVER);
canvas->floodFill(x + 1, y + 1, TFT_BLUE);
}
else
{
canvas->drawRoundRect(x, y, w, h, 1, COLOR_LIGHTGRAY);
}
x += w + 2;
}
}
inline void drawBatteryIndicator(M5Canvas *canvas, int x, int y, int batteryPct)
{
int battw = 24;
int batth = 11;
int ya = y - batth / 2;
// determine battery color and charge width from charge level
int chgw = (battw - 2) * batteryPct / 100;
uint16_t batColor = COLOR_TEAL;
if (batteryPct < 100)
{
int r = ((100 - batteryPct) / 100.0) * 256;
int g = (batteryPct / 100.0) * 256;
batColor = canvas->color565(r, g, 0);
}
canvas->fillRoundRect(x, ya, battw, batth, 2, TFT_SILVER);
canvas->fillRect(x - 2, y - 2, 2, 4, TFT_SILVER);
canvas->fillRect(x + 1, ya + 1, battw - 2 - chgw, batth - 2, COLOR_DARKGRAY); // 1px margin from outer battery
canvas->fillRect(x + 1 + battw - 2 - chgw, ya + 1, chgw, batth - 2, batColor); // 1px margin from outer battery
}
inline void drawRssiSymbol(M5Canvas *canvas, int x, int y, bool init, int rssi)
{
const uint8_t bar1 = 3, bar2 = 8, bar3 = 13;
const uint8_t barW = 3;
const uint8_t barY = y - bar3 / 2;
const uint8_t barSpace = 2;
if (!init)
rssi = -1000;
// easy way to "center" coords
x -= 7;
canvas->drawLine(x, barY, x, barY + bar3 - 1, TFT_SILVER);
canvas->drawTriangle(x - 3, barY, x + 3, barY, x, barY + 3, TFT_SILVER);
uint8_t barX = x + 4;
(rssi > -100)
? canvas->fillRect(barX, barY + (bar3 - bar1), barW, bar1, COLOR_ORANGE)
: canvas->drawRect(barX, barY + (bar3 - bar1), barW, bar1, TFT_SILVER);
barX += barW + barSpace;
(rssi > -80)
? canvas->fillRect(barX, barY + (bar3 - bar2), barW, bar2, COLOR_ORANGE)
: canvas->drawRect(barX, barY + (bar3 - bar2), barW, bar2, TFT_SILVER);
barX += barW + barSpace;
(rssi > -60)
? canvas->fillRect(barX, barY + (bar3 - bar3), barW, bar3, COLOR_ORANGE)
: canvas->drawRect(barX, barY + (bar3 - bar3), barW, bar3, TFT_SILVER);
if (!init)
{
int x1 = x, x2 = x + 3 * (barSpace + barW);
int y1 = barY, y2 = barY + bar3;
for (int i = 0; i < barW; i++)
{
canvas->drawLine(x1 + i, y1, x2 - (barW - i), y2, TFT_RED);
canvas->drawLine(x2 - (barW - i), y1, x1 + i, y2, TFT_RED);
}
}
}
inline void drawSensorColor(M5Canvas *canvas, int x, int y, Color color)
{
const uint16_t FADE_RED = interpolateColors(COLOR_LIGHTGRAY, TFT_RED, 50);
const uint16_t FADE_YELLOW = interpolateColors(COLOR_LIGHTGRAY, TFT_YELLOW, 50);
const uint16_t FADE_GREEN = interpolateColors(COLOR_LIGHTGRAY, TFT_GREEN, 50);
const uint16_t FADE_BLUE = interpolateColors(COLOR_LIGHTGRAY, TFT_BLUE, 50);
int r = 5;
int t = 2;
canvas->fillArc(x, y, r, r + t, 180, 270, FADE_RED);
canvas->fillArc(x, y, r, r + t, 270, 360, FADE_YELLOW);
canvas->fillArc(x, y, r, r + t, 0, 90, FADE_GREEN);
canvas->fillArc(x, y, r, r + t, 90, 180, FADE_BLUE);
canvas->drawCircle(x, y, r - 1, TFT_SILVER);
canvas->drawCircle(x, y, r + t + 1, TFT_SILVER);
// show raw sensor color in middle if on
if (color != Color::NONE)
{
canvas->floodFill(x, y, BtColors[color]);
}
}
inline void drawPauseSymbol(M5Canvas *canvas, int x, int y)
{
int w = 9;
canvas->fillRect(x - w / 2, y - w / 2, w / 3, w, TFT_SILVER);
canvas->fillRect(x - w / 2 + 2 * (w / 3), y - w / 2, w / 3, w, TFT_SILVER);
}
inline void drawMotorSymbol(M5Canvas *canvas, int x, int y, RemoteAction action)
{
int w, h;
switch (action)
{
case RemoteAction::SpdUp:
w = 6;
h = 9;
canvas->fillTriangle(x, y - h / 2, x - w, y + h / 2, x + w, y + h / 2, TFT_SILVER);
break;
case RemoteAction::Brake:
w = 9;
canvas->fillRect(x - w / 2, y - w / 2, w, w, TFT_SILVER);
break;
case RemoteAction::SpdDn:
w = 6;
h = 9;
canvas->fillTriangle(x, y + h / 2, x + w, y - h / 2, x - w, y - h / 2, TFT_SILVER);
break;
default:
break;
}
}
inline void drawSensorSymbol(M5Canvas *canvas, int x, int y, Color color, RemoteAction action, int spdupFunction, int8_t stopFunction, int8_t spddnFunction)
{
switch (action)
{
case RemoteAction::SpdUp:
if (spdupFunction >= 0)
drawMotorSymbol(canvas, x, y, action);
break;
case RemoteAction::Brake:
if (stopFunction == 0)
{
drawMotorSymbol(canvas, x, y, action);
}
else if (stopFunction > 0)
{
int gap = canvas->fontWidth();
drawPauseSymbol(canvas, x - gap + 1 , y);
// match muted background color of button
int adjustedColor = interpolateColors(COLOR_LIGHTGRAY, BtColors[color], 50);
canvas->setTextColor(TFT_SILVER);
canvas->setTextDatum(middle_center);
canvas->setTextSize(1);
canvas->drawString(String(stopFunction), x + gap, y + 1);
}
break;
case RemoteAction::SpdDn:
if (spddnFunction >= 0)
drawMotorSymbol(canvas, x, y, action);
break;
}
}
inline void drawPulseSymbol(M5Canvas *canvas, int x, int y, bool highPulse)
{
int pulseW = 10;
int pulseH = 10;
int x1, x2, y1, y2;
x1 = x - pulseW / 2;
y1 = y - pulseH / 2;
x2 = x + pulseW / 2;
y2 = y + pulseH / 2;
if (highPulse)
{
canvas->drawFastHLine(x1 - pulseW / 2 + 1, y2, pulseW / 2, TFT_SILVER);
canvas->drawFastVLine(x1, y1, pulseH, TFT_SILVER);
canvas->drawFastHLine(x1, y1, pulseW, TFT_SILVER);
canvas->drawFastVLine(x2, y1, pulseH, TFT_SILVER);
canvas->drawFastHLine(x2, y2, pulseW / 2, TFT_SILVER);
}
else
{
canvas->drawFastHLine(x1 - pulseW / 2 + 1, y1, pulseW / 2, TFT_SILVER);
canvas->drawFastVLine(x1, y1, pulseH, TFT_SILVER);
canvas->drawFastHLine(x1, y2, pulseW, TFT_SILVER);
canvas->drawFastVLine(x2, y1, pulseH, TFT_SILVER);
canvas->drawFastHLine(x2, y1, pulseW / 2, TFT_SILVER);
}
}
inline void drawSwitchToggleSymbol(M5Canvas *canvas, int x, int y)
{
canvas->pushImage(x - switchsymbolWidth / 2, y - switchSymbolHeight / 2, switchsymbolWidth, switchSymbolHeight, (uint16_t *)switchSymbol, transparencyColor);
}
inline void drawSwitchSymbol(M5Canvas *canvas, int x, int y, RemoteAction action, bool *portFunction)
{
switch (action)
{
case RemoteAction::SpdUp:
drawPulseSymbol(canvas, x, y, true);
break;
case RemoteAction::Brake:
drawSwitchToggleSymbol(canvas, x, y);
break;
case RemoteAction::SpdDn:
drawPulseSymbol(canvas, x, y, false);
break;
}
}
inline void drawIrChannelSymbol(M5Canvas *canvas, int x, int y, byte irChannel)
{
canvas->setTextColor(TFT_SILVER);
canvas->setTextDatum(middle_center);
canvas->drawString(String(irChannel + 1), x + 1, y, &fonts::Font2);
}
inline void drawIrModeSymbol(M5Canvas *canvas, int x, int y, uint8_t irMode)
{
canvas->fillCircle(x, y, 1, TFT_SILVER);
if (irMode == 1 || irMode == 2)
{
canvas->fillTriangle(x, y + 5, x - 6, y + 9, x + 6, y + 9, TFT_SILVER);
}
else
{
canvas->drawTriangle(x, y + 5, x - 6, y + 9, x + 6, y + 9, TFT_SILVER);
}
if (irMode == 2 || irMode == 3)
{
canvas->fillArc(x, y, 4, 4, 140, 40, TFT_SILVER);
canvas->fillArc(x, y, 7, 7, 140, 40, TFT_SILVER);
}
}
inline void drawSBrickSensorReading(M5Canvas *canvas, int x, int y, RemoteAction action, bool isMotionSensor, float voltage, float neutralV)
{
char deltaV[6];
uint8_t gap = 4;
int sx;
y += 1;
x += 3;
canvas->setTextDatum(middle_center);
String name = (isMotionSensor) ? "Motion" : "Tilt";
switch (action)
{
case RemoteAction::SpdUp:
canvas->drawString(name, x, y - gap, &fonts::TomThumb);
canvas->drawString("Sensr", x, y + gap, &fonts::TomThumb);
break;
case RemoteAction::Brake:
canvas->drawLine(x - 9, y - gap + 1, x - 4, y - gap - 3, TFT_SILVER);
canvas->drawLine(x - 4, y - gap - 3, x, y - gap, TFT_SILVER);
canvas->drawLine(x, y - gap - 1, x + 4, y - gap - 5, TFT_SILVER);
canvas->drawString(String(voltage, 2) + "V", x, y + gap, &fonts::TomThumb);
break;
case RemoteAction::SpdDn:
canvas->drawTriangle(x - 6, y - gap + 1, x + 1, y - gap + 1, x - 2, y - gap - 5, TFT_SILVER);
sprintf(deltaV, "%+5.2f", voltage - neutralV);
canvas->drawString(String(deltaV) + "V", x, y + gap, &fonts::TomThumb);
break;
}
}
inline void drawButtonSymbol(M5Canvas *canvas, Button &button, int x, int y, State &state)
{
x += button.w / 2;
y += button.h / 2;
switch (button.action)
{
case RemoteAction::BtConnection:
switch (button.device)
{
case RemoteDevice::PoweredUp:
drawRssiSymbol(canvas, x, y, state.lpf2Connected, state.lpf2Rssi);
break;
case RemoteDevice::SBrick:
drawRssiSymbol(canvas, x, y, state.sBrickConnected, state.sbrickRssi);
break;
case RemoteDevice::CircuitCubes:
drawRssiSymbol(canvas, x, y, state.circuitCubesConnected, state.circuitCubesRssi);
break;
}
break;
case RemoteAction::Lpf2Color:
drawSensorColor(canvas, x, y, state.lpf2SensorColor);
break;
case RemoteAction::IrChannel:
drawIrChannelSymbol(canvas, x, y, state.irChannel);
break;
case RemoteAction::IrMode:
drawIrModeSymbol(canvas, x, y, state.irMode);
break;
case RemoteAction::SpdUp:
case RemoteAction::Brake:
case RemoteAction::SpdDn:
if (button.device == RemoteDevice::PoweredUp && button.port == state.lpf2SensorPort)
drawSensorSymbol(canvas, x, y, state.lpf2SensorSpdUpColor, button.action, state.lpf2SensorSpdUpFunction, state.lpf2SensorStopFunction, state.lpf2SensorSpdDnFunction);
else if (button.device == RemoteDevice::PowerFunctionsIR && state.irPortFunction[button.port])
drawSwitchSymbol(canvas, x, y, button.action, state.irPortFunction);
else if (button.device == RemoteDevice::SBrick && button.port == state.sbrickMotionSensorPort)
drawSBrickSensorReading(canvas, x, y, button.action, true, state.sbrickMotionSensorV, state.sbrickMotionSensorNeutralV);
else if (button.device == RemoteDevice::SBrick && button.port == state.sbrickTiltSensorPort)
drawSBrickSensorReading(canvas, x, y, button.action, false, state.sbrickTiltSensorV, state.sbrickTiltSensorNeutralV);
else
drawMotorSymbol(canvas, x, y, button.action);
break;
default:
break;
}
}