Thermal Unit
支持以下产品:
UiFlow2 应用示例
热成像
在 UiFlow2 上打开 cores3_thermal_imaging.m5f2 项目。
案例使用 M5Stack 的 UnitThermal 热成像模块,实现了一个基础的热成像功能。
UiFlow2 代码块:
示例输出:
无
MicroPython 应用示例
热成像
案例使用 M5Stack 的 UnitThermal 热成像模块,实现了一个基础的热成像功能。
MicroPython 代码块:
1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD 2# 3# SPDX-License-Identifier: MIT 4 5import os, sys, io 6import M5 7from M5 import * 8from hardware import I2C 9from hardware import Pin 10from unit import ThermalUnit 11import math 12 13 14i2c0 = None 15thermal_0 = None 16temp = None 17min2 = None 18max2 = None 19color = None 20ratio = None 21r = None 22templist = None 23g = None 24min_temp = None 25b = None 26max_temp = None 27y = None 28x = None 29c = None 30 31 32def temperature_to_color(temp, min2, max2): 33 global color, ratio, r, templist, g, min_temp, b, max_temp, y, x, c, i2c0, thermal_0 34 # Clamp the temperature value to be within the min2 and max2 range 35 temp = min(max(temp, min2), max2) 36 # Calculate the ratio of the temperature within the given range [0.0 - 1.0] 37 ratio = (temp - min2) / (max2 - min2) 38 # Red increases with temperature 39 r = int(255 * ratio) 40 # Green peaks in the middle of the range 41 g = int(255 * ((1 - math.fabs(ratio - 0.5)) * 2)) 42 # Blue decreases with temperature 43 b = int(255 * (1 - ratio)) 44 # Combine R, G, B into a single 24-bit color value (0xRRGGBB) 45 color = r * 65536 + (g * 256 + b) 46 # Return the color value 47 return color 48 49 50def setup(): 51 global \ 52 i2c0, \ 53 thermal_0, \ 54 temp, \ 55 color, \ 56 ratio, \ 57 min2, \ 58 max2, \ 59 r, \ 60 templist, \ 61 g, \ 62 min_temp, \ 63 b, \ 64 max_temp, \ 65 c, \ 66 x, \ 67 y 68 69 M5.begin() 70 Widgets.fillScreen(0x222222) 71 i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=400000) 72 thermal_0 = ThermalUnit(i2c0) 73 thermal_0.set_refresh_rate(1) 74 M5.Lcd.clear(0x000000) 75 76 77def loop(): 78 global \ 79 i2c0, \ 80 thermal_0, \ 81 temp, \ 82 color, \ 83 ratio, \ 84 min2, \ 85 max2, \ 86 r, \ 87 templist, \ 88 g, \ 89 min_temp, \ 90 b, \ 91 max_temp, \ 92 c, \ 93 x, \ 94 y 95 M5.update() 96 thermal_0.update_temperature_buffer() 97 templist = thermal_0.get_temperature_buffer() 98 min_temp = thermal_0.get_min_temperature 99 max_temp = thermal_0.get_max_temperature 100 M5.Lcd.setFont(M5.Lcd.FONTS.DejaVu18) 101 M5.Lcd.fillRect(35, 10, 250, 20, 0x000000) 102 M5.Lcd.setCursor(35, 10) 103 M5.Lcd.print((str("min: ") + str(min_temp)), 0x3366FF) 104 M5.Lcd.setCursor(165, 10) 105 M5.Lcd.print((str("max: ") + str(max_temp)), 0xCC0000) 106 for y in range(24): 107 for x in range(32): 108 c = temperature_to_color(templist[int((y * 32 + x) - 1)], 20, 40) 109 M5.Lcd.fillRect(80 + x * 5, 60 + y * 5, 5, 5, c) 110 111 112if __name__ == "__main__": 113 try: 114 setup() 115 while True: 116 loop() 117 except (Exception, KeyboardInterrupt) as e: 118 try: 119 from utility import print_error_msg 120 121 print_error_msg(e) 122 except ImportError: 123 print("please update to latest firmware")
示例输出:
无