display – LCD 显示
display 模块驱动开发板上的 LCD。创建一个 ESP32Display(别名为 display.Display),并用 ESP32Display.write() 把图像帧送到屏幕。
摄像头预览
import sensor, display
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
lcd = display.ESP32Display()
while True:
lcd.write(sensor.snapshot(), fit=True)
使用 fit=True 时,源图像会缩放以适配屏幕,并沿用显示驱动的默认放置方式。显示对象应只创建一次并持续复用;重复创建对象会重新初始化屏幕资源。
定位、缩放与裁剪
使用 x 和 y 设置显示位置,使用 x_scale 和 y_scale 显式缩放,并通过 roi 仅显示源图像中的指定区域:
img = sensor.snapshot()
lcd.write(
img,
x=20,
y=10,
x_scale=0.5,
y_scale=0.5,
roi=(40, 30, 160, 120),
fit=False,
)
当 fit=False 时,输出由显式指定的位置和缩放比例控制。ROI 使用源图像坐标,格式为 (x, y, width, height);缩小 ROI 还可以减少送入显示链路的图像数据量。
背光与资源释放
lcd = display.Display(backlight=80)
print("panel:", lcd.width(), "x", lcd.height())
lcd.backlight(30)
lcd.clear()
lcd.deinit()
背光值为 0 到 100 的百分比。应用永久释放显示设备时应调用 deinit();驱动反初始化后不得继续写入图像帧。
参见
可运行示例:example/06-Peripherals/01-Display/lcd_preview.py。
Classes
- class display.ESP32Display(width=..., height=..., refresh=..., *, backlight=...)
ESP32 display object for the board LCD.
Create and initialize the display.
- 参数:
width – requested display width; 0 uses board default.
height – requested display height; 0 uses board default.
refresh – target refresh rate in Hz.
backlight – initial backlight percentage, from 0 to 100.
- deinit()
Release the display driver resources.
- width()
Return the physical display width in pixels.
- height()
Return the physical display height in pixels.
- clear(display_off=...)
Clear the display.
- 参数:
display_off – True may turn the panel output off when supported by the board.
- backlight(value=...)
Get or set backlight brightness.
- 参数:
value – None returns current brightness; otherwise set 0 to 100 percent.
- write(image, *, x=..., y=..., x_scale=..., y_scale=..., roi=..., fit=...)
Draw an image on the display. x, y: destination top-left position in display pixels. x_scale, y_scale: optional manual scale factors.
- 参数:
image – source image, normally from sensor.snapshot().
roi – optional source rectangle (x, y, w, h).
fit – True scales the image to fit the display area.