任务理解
在下单阶段,差额部分下单了M5的SH1107
因为CPX没有显示模块,让传感器的读值可以更直观
代码部分
import board
import displayio
import terminalio
from adafruit_display_text import label
import adafruit_displayio_sh1107
import gc
import os
import busio
import random
import time
import analogio
import adafruit_thermistor
gc.enable()
# release any currently configured displays
displayio.release_displays()
# Setup I2C
i2c = busio.I2C(board.SCL, board.SDA) # uses board.SCL and board.SDA
display_bus = displayio.I2CDisplay(i2c, device_address=0x3C)
# SH1107 is vertically oriented 64x128
WIDTH = 128
HEIGHT = 64
display = adafruit_displayio_sh1107.SH1107(
display_bus, width=WIDTH, height=HEIGHT, rotation=0
)
splash = displayio.Group()
display.root_group = splash
default_text = " "*7
text_area = label.Label(terminalio.FONT, text=default_text, scale=2, color=0xFFFFFF, x=0, y=10)
display.root_group =text_area
sensor_light = analogio.AnalogIn(board.LIGHT)
sensor_temp = adafruit_thermistor.Thermistor(board.TEMPERATURE, 10000, 10000, 25, 3950)
while True:
gc.collect()
print('mem_free',gc.mem_free())
temp_value = str(round(sensor_temp.temperature,1))
light_value = str(sensor_light.value)
show_value = temp_value + '/' + light_value
# show_value = 'T/L' + '\n' + show_value
text_new= show_value
text_area.text = text_new
time.sleep(1)
主要逻辑
1. 主要部分是传感器读取,细节请参考
2. 头部需要 在lib文件夹中引入 adafruit_display_text 和 adafruit_displayio_sh1107
3. Line 33~42 主要实例化 显示部分,最后在大循环中定时把传感器的值跟新到显示区域的text
展示
本帖最后由 disk0 于 2024-8-18 17:29 编辑