-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.hpp
616 lines (503 loc) · 18.5 KB
/
draw.hpp
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*================================================================================
* Functions for drawing widget
*================================================================================*/
/*--------------------------------------------------------------------------------
* Functions prototyping
*--------------------------------------------------------------------------------*/
static void DrawTemperatureMarker(void);
static void DrawTemperatureRange(uint8_t type);
static void DrawScreen(const Widget_t *widget);
static void DrawWidget(const Widget_t *widget, uint8_t offset = 0);
static void DrawButton(const Widget_t *widget, uint8_t offset = 0);
static void DrawToggle(const Widget_t *widget, bool check = false);
static void DrawCheck (const Widget_t *widget, bool check = false);
static void DrawPress (const Widget_t *widget, Event_t event = EVENT_INIT);
static void DrawSlider(const Widget_t *widget, int16_t pos, bool enable = true);
static void DrawRadio (const Widget_t *widget, uint8_t n_widget, uint8_t selected = 0);
static void DrawThumb (const Widget_t *widget, const char *path);
static void DrawVideo (const Widget_t *widget, const char *path);
/*--------------------------------------------------------------------------------
* Sprite object
*--------------------------------------------------------------------------------*/
#if defined (LOVYANGFX_HPP_)
static LGFX_Sprite sprite_draw(&lcd);
#elif defined (_TFT_eSPIH_)
static TFT_eSprite sprite_draw(&tft);
#endif
#ifdef _TFT_eSPIH_
/*--------------------------------------------------------------------------------
* Helper function for TFT_eSPI
* Include the PNG decoder library, available via the IDE library manager
* https://github.com/Bodmer/TFT_eSPI/tree/master/examples/PNG%20Images
* pngDraw: Callback function to draw pixels to the display
*--------------------------------------------------------------------------------*/
#include <PNGdec.h>
static PNG png; // PNG decoder instance
// Position variables must be global (PNGdec does not handle position coordinates)
static uint16_t xpos = 0;
static uint16_t ypos = 0;
/*--------------------------------------------------------------------------------
* This next function will be called during decoding of the png file to
* render each image line to the TFT. If you use a different TFT library
* you will need to adapt this function to suit.
*--------------------------------------------------------------------------------*/
static void pngDraw(PNGDRAW *pDraw) {
uint16_t lineBuffer[TFT_WIDTH > TFT_HEIGHT ? TFT_WIDTH : TFT_HEIGHT];
png.getLineAsRGB565(pDraw, lineBuffer, PNG_RGB565_BIG_ENDIAN, 0xffffffff);
GFX_EXEC(pushImage(xpos, ypos + pDraw->y, pDraw->iWidth, 1, lineBuffer));
}
static void pngSprite(PNGDRAW *pDraw) {
uint16_t lineBuffer[TFT_WIDTH > TFT_HEIGHT ? TFT_WIDTH : TFT_HEIGHT];
png.getLineAsRGB565(pDraw, lineBuffer, PNG_RGB565_BIG_ENDIAN, 0xffffffff);
sprite_draw.pushImage(xpos, ypos + pDraw->y, pDraw->iWidth, 1, lineBuffer);
}
static void DrawPNG(const uint8_t *img, size_t size, uint16_t x, uint16_t y, PNG_DRAW_CALLBACK *draw) {
xpos = x;
ypos = y;
if (png.openFLASH((uint8_t*)img, size, draw) == PNG_SUCCESS) {
GFX_EXEC(startWrite());
png.decode(NULL, 0);
GFX_EXEC(endWrite());
// png.close(); // Required for files, not needed for FLASH arrays
}
}
#endif // _TFT_eSPIH_
/*--------------------------------------------------------------------------------
* Converting byte order according to MCU architecture
*--------------------------------------------------------------------------------*/
#if defined (__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
#define SWAP_ENDIAN(type, a, b) { type tmp = a; a = b; b = tmp; }
// Alternatives to std::byteswap (C++23)
// big-endian (PNG) --> little-endian (ESP32)
uint32_t swap_endian(uint32_t v) {
uint8_t r[4];
*(uint32_t*)r = v;
SWAP_ENDIAN(uint8_t, r[0], r[3]);
SWAP_ENDIAN(uint8_t, r[1], r[2]);
return *(uint32_t*)r;
}
#else // __ORDER_BIG_ENDIAN__ or __ORDER_PDP_ENDIAN__ or not defined
#define swap_endian(v) (v)
#endif // __BYTE_ORDER__
/*--------------------------------------------------------------------------------
* Get width and height of PNG image
*--------------------------------------------------------------------------------*/
#define PNG_HEADER_WIDTH 16 // PNG file signature + offset from chunk data
#define PNG_HEADER_HEIGHT 20 // PNG file signature + offset from chunk data
static inline uint32_t get_width (const uint8_t *data) __attribute__((always_inline));
static inline uint32_t get_height(const uint8_t *data) __attribute__((always_inline));
static inline uint32_t get_width (const uint8_t *data) { return swap_endian(*(uint32_t*)(data + PNG_HEADER_WIDTH )); }
static inline uint32_t get_height(const uint8_t *data) { return swap_endian(*(uint32_t*)(data + PNG_HEADER_HEIGHT)); }
/*--------------------------------------------------------------------------------
* Marker icons
*--------------------------------------------------------------------------------*/
#include "marker.h"
static constexpr Image_t icon_marker[] = {
{ icon_marker0, sizeof(icon_marker0) }, // 14 x 14
{ icon_marker1, sizeof(icon_marker1) }, // 16 x 16
};
/*--------------------------------------------------------------------------------
* Draw marker with value
*--------------------------------------------------------------------------------*/
static void DrawMarker(const Image_t *image, Temperature_t *temp) {
#define TEST_MARKER false
#if TEST_MARKER
temp->x = MLX90640_COLS / 2;
temp->y = MLX90640_ROWS / 2;
#endif
// check onMainInside() in widgets.hpp
const int box = mlx_cnf.box_size * mlx_cnf.interpolation;
const int W = box * (MLX90640_COLS - 1);
const int Y = box * temp->y;
#if ENA_OUTWARD_CAMERA
const int X = box * (MLX90640_COLS - 1 - temp->x);
#else
const int X = box * temp->x;
#endif
const int w = get_width (image->data);
const int h = get_height(image->data);
#if defined (LOVYANGFX_HPP_)
sprite_draw.setPsram(true);
sprite_draw.createSprite(w, h);
sprite_draw.drawPng(image->data, image->size, 0, 0);
sprite_draw.pushSprite(&lcd_sprite, X - (w >> 1), Y - (h >> 1), TFT_BLACK);
sprite_draw.deleteSprite();
#elif defined (_TFT_eSPIH_)
sprite_draw.createSprite(w, h);
DrawPNG(image->data, image->size, 0, 0, pngSprite);
sprite_draw.pushToSprite(&tft_sprite, X - (w >> 1), Y - (h >> 1), TFT_BLACK);
sprite_draw.deleteSprite();
#endif
#if TEST_MARKER
GFX_FAST(drawLine(0, 0, MLX90640_COLS * box - 1, MLX90640_ROWS * box - 1, TFT_RED));
GFX_FAST(drawLine(MLX90640_COLS * box - 1, 0, 0, MLX90640_ROWS * box - 1, TFT_RED));
#endif
char buf[BUF_SIZE];
sprintf(buf, "%4.1f", temp->v);
GFX_FAST(drawString(buf, constrain(X, w, W - w), Y + (temp->y < MLX90640_ROWS / 2 ? h : -h)));
}
/*--------------------------------------------------------------------------------
* Draw temperature with markers and values
*--------------------------------------------------------------------------------*/
void DrawTemperatureMarker(void) {
const int box = mlx_cnf.box_size * mlx_cnf.interpolation;
GFX_FAST(setTextSize(1));
GFX_FAST(setTextDatum(CC_DATUM));
GFX_EXEC(setClipRect(0, 0, MLX90640_COLS * box, MLX90640_ROWS * box));
if (mlx_cnf.marker_mode & 1) {
DrawMarker(&icon_marker[0], &_te_min);
DrawMarker(&icon_marker[0], &_te_max);
}
if (mlx_cnf.marker_mode & 2) {
DrawMarker(&icon_marker[1], &te_pick);
}
GFX_EXEC(clearClipRect());
}
/*--------------------------------------------------------------------------------
* Draw temperature color bar and range
*--------------------------------------------------------------------------------*/
void DrawTemperatureRange(uint8_t type) {
const int box = mlx_cnf.box_size * mlx_cnf.interpolation;
const int w = box * MLX90640_COLS;
int y = box * MLX90640_ROWS + 3;
GFX_EXEC(startWrite());
// Draw color bar
if (type & 1) {
const uint16_t *hm = heatmap[mlx_cnf.color_scheme];
for (int i = 0; i < N_HEATMAP; ++i) {
int x = map(i, 0, N_HEATMAP, 0, w);
#if defined (LOVYANGFX_HPP_)
GFX_EXEC(writeFastVLine(x, y, FONT_HEIGHT, hm[i]));
#else
GFX_EXEC(drawFastVLine(x, y, FONT_HEIGHT, hm[i]));
#endif
}
}
// Draw thermal range
if (type & 2) {
y += FONT_HEIGHT + 4;
const uint8_t size = (box > 4 ? 2 : 1);
const int font_w = (size == 2 ? FONT_WIDTH : FONT_WIDTH >> 1);
const int font_h = (size == 2 ? FONT_HEIGHT : FONT_HEIGHT >> 1);
GFX_EXEC(setTextSize(size));
GFX_EXEC(setTextDatum(TL_DATUM));
gfx_printf(0, y, "%d ", mlx_cnf.range_min);
GFX_EXEC(setTextDatum(TR_DATUM));
gfx_printf(w, y, " %d", mlx_cnf.range_max);
if (box > 1) {
GFX_EXEC(setTextDatum(TC_DATUM));
gfx_printf(w / 2, y, " %3.1f ", (float)(mlx_cnf.range_min + mlx_cnf.range_max) / 2.0f);
}
}
GFX_EXEC(endWrite());
GFX_EXEC(setTextSize(2));
GFX_EXEC(setTextDatum(TL_DATUM));
}
/*--------------------------------------------------------------------------------
* Draw widget
*--------------------------------------------------------------------------------*/
static void DrawWidget(const Widget_t *widget, uint8_t offset /* = 0 */) {
const Image_t *image = &widget->image[offset];
if (image) {
GFX_EXEC(startWrite());
#if defined (LOVYANGFX_HPP_)
GFX_EXEC(drawPng(image->data, image->size, widget->x, widget->y));
#elif defined (_TFT_eSPIH_)
DrawPNG(image->data, image->size, widget->x, widget->y, pngDraw);
#endif
CHECK_WIDGET_AREA(drawRect(widget->x, widget->y, widget->w, widget->h, TFT_RED));
GFX_EXEC(endWrite());
}
}
/*--------------------------------------------------------------------------------
* Draw screen
*--------------------------------------------------------------------------------*/
static void DrawScreen(const Widget_t *widget) {
DrawWidget(widget, 0);
}
/*--------------------------------------------------------------------------------
* Draw a button-like icon
*--------------------------------------------------------------------------------*/
static void DrawButton(const Widget_t *widget, uint8_t offset /* = 0 */) {
DrawWidget(widget, offset);
}
/*--------------------------------------------------------------------------------
* Draw slider
*--------------------------------------------------------------------------------*/
static void DrawSlider(const Widget_t *widget, int16_t pos, bool enable /* = true */) {
const Image_t *bar = &widget->image[0];
const Image_t *knob = &widget->image[enable ? 1 : 2]; // [1]: enable, [2]: disable
if (bar && knob) {
GFX_EXEC(startWrite());
#if defined (LOVYANGFX_HPP_)
sprite_draw.setPsram(true);
sprite_draw.createSprite(get_width(bar->data), get_height(bar->data));
sprite_draw.drawPng(bar->data, bar->size, 0, 0);
sprite_draw.drawPng(knob->data, knob->size, pos, 0);
sprite_draw.pushSprite(widget->x, widget->y);
sprite_draw.deleteSprite();
#elif defined (_TFT_eSPIH_)
sprite_draw.createSprite(get_width(bar->data), get_height(bar->data));
DrawPNG(bar->data, bar->size, 0, 0, pngSprite);
DrawPNG(knob->data, knob->size, pos, 0, pngSprite);
sprite_draw.pushSprite(widget->x, widget->y);
sprite_draw.deleteSprite();
#endif
CHECK_WIDGET_AREA(drawRect(widget->x, widget->y, widget->w, widget->h, TFT_RED));
GFX_EXEC(endWrite());
}
}
static void DrawToggle(const Widget_t *widget, bool check /* = false */) {
DrawButton(widget, check ? 1 : 0);
}
static void DrawCheck(const Widget_t *widget, bool check /* = false */) {
DrawButton(widget, check ? 1 : 0);
}
static void DrawRadio(const Widget_t *widget, uint8_t n_widget, uint8_t selected /* = 0 */) {
for (int i = 0; i < n_widget; ++i) {
DrawButton(&widget[i], (int8_t)(i == selected));
}
}
/*--------------------------------------------------------------------------------
* Rendering the pressed effects
*--------------------------------------------------------------------------------*/
#define PRESSED_BUFFER_LEN 64 // [px]
#define PRESSED_OFFSET 1 // 1 or 2 (need a wider background image)
static void DrawPress(const Widget_t *widget, Event_t event /* = EVENT_INIT */) {
int16_t offset;
const uint16_t x = widget->x;
const uint16_t y = widget->y;
const uint16_t w = widget->w;
const uint16_t h = widget->h;
const uint16_t d = PRESSED_OFFSET * 2;
#if defined (LOVYANGFX_HPP_)
lgfx::rgb888_t rgb[PRESSED_BUFFER_LEN];
#elif defined (_TFT_eSPIH_)
uint16_t rgb[PRESSED_BUFFER_LEN];
#endif
if (event & EVENT_FALLING) {
offset = +PRESSED_OFFSET;
} else if (event & EVENT_RISING) {
offset = -PRESSED_OFFSET;
} else {
offset = 0;
}
if (w + d <= PRESSED_BUFFER_LEN) {
GFX_EXEC(startWrite());
if (offset >= 0) {
for (int i = y + h + offset - 1; i >= y - offset; --i) {
GFX_EXEC(readRect (x - offset, i, w + d, 1, rgb));
GFX_EXEC(pushImage(x + offset, i + offset, w + d, 1, rgb));
}
} else {
for (int i = y - offset; i <= y + h + offset - 1; ++i) {
GFX_EXEC(readRect (x - offset, i, w + d, 1, rgb));
GFX_EXEC(pushImage(x + offset, i + offset, w + d, 1, rgb));
}
}
CHECK_WIDGET_AREA(drawRect(widget->x, widget->y, widget->w, widget->h, TFT_RED));
GFX_EXEC(endWrite());
}
if (event & EVENT_RISING) {
touch_clear();
}
}
#if defined (_TFT_eSPIH_)
/*--------------------------------------------------------------------------------
* A simplified version that displays a thumbnail of a bitmap image
*--------------------------------------------------------------------------------*/
static uint16_t read16(File &f) {
uint16_t result;
((uint8_t *)&result)[0] = f.read();
((uint8_t *)&result)[1] = f.read();
return result;
}
static uint32_t read32(File &f) {
uint32_t result;
((uint8_t *)&result)[0] = f.read();
((uint8_t *)&result)[1] = f.read();
((uint8_t *)&result)[2] = f.read();
((uint8_t *)&result)[3] = f.read();
return result;
}
static bool drawBmpFile(fs::FS &fs, const char *path, int32_t x = 0, int32_t y = 0, int32_t maxWidth = 0, int32_t maxHeight = 0, int32_t offX = 0, int32_t offY = 0, float scale_x = 1.0f, float scale_y = 1.0f) {
uint32_t bmpWidth, bmpHeight;
uint8_t bmpDepth;
uint32_t bmpImageoffset;
uint32_t rowSize;
float w, h;
File file = fs.open(path);
if (!file) {
return false;
}
read16(file);
read32(file);
read32(file);
bmpImageoffset = read32(file);
read32(file);
bmpWidth = read32(file);
bmpHeight = read32(file);
bmpDepth = read16(file);
rowSize = (bmpWidth * 3 + 3) & ~3;
offX += x;
offY += y;
y = bmpHeight * scale_y;
w = h = 0.0f;
for (int row = 0; row < bmpHeight; ++row) {
h += scale_y;
if (h < 0.999f) {
continue;
}
--h;
uint32_t pos = bmpImageoffset + row * rowSize;
file.seek(pos);
uint8_t rgb[bmpWidth][3]; // assume 24bit color
file.read((uint8_t *)rgb, sizeof(rgb));
GFX_EXEC(startWrite());
x = 0; --y;
for (int col = 0; col < bmpWidth; ++col) {
w += scale_x;
if (w < 0.999f) {
continue;
}
--w;
uint8_t b = rgb[col][0];
uint8_t g = rgb[col][1];
uint8_t r = rgb[col][2];
GFX_EXEC(drawPixel(x + offX, y + offY, GFX_EXEC(color565(r, g, b))));
++x;
}
GFX_EXEC(endWrite());
}
file.close();
return true;
}
#endif
/*--------------------------------------------------------------------------------
* Draw an image from a bitmap file
*--------------------------------------------------------------------------------*/
static void DrawThumb(const Widget_t *widget, const char *path) {
GFX_EXEC(fillRect(widget->x, widget->y, widget->w, widget->h, TFT_BLACK));
#if defined (LOVYANGFX_HPP_) && (defined (_SD_H_) || SD_FAT_VERSION <= 20203)
GFX_EXEC(drawBmpFile(
SD,
path,
widget->x, widget->y, widget->w, widget->h,
0, 0,
0.4f, 0.4f // 320 x 240 --> 128 x 96
));
#else
drawBmpFile(
SD,
path,
widget->x, widget->y, widget->w, widget->h,
0, 0,
0.4f, 0.4f // 320 x 240 --> 128 x 96
);
#endif
}
/*--------------------------------------------------------------------------------
* Render images from a raw file
*--------------------------------------------------------------------------------*/
class MLXViewer {
private:
String path;
const Widget_t *widget;
uint32_t frameNo;
uint32_t frameCount;
const uint32_t frameSize = (MLX90640_COLS * MLX90640_ROWS * sizeof(float));
public:
MLXViewer() { path = ""; }
private:
bool read(int n) {
File file = SD.open(path, FILE_READ);
if (file) {
file.seek(n * frameSize);
uint32_t len = file.read((uint8_t*)src[0], frameSize);
file.close();
if (len == frameSize) {
return true;
}
}
return false;
}
void render(void) {
if (read(frameNo)) {
const int dst_rows = widget->h;
const int dst_cols = widget->w;
const uint16_t *hm = heatmap[mlx_cnf.color_scheme];
// measure min/max temperature
filter_temperature(src[0]);
GFX_EXEC(startWrite());
GFX_FAST(createSprite(dst_cols, dst_rows));
interpolate_image(src[0], MLX90640_ROWS, MLX90640_COLS, dst_rows, dst_cols);
GFX_FAST(pushSprite(widget->x, widget->y));
GFX_FAST(deleteSprite());
GFX_EXEC(endWrite());
}
}
public:
bool open(const Widget_t *_widget, const char *_path) {
if (sdcard_mount()) { // already mounted in onFileManagerScreen()
widget = _widget;
path = String(_path);
File file = SD.open(path, FILE_READ);
if (file) {
frameNo = 0;
frameCount = file.size() / frameSize;
file.close();
DBG_EXEC(printf("%s: %d frames\n", _path, frameCount));
render();
return true;
} else {
DBG_EXEC(printf("%s: open error\n", _path));
}
}
return false;
}
void close(void) {
path = "";
frameNo = 0;
frameCount = 0;
}
bool isOpened(void) {
return (path.length() != 0);
}
bool isTop(void) {
return frameNo == 0;
}
bool isEnd(void) {
return frameNo == frameCount - 1;
}
bool rewind(void) {
if (isOpened()) {
frameNo = 0;
render();
return true;
} else {
return false;
}
}
bool next(void) {
if (isOpened()) {
if (frameNo < frameCount - 1) {
++frameNo;
render();
}
return (frameNo < frameCount -1);
} else {
return false;
}
}
bool prev(void) {
if (isOpened()) {
if (frameNo > 0) {
frameNo--;
render();
}
return (frameNo > 0);
} else {
return false;
}
}
};