[Digi-Key Follow me Issue 2] Task 4: WS2812B Effect Control
[Copy link]
This post was last edited by aramy on 2023-9-25 10:06
Subtask 2: WS2812B effect control - Complete a Neopixel (12 or more LEDs) controller and display the effects through buttons and screen switching.
The light strips I bought before have been sitting there gathering dust. The light strips are made of WS2812B lamp beads, with 8 lamp beads in one strip. WS2812B is an intelligent control LED light source that integrates the control circuit and RGB chip in a 5050 component package. The internal includes intelligent digital port data latch and signal shaping and amplification drive circuit. It also includes a precise internal oscillator and a voltage-programmable constant current control part to effectively ensure the high consistency of the light color of the pixel points. The data transmission protocol adopts a single NZR communication mode. After the pixel is powered on and reset, the DIN port receives data from the controller. The first pixel collects the initial 24-bit data and then sends it to the internal data latch. The other data shaped by the internal signal shaping and amplification circuit is sent to the next cascade pixel through the DO port. After each pixel is transmitted, the signal is reduced by 24 bits. The pixel adopts self-shaping transmission technology, so that the number of pixel cascades is not limited by signal transmission, but only depends on the speed of signal transmission. Reset is >280us, and there will be no false reset during interruption. The refresh rate is updated to 2KHz, flicker-free, improving the excellent display effect.
It is not easy to drive WS2812, as the timing requirement is very high. However, the cpy has already built the wheels, so you can just call the library to drive it, which makes it very simple. Here, two light strips are connected to form a ring, with a total of 16 lamp beads. Use Dupont wire to connect to the D12 interface of the Adafruit ESP32-S3 TFT Feather board. The power supply is also taken from the development board.
Code flow, use buttons to control the color of the lamp beads. The buttons are queried using polling. Polling is not a good method. If you want to make special effects for the lamp beads, it will take a long time to control WS2812 and affect the key response. I tried cpy multithreading, but there were still problems, so I gave up. WS2812 displays the color and also displays the color corresponding to the current lamp bead on the screen.
import board
import digitalio
import time
import neopixel
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font
pixel = neopixel.NeoPixel(board.D12, 16)
pixel.brightness = 0.8
button = digitalio.DigitalInOut(board.BUTTON)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP
but_in=3
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
display = board.DISPLAY
font_file = "fonts/LibreBodoniv2002-Bold-27.bdf"
font = bitmap_font.load_font(font_file)
grape = (190, 75, 219)
pixel.fill(0xFF0000)
# 按键控制LED
def colbutton():
global but_in
if(not button.value):
but_in=but_in+1
if (but_in>3):
but_in=0
def dispinfo(str):
if(str=="RED"):
color = 0xFF0000
text_area = label.Label(font, text="RED", color=color)
text_area.x = 100
text_area.y = 60
display.show(text_area)
pixel.fill(color)
elif(str=="GREEN"):
color = 0x00FF00
text_area = label.Label(font, text="GREEN", color=color)
text_area.x = 80
text_area.y = 60
pixel.fill(color)
elif(str=="BLUE"):
color = 0x0000FF
text_area = label.Label(font, text="BLUE", color=color)
text_area.x = 90
text_area.y = 60
pixel.fill(color)
else :
color = 0xFFFFFF
text_area = label.Label(font, text="WAITING", color=color)
text_area.x = 70
text_area.y = 60
pixel.fill(color)
display.show(text_area)
while True:
colbutton()
if(but_in==0):
dispinfo("RED")
elif(but_in==1):
dispinfo("BLUE")
elif(but_in==2):
dispinfo("GREEN")
else:
dispinfo("WAITING")
time.sleep(0.5)
|