M5Stack Thermal(ATOMS3) - eiichiromomma/CVMLAB GitHub Wiki

(M5Stack) Thermal(ATOMS3)

Core2の方よりまともな描画で。(UIFlow2)

矩形が使えるので128x128の画面を使って4x4のRectangleを32x24置く。M5Stack Basicだとupdate_temperature_bufferでメモリエラーが起きる(あとi2cの設定を弄らないと動かない)。

グレー階調表示

だいたい2秒ちょいで更新されるのでそれなりに実用。

import os, sys, io
import M5
from M5 import *
from hardware import *
from unit import ThermalUnit

label0 = None
i2c0 = None
thermal_0 = None
H = 24
W = 32
pRects = []

def setup():
  global label0, i2c0, thermal_0, pRects
  M5.begin()
  label0 = Widgets.Label("label0", 0, 110, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu18)
  for y in range(H):
    for x in range(W):
      pRects.append(Widgets.Rectangle(x*4, y*4, 4, 4, 0x000000, 0x000000))
  i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
  thermal_0 = ThermalUnit(i2c0)

def loop():
  global label0, i2c0, thermal_0, pRects
  M5.update()
  tempList = thermal_0.update_temperature_buffer()
  maxval = 0
  for y in range(H):
    for x in range(W):  
      temp = tempList[x+y*W]
      if maxval < temp:
        maxval = temp
      label0.setText(f'Max: {maxval:.1f}')
      itemp = int(((temp - 20) / 20.) * 255. )
      pRects[x+y*W].setColor(color=(itemp << 16) | (itemp << 8) | itemp, fill_c=(itemp << 16) | (itemp << 8) | itemp)

if __name__ == '__main__':
  try:
    setup()
    while True:
      loop()
  except (Exception, KeyboardInterrupt) as e:
    try:
      from utility import print_error_msg
      print_error_msg(e)
    except ImportError:
      print("please update to latest firmware")

擬似カラー表示

カラーテーブルを使おうと思ったらメモリエラーが起きたので簡単な条件分けで色が振れるhot-to-coldで都度計算。

import os, sys, io
import M5
from M5 import *
from hardware import *
from unit import ThermalUnit

label0 = None
i2c0 = None
thermal_0 = None
H = 24
W = 32

pRects = []

def setup():
  global label0, i2c0, thermal_0, pRects
  M5.begin()
  label0 = Widgets.Label("label0", 0, 110, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu18)
  for y in range(H):
    for x in range(W):
      pRects.append(Widgets.Rectangle(x*4, y*4, 4, 4, 0x000000, 0x000000))
  i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
  thermal_0 = ThermalUnit(i2c0)

def loop():
  global label0, i2c0, thermal_0, pRects
  M5.update()
  tempList = thermal_0.update_temperature_buffer()
  maxval = 0
  for y in range(H):
    for x in range(W):  
      temp = tempList[x+y*W]
      if maxval < temp:
        maxval = temp
      label0.setText(f'Max: {maxval:.1f}')
      itemp = int(((temp - 20) / 20.) * 255. )
      if itemp < 64:
        red = 0
        green = itemp*4
        blue = 255
      elif itemp < 128:
        red = 0
        green = 255
        blue = 255- (itemp-64)*4
      elif itemp < 192:
        red = 128+(itemp-128)*4
        green = 255
        blue = 0
      else:
        red = 255
        green = 255 - (itemp-192)*4
        blue = 0
      pRects[x+y*W].setColor(color=(int(red) << 16) | (int(green) << 8) | int(blue), fill_c=(int(red) << 16) | (int(green) << 8) | int(blue))

if __name__ == '__main__':
  try:
    setup()
    while True:
      loop()
  except (Exception, KeyboardInterrupt) as e:
    try:
      from utility import print_error_msg
      print_error_msg(e)
    except ImportError:
      print("please update to latest firmware")