【得捷电子Follow me第2期】任务4----分任务2:WS2812B效果控制
[复制链接]
本帖最后由 怀66 于 2023-9-10 09:16 编辑
任务4----分任务2:完成一个Neopixel(12灯珠或以上)控制器,通过按键和屏幕切换展示效果
这次任务基本上就是在任务三上面改进。任务三连接:【得捷电子Follow me第2期】任务3:控制WS2812B - DigiKey得捷电子技术专区 - 电子工程世界-论坛 (eeworld.com.cn)
我手头上刚好有一个底板,底板上有12个rgb彩灯,这里就不放连线图了,因为兄弟们不一定有和我一样的底板。主要就是连接电源线、地线、esp32的一个数字io-D5连接rgb彩灯的数据输入、再找两个数字io连接两个按键。硬件连接就这样。
其实主要就是把原来的 pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=1)
改为 pixel = neopixel.NeoPixel(board.D5, 12, brightness=1)
就是把rgb灯数据接口改为你用的就好啦。
另外,在任务三基础上,还要求要使用屏幕展示效果,于是我就去官方的库里找了一些驱动屏幕的资料,然后自己瞎吉儿乱搞,找到一些改变屏幕状态的核心语句,就可以让屏幕同步显示当前彩灯的颜色啦。
效果:光线原因拍出来不是很好看
鎾斁鍣ㄥ姞杞藉け璐�: 鏈娴嬪埌Flash Player锛岃鍒� 瀹夎
任务4-rgb彩灯
这个任务也完成的比较简单,主要是屏幕控制方面云里雾里的,程序逻辑都没怎么看懂,但是,能跑就行!哈!对了其中彩虹的逻辑其实就是固定一个rgb中的原色值,另外两个值一个加一个减,就实现了渐变效果。还有诸如单独控制某个小灯的值我放在setcolor函数里了。😊
代码:
- import time, board, neopixel, digitalio
- import touchio
- import displayio
- from adafruit_display_text import label
- from adafruit_st7789 import ST7789
- from adafruit_bitmap_font import bitmap_font
-
-
-
- BORDER = 20
- FONTSCALE = 2
- BACKGROUND_COLOR = 0xffffff
- TEXT_COLOR = 0x000000
- font_file = "wenquanyi_13px.pcf"
- font = bitmap_font.load_font(font_file)
-
-
- displayio.release_displays()
-
- spi = board.SPI()
- tft_cs = board.TFT_CS
- tft_dc = board.TFT_DC
-
- display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs)
- display = ST7789(
- display_bus, rotation=270, width=240, height=135, rowstart=40, colstart=53
- )
-
-
- splash = displayio.Group()
- display.show(splash)
-
- color_bitmap = displayio.Bitmap(display.width, display.height, 1)
- color_palette = displayio.Palette(1)
- color_palette[0] = BACKGROUND_COLOR
-
- bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
- splash.append(bg_sprite)
-
-
- text = "这里显示颜色"
- text_area = label.Label(font, text=text, color=TEXT_COLOR)
- text_width = text_area.bounding_box[2] * FONTSCALE
- text_group = displayio.Group(
- scale=FONTSCALE,
- x=display.width // 2 - text_width // 2,
- y=display.height // 2,
- )
- text_group.append(text_area)
- splash.append(text_group)
-
-
-
- color = [(255,0,0), (0,255,0), (0,0,255), (0,255,255),\
- (255,0,255), (255,255,0), (255,255,255), (0,0,0)]
- pixel = neopixel.NeoPixel(board.D5, 12, brightness=1)
-
-
- button2 = digitalio.DigitalInOut(board.D10)
- button2.direction = digitalio.Direction.INPUT
- button2.pull = digitalio.Pull.UP
-
- button1 = digitalio.DigitalInOut(board.D9)
- button1.direction = digitalio.Direction.INPUT
- button1.pull = digitalio.Pull.UP
-
- state = 0
- press_time = 0
-
-
- touch_one = touchio.TouchIn(board.D12)
- touch_two = touchio.TouchIn(board.A4)
-
-
- def setcolor(n,color):
- pixel[n] = color
-
- def on():
- for _ in range(12):
- pixel[_] = (122//16,238//16,78//16)
-
- def off():
- for _ in range(12):
- pixel[_] = (0,0,0)
-
- def rainbow():
- while not touch_one.value:
- pass
- if touch_one.value:
- while True:
-
- r = 255
- g = 0
- b = 0
- for _ in range(12):
- pixel[_] = (r,g,b)
- r = r - 20
- g = g + 20
- time.sleep(0.01)
-
-
- r = 0
- g = 255
- b = 0
- for _ in range(12):
- pixel[_] = (r,g,b)
- g = g - 20
- b = b + 20
- time.sleep(0.01)
-
-
- r = 0
- g = 0
- b = 255
- for _ in range(12):
- pixel[_] = (r,g,b)
- b = b - 21
- r = r + 21
- time.sleep(0.01)
- if touch_two.value:
- off()
- break
-
- off()
- while True:
- if state >= len(color):
- state = 0
- time.sleep(0.01)
- if button1.value == 0:
- while not button1.value:
- pass
- print('touch above begin rainbow show, and touch below exit')
- text_area.color = 0xffffff
- text_area.text = '彩虹模式'
- color_palette[0] = 0x000000
- rainbow()
- text_area.color = 0x000000
- text_area.text = '这里显示颜色'
- color_palette[0] = 0xffffff
- if button2.value == 0:
- while not button2.value:
- press_time += 0.001
- if 0 <= press_time <= 200:
- print(press_time, 'duanan')
- pixel.fill(color[state])
- if color[state] == (0,0,0):
- text_area.color = 0xffffff
- text_area.text = '这是关闭状态'
- color_palette[0] = 0x7F7F7F
- else:
- text_area.text = '这里显示颜色'
- text_area.color = TEXT_COLOR
- color_palette[0] = color[state]
- state += 1
-
- elif press_time > 200:
- print(press_time, 'changan')
- pixel.fill((0,0,0))
- state = 0
- text_area.color = 0xffffff
- text_area.text = '这是关闭状态'
- color_palette[0] = 0x7F7F7F
-
- press_time = 0
-
-
-
-
-
-
|