-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMLX90640.ino
216 lines (187 loc) · 7.4 KB
/
MLX90640.ino
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
/*================================================================================
* Thermograpy camera
* Copyright (c) 2024 - 2025 embedded-kiddie
* Released under the MIT license
* https://opensource.org/license/mit
*================================================================================*/
#include <Arduino.h>
#include "pin_assign.h"
/*--------------------------------------------------------------------------------
* Step 1: Configure serial output for debugging
*--------------------------------------------------------------------------------*/
#define DEBUG false
#if DEBUG
#define DBG_EXEC(x) x
#else
#define DBG_EXEC(x)
#endif
/*--------------------------------------------------------------------------------
* Step 2: Configure operational settings
*--------------------------------------------------------------------------------*/
#define ENA_INTERPOLATION true // Enable interpolation
#define ENA_MULTITASKING true // Enable multi-task by FreeRTOS
#define ENA_OUTWARD_CAMERA false // Camera orientation (true: Outward, false: Selfie)
/*--------------------------------------------------------------------------------
* Step 3: Select GFX Library
*--------------------------------------------------------------------------------*/
#if 1
#define USE_LOVYAN_GFX
/*--------------------------------------------------------------------------------
* LovyanGFX
* Note: Currently 'AUTODETECT' only works for 'ESP32 2432S028R'.
* For other boards you need to configure appropriately to fit your device.
* See https://github.com/lovyan03/LovyanGFX/blob/master/src/lgfx/boards.hpp
*--------------------------------------------------------------------------------*/
#define USE_AUTODETECT true
#else
#define USE_TFT_ESPI
/*--------------------------------------------------------------------------------
* TFT_eSPI
* CYD requires XPT2046 touch screen library.
* See SPI bus assignment in 'boards/Setup_CYD_2432S028R.h' and 'pin_assign.h'.
* https://github.com/Bodmer/TFT_eSPI/discussions/3123
* https://forum.arduino.cc/t/cyd-sd-issues-probably-due-to-touch-pins/1280758/3
* https://github.com/witnessmenow/ESP32-Cheap-Yellow-Display/discussions/88
* https://github.com/TheNitek/XPT2046_Bitbang_Arduino_Library
*--------------------------------------------------------------------------------*/
#if defined (ARDUINO_ESP32_2432S028R)
#include <XPT2046_Bitbang.h>
#endif
#endif
/*--------------------------------------------------------------------------------
* Step 4: Configure flash memory setting to save touch calibration data
* Preferences requires at least 2 partitions. Check the partition scheme on Tools.
*--------------------------------------------------------------------------------*/
#define USE_PREFERENCES false
/*--------------------------------------------------------------------------------
* Step 5: Configure resolution settings
*--------------------------------------------------------------------------------*/
#if ENA_INTERPOLATION
#define INTERPOLATE_SCALE 8
#define BOX_SIZE 1
#define REFRESH_RATE (ENA_MULTITASKING ? MLX90640_32_HZ : MLX90640_16_HZ)
#else
#define INTERPOLATE_SCALE 1
#define BOX_SIZE 8
#define REFRESH_RATE (ENA_MULTITASKING ? MLX90640_32_HZ : MLX90640_16_HZ)
#endif
/*--------------------------------------------------------------------------------
* Heat map
*--------------------------------------------------------------------------------*/
#define N_HEATMAP 512 // 256 or 512 --> heatmap.h
constexpr uint16_t heatmap[2][N_HEATMAP] = {
#include "heatmap.h" // 0; Rainbow, 1: Inferno
};
/*--------------------------------------------------------------------------------
* Load modules
*--------------------------------------------------------------------------------*/
#include "mlx.hpp"
#include "gfx.hpp"
#include "task.hpp"
#include "touch.hpp"
#include "sdcard.hpp"
#include "filter.hpp"
#include "interpolation.hpp"
#include "widget.hpp"
/*--------------------------------------------------------------------------------
* Input process - Get thermal image from MLX90640
*--------------------------------------------------------------------------------*/
void ProcessInput(uint8_t bank) {
// Refresh MLX90640 when refresh rate changes
bool refresh = mlx_refresh();
// Get new image
if (mlx.getFrame(src[bank]) != 0) {
gfx_printf(TFT_WIDTH / 2 - FONT_WIDTH * 2, TFT_HEIGHT / 2 - FONT_HEIGHT * 4, "Failed");
DBG_EXEC(printf("Failed\n"));
delay(1000); // false = no new frame capture
}
if (!refresh) {
// Measure temperature for min/max/pickup
filter_temperature(src[bank]);
} else {
// Avoid cluttered image
for (int i = 0; i < MLX90640_COLS * MLX90640_ROWS; ++i) {
src[bank][i] = (float)mlx_cnf.range_min;
}
}
}
/*--------------------------------------------------------------------------------
* Output process - Interpolate thermal image and display on LCD.
*--------------------------------------------------------------------------------*/
void ProcessOutput(uint8_t bank, uint32_t inputStart, uint32_t inputFinish) {
// Widget controller
State_t state = widget_control();
if (bank != NOT_UPDATED && (state == STATE_MAIN || state == STATE_THERMOGRAPH)) {
static uint32_t outputFinish, outputPeriod, prevStart;
uint32_t outputStart = millis();
const int dst_rows = mlx_cnf.interpolation * MLX90640_ROWS;
const int dst_cols = mlx_cnf.interpolation * MLX90640_COLS;
const int box_size = mlx_cnf.box_size;
GFX_EXEC(startWrite());
GFX_FAST(createSprite(dst_cols * box_size, dst_rows * box_size));
interpolate_image(src[bank], MLX90640_ROWS, MLX90640_COLS, dst_rows, dst_cols);
if (mlx_cnf.marker_mode) {
static uint32_t prevUpdate;
if (outputStart - prevUpdate > 1000) {
prevUpdate = outputStart;
filter_update();
}
DrawTemperatureMarker();
}
if (mlx_cnf.range_auto) {
DrawTemperatureRange(2);
}
if (state == STATE_MAIN) {
gfx_printf(260 + FONT_WIDTH, LINE_HEIGHT * 3.5, "%4d", inputFinish - inputStart); // input processing time
gfx_printf(260 + FONT_WIDTH, LINE_HEIGHT * 5.0, "%4d", outputPeriod); // output processing time
float v = 1000.0f / (float)(outputStart - prevStart);
gfx_printf(260 + FONT_WIDTH, LINE_HEIGHT * 2.0, "%4.1f", v); // FPS
v = mlx.getTa(false);
if (0.0f < v && v < 100.0f) {
gfx_printf(260 + FONT_WIDTH, LINE_HEIGHT * 6.5, "%4.1f", v); // Sensor temperature
}
}
GFX_FAST(pushSprite(0, 0));
GFX_FAST(deleteSprite());
GFX_EXEC(endWrite());
// Save video
if (mlx_cap.recording) {
sdcard_record((uint8_t*)src[bank], sizeof(src[bank]), mlx_cap.filename);
}
#if DEBUG
// Capture screen
else if (Serial.available()) {
Serial.readStringUntil('\n');
sdcard_save();
}
#endif
// Update processing time
prevStart = outputStart;
outputFinish = millis();
outputPeriod = outputFinish - outputStart;
}
}
void setup() {
// Prevent blocking when the USB cable is not connected to PC
Serial.begin(115200);
DBG_EXEC(delay(500));
// Initialize peripherals
mlx_setup();
gfx_setup();
touch_setup();
sdcard_setup();
widget_setup();
#if ENA_MULTITASKING
// Start tasks
task_setup(ProcessInput, ProcessOutput);
#endif
}
void loop() {
#if ENA_MULTITASKING
delay(1000);
#else
uint32_t inputStart = millis();
ProcessInput(0); // always use bank 0
ProcessOutput(0, inputStart, millis());
#endif
}