简介
在上一个章节中我们实现了颜色传感器读取的操作,那么本篇文章我们将实现屏幕的RGB显示,通过颜色传感器读取对应的颜色然后控制屏幕显示不同的颜色。【Let'sdo第3期-拾色播放器DIY】--驱动颜色传感器
这里的代码比较多,颜色传感器的代码和分析在上一篇文章中已经分析过了,本章节主要是在上个代码中进行修改。
background = displayio.Bitmap(screen_width, screen_height, 1) bg_palette = displayio.Palette(1) bg_palette[0] = 0x000000 bg_tilegrid = displayio.TileGrid( background, pixel_shader=bg_palette, x=0, y=0 ) splash.append(bg_tilegrid)
首先创建一个屏幕大小的bitmap,然后把这个bitmap贴到屏幕上。然后创建一个更新屏幕颜色的方法。
def update_background_color(r, g, b): hex_color = (r << 16) | (g << 8) | b bg_palette[0] = hex_color
这个更新颜色的方法会更新上述创建的位图的颜色数组。当我们从颜色传感器中获取到颜色的RGB数据后(通过滤波、每次打开一个通道读取到PWM)然后调用上述的方法更新颜色。
r, g, b = color_sensor.read_rgb() update_led(r, g, b) update_display(r, g, b)
当调用update_display的时候刷新屏幕
def update_display(r, g, b):
color_palette[0] = (r << 16) | (g << 8) | b
rgb_value_label.text = f"R:{r:3d} G:{g:3d} B:{b:3d}"
hex_label.text = f"#{r:02X}{g:02X}{b:02X}"
if screen_effect == 0:
update_background_color(r, g, b)
display.refresh()白色(奶白色)

绿色

蓝色

代码附件
我要赚赏金
