回到首页 返回首页
回到顶部 回到顶部
返回上一页 返回上一页

python+pinpong库 制作幻彩灯 简单

头像 2021.12.19 533 0

看了云天老师 Mind+python 流光彩灯,自己也想做个玩玩。

原贴:https://mc.dfrobot.com.cn/thread-311681-1-1.html

材料清单

  • arduino 板 X1
  • 65彩灯 X2

步骤1 焊接彩灯

project-image

步骤2 制作光板

project-image

同样点亮试试。

步骤3 应用pinpong库

参照了云天老师的代码思路,捕捉屏幕随机的坐标中的色彩,输入到arduino中。

一样的用ctypes 库,网上也找了些代码,也有些解决方式,发现还是用ctypes最简单。

在云天老师的代码上,稍做修改,也算抄袭了。

代码
from ctypes import *  # 获取屏幕上某个坐标的颜色
import time
from pinpong.board import Board,Pin,NeoPixel
import numpy as npy
NEOPIXEL_PIN = Pin.D4
PIXELS_NUM = 65 #灯数
Board("uno").begin()  #初始化,选择板型和端口号,不输入端口号则进行自动识别
np = NeoPixel(Pin(NEOPIXEL_PIN), PIXELS_NUM)

gdi32 = windll.gdi32
user32 = windll.user32
user32.SetProcessDPIAware()
w, h= [user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)] 
def get_color(x, y):
    hdc = user32.GetDC(None)  # 获取颜色值
    pixel = gdi32.GetPixel(hdc, x, y)  # 提取RGB值
    r = pixel & 0x0000ff
    g = (pixel & 0x00ff00) >> 8
    b = pixel >> 16
    return (int(r),int(g),int(b))
success=True
n0=13
h0=int(h/n0)
n1=22
w1=int(w/n1)
n2=14
h2=int(h/n2)

while success:
    #右侧
    data=[]
    for j in range(0,n0):
      t0=h0*j+int(h0/2)
      rgb1,rgb2,rgb3=get_color(w-22, t0)
      k=12-j
      np[k] =(rgb1,rgb2,rgb3)
     #顶灯
    data=[]
    for j in range(0,n1):
     t1=w1*j+int(w1/2)
     rgb1,rgb2,rgb3=get_color(t1, 22)
     k=34-j
     np[k] =(rgb1,rgb2,rgb3)
    #左侧
    data=[]
    for j in range(0,n2):
      t2=h2*j+int(h2/2)
      rgb1,rgb2,rgb3=get_color(22, t2)
      k=47-j
      np[k] =(rgb1,rgb2,rgb3)
    #底灯
    data=[]
    for j in range(0,n1):
     t1=w1*j+int(w1/2)
     rgb1,rgb2,rgb3=get_color(t1, 22)
     k=64-j
     np[k] =(rgb1,rgb2,rgb3)
    
    


project-image
project-image

光板随着电脑屏幕 的色调而发生变化

这个项目真的有意思,可以延伸许多项目。

评论

user-avatar