【得捷电子Follow me第2期】任务3:按键控制ws2812B
[复制链接]
本帖最后由 TL-LED 于 2023-9-28 10:42 编辑
学习下使用按键控制板载Neopixel LED的显示和颜色切换
1、复制文件到开发板盘符下的lib文件夹下
2、测试代码
copy.py
import time
import board
import neopixel
import digitalio
from adafruit_display_text import bitmap_label
import terminalio
from adafruit_bitmap_font import bitmap_font
font = bitmap_font.load_font("fonts\wenquanyi_10pt.pcf")
color = 0x00FF00
text2 = "W2812B 关闭 "
scale = 2
text_area2 = bitmap_label.Label(font, text=text2, scale=scale)
text_area2.x = 10
text_area2.y = 30
text_area2.color = color
board.DISPLAY.show(text_area2)
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
pixel.brightness = 0.5
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue= (0, 0, 255)
black = (0, 0, 0)
button = digitalio.DigitalInOut(board.BUTTON)
button.switch_to_input(pull=digitalio.Pull.UP)
pixel.fill(black)
but=0;
while True:
if not button.value:
but=but+1
while (not button.value):
pass
if but==0:
pixel.fill(black)
text2 = "W2812B 关闭 "
text_area2 = bitmap_label.Label(font, ext=text2, scale=scale )
text_area2.x = 10
text_area2.y = 30
text_area2.color = 0x000000
board.DISPLAY.show(text_area2)
elif but==1:
pixel.fill(red)
text2 = "W2812B 红色 "
text_area2 = bitmap_label.Label(font, text=text2, scale=scale)
text_area2.x = 10
text_area2.y = 30
text_area2.color = 0xff0000
board.DISPLAY.show(text_area2)
elif but==2:
pixel.fill(green)
text2 = "W2812B 绿色 "
text_area2 = bitmap_label.Label(font, text=text2, scale=scale)
text_area2.x = 10
text_area2.y = 30
text_area2.color = 0x00ff00
board.DISPLAY.show(text_area2)
elif but==3:
pixel.fill(blue)
text2 = "W2812B 蓝色 "
text_area2 = bitmap_label.Label(font, text=text2, scale=scale)
text_area2.x = 10
text_area2.y = 30
text_area2.color = 0x0000ff
board.DISPLAY.show(text_area2)
elif but==4:
pixel.fill(white)
text2 = "W2812B 白色 "
text_area2 = bitmap_label.Label(font, text=text2, scale=scale)
text_area2.x = 10
text_area2.y = 30
text_area2.color = 0xffffff
board.DISPLAY.show(text_area2)
#a=a+1
if but>4:
but=0
time.sleep(0.1)
3、运行效果
通过按键切换WS2812B显示的颜色,同时在LCD上显示相应状态颜色的汉字
w2812b
|