Skip to content

Commit 45f9b2b

Browse files
Implement drawBmpFile() to draw thumbnail for TFT_eSPI.
1 parent 7126f9f commit 45f9b2b

File tree

2 files changed

+111
-16
lines changed

2 files changed

+111
-16
lines changed

README.md

+20-14
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ It comes with a graphical user interface to properly configure the MLX90640 for
3131

3232
## Hardware Setup
3333

34+
For GPIO pin assignments, see [`pin_assign.h`](pin_assign.h).
35+
3436
<details>
3537
<summary>XIAO ESP32S3</summary>
3638

@@ -70,6 +72,8 @@ It comes with a graphical user interface to properly configure the MLX90640 for
7072
- GFX library
7173
- LovyanGFX : [1.2.0][4]
7274
- TFT_eSPI : [2.5.43][5]
75+
- Touchscreen library
76+
- XPT2046_Touchscreen : [Version 1.4][6] (only for TFT_eSPI)
7377

7478
### File Structure and Dependencies
7579

@@ -131,15 +135,15 @@ MLX90640.ino
131135

132136
## Resouces
133137

134-
- [Melexis MLX90640][6]
135-
- [Datasheet][7]
138+
- [Melexis MLX90640][7]
139+
- [Datasheet][8]
136140
- I2C Frequency: 1MHz (Fast mode+)
137-
- [Breakout board: GY-MCU90640][8]
138-
- [Alliexpress][9]
141+
- [Breakout board: GY-MCU90640][9]
142+
- [Alliexpress][10]
139143

140-
- [Seeed Studio XIAO ESP32S3][10]
141-
- [ESP32-S3 Series Datasheet][11]
142-
- [ESP32 Technical Reference Manual][12]
144+
- [Seeed Studio XIAO ESP32S3][11]
145+
- [ESP32-S3 Series Datasheet][12]
146+
- [ESP32 Technical Reference Manual][13]
143147

144148
- [witnessmenow/ESP32-Cheap-Yellow-Display][20]
145149

@@ -154,19 +158,21 @@ MLX90640.ino
154158

155159
[5]: https://github.com/Bodmer/TFT_eSPI/releases/tag/V2.5.43 "Release Bug fixes · Bodmer/TFT_eSPI"
156160

157-
[6]: https://www.melexis.com/en/product/MLX90640/Far-Infrared-Thermal-Sensor-Array "Far Infrared Thermal Sensor Array (32x24 RES) I Melexis"
161+
[6]: https://github.com/PaulStoffregen/XPT2046_Touchscreen/releases/tag/v1.4 "Release Version 1.4 · PaulStoffregen/XPT2046_Touchscreen"
162+
163+
[7]: https://www.melexis.com/en/product/MLX90640/Far-Infrared-Thermal-Sensor-Array "Far Infrared Thermal Sensor Array (32x24 RES) I Melexis"
158164

159-
[7]: https://www.melexis.com/en/documents/documentation/datasheets/datasheet-mlx90640 "Datasheet for MLX90640 I Melexis"
165+
[8]: https://www.melexis.com/en/documents/documentation/datasheets/datasheet-mlx90640 "Datasheet for MLX90640 I Melexis"
160166

161-
[8]: https://github.com/vvkuryshev/GY-MCU90640-RPI-Python/blob/master/GY_MCU9064%20user%20manual%20v1.pdf "vvkuryshev/GY-MCU90640-RPI-Python: The script to connect the thermal image module GY-MCU90640 to Raspberry Pi."
167+
[9]: https://github.com/vvkuryshev/GY-MCU90640-RPI-Python/blob/master/GY_MCU9064%20user%20manual%20v1.pdf "vvkuryshev/GY-MCU90640-RPI-Python: The script to connect the thermal image module GY-MCU90640 to Raspberry Pi."
162168

163-
[9]: https://www.aliexpress.com/item/1005006674751991.html
169+
[10]: https://www.aliexpress.com/item/1005006674751991.html
164170

165-
[10]: https://wiki.seeedstudio.com/xiao_esp32s3_getting_started/ "Getting Started with Seeed Studio XIAO ESP32S3 (Sense) - Seeed Studio Wiki"
171+
[11]: https://wiki.seeedstudio.com/xiao_esp32s3_getting_started/ "Getting Started with Seeed Studio XIAO ESP32S3 (Sense) - Seeed Studio Wiki"
166172

167-
[11]: https://www.espressif.com/sites/default/files/documentation/esp32-s3_datasheet_en.pdf
173+
[12]: https://www.espressif.com/sites/default/files/documentation/esp32-s3_datasheet_en.pdf
168174

169-
[12]: https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf
175+
[13]: https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf
170176

171177
[20]: https://github.com/witnessmenow/ESP32-Cheap-Yellow-Display "witnessmenow/ESP32-Cheap-Yellow-Display: Building a community around a cheap ESP32 Display with a touch screen"
172178

draw.hpp

+91-2
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,87 @@ static void DrawPress(const Widget_t *widget, Event_t event /* = EVENT_INIT */)
383383
}
384384
}
385385

386+
#if defined (_TFT_eSPIH_)
387+
/*--------------------------------------------------------------------------------
388+
* A simplified version that displays a thumbnail of a bitmap image
389+
*--------------------------------------------------------------------------------*/
390+
static uint16_t read16(File &f) {
391+
uint16_t result;
392+
((uint8_t *)&result)[0] = f.read();
393+
((uint8_t *)&result)[1] = f.read();
394+
return result;
395+
}
396+
397+
static uint32_t read32(File &f) {
398+
uint32_t result;
399+
((uint8_t *)&result)[0] = f.read();
400+
((uint8_t *)&result)[1] = f.read();
401+
((uint8_t *)&result)[2] = f.read();
402+
((uint8_t *)&result)[3] = f.read();
403+
return result;
404+
}
405+
406+
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) {
407+
uint32_t bmpWidth, bmpHeight;
408+
uint8_t bmpDepth;
409+
uint32_t bmpImageoffset;
410+
uint32_t rowSize;
411+
float w, h;
412+
413+
File file = fs.open(path);
414+
if (!file) {
415+
return false;
416+
}
417+
418+
read16(file);
419+
read32(file);
420+
read32(file);
421+
bmpImageoffset = read32(file);
422+
read32(file);
423+
bmpWidth = read32(file);
424+
bmpHeight = read32(file);
425+
bmpDepth = read16(file);
426+
rowSize = (bmpWidth * 3 + 3) & ~3;
427+
428+
offX += x;
429+
offY += y;
430+
y = bmpHeight * scale_y;
431+
w = h = 0.0f;
432+
433+
for (int row = 0; row < bmpHeight; ++row) {
434+
h += scale_y;
435+
if (h < 0.999f) {
436+
continue;
437+
}
438+
h -= 1.0f;
439+
440+
uint32_t pos = bmpImageoffset + row * rowSize;
441+
file.seek(pos);
442+
443+
uint8_t rgb[bmpWidth][3]; // assume 24bit color
444+
file.read((uint8_t *)rgb, sizeof(rgb));
445+
446+
GFX_EXEC(startWrite());
447+
x = 0; y--;
448+
for (int col = 0; col < bmpWidth; ++col) {
449+
w += scale_x;
450+
if (w < 0.999f) {
451+
continue;
452+
}
453+
w -= 1.0f;
454+
uint8_t b = rgb[col][0];
455+
uint8_t g = rgb[col][1];
456+
uint8_t r = rgb[col][2];
457+
GFX_EXEC(drawPixel(x + offX, y + offY, GFX_EXEC(color565(r, g, b))));
458+
x++;
459+
}
460+
GFX_EXEC(endWrite());
461+
}
462+
file.close();
463+
return true;
464+
}
465+
#endif
466+
386467
/*--------------------------------------------------------------------------------
387468
* Draw an image from a bitmap file
388469
*--------------------------------------------------------------------------------*/
@@ -396,11 +477,19 @@ static void DrawThumb(const Widget_t *widget, const char *path) {
396477
path,
397478
widget->x, widget->y, widget->w, widget->h,
398479
0, 0,
399-
0.4, 0.4 // 320 x 240 --> 128 x 96
480+
0.4f, 0.4f // 320 x 240 --> 128 x 96
400481
));
401482

402483
#else
403-
#warning Requires implementation in DrawThumb()
484+
485+
drawBmpFile(
486+
SD,
487+
path,
488+
widget->x, widget->y, widget->w, widget->h,
489+
0, 0,
490+
0.4f, 0.4f // 320 x 240 --> 128 x 96
491+
);
492+
404493
#endif
405494
}
406495

0 commit comments

Comments
 (0)