Thermal Unit

支持以下产品:

ThermalUnit

UiFlow2 应用示例

热成像

在 UiFlow2 上打开 cores3_thermal_imaging.m5f2 项目。

案例使用 M5Stack 的 UnitThermal 热成像模块,实现了一个基础的热成像功能。

UiFlow2 代码块:

cores3_thermal_imaging.png

示例输出:

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")

示例输出:

class ThermalUnit

Constructors

class ThermalUnit(i2c, address: int = 0x33)

创建一个 ThermalUnit 对象。

参数:
  • i2c – I2C 对象

  • address – I2C 地址,默认为 0x33。

UiFlow2:

init.png

Methods

property ThermalUnit.get_max_temperature
Type:

float

获取最大温度/

UiFlow2:

get_max_temperature.png

property ThermalUnit.get_min_temperature
Type:

float

获取最小温度。

UiFlow2:

get_min_temperature.png

property ThermalUnit.get_midpoint_temperature
Type:

float

获取中间温度。

UiFlow2:

get_midpoint_temperature.png

ThermalUnit.get_pixel_temperature(x: int, y: int) float

获取指定坐标像素的温度。

参数:
  • x (int) – 坐标 x

  • y (int) – 坐标 y

返回:

指定像素点的温度

UiFlow2:

get_pixel_temperature.png

property ThermalUnit.get_refresh_rate
Type:

float

获取刷新率

UiFlow2:

get_refresh_rate.png

ThermalUnit.get_temperature_buffer() list

获取温度缓冲区数据。

返回:

温度缓冲区数据。

UiFlow2:

get_temperature_buffer.png

ThermalUnit.set_refresh_rate(rate: int) None

设置刷新率。

参数:

rate (int) – 刷新率 Hz。

UiFlow2:

设置刷新率。

ThermalUnit.update_temperature_buffer() bytes

读取更新数据到温度缓冲区。

返回:

温度缓冲区数据。

UiFlow2:

读取更新数据到温度缓冲区。